Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

172 lignes
5.7KB

  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. end
  19. methods
  20. function fg = FunctionGenerator(ipAddress,port,channel)
  21. %% FUNCTION_GENERATOR Control a function generator.
  22. % fg = FUNCTION_GENERATOR('IP', PORT, GPIB_CHAN) connects
  23. % to a function generator on IPv4 address 'IP', on port
  24. % 'PORT' and GPIO channel 'GPIB_CHAN', using the Equipment
  25. % class.
  26. %
  27. % See also EQUIPMENT, OSCILLOSCOPE, DIGITALMULTIMETER.
  28. fg@Equipment(ipAddress,port,channel);
  29. end
  30. function w = get.waveform(fg)
  31. %Get the function generator waveform setting on waveform
  32. % variable access, using the corresponding SCPI command.
  33. w = FunctionGenerator.getWave(fg.query('FUNCtion:SHAPe?'));
  34. end
  35. function f = get.frequency(fg)
  36. %Get function generator frequency setting on frequency variable
  37. % access, using the corresponding SCPI command.
  38. f = str2num(fg.query('FREQuency?'));
  39. end
  40. function v = get.voltage(fg)
  41. %Get function generator output voltage setting on output
  42. % variable access, using the corresponding SCPI command.
  43. v = str2num(fg.query('VOLTage?'));
  44. end
  45. function u = get.unit(fg)
  46. %Get function generator output voltage unit setting on offset
  47. % variable access, using the corresponding SCPI command.
  48. u = FunctionGenerator.getUnit(fg.query('VOLTage:UNIT?'));
  49. end
  50. function o = get.offset(fg)
  51. %Get function generator offset setting on offset variable
  52. % access, using the corresponding SCPI command.
  53. o = str2num(fg.query('VOLTage:OFFSet?'));
  54. end
  55. function l = get.load(fg)
  56. %Get function generator load setting on load variable
  57. % access, using the corresponding SCPI command.
  58. l = FunctionGenerator.getLoad(fg.query('OUTPut:LOAD?'));
  59. end
  60. function fg = set.waveform(fg,w)
  61. %Set function generator waveform setting on waveform variable
  62. % access, using the corresponding SCPI command.
  63. fg.write(['FUNCtion:SHAPe ' FunctionGenerator.getWave(w)]);
  64. end
  65. function fg = set.frequency(fg,f)
  66. %Set function generator frequency setting on frequency variable
  67. % access, using the corresponding SCPI command.
  68. Equipment.forceNum(f);
  69. fg.write(['FREQuency ' num2str(f)]);
  70. end
  71. function fg = set.voltage(fg,v)
  72. %Set function generator output voltage on voltage variable
  73. % access, using the corresponding SCPI command.
  74. Equipment.forceNum(v);
  75. fg.write(['VOLTage ' num2str(v)]);
  76. end
  77. function fg = set.unit(fg,u)
  78. %Set function generator output voltage unit setting on unit
  79. % variable access, using the corresponding SCPI command.
  80. fg.write(['VOLTage:UNIT ' FunctionGenerator.getUnit(u)]);
  81. end
  82. function fg = set.offset(fg,o)
  83. %Set function generator offset setting on offset variable access,
  84. % using the corresponding SCPI command.
  85. Equipment.forceNum(o);
  86. fg.write(['VOLTage:OFFSet ' num2str(o)]);
  87. end
  88. function fg = set.load(fg,l)
  89. %Set function generator load setting on load variable access,
  90. % using the corresponding SCPI command.
  91. fg.write(['OUTPut:LOAD ' FunctionGenerator.getLoad(l)]);
  92. end
  93. end
  94. methods (Static)
  95. function w = getWave(win)
  96. %GETWAVE Returns the waveform ('sinusoid', 'square', 'triangle',
  97. % 'ramp', 'noise', 'dc', 'user', 'minimum' or 'maximum') from
  98. % function generator output or (possibly malformed) user input.
  99. %Match two letter inputs first, single letters or single numbers
  100. % second. 's' is a special case (case sensitive for sine or Square).
  101. % Longer inputs are matched and reduced to two letters.
  102. w = regexp(num2str(win), '(si|sq|mi|ma|[trndu1-7]|s)', 'match', 'once', 'ignorecase');
  103. %Error if not matched.
  104. if isempty(w)
  105. error(['Invalid waveform: ' win]);
  106. end
  107. %Replace two letter, single letter or single number by correct
  108. % waveform.
  109. w = regexprep(w, {'^(1|si|(?-i)s)$', '^(2|sq|(?-i)S)$', '^[3t]$', '^[4r]$', '^[5n]$', '^[6d]$', '^[7u]$', '^mi$', '^ma$'}, ...
  110. {'sinusoid', 'square', 'triangle', 'ramp', 'noise', 'dc', 'user', 'minimum', 'maximum'}, 'ignorecase');
  111. end
  112. function u = getUnit(uin)
  113. %GETUNIT Returns the unit ('Vpp', 'Vrms', 'dBm' or 'default')
  114. % from function generator output or (possibly malformed) user
  115. % input.
  116. %Match correct single number or double letter inputs.
  117. % Longer inputs are matched and reduced to two letters.
  118. u = regexp(num2str(uin), '([1-4]|vp|vr|db|de)', 'match', 'once', 'ignorecase');
  119. %Error if not matched.
  120. if isempty(u)
  121. error(['Invalid voltage unit: ' uin]);
  122. end
  123. %Replace two letter or single number by correct unit.
  124. u = regexprep(u, {'^(1|vp)$', '^(2|vr)$', '^(3|db)$', '^(4|de)$'}, ...
  125. {'Vpp', 'Vrms', 'dBm', 'default'}, 'ignorecase');
  126. end
  127. function u = getLoad(lin)
  128. %GETLOAD Returns the load setting ('50', 'infinity', 'minimum',
  129. % or 'maximum') from function generator output or
  130. % (possibly malformed) user input.
  131. %Match correct number or double letter inputs.
  132. % Longer inputs are matched and reduced to two letters.
  133. l = regexp(num2str(lin), '([12]|50|in|mi|ma)', 'match', 'once', 'ignorecase');
  134. %Error if not matched.
  135. if isempty(l)
  136. error(['Invalid load: "' lin '".']);
  137. end
  138. %Replace two letter or single number by correct load setting.
  139. u = regexprep(l, {'^(1|50)$', '^(2|in)$', '^mi$', '^ma$'}, ...
  140. {'50', 'infinity', 'minimum', 'maximum'}, 'ignorecase');
  141. end
  142. end
  143. end