Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

224 рядки
7.8KB

  1. classdef FunctionGeneratorClass < EquipmentClass
  2. % FUNCTIONGENERATORCLASS Control a function generator.
  3. % fg = FUNCTIONGENERATORCLASS('IP', PORT, GPIB_CHAN) connects
  4. % to a function generator on IPv4 address 'IP', on port
  5. % 'PORT' and GPIO channel 'GPIB_CHAN', using the Equipment
  6. % class.
  7. %
  8. % See also EQUIPMENTCLASS, OSCILLOSCOPECLASS, DIGITALMULTIMETERCLASS.
  9. properties (Dependent)
  10. waveform %Waveform shape
  11. frequency %Frequency setting in hertz
  12. voltage %Amplitude setting as defined by FUNCTIONGENERATORCLASS.UNIT
  13. unit %unit of amplitude: VPP|VRMS|DBM|DEFault
  14. offset %Offset setting in volt
  15. load %output impedence setting 50|inf
  16. output %output enable setting. Only available on 332xx and newer.
  17. end
  18. methods
  19. function fg = FunctionGeneratorClass(ipAddress,port,channel)
  20. % FUNCTIONGENERATORCLASS Control a function generator.
  21. % fg = FUNCTIONGENERATORCLASS('IP', PORT, GPIB_CHAN) connects
  22. % to a function generator on IPv4 address 'IP', on port
  23. % 'PORT' and GPIO channel 'GPIB_CHAN', using the Equipment
  24. % class.
  25. %
  26. % See also EQUIPMENTCLASS, OSCILLOSCOPECLASS, DIGITALMULTIMETERCLASS.
  27. fg@EquipmentClass(ipAddress,port,channel);
  28. end
  29. function w = get.waveform(fg)
  30. %Get the function generator waveform setting on waveform
  31. % variable access, using the corresponding SCPI command.
  32. w = FunctionGeneratorClass.getWave(fg.query('FUNCtion:SHAPe?'));
  33. end
  34. function f = get.frequency(fg)
  35. %Get function generator frequency setting on frequency variable
  36. % access, using the corresponding SCPI command.
  37. f = str2num(fg.query('FREQuency?'));
  38. end
  39. function v = get.voltage(fg)
  40. %Get function generator output voltage setting on output
  41. % variable access, using the corresponding SCPI command.
  42. v = str2num(fg.query('VOLTage?'));
  43. end
  44. function u = get.unit(fg)
  45. %Get function generator output voltage unit setting on offset
  46. % variable access, using the corresponding SCPI command.
  47. u = FunctionGeneratorClass.getUnit(fg.query('VOLTage:UNIT?'));
  48. end
  49. function o = get.offset(fg)
  50. %Get function generator offset setting on offset variable
  51. % access, using the corresponding SCPI command.
  52. o = str2num(fg.query('VOLTage:OFFSet?'));
  53. end
  54. function l = get.load(fg)
  55. %Get function generator load setting on load variable
  56. % access, using the corresponding SCPI command.
  57. l = FunctionGeneratorClass.getLoad(fg.query('OUTPut:LOAD?'));
  58. end
  59. function out = get.output(fg)
  60. if strcmp(fg.model(1:3),'335') || strcmp(fg.model(1:3),'332')
  61. out = fg.query('OUTP?');
  62. else
  63. warning('This generator does not support this function');
  64. end
  65. end
  66. function fg = set.waveform(fg,w)
  67. %Set function generator waveform setting on waveform variable
  68. % access, using the corresponding SCPI command.
  69. fg.write(['FUNCtion:SHAPe ' FunctionGeneratorClass.getWave(w)]);
  70. end
  71. function fg = set.frequency(fg,f)
  72. %Set function generator frequency setting on frequency variable
  73. % access, using the corresponding SCPI command.
  74. Equipment.forceNum(f);
  75. fg.write(['FREQuency ' num2str(f)]);
  76. end
  77. function fg = set.voltage(fg,v)
  78. %Set function generator output voltage on voltage variable
  79. % access, using the corresponding SCPI command.
  80. Equipment.forceNum(v);
  81. fg.write(['VOLTage ' num2str(v)]);
  82. end
  83. function fg = set.unit(fg,u)
  84. %Set function generator output voltage unit setting on unit
  85. % variable access, using the corresponding SCPI command.
  86. fg.write(['VOLTage:UNIT ' FunctionGeneratorClass.getUnit(u)]);
  87. end
  88. function fg = set.offset(fg,o)
  89. %Set function generator offset setting on offset variable access,
  90. % using the corresponding SCPI command.
  91. Equipment.forceNum(o);
  92. fg.write(['VOLTage:OFFSet ' num2str(o)]);
  93. end
  94. function fg = set.load(fg,l)
  95. %Set function generator load setting on load variable access,
  96. % using the corresponding SCPI command.
  97. fg.write(['OUTPut:LOAD ' FunctionGeneratorClass.getLoad(l)]);
  98. end
  99. function fg = set.output(fg,out)
  100. if strcmp(fg.model(1:3),'335') || strcmp(fg.model(1:3),'332')
  101. fg.write(['OUTP ' EquipmentClass.optionnum2str(out)])
  102. end
  103. end
  104. function downloadwaveform(fg,waveform,name)
  105. switch fg.model
  106. case '33120A'
  107. downloadwaveform_legacy(fg,waveform,name);
  108. otherwise
  109. downloadwaveform_new(fg,waveform,name);
  110. end
  111. end
  112. function downloadwaveform_legacy(fg,waveform,name)
  113. name_trunc = name(1:min(length(name),8));
  114. fg.opc;
  115. fg.write_noerror(['data volatile' num2str(waveform,',%0.4f')]);
  116. fg.opc;
  117. fg.error;
  118. fg.write_noerror(['data:copy ' name_trunc]);
  119. fg.opc;
  120. fg.error;
  121. fg.write(['func:user' name_trunc]);
  122. end
  123. function downloadwaveform_new(fg,waveform,name)
  124. fg.disableEOI;
  125. fg.write_unsafe(['data:arb ' name ',']);
  126. fg.enableEOI;
  127. binblockwrite(fg.tcp,waveform,'float')
  128. fg.error;
  129. fg.write(['func:arb ' name])
  130. fg.write(['MMEM:STORE:DATA "INT:\' name '.arb"'])
  131. end
  132. function scintilla(fg,periods)
  133. periods = round(periods);
  134. x = linspace(-periods*pi,periods*pi,1600);
  135. y = exp(-abs(x./6)).*sin(x)./0.8;
  136. fg.downloadwaveform(y,'scintilla');
  137. end
  138. end
  139. methods (Static)
  140. function w = getWave(win)
  141. %GETWAVE Returns the waveform ('sinusoid', 'square', 'triangle',
  142. % 'ramp', 'noise', 'dc', 'user', 'minimum' or 'maximum') from
  143. % function generator output or (possibly malformed) user input.
  144. %Match two letter inputs first, single letters or single numbers
  145. % second. 's' is a special case (case sensitive for sine or Square).
  146. % Longer inputs are matched and reduced to two letters.
  147. w = regexp(num2str(win), '(si|sq|mi|ma|[trndu1-7]|s)', 'match', 'once', 'ignorecase');
  148. %Error if not matched.
  149. if isempty(w)
  150. error(['Invalid waveform: ' win]);
  151. end
  152. %Replace two letter, single letter or single number by correct
  153. % waveform.
  154. w = regexprep(w, {'^(1|si|(?-i)s)$', '^(2|sq|(?-i)S)$', '^[3t]$', '^[4r]$', '^[5n]$', '^[6d]$', '^[7u]$', '^mi$', '^ma$'}, ...
  155. {'sinusoid', 'square', 'triangle', 'ramp', 'noise', 'dc', 'user', 'minimum', 'maximum'}, 'ignorecase');
  156. end
  157. function u = getUnit(uin)
  158. %GETUNIT Returns the unit ('Vpp', 'Vrms', 'dBm' or 'default')
  159. % from function generator output or (possibly malformed) user
  160. % input.
  161. %Match correct single number or double letter inputs.
  162. % Longer inputs are matched and reduced to two letters.
  163. u = regexp(num2str(uin), '([1-4]|vp|vr|db|de)', 'match', 'once', 'ignorecase');
  164. %Error if not matched.
  165. if isempty(u)
  166. error(['Invalid voltage unit: ' uin]);
  167. end
  168. %Replace two letter or single number by correct unit.
  169. u = regexprep(u, {'^(1|vp)$', '^(2|vr)$', '^(3|db)$', '^(4|de)$'}, ...
  170. {'Vpp', 'Vrms', 'dBm', 'default'}, 'ignorecase');
  171. end
  172. function u = getLoad(lin)
  173. %GETLOAD Returns the load setting ('50', 'infinity', 'minimum',
  174. % or 'maximum') from function generator output or
  175. % (possibly malformed) user input.
  176. %Match correct number or double letter inputs.
  177. % Longer inputs are matched and reduced to two letters.
  178. l = regexp(num2str(lin), '([12]|50|in|mi|ma)', 'match', 'once', 'ignorecase');
  179. %Error if not matched.
  180. if isempty(l)
  181. error(['Invalid load: "' lin '".']);
  182. end
  183. %Replace two letter or single number by correct load setting.
  184. u = regexprep(l, {'^(1|50)$', '^(2|in)$', '^mi$', '^ma$'}, ...
  185. {'50', 'infinity', 'minimum', 'maximum'}, 'ignorecase');
  186. end
  187. end
  188. end