hdawg_example_awg_commandtableΒΆ

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
function hdawg_example_awg_commandtable(device_id)
% HDAWG_EXAMPLE_AWG_COMMANDTABLE demonstrates how to connect and use to a 
% Zurich Instruments HDAWG and upload and run an AWG program using the 
% Command Table feature.
%
% USAGE HDAWG_EXAMPLE_AWG_COMMANDTABLE(DEVICE_ID)
%
% DEVICE_ID should be a string, e.g., 'dev8006' or 'hdawg-dev8006'.
%
% NOTE This example can only be ran on HDAWG instruments.
%
% NOTE Additional configuration: Connect wave outputs 1 and 2 of HDAWG 
% to input channels 1 and 2 of an osciloscope with an SMA/BNC cable. The
% generated signals are Gaussian with opposite signs and equal amplitusdes 
%
%
% NOTE Please ensure that the ziDAQ folders 'Driver' and 'Utils' are in 
% your MATLAB path. To do this (temporarily) for one MATLAB session please 
% navigate to the ziDAQ base folder containing the 'Driver', 'Examples' 
% and 'Utils' subfolders and run the MATLAB function ziAddPath().
% >>> ziAddPath;
%
% Use either of the commands:
% >>> help ziDAQ
% >>> doc ziDAQ
% in the MATLAB command window to obtain help on all available ziDAQ commands.
% Copyright 2008-2021 Zurich Instruments AG

clear ziDAQ;

if ~exist('device_id', 'var')
    error(['No value for device_id specified. The first argument to the ' ...
           'example should be the device ID on which to run the example, ' ...
           'e.g. ''dev2006'' or ''uhf-dev2006''.'])
end

% Check the ziDAQ MEX (DLL) and Utility functions can be found in Matlab's path.
if ~(exist('ziDAQ', 'file') == 3) && ~(exist('ziCreateAPISession', 'file') == 2)
    fprintf('Failed to either find the ziDAQ mex file or ziDevices() utility.\n')
    fprintf('Please configure your path using the ziDAQ function ziAddPath().\n')
    fprintf('This can be found in the API subfolder of your LabOne installation.\n');
    fprintf('On Windows this is typically:\n');
    fprintf('C:\\Program Files\\Zurich Instruments\\LabOne\\API\\MATLAB2012\\\n');
    return
end

% The API level supported by this example.
apilevel_example = 6;
% Create an API session; connect to the correct Data Server for the device.
required_err_msg = 'This example can only be ran on an HDAWG.';
[device, ~] = ziCreateAPISession(device_id, apilevel_example, ...
                                     'required_devtype', 'HDAWG', ...
                                     'required_err_msg', required_err_msg);
ziApiServerVersionCheck();

% Create a base configuration: Disable all available outputs, awgs,...
ziDisableEverything(device);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Device settings 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% 'system/awg/channelgrouping' : Configure how many independent sequencers
%   should run on the AWG and how the outputs are grouped by sequencer.
%   0 : 4x2 with HDAWG8; 2x2 with HDAWG4.
%   1 : 2x4 with HDAWG8; 1x4 with HDAWG4.
%   2 : 1x8 with HDAWG8.
% Configure the HDAWG to use one sequencer for each pair of output channels
ziDAQ('setInt', ['/', device '/system/awg/channelgrouping'], 0);

% Turn on signal output 1 and signal output 2
ziDAQ('setInt', ['/' device '/sigouts/0/on'], 1);
ziDAQ('setInt', ['/' device '/sigouts/1/on'], 1);

% Set the output range to 1 V
ziDAQ('setDouble', ['/' device '/sigouts/0/range'], 1.0);
ziDAQ('setDouble', ['/' device '/sigouts/1/range'], 1.0);

% Set the output amplitude to 1 V
amplitude = 1.0;
ziDAQ('setDouble', ['/' device '/awgs/0/outputs/0/gains/0'], amplitude);
ziDAQ('setDouble', ['/' device '/awgs/0/outputs/1/gains/1'], amplitude);

% Turn off modulation mode
ziDAQ('setInt', ['/' device '/awgs/0/outputs/0/modulation/mode'], 0);
ziDAQ('setInt', ['/' device '/awgs/0/outputs/1/modulation/mode'], 0);

