|
| 1 | +from lewis.devices import Device |
| 2 | + |
| 3 | + |
| 4 | +class SimulatedAG33220A(Device): |
| 5 | + """Simulated AG33220A |
| 6 | + """ |
| 7 | + |
| 8 | + connected = True |
| 9 | + |
| 10 | + # Constants |
| 11 | + AMP_MIN = 0.01 |
| 12 | + AMP_MAX = 10 |
| 13 | + OFF_MAX = 4.995 |
| 14 | + VOLT_MAX = 5 |
| 15 | + VOLT_MIN = -5 |
| 16 | + VOLT_LOW_MAX = 4.99 |
| 17 | + VOLT_HIGH_MIN = -4.99 |
| 18 | + VOLT_PRECISION = 0.01 |
| 19 | + FREQ_MINS = { |
| 20 | + "SIN": 10**-6, |
| 21 | + "SQU": 10**-6, |
| 22 | + "RAMP": 10**-6, |
| 23 | + "PULS": 5 * 10**-4, |
| 24 | + "NOIS": 10**-6, |
| 25 | + "USER": 10**-6, |
| 26 | + } |
| 27 | + FREQ_MAXS = { |
| 28 | + "SIN": 2 * 10**7, |
| 29 | + "SQU": 2 * 10**7, |
| 30 | + "RAMP": 2 * 10**5, |
| 31 | + "PULS": 5 * 10**6, |
| 32 | + "NOIS": 2 * 10**7, |
| 33 | + "USER": 6 * 10**6, |
| 34 | + } |
| 35 | + |
| 36 | + # Device variables |
| 37 | + idn = "Agilent Technologies,33220A-MY44033103,2.02-2.02-22-2" |
| 38 | + amplitude = 0.1 |
| 39 | + frequency = 1000 |
| 40 | + offset = 0 |
| 41 | + units = "VPP" |
| 42 | + function = "SIN" |
| 43 | + output = "ON" |
| 44 | + voltage_high = 0.05 |
| 45 | + voltage_low = -0.05 |
| 46 | + range_auto = "OFF" |
| 47 | + |
| 48 | + def limit(self, value, minimum, maximum): |
| 49 | + """Limits an input number between two given numbers or sets the value to the maximum or minimum. |
| 50 | +
|
| 51 | + :param value: the value to be limited |
| 52 | + :param minimum: the smallest that the value can be |
| 53 | + :param maximum: the largest that the value can be |
| 54 | +
|
| 55 | + :return: the value after it has been limited |
| 56 | + """ |
| 57 | + if type(value) is str: |
| 58 | + try: |
| 59 | + value = float(value) |
| 60 | + except ValueError: |
| 61 | + return {"MIN": minimum, "MAX": maximum}[value] |
| 62 | + |
| 63 | + return max(min(value, maximum), minimum) |
| 64 | + |
| 65 | + def set_new_amplitude(self, new_amplitude): |
| 66 | + """Changing the amplitude to the new amplitude whilst also changing the offset if voltage high or low is |
| 67 | + outside the boundary. The volt high and low are then updated. |
| 68 | +
|
| 69 | + :param new_amplitude: the amplitude to set the devices amplitude to |
| 70 | + """ |
| 71 | + new_amplitude = self.limit(new_amplitude, self.AMP_MIN, self.AMP_MAX) |
| 72 | + |
| 73 | + peak_amp = 0.5 * new_amplitude |
| 74 | + if self.offset + peak_amp > self.VOLT_MAX: |
| 75 | + self.offset = self.VOLT_MAX - peak_amp |
| 76 | + elif self.offset - peak_amp < self.VOLT_MIN: |
| 77 | + self.offset = self.VOLT_MIN + peak_amp |
| 78 | + |
| 79 | + self.amplitude = new_amplitude |
| 80 | + |
| 81 | + self._update_volt_high_and_low(self.amplitude, self.offset) |
| 82 | + |
| 83 | + def set_new_frequency(self, new_frequency): |
| 84 | + """Sets the frequency within limits between upper and lower bound (depends on the function). |
| 85 | +
|
| 86 | + :param new_frequency: the frequency to set to |
| 87 | + """ |
| 88 | + self.frequency = self.limit( |
| 89 | + new_frequency, self.FREQ_MINS[self.function], self.FREQ_MAXS[self.function] |
| 90 | + ) |
| 91 | + |
| 92 | + def set_new_voltage_high(self, new_voltage_high): |
| 93 | + """Sets a new voltage high which then changes the voltage low to keep it lower. |
| 94 | + The voltage offset and amplitude are then updated. |
| 95 | +
|
| 96 | + :param new_voltage_high: the value of voltage high to set to |
| 97 | + """ |
| 98 | + new_voltage_high = self.limit(new_voltage_high, self.VOLT_HIGH_MIN, self.VOLT_MAX) |
| 99 | + if new_voltage_high <= self.voltage_low: |
| 100 | + self.voltage_low = self.limit( |
| 101 | + new_voltage_high - self.VOLT_PRECISION, self.VOLT_MIN, new_voltage_high |
| 102 | + ) |
| 103 | + self._update_volt_and_offs(self.voltage_low, new_voltage_high) |
| 104 | + |
| 105 | + def set_new_voltage_low(self, new_voltage_low): |
| 106 | + """Sets a new voltage high which then changes the voltage low to keep it higher. |
| 107 | + The voltage offset and amplitude are then updated. |
| 108 | +
|
| 109 | + :param new_voltage_low: the value of voltage low which is to be set |
| 110 | + """ |
| 111 | + new_voltage_low = self.limit(new_voltage_low, self.VOLT_MIN, self.VOLT_LOW_MAX) |
| 112 | + if new_voltage_low >= self.voltage_high: |
| 113 | + self.voltage_high = self.limit( |
| 114 | + new_voltage_low + self.VOLT_PRECISION, new_voltage_low, self.VOLT_MAX |
| 115 | + ) |
| 116 | + self._update_volt_and_offs(new_voltage_low, self.voltage_high) |
| 117 | + |
| 118 | + def _update_volt_and_offs(self, new_low, new_high): |
| 119 | + """Updates the value of amplitude and offset if there is a change in voltage low or voltage high. |
| 120 | +
|
| 121 | + :param new_low: the value of voltage low |
| 122 | + :param new_high: the value of voltage high |
| 123 | + """ |
| 124 | + self.voltage_high = new_high |
| 125 | + self.voltage_low = new_low |
| 126 | + self.amplitude = self.voltage_high - self.voltage_low |
| 127 | + self.offset = (self.voltage_high + self.voltage_low) / 2 |
| 128 | + |
| 129 | + def set_offs_and_update_voltage(self, new_offset): |
| 130 | + """Sets the value of offset and updates the amplitude, voltage low and voltage high for a new value of the offset. |
| 131 | +
|
| 132 | + :param new_offset: the new offset to be set |
| 133 | + """ |
| 134 | + new_offset = self.limit(new_offset, -self.OFF_MAX, self.OFF_MAX) |
| 135 | + if new_offset + self.voltage_high > self.VOLT_MAX: |
| 136 | + self.amplitude = 2 * (self.VOLT_MAX - new_offset) |
| 137 | + self.voltage_high = self.VOLT_MAX |
| 138 | + self.voltage_low = self.VOLT_MAX - self.amplitude |
| 139 | + elif new_offset + self.voltage_low < self.VOLT_MIN: |
| 140 | + self.amplitude = 2 * (self.VOLT_MIN - new_offset) |
| 141 | + self.voltage_low = self.VOLT_MIN |
| 142 | + self.voltage_high = self.VOLT_MIN + self.amplitude |
| 143 | + else: |
| 144 | + self._update_volt_high_and_low(self.amplitude, new_offset) |
| 145 | + self.offset = new_offset |
| 146 | + |
| 147 | + def _update_volt_high_and_low(self, new_volt, new_offs): |
| 148 | + """Updates the value of voltage high and low for a given value of amplitude and offset. |
| 149 | +
|
| 150 | + :param new_volt: the value of the amplitude |
| 151 | + :param new_offs: the value of the offset |
| 152 | + """ |
| 153 | + self.offset = new_offs |
| 154 | + self.amplitude = new_volt |
| 155 | + self.voltage_high = new_offs + new_volt / 2 |
| 156 | + self.voltage_low = new_offs - new_volt / 2 |
| 157 | + |
| 158 | + def get_output(self): |
| 159 | + return ["OFF", "ON"].index(self.output) |
| 160 | + |
| 161 | + def get_range_auto(self): |
| 162 | + possible_ranges = ["OFF", "ON", "ONCE"] |
| 163 | + return possible_ranges.index(self.range_auto) |
| 164 | + |
| 165 | + def set_function(self, new_function): |
| 166 | + self.function = new_function |
| 167 | + self.frequency = self.limit( |
| 168 | + self.frequency, self.FREQ_MINS[new_function], self.FREQ_MAXS[new_function] |
| 169 | + ) |
0 commit comments