| 1 |
# |
| 2 |
# This file is part of the PyMeasure package. |
| 3 |
# |
| 4 |
# Copyright (c) 2013-2024 PyMeasure Developers |
| 5 |
# |
| 6 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
# of this software and associated documentation files (the "Software"), to deal |
| 8 |
# in the Software without restriction, including without limitation the rights |
| 9 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 |
# copies of the Software, and to permit persons to whom the Software is |
| 11 |
# furnished to do so, subject to the following conditions: |
| 12 |
# |
| 13 |
# The above copyright notice and this permission notice shall be included in |
| 14 |
# all copies or substantial portions of the Software. |
| 15 |
# |
| 16 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 |
# THE SOFTWARE. |
| 23 |
# |
| 24 |
|
| 25 |
import logging |
| 26 |
|
| 27 |
from pymeasure.instruments import Instrument, SCPIMixin |
| 28 |
from pymeasure.instruments.validators import (strict_discrete_set, |
| 29 |
truncated_range) |
| 30 |
|
| 31 |
log = logging.getLogger(__name__) |
| 32 |
log.addHandler(logging.NullHandler()) |
| 33 |
|
| 34 |
|
| 35 |
def process_sequence(sequence): |
| 36 |
""" |
| 37 |
Check and prepare sequence data. |
| 38 |
|
| 39 |
:param sequence: Sequence data, in the form [voltage1, current1, time1, |
| 40 |
voltage2, current2, time2, ..., voltage128, current128, time128] with |
| 41 |
voltages in V, currents in A, and times in s. Dwell times are between |
| 42 |
0.06 and 10 s. |
| 43 |
:type sequence: list of float |
| 44 |
:return: Sequence data in the form "Voltage1,Current1,Time1,Voltage2, |
| 45 |
Current2,Time2,...,Voltage128,Current128,Time128" |
| 46 |
:rtype: str |
| 47 |
""" |
| 48 |
if not len(sequence) % 3 == 0: |
| 49 |
raise ValueError("Sequence must contain multiple of 3 values.") |
| 50 |
if any(t > 10 or t < 0.06 for t in sequence[2::3]): |
| 51 |
raise ValueError("Dwell times must be between 0.06 and 10 s.") |
| 52 |
# turn sequence data into a string |
| 53 |
sequence = ",".join(str(s) for s in sequence) |
| 54 |
return sequence |
| 55 |
|
| 56 |
|
| 57 |
class HMP4040(SCPIMixin, Instrument): |
| 58 |
"""Represents a Rohde&Schwarz HMP4040 power supply.""" |
| 59 |
|
| 60 |
def __init__(self, adapter, **kwargs): |
| 61 |
kwargs.setdefault("name", "Rohde&Schwarz HMP4040") |
| 62 |
super().__init__( |
| 63 |
adapter, |
| 64 |
**kwargs |
| 65 |
) |
| 66 |
|
| 67 |
# System Setting Commands ------------------------------------------------- |
| 68 |
|
| 69 |
def beep(self): |
| 70 |
"""Emit a single beep from the instrument.""" |
| 71 |
self.write("SYST:BEEP") |
| 72 |
|
| 73 |
control_method = Instrument.setting( |
| 74 |
"SYST:%s", |
| 75 |
""" |
| 76 |
Control manual front panel ('LOC'), remote ('REM') or manual/remote |
| 77 |
control('MIX') control or locks the front panel control ('RWL'). |
| 78 |
""", |
| 79 |
validator=strict_discrete_set, |
| 80 |
values=["LOC", "REM", "MIX", "RWL"], |
| 81 |
) |
| 82 |
|
| 83 |
version = Instrument.measurement( |
| 84 |
"SYST:VERS?", |
| 85 |
"Get the SCPI version the instrument's command set complies with.", |
| 86 |
) |
| 87 |
|
| 88 |
# Channel Selection Commands ---------------------------------------------- |
| 89 |
|
| 90 |
selected_channel = Instrument.control( |
| 91 |
"INST:NSEL?", |
| 92 |
"INST:NSEL %s", |
| 93 |
"Control the selected channel.", |
| 94 |
validator=strict_discrete_set, |
| 95 |
values=[1, 2, 3, 4], |
| 96 |
cast=int |
| 97 |
) |
| 98 |
|
| 99 |
# Voltage Settings -------------------------------------------------------- |
| 100 |
|
| 101 |
voltage = Instrument.control( |
| 102 |
"VOLT?", "VOLT %s", |
| 103 |
"Control output voltage in V. Increment 0.001 V." |
| 104 |
) |
| 105 |
|
| 106 |
min_voltage = Instrument.measurement( |
| 107 |
"VOLT? MIN", "Get minimum voltage in V." |
| 108 |
) |
| 109 |
|
| 110 |
max_voltage = Instrument.measurement( |
| 111 |
"VOLT? MAX", "Get maximum voltage in V." |
| 112 |
) |
| 113 |
|
| 114 |
def voltage_to_min(self): |
| 115 |
"""Set voltage of the selected channel to its minimum value.""" |
| 116 |
self.write("VOLT MIN") |
| 117 |
|
| 118 |
def voltage_to_max(self): |
| 119 |
"""Set voltage of the selected channel to its maximum value.""" |
| 120 |
self.write("VOLT MAX") |
| 121 |
|
| 122 |
voltage_step = Instrument.control( |
| 123 |
"VOLT:STEP?", |
| 124 |
"VOLT:STEP %s", |
| 125 |
"Control voltage step in V. Default 1 V.", |
| 126 |
validator=truncated_range, |
| 127 |
values=[0, 32.050], |
| 128 |
) |
| 129 |
|
| 130 |
def step_voltage_up(self): |
| 131 |
"""Increase voltage by one step.""" |
| 132 |
self.write("VOLT UP") |
| 133 |
|
| 134 |
def step_voltage_down(self): |
| 135 |
"""Decrease voltage by one step.""" |
| 136 |
self.write("VOLT DOWN") |
| 137 |
|
| 138 |
# Current Settings -------------------------------------------------------- |
| 139 |
|
| 140 |
current = Instrument.control( |
| 141 |
"CURR?", |
| 142 |
"CURR %s", |
| 143 |
"Control output current in A. Range depends on instrument type.", |
| 144 |
) |
| 145 |
|
| 146 |
min_current = Instrument.measurement( |
| 147 |
"CURR? MIN", "Get minimum current in A." |
| 148 |
) |
| 149 |
|
| 150 |
max_current = Instrument.measurement( |
| 151 |
"CURR? MAX", "Get maximum current in A." |
| 152 |
) |
| 153 |
|
| 154 |
def current_to_min(self): |
| 155 |
"""Set current of the selected channel to its minimum value.""" |
| 156 |
self.write("CURR MIN") |
| 157 |
|
| 158 |
def current_to_max(self): |
| 159 |
"""Set current of the selected channel to its maximum value.""" |
| 160 |
self.write("CURR MAX") |
| 161 |
|
| 162 |
current_step = Instrument.control( |
| 163 |
"CURR:STEP?", "CURR:STEP %s", "Control current step in A." |
| 164 |
) |
| 165 |
|
| 166 |
def step_current_up(self): |
| 167 |
"""Increase current by one step.""" |
| 168 |
self.write("CURR UP") |
| 169 |
|
| 170 |
def step_current_down(self): |
| 171 |
"""Decreases current by one step.""" |
| 172 |
self.write("CURR DOWN") |
| 173 |
|
| 174 |
# Combined Voltage And Current Settings ----------------------------------- |
| 175 |
|
| 176 |
voltage_and_current = Instrument.control( |
| 177 |
"APPL?", |
| 178 |
"APPL %s, %s", |
| 179 |
"Control output voltage (V) and current (A).", |
| 180 |
) |
| 181 |
|
| 182 |
# Output Settings --------------------------------------------------------- |
| 183 |
|
| 184 |
selected_channel_active = Instrument.control( |
| 185 |
"OUTP:SEL?", |
| 186 |
"OUTPUT:SEL %s", |
| 187 |
"Control the selected channel to active or inactive or check its status.", |
| 188 |
values={True: 1, False: 0}, |
| 189 |
map_values=True, |
| 190 |
) |
| 191 |
|
| 192 |
output_enabled = Instrument.control( |
| 193 |
"OUTP:GEN?", |
| 194 |
"OUTP:GEN %s", |
| 195 |
"Control the output on or off or check the output status.", |
| 196 |
values={True: 1, False: 0}, |
| 197 |
map_values=True, |
| 198 |
) |
| 199 |
|
| 200 |
# The following commands are for making it easier to change the selected |
| 201 |
# channels and activate/deactivate them. |
| 202 |
|
| 203 |
def set_channel_state(self, channel, state): |
| 204 |
""" |
| 205 |
Set the state of the channel to active or inactive. |
| 206 |
|
| 207 |
:param channel: Channel number to set the state of. |
| 208 |
:type channel: int |
| 209 |
:param state: State of the channel, i.e. True for active, False for |
| 210 |
inactive. |
| 211 |
:type state: bool |
| 212 |
""" |
| 213 |
# Save current selected channel before switching. |
| 214 |
selected_channel = self.selected_channel |
| 215 |
self.selected_channel = channel |
| 216 |
self.selected_channel_active = state |
| 217 |
# Restore previously selected channel. |
| 218 |
self.selected_channel = selected_channel |
| 219 |
|
| 220 |
# Measurement Commands ---------------------------------------------------- |
| 221 |
|
| 222 |
measured_voltage = Instrument.measurement( |
| 223 |
"MEAS:VOLT?", "Get voltage in V." |
| 224 |
) |
| 225 |
|
| 226 |
measured_current = Instrument.measurement( |
| 227 |
"MEAS:CURR?", "Get current in A." |
| 228 |
) |
| 229 |
|
| 230 |
# Arbitrary Sequence Commands --------------------------------------------- |
| 231 |
|
| 232 |
def clear_sequence(self, channel): |
| 233 |
"""Clear the sequence of the selected channel.""" |
| 234 |
channel = strict_discrete_set(channel, [1, 2, 3, 4]) |
| 235 |
self.write(f"ARB:CLEAR {channel}") |
| 236 |
|
| 237 |
sequence = Instrument.setting( |
| 238 |
"ARB:DATA %s", |
| 239 |
"Set sequence of triplets of voltage (V), current (A) and dwell " |
| 240 |
"time (s).", |
| 241 |
set_process=process_sequence, |
| 242 |
) |
| 243 |
|
| 244 |
repetitions = Instrument.control( |
| 245 |
"ARB:REP?", |
| 246 |
"ARB:REP %s", |
| 247 |
"Control umber of repetitions (0...255). If 0 is entered, the sequence is" |
| 248 |
"repeated indefinitely.", |
| 249 |
validator=strict_discrete_set, |
| 250 |
values=range(256), |
| 251 |
cast=int, |
| 252 |
) |
| 253 |
|
| 254 |
def load_sequence(self, slot): |
| 255 |
"""Load a saved waveform from internal memory (slot 1, 2 or 3).""" |
| 256 |
slot = strict_discrete_set(slot, [1, 2, 3]) |
| 257 |
self.write(f"ARB:REST {slot}") |
| 258 |
|
| 259 |
def save_sequence(self, slot): |
| 260 |
""" |
| 261 |
Save the sequence defined in the sequence property to internal memory |
| 262 |
(slot 1, 2 or 3). |
| 263 |
""" |
| 264 |
slot = strict_discrete_set(slot, [1, 2, 3]) |
| 265 |
self.write(f"ARB:SAVE {slot}") |
| 266 |
|
| 267 |
def start_sequence(self, channel): |
| 268 |
"""Start the sequence of the selected channel.""" |
| 269 |
channel = strict_discrete_set(channel, [1, 2, 3, 4]) |
| 270 |
self.write(f"ARB:START {channel}") |
| 271 |
|
| 272 |
def stop_sequence(self, channel): |
| 273 |
"""Stop the sequence defined in the sequence property of the selected |
| 274 |
channel.""" |
| 275 |
channel = strict_discrete_set(channel, [1, 2, 3, 4]) |
| 276 |
self.write(f"ARB:STOP {channel}") |
| 277 |
|
| 278 |
def transfer_sequence(self, channel): |
| 279 |
""" |
| 280 |
Transfer the sequence defined in the sequence property to the selected |
| 281 |
channel. |
| 282 |
""" |
| 283 |
channel = strict_discrete_set(channel, [1, 2, 3, 4]) |
| 284 |
self.write(f"ARB:TRAN {channel}") |