Debian Codegraph @

pymeasure/instruments/thorlabs/thorlabspm100usb.py

raw
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, SCPIUnknownMixin
28 from pymeasure.instruments.validators import truncated_range
29
30 log = logging.getLogger(__name__)
31 log.addHandler(logging.NullHandler())
32
33
34 class ThorlabsPM100USB(SCPIUnknownMixin, Instrument):
35 """Represents Thorlabs PM100USB powermeter."""
36
37 def __init__(self, adapter, name="ThorlabsPM100USB powermeter", **kwargs):
38 super().__init__(
39 adapter, name, **kwargs
40 )
41 self._set_flags()
42
43 wavelength_min = Instrument.measurement(
44 "SENS:CORR:WAV? MIN", "Measure minimum wavelength, in nm"
45 )
46
47 wavelength_max = Instrument.measurement(
48 "SENS:CORR:WAV? MAX", "Measure maximum wavelength, in nm"
49 )
50
51 @property
52 def wavelength(self):
53 """Control the wavelength in nm."""
54 value = self.values("SENSE:CORR:WAV?")[0]
55 return value
56
57 @wavelength.setter
58 def wavelength(self, value):
59 """Wavelength in nm."""
60 if self.wavelength_settable:
61 # Store min and max wavelength to only request them once.
62 if not hasattr(self, "_wavelength_min"):
63 self._wavelength_min = self.wavelength_min
64 if not hasattr(self, "_wavelength_max"):
65 self._wavelength_max = self.wavelength_max
66
67 value = truncated_range(
68 value, [self._wavelength_min, self._wavelength_max]
69 )
70 self.write(f"SENSE:CORR:WAV {value}")
71 else:
72 raise AttributeError(
73 f"{self.sensor_name} does not allow setting the wavelength."
74 )
75
76 @property
77 def power(self):
78 """Measure the power in W."""
79 if self.is_power_sensor:
80 return self.values("MEAS:POW?")[0]
81 else:
82 raise AttributeError(f"{self.sensor_name} is not a power sensor.")
83
84 @property
85 def energy(self):
86 """Measure the energy in J."""
87 if self.is_energy_sensor:
88 return self.values("MEAS:ENER?")[0]
89 else:
90 raise AttributeError(
91 f"{self.sensor_name} is not an energy sensor."
92 )
93
94 def _set_flags(self):
95 """Get sensor info and write flags."""
96 response = self.values("SYST:SENSOR:IDN?")
97 if response[0] == "no sensor":
98 raise OSError("No sensor connected.")
99 self.sensor_name = response[0]
100 self.sensor_sn = response[1]
101 self.sensor_cal_msg = response[2]
102 self.sensor_type = response[3]
103 self.sensor_subtype = response[4]
104 _flags_str = response[5]
105
106 # interpretation of the flags, see p. 49 of the manual:
107 # https://www.thorlabs.de/_sd.cfm?fileName=17654-D02.pdf&partNumber=PM100D
108
109 # Convert to binary representation and pad zeros to 9 bit for sensors
110 # where not all flags are present.
111 _flags_str = format(int(_flags_str), "09b")
112 # Reverse the order so it matches the flag order from the manual, i.e.
113 # from decimal values from 1 to 256.
114 _flags_str = _flags_str[::-1]
115
116 # Convert to boolean.
117 self.flags = [x == "1" for x in _flags_str]
118
119 # setting the flags; _dn are unused; decimal values as comments
120 (
121 self.is_power_sensor, # 1
122 self.is_energy_sensor, # 2
123 _d4, # 4
124 _d8, # 8
125 self.response_settable, # 16
126 self.wavelength_settable, # 32
127 self.tau_settable, # 64
128 _d128, # 128
129 self.has_temperature_sensor, # 256
130 ) = self.flags