選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

206 行
6.9KB

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