| @@ -1,8 +1,15 @@ | |||||
| classdef FunctionGenerator < Equipment | classdef FunctionGenerator < Equipment | ||||
| %FUNCTION_GENERATOR Summary of this class goes here | |||||
| % Detailed explanation goes here | |||||
| %% FUNCTION_GENERATOR Control a function generator. | |||||
| % fg = FUNCTION_GENERATOR('IP', PORT, GPIB_CHAN) connects | |||||
| % to a function generator on IPv4 address 'IP', on port | |||||
| % 'PORT' and GPIO channel 'GPIB_CHAN', using the Equipment | |||||
| % class. | |||||
| % | |||||
| % See also EQUIPMENT, OSCILLOSCOPE, DIGITALMULTIMETER. | |||||
| properties (Dependent) | properties (Dependent) | ||||
| %Dependent properties; stored on function generator, | |||||
| % read from or written to function generator when required. | |||||
| waveform | waveform | ||||
| frequency | frequency | ||||
| voltage | voltage | ||||
| @@ -13,91 +20,148 @@ classdef FunctionGenerator < Equipment | |||||
| methods | methods | ||||
| function fg = FunctionGenerator(ipAddress,port,channel) | function fg = FunctionGenerator(ipAddress,port,channel) | ||||
| %% FUNCTION_GENERATOR Control a function generator. | |||||
| % fg = FUNCTION_GENERATOR('IP', PORT, GPIB_CHAN) connects | |||||
| % to a function generator on IPv4 address 'IP', on port | |||||
| % 'PORT' and GPIO channel 'GPIB_CHAN', using the Equipment | |||||
| % class. | |||||
| % | |||||
| % See also EQUIPMENT, OSCILLOSCOPE, DIGITALMULTIMETER. | |||||
| fg@Equipment(ipAddress,port,channel); | fg@Equipment(ipAddress,port,channel); | ||||
| end | end | ||||
| function w = get.waveform(fg) | function w = get.waveform(fg) | ||||
| %Get the function generator waveform setting on waveform | |||||
| % variable access, using the corresponding SCPI command. | |||||
| w = FunctionGenerator.getWave(fg.query('FUNCtion:SHAPe?')); | w = FunctionGenerator.getWave(fg.query('FUNCtion:SHAPe?')); | ||||
| end | end | ||||
| function f = get.frequency(fg) | function f = get.frequency(fg) | ||||
| %Get function generator frequency setting on frequency variable | |||||
| % access, using the corresponding SCPI command. | |||||
| f = str2num(fg.query('FREQuency?')); | f = str2num(fg.query('FREQuency?')); | ||||
| end | end | ||||
| function v = get.voltage(fg) | function v = get.voltage(fg) | ||||
| %Get function generator output voltage setting on output | |||||
| % variable access, using the corresponding SCPI command. | |||||
| v = str2num(fg.query('VOLTage?')); | v = str2num(fg.query('VOLTage?')); | ||||
| end | end | ||||
| function u = get.unit(fg) | function u = get.unit(fg) | ||||
| %Get function generator output voltage unit setting on offset | |||||
| % variable access, using the corresponding SCPI command. | |||||
| u = FunctionGenerator.getUnit(fg.query('VOLTage:UNIT?')); | u = FunctionGenerator.getUnit(fg.query('VOLTage:UNIT?')); | ||||
| end | end | ||||
| function o = get.offset(fg) | function o = get.offset(fg) | ||||
| %Get function generator offset setting on offset variable | |||||
| % access, using the corresponding SCPI command. | |||||
| o = str2num(fg.query('VOLTage:OFFSet?')); | o = str2num(fg.query('VOLTage:OFFSet?')); | ||||
| end | end | ||||
| function l = get.load(fg) | function l = get.load(fg) | ||||
| %Get function generator load setting on load variable | |||||
| % access, using the corresponding SCPI command. | |||||
| l = FunctionGenerator.getLoad(fg.query('OUTPut:LOAD?')); | l = FunctionGenerator.getLoad(fg.query('OUTPut:LOAD?')); | ||||
| end | end | ||||
| function fg = set.waveform(fg,w) | function fg = set.waveform(fg,w) | ||||
| %Set function generator waveform setting on waveform variable | |||||
| % access, using the corresponding SCPI command. | |||||
| fg.write(['FUNCtion:SHAPe ' FunctionGenerator.getWave(w)]); | fg.write(['FUNCtion:SHAPe ' FunctionGenerator.getWave(w)]); | ||||
| end | end | ||||
| function fg = set.frequency(fg,f) | function fg = set.frequency(fg,f) | ||||
| %Set function generator frequency setting on frequency variable | |||||
| % access, using the corresponding SCPI command. | |||||
| Equipment.forceNum(f); | Equipment.forceNum(f); | ||||
| fg.write(['FREQuency ' num2str(f)]); | fg.write(['FREQuency ' num2str(f)]); | ||||
| end | end | ||||
| function fg = set.voltage(fg,v) | function fg = set.voltage(fg,v) | ||||
| %Set function generator output voltage on voltage variable | |||||
| % access, using the corresponding SCPI command. | |||||
| Equipment.forceNum(v); | Equipment.forceNum(v); | ||||
| fg.write(['VOLTage ' num2str(v)]); | fg.write(['VOLTage ' num2str(v)]); | ||||
| end | end | ||||
| function fg = set.unit(fg,u) | function fg = set.unit(fg,u) | ||||
| %Set function generator output voltage unit setting on unit | |||||
| % variable access, using the corresponding SCPI command. | |||||
| fg.write(['VOLTage:UNIT ' FunctionGenerator.getUnit(u)]); | fg.write(['VOLTage:UNIT ' FunctionGenerator.getUnit(u)]); | ||||
| end | end | ||||
| function fg = set.offset(fg,o) | function fg = set.offset(fg,o) | ||||
| %Set function generator offset setting on offset variable access, | |||||
| % using the corresponding SCPI command. | |||||
| Equipment.forceNum(o); | Equipment.forceNum(o); | ||||
| fg.write(['VOLTage:OFFSet ' num2str(o)]); | fg.write(['VOLTage:OFFSet ' num2str(o)]); | ||||
| end | end | ||||
| function fg = set.load(fg,l) | function fg = set.load(fg,l) | ||||
| %Set function generator load setting on load variable access, | |||||
| % using the corresponding SCPI command. | |||||
| fg.write(['OUTPut:LOAD ' FunctionGenerator.getLoad(l)]); | fg.write(['OUTPut:LOAD ' FunctionGenerator.getLoad(l)]); | ||||
| end | end | ||||
| end | end | ||||
| methods (Static) | methods (Static) | ||||
| function w = getWave(win) | function w = getWave(win) | ||||
| %GETWAVE Returns the waveform ('sinusoid', 'square', 'triangle', | |||||
| % 'ramp', 'noise', 'dc', 'user', 'minimum' or 'maximum') from | |||||
| % function generator output or (possibly malformed) user input. | |||||
| %Match two letter inputs first, single letters or single numbers | |||||
| % second. 's' is a special case (case sensitive for sine or Square). | |||||
| % Longer inputs are matched and reduced to two letters. | |||||
| w = regexp(num2str(win), '(si|sq|mi|ma|[trndu1-7]|s)', 'match', 'once', 'ignorecase'); | w = regexp(num2str(win), '(si|sq|mi|ma|[trndu1-7]|s)', 'match', 'once', 'ignorecase'); | ||||
| %Error if not matched. | |||||
| if isempty(w) | if isempty(w) | ||||
| error(['Invalid waveform: ' win]); | error(['Invalid waveform: ' win]); | ||||
| end | end | ||||
| %Replace two letter, single letter or single number by correct | |||||
| % waveform. | |||||
| w = regexprep(w, {'^(1|si|(?-i)s)$', '^(2|sq|(?-i)S)$', '^[3t]$', '^[4r]$', '^[5n]$', '^[6d]$', '^[7u]$', '^mi$', '^ma$'}, ... | w = regexprep(w, {'^(1|si|(?-i)s)$', '^(2|sq|(?-i)S)$', '^[3t]$', '^[4r]$', '^[5n]$', '^[6d]$', '^[7u]$', '^mi$', '^ma$'}, ... | ||||
| {'sinusoid', 'square', 'triangle', 'ramp', 'noise', 'dc', 'user', 'minimum', 'maximum'}, 'ignorecase'); | {'sinusoid', 'square', 'triangle', 'ramp', 'noise', 'dc', 'user', 'minimum', 'maximum'}, 'ignorecase'); | ||||
| end | end | ||||
| function u = getUnit(uin) | function u = getUnit(uin) | ||||
| %GETUNIT Returns the unit ('Vpp', 'Vrms', 'dBm' or 'default') | |||||
| % from function generator output or (possibly malformed) user | |||||
| % input. | |||||
| %Match correct single number or double letter inputs. | |||||
| % Longer inputs are matched and reduced to two letters. | |||||
| u = regexp(num2str(uin), '([1-4]|vp|vr|db|de)', 'match', 'once', 'ignorecase'); | u = regexp(num2str(uin), '([1-4]|vp|vr|db|de)', 'match', 'once', 'ignorecase'); | ||||
| %Error if not matched. | |||||
| if isempty(u) | if isempty(u) | ||||
| error(['Invalid voltage unit: ' uin]); | error(['Invalid voltage unit: ' uin]); | ||||
| end | end | ||||
| %Replace two letter or single number by correct unit. | |||||
| u = regexprep(u, {'^(1|vp)$', '^(2|vr)$', '^(3|db)$', '^(4|de)$'}, ... | u = regexprep(u, {'^(1|vp)$', '^(2|vr)$', '^(3|db)$', '^(4|de)$'}, ... | ||||
| {'Vpp', 'Vrms', 'dBm', 'default'}, 'ignorecase'); | {'Vpp', 'Vrms', 'dBm', 'default'}, 'ignorecase'); | ||||
| end | end | ||||
| function u = getLoad(lin) | function u = getLoad(lin) | ||||
| %GETLOAD Returns the load setting ('50', 'infinity', 'minimum', | |||||
| % or 'maximum') from function generator output or | |||||
| % (possibly malformed) user input. | |||||
| %Match correct number or double letter inputs. | |||||
| % Longer inputs are matched and reduced to two letters. | |||||
| l = regexp(num2str(lin), '([12]|50|in|mi|ma)', 'match', 'once', 'ignorecase'); | l = regexp(num2str(lin), '([12]|50|in|mi|ma)', 'match', 'once', 'ignorecase'); | ||||
| %Error if not matched. | |||||
| if isempty(l) | if isempty(l) | ||||
| error(['Invalid load: "' lin '".']); | error(['Invalid load: "' lin '".']); | ||||
| end | end | ||||
| %Replace two letter or single number by correct load setting. | |||||
| u = regexprep(l, {'^(1|50)$', '^(2|in)$', '^mi$', '^ma$'}, ... | u = regexprep(l, {'^(1|50)$', '^(2|in)$', '^mi$', '^ma$'}, ... | ||||
| {'50', 'infinity', 'minimum', 'maximum'}, 'ignorecase'); | {'50', 'infinity', 'minimum', 'maximum'}, 'ignorecase'); | ||||
| end | end | ||||