To receive notifications about scheduled maintenance, please subscribe to the mailing-list gitlab-operations@sympa.ethz.ch. You can subscribe to the mailing-list at https://sympa.ethz.ch

Commit caa33dd1 authored by mikolajr's avatar mikolajr
Browse files

In PfeifferTPG device: refactor get full scale methods common code

parent 4506bb0f
......@@ -28,6 +28,14 @@ from ..utils.enum import NameEnum
Number = Union[int, float]
class PfeifferTPGError(Exception):
"""
Error with the Pfeiffer TPG Controller.
"""
pass
@configdataclass
class PfeifferTPGSerialCommunicationConfig(SerialCommunicationConfig):
#: Baudrate for Pfeiffer TPG controllers is 9600 baud
......@@ -402,10 +410,21 @@ class PfeifferTPG(SingleCommDevice):
return ret
def _set_full_scale(self, fsr: List[int], unitless: bool) -> None:
"""
Set the full scale range of the attached sensors. See lookup table between
command and corresponding pressure in the device user manual.
:param fsr: list of full scale range values, like `[0, 1, 3, 3, 2, 0]` for
`unitless = True` scale or `[0.01, 1000]` otherwise (mbar units scale)
:param unitless: flag to indicate scale of range values; if `False` then mbar
units scale
:raises SerialCommunicationIOError: when communication port is not opened
:raises PfeifferTPGError: if command fails
"""
if len(fsr) != self.number_of_sensors:
raise ValueError(
f"Argument fsr should be of length "
f"{self.number_of_sensors}. Received length {len(fsr)}."
f"Argument fsr should be of length {self.number_of_sensors}. "
f"Received length {len(fsr)}."
)
possible_values_map = (
......@@ -416,7 +435,8 @@ class PfeifferTPG(SingleCommDevice):
if wrong_values:
raise ValueError(
f"Argument fsr contains invalid values: {wrong_values}. Accepted "
f"values are {list(self.config.model.full_scale_ranges.values())}"
f"values are {list(possible_values_map.items())}"
f"{'' if unitless else ' mbar'}."
)
str_fsr = ",".join([
......@@ -424,28 +444,20 @@ class PfeifferTPG(SingleCommDevice):
])
try:
self.com.send_command(f"FSR,{str_fsr}")
logging.info(f"Set sensors full scale to {fsr} respectively.")
logging.info(f"Set sensors full scale to {fsr} (unitless) respectively.")
except PfeifferTPGError as e:
logging.error("Setting sensors full scale failed.")
raise e
def set_full_scale_unitless(self, fsr: List[int]) -> None:
"""
Set the full scale range of the attached sensors. See lookup table between
command and corresponding pressure in the device user manual.
:param fsr: list of full scale range values, like [0, 1, 3, 3, 2, 0]
:raises SerialCommunicationIOError: when communication port is not opened
:raises PfeifferTPGError: if command fails
"""
self._set_full_scale(fsr, True)
def get_full_scale_unitless(self) -> List[int]:
def _get_full_scale(self, unitless: bool) -> List[int]:
"""
Get the full scale range of the attached sensors. See lookup table between
command and corresponding pressure in the device user manual.
:return: list of full scale range values, like [0, 1, 3, 3, 2, 0]
:param unitless: flag to indicate scale of range values; if `False` then mbar
units scale
:return: list of full scale range values, like `[0, 1, 3, 3, 2, 0]` for
`unitless = True` scale or `[0.01, 1000]` otherwise (mbar units scale)
:raises SerialCommunicationIOError: when communication port is not opened
:raises PfeifferTPGError: if command fails
"""
......@@ -463,20 +475,48 @@ class PfeifferTPG(SingleCommDevice):
]
if wrong_values:
raise PfeifferTPGError(
f"The controller returned the full scale range values: {answer}. The "
f"values {wrong_values} are invalid. Accepted values are "
f"The controller returned the full unitless scale range values: "
f"{answer}. The values {wrong_values} are invalid. Accepted values are "
f"{list(self.config.model.full_scale_ranges_reversed.keys())}."
)
fsr = [int(v) for v in answer_values]
logging.info(f"Obtained full scale range of all sensors as {fsr}.")
fsr = [
int(v) if unitless else self.config.model.full_scale_ranges_reversed[int(v)]
for v in answer_values
]
logging.info(
f"Obtained full scale range of all sensors as {fsr}"
f"{'' if unitless else ' mbar'}."
)
return fsr
def set_full_scale_unitless(self, fsr: List[int]) -> None:
"""
Set the full scale range of the attached sensors. See lookup table between
command and corresponding pressure in the device user manual.
:param fsr: list of full scale range values, like `[0, 1, 3, 3, 2, 0]`
:raises SerialCommunicationIOError: when communication port is not opened
:raises PfeifferTPGError: if command fails
"""
self._set_full_scale(fsr, True)
def get_full_scale_unitless(self) -> List[int]:
"""
Get the full scale range of the attached sensors. See lookup table between
command and corresponding pressure in the device user manual.
:return: list of full scale range values, like [0, 1, 3, 3, 2, 0]
:raises SerialCommunicationIOError: when communication port is not opened
:raises PfeifferTPGError: if command fails
"""
return self._get_full_scale(True)
def set_full_scale_mbar(self, fsr: List[Number]) -> None:
"""
Set the full scale range of the attached sensors (in unit mbar)
:param fsr: full scale range values in mbar, for example [0.01, 1000]
:param fsr: full scale range values in mbar, for example `[0.01, 1000]`
:raises SerialCommunicationIOError: when communication port is not opened
:raises PfeifferTPGError: if command fails
"""
......@@ -491,34 +531,4 @@ class PfeifferTPG(SingleCommDevice):
:raises PfeifferTPGError: if command fails
"""
try:
answer = self.com.query("FSR")
except PfeifferTPGError:
logging.info("Query full scale range of all sensors failed.")
raise
answer_values = answer.split(",")
wrong_values = [
v for v in answer_values
if not self.config.model.is_valid_scale_range_reversed_str(v)
]
if wrong_values:
raise PfeifferTPGError(
f"The controller returned the full scale range values: {answer}. The "
f"values {wrong_values} are invalid. Accepted values are "
f"{list(self.config.model.full_scale_ranges_reversed.keys())}."
)
fsr = [
self.config.model.full_scale_ranges_reversed[int(i)] for i in answer_values
]
logging.info(f"Obtained full scale range of sensors as {fsr} mbar.")
return fsr
class PfeifferTPGError(Exception):
"""
Error with the Pfeiffer TPG Controller.
"""
pass
return self._get_full_scale(False)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment