Commit b16daa8d authored by mikolajr's avatar mikolajr
Browse files

In ILS2T device: use f-strings

parent 11147ec0
...@@ -314,8 +314,8 @@ class ILS2T(SingleCommDevice): ...@@ -314,8 +314,8 @@ class ILS2T(SingleCommDevice):
cleaned_io_scanning_mode = self._clean_ioscanning_mode_values(kwargs) cleaned_io_scanning_mode = self._clean_ioscanning_mode_values(kwargs)
action_bits = "{0:03b}".format(cleaned_io_scanning_mode["action"]) action_bits = f"{cleaned_io_scanning_mode['action']:03b}"
mode_bits = "{0:04b}".format(cleaned_io_scanning_mode["mode"]) mode_bits = f"{cleaned_io_scanning_mode['mode']:04b}"
builder = BinaryPayloadBuilder(byteorder=Endian.Big, wordorder=Endian.Big) builder = BinaryPayloadBuilder(byteorder=Endian.Big, wordorder=Endian.Big)
# add the first byte: Drive control # add the first byte: Drive control
...@@ -374,9 +374,7 @@ class ILS2T(SingleCommDevice): ...@@ -374,9 +374,7 @@ class ILS2T(SingleCommDevice):
all_keys = set(self.DEFAULT_IO_SCANNING_CONTROL_VALUES.keys()) all_keys = set(self.DEFAULT_IO_SCANNING_CONTROL_VALUES.keys())
superfluous_keys = io_scanning_keys.difference(all_keys) superfluous_keys = io_scanning_keys.difference(all_keys)
if superfluous_keys: if superfluous_keys:
raise ValueError( raise ValueError(f"Unrecognized mode keys: {list(superfluous_keys)}")
"Unrecognized mode keys: {}".format(list(superfluous_keys))
)
# fill up io_scanning_values with defaults, if they are not set # fill up io_scanning_values with defaults, if they are not set
for mode_key, default_value in self.DEFAULT_IO_SCANNING_CONTROL_VALUES.items(): for mode_key, default_value in self.DEFAULT_IO_SCANNING_CONTROL_VALUES.items():
...@@ -387,21 +385,22 @@ class ILS2T(SingleCommDevice): ...@@ -387,21 +385,22 @@ class ILS2T(SingleCommDevice):
# JOG mode # JOG mode
if io_scanning_values["mode"] == self.Mode.JOG: if io_scanning_values["mode"] == self.Mode.JOG:
if not io_scanning_values["action"] == self.ACTION_JOG_VALUE: io_scanning_value = io_scanning_values["action"]
raise IoScanningModeValueError( if not io_scanning_value == self.ACTION_JOG_VALUE:
"Wrong action: {}".format(io_scanning_values["action"]) raise IoScanningModeValueError(f"Wrong action: {io_scanning_value}")
)
io_scanning_value = io_scanning_values["ref_16"]
try: try:
self.Ref16Jog(io_scanning_values["ref_16"]) self.Ref16Jog(io_scanning_value)
except ValueError: except ValueError:
raise IoScanningModeValueError( raise IoScanningModeValueError(
"Wrong value in ref_16 ({})".format(io_scanning_values["ref_16"]) f"Wrong value in ref_16 ({io_scanning_value})"
) )
if not io_scanning_values["ref_32"] == 0: io_scanning_value = io_scanning_values["ref_32"]
if not io_scanning_value == 0:
raise IoScanningModeValueError( raise IoScanningModeValueError(
"Wrong value in ref_32 ({})".format(io_scanning_values["ref_32"]) f"Wrong value in ref_32 ({io_scanning_value})"
) )
return io_scanning_values return io_scanning_values
...@@ -409,29 +408,28 @@ class ILS2T(SingleCommDevice): ...@@ -409,29 +408,28 @@ class ILS2T(SingleCommDevice):
# PTP mode # PTP mode
if io_scanning_values["mode"] == self.Mode.PTP: if io_scanning_values["mode"] == self.Mode.PTP:
io_scanning_value = io_scanning_values["action"]
try: try:
self.ActionsPtp(io_scanning_values["action"]) self.ActionsPtp(io_scanning_value)
except ValueError: except ValueError:
raise IoScanningModeValueError( raise IoScanningModeValueError(f"Wrong action: {io_scanning_value}")
"Wrong action: {}".format(io_scanning_values["action"])
)
if not self._is_valid_rpm(io_scanning_values["ref_16"]): io_scanning_value = io_scanning_values["ref_16"]
if not self._is_valid_rpm(io_scanning_value):
raise IoScanningModeValueError( raise IoScanningModeValueError(
"Wrong value in ref_16 ({})".format(io_scanning_values["ref_16"]) f"Wrong value in ref_16 ({io_scanning_value})"
) )
if not self._is_int32(io_scanning_values["ref_32"]): io_scanning_value = io_scanning_values["ref_32"]
if not self._is_int32(io_scanning_value):
raise IoScanningModeValueError( raise IoScanningModeValueError(
"Wrong value in ref_32 ({})".format(io_scanning_values["ref_32"]) f"Wrong value in ref_32 ({io_scanning_value})"
) )
return io_scanning_values return io_scanning_values
# default # default
raise IoScanningModeValueError( raise IoScanningModeValueError(f"Wrong mode: {io_scanning_values['mode']}")
"Wrong mode: {}".format(io_scanning_values["mode"])
)
def _is_valid_rpm(self, num: int) -> bool: def _is_valid_rpm(self, num: int) -> bool:
""" """
...@@ -513,9 +511,9 @@ class ILS2T(SingleCommDevice): ...@@ -513,9 +511,9 @@ class ILS2T(SingleCommDevice):
max_step = self.RegDatatype.INT32.max # type: ignore max_step = self.RegDatatype.INT32.max # type: ignore
# use _is_int32 instead? # use _is_int32 instead?
if not abs(steps) < max_step: if not abs(steps) < max_step:
logging.warning("number of steps is too big: {}".format(steps)) logging.warning(f"number of steps is too big: {steps}")
logging.info("Perform number of steps: {}".format(steps)) logging.info(f"Perform number of steps: {steps}")
self.do_ioscanning_write( self.do_ioscanning_write(
enable_driver_en=1, enable_driver_en=1,
...@@ -535,9 +533,9 @@ class ILS2T(SingleCommDevice): ...@@ -535,9 +533,9 @@ class ILS2T(SingleCommDevice):
max_position = self.RegDatatype.INT32.max # type: ignore max_position = self.RegDatatype.INT32.max # type: ignore
# use _is_int32 instead? # use _is_int32 instead?
if not abs(position) < max_position: if not abs(position) < max_position:
logging.warning("position is out of range: {}".format(position)) logging.warning(f"position is out of range: {position}")
logging.info("Absolute position: {}".format(position)) logging.info(f"Absolute position: {position}")
self.do_ioscanning_write( self.do_ioscanning_write(
enable_driver_en=1, enable_driver_en=1,
...@@ -553,7 +551,7 @@ class ILS2T(SingleCommDevice): ...@@ -553,7 +551,7 @@ class ILS2T(SingleCommDevice):
:param steps: Number of steps. :param steps: Number of steps.
""" """
logging.info("Motor steps requested: {}".format(steps)) logging.info(f"Motor steps requested: {steps}")
position_before = self.get_position() position_before = self.get_position()
...@@ -572,34 +570,22 @@ class ILS2T(SingleCommDevice): ...@@ -572,34 +570,22 @@ class ILS2T(SingleCommDevice):
if position_before + steps != position_after: if position_before + steps != position_after:
flt_dict = self.get_error_code() flt_dict = self.get_error_code()
self.flt_list.append(flt_dict) self.flt_list.append(flt_dict)
log_msg = (
f"The position does not align with the requested step number. "
f"Before: {position_before}, after: {position_after}, "
f"requested: {steps}, "
f"real difference: {position_after - position_before,}"
)
if "empty" in flt_dict[0].keys(): if "empty" in flt_dict[0].keys():
logging.warning( logging.warning(
"no error in drive, " "something different must have gone wrong" "no error in drive, " "something different must have gone wrong"
) )
logging.warning( logging.warning(log_msg)
"The position does not align with the requested step "
"number. Before: {0}, after: {1}, requested: {2}, "
"real difference: {3}".format(
position_before,
position_after,
steps,
position_after - position_before,
)
)
else: else:
# Despite drive error/malfunction don't break the code/experiment # Despite drive error/malfunction don't break the code/experiment
# execution by raising an error; continuing as nothing happened. # execution by raising an error; continuing as nothing happened.
logging.critical("error in drive, drive is know maybe locked") logging.critical("error in drive, drive is know maybe locked")
logging.critical( logging.critical(log_msg)
"The position does not align with the requested step "
"number. Before: {0}, after: {1}, requested: {2}, "
"real difference: {3}".format(
position_before,
position_after,
steps,
position_after - position_before,
)
)
def absolute_position_and_wait(self, position: int) -> None: def absolute_position_and_wait(self, position: int) -> None:
""" """
...@@ -608,7 +594,7 @@ class ILS2T(SingleCommDevice): ...@@ -608,7 +594,7 @@ class ILS2T(SingleCommDevice):
:param position: absolute position of motor in user defined steps. :param position: absolute position of motor in user defined steps.
""" """
logging.info("absolute position requested: {}".format(position)) logging.info(f"absolute position requested: {position}")
position_before = self.get_position() position_before = self.get_position()
...@@ -627,24 +613,19 @@ class ILS2T(SingleCommDevice): ...@@ -627,24 +613,19 @@ class ILS2T(SingleCommDevice):
if position != position_after: if position != position_after:
flt_dict = self.get_error_code() flt_dict = self.get_error_code()
self.flt_list.append(flt_dict) self.flt_list.append(flt_dict)
log_msg = (
f"The position does not align with the requested absolute position. "
f"Before: {position_before}, after: {position_after}, "
f"requested: {position}."
)
if "empty" in flt_dict[0].keys(): if "empty" in flt_dict[0].keys():
logging.warning( logging.warning(
"no error in drive, " "something different must have gone wrong" "no error in drive, " "something different must have gone wrong"
) )
logging.warning( logging.warning(log_msg)
"The position does not align with the requested absolute position. "
"Before: {0}, after: {1}, requested: {2}".format(
position_before, position_after, position,
)
)
else: else:
logging.critical("error in drive, drive is know maybe locked") logging.critical("error in drive, drive is know maybe locked")
logging.critical( logging.critical(log_msg)
"The position does not align with the requested absolute position. "
"Before: {0}, after: {1}, requested: {2}".format(
position_before, position_after, position,
)
)
def disable(self) -> None: def disable(self) -> None:
""" """
...@@ -722,12 +703,12 @@ class ILS2T(SingleCommDevice): ...@@ -722,12 +703,12 @@ class ILS2T(SingleCommDevice):
""" """
if not self._is_int32(revolutions): if not self._is_int32(revolutions):
err_msg = "Wrong scaling factor: revolutions = {}".format(revolutions) err_msg = f"Wrong scaling factor: revolutions = {revolutions}"
logging.error(err_msg) logging.error(err_msg)
raise ScalingFactorValueError(err_msg) raise ScalingFactorValueError(err_msg)
if not self._is_int32(steps): if not self._is_int32(steps):
err_msg = "Wrong scaling factor: steps = {}".format(steps) err_msg = f"Wrong scaling factor: steps = {steps}"
logging.error(err_msg) logging.error(err_msg)
raise ScalingFactorValueError(err_msg) raise ScalingFactorValueError(err_msg)
...@@ -812,9 +793,7 @@ class ILS2T(SingleCommDevice): ...@@ -812,9 +793,7 @@ class ILS2T(SingleCommDevice):
:param fast: RPM for fast jog mode. :param fast: RPM for fast jog mode.
""" """
logging.info( logging.info(f"Setting Jog RPM. Slow = {slow} RPM, Fast = {fast} RPM.")
"Setting Jog RPM. Slow = {0} RPM, Fast = {1} RPM.".format(slow, fast)
)
self.com.write_registers(self.RegAddr.JOGN_SLOW.value, [0, slow]) self.com.write_registers(self.RegAddr.JOGN_SLOW.value, [0, slow])
self.com.write_registers(self.RegAddr.JOGN_FAST.value, [0, fast]) self.com.write_registers(self.RegAddr.JOGN_FAST.value, [0, fast])
...@@ -893,9 +872,7 @@ class ILS2T(SingleCommDevice): ...@@ -893,9 +872,7 @@ class ILS2T(SingleCommDevice):
self.com.write_registers(self.RegAddr.RAMP_N_MAX.value, [0, rpm]) self.com.write_registers(self.RegAddr.RAMP_N_MAX.value, [0, rpm])
else: else:
raise ILS2TException( raise ILS2TException(
"RPM out of range: {rpm} not in (0, {rpm_max}".format( f"RPM out of range: {rpm} not in (0, {self.config.rpm_max_init}]"
rpm=rpm, rpm_max=self.config.rpm_max_init,
)
) )
def set_ramp_type(self, ramp_type: int = -1) -> None: def set_ramp_type(self, ramp_type: int = -1) -> None:
......
Supports Markdown
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