% Clear the command table
ziDAQ('setInt', ['/' device '/awgs/0/commandtable/clear'], 1);

% Ensure the device configuration has taken effect.
ziDAQ('sync');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% AWG sequence
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% AWG SeqC program
awg_program_lines = {...
     '// Define placeholder with 1024 samples',...
     'wave p = placeholder(1024);',...
     '',...
     '// Assign placeholder to waveform index 10',...
     'assignWaveIndex(p, p, 10);',...
     '',...
     'while(true) {',...
     '   executeTableEntry(0);',...
     '}'};
awg_program = sprintf('%s\n',awg_program_lines{:});

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Command Table JSON content
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%% Read the command table from a JSON file if available. 
% json_file = 'command_table.json';
% json_str = fileread(json_file);

%%% Provide JSON string directly if there is no JSON file available
json_str_lines = {...
    '{',...
    '"$schema": "http://docs.zhinst.com/hdawg/commandtable/v2/schema",',...
    '"header": {',...
    '"version": "0.2",',...
    '"partial": false',...
    '},',...
    '"table": [',...
    '{',...
    '"index": 0,',...
    '"waveform": {',...
    '"index": 10',...
    '},',...
    '"amplitude0": {',...
    '"value": 1.0',...
    '},',...
    '"amplitude1": {',...
    '"value": 1.0',...
    '}',...
    '}',...
    ']',...
    '}'};
json_str = sprintf('%s\n', json_str_lines{:});

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Compile and upload
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Create an instance of the AWG Module
h = ziDAQ('awgModule');
ziDAQ('set', h, 'device', device);
ziDAQ('execute', h);

% Transfer the AWG sequence program. Compilation starts automatically.
ziDAQ('set', h, 'compiler/sourcestring', awg_program);
% Note: when using an AWG program from a source file (and only then), the compiler needs to
% be started explicitly with awgModule.set('compiler/start', 1)
timeout = 10;
time_start = tic;
while ziDAQ('getInt', h, 'compiler/status') == -1
    pause(0.1);
    if toc(time_start) > timeout
        ziDAQ('clear', h);
        error('Compiler failed to finish after %f s.', timeout);
    end
end

% Check the statuse of compilation
compilerStatus = ziDAQ('getInt', h, 'compiler/status');
compilerString = ziDAQ('getString', h, 'compiler/statusstring');
if compilerStatus == 1
    % Compilation failed, send an error message
    ziDAQ('clear', h);
    error(char(compilerString))
else
    if compilerStatus == 0
        disp('Compilation successful with no warnings, will upload the program to the instrument.');
    end
    if compilerStatus == 2
        disp('Compilation successful with warnings, will upload the program to the instrument.');
        disp(['Compiler warning: ' char(compilerString)]);
    end
    % Wait for waveform upload to finish
    timeout = 10;
    while ziDAQ('getDouble', h, 'progress') < 1
        pause(0.1);
        fprintf('Uploading waveform, progress: %.1f%%.\n', 100*ziDAQ('getDouble', h, 'progress'));
        if toc(time_start) > timeout
            ziDAQ('clear', h);
            error('Failed to upload waveform after %f s.', timeout);
        end
    end
end

% Release AWG module resources.
ziDAQ('clear', h);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Upload the Command Table 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

ziDAQ('setVector', ['/' device '/awgs/0/commandtable/data'], json_str);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Replace the placeholder with a waveform
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Define a Gaussian waveform 
x = 0:1:1023;
sigma = 150;
w = exp(-0.5*((x - 512)/sigma).^2);

% Upload waveform data with the index 10 (this is the index assigned with 
% the assignWaveIndex sequencer instruction
index = 10;
w_int16 = typecast(int16((pow2(15) - 1)*w), 'int16');
waveform = [w_int16; -w_int16];
ziDAQ('setVector',['/' device '/awgs/0/waveform/waves/' num2str(index)], waveform);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Enable the AWG sequencer
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

fprintf('Enabling the AWG.\n');
ziDAQ('setInt', ['/' device '/awgs/0/single'], 1);
ziDAQ('setInt', ['/' device '/awgs/0/enable'], 1);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end