Commit 76d21c07 authored by mikolajr's avatar mikolajr
Browse files

In ILS2T device: wait time config opts, clean config values, tests for config...

In ILS2T device: wait time config opts, clean config values, tests for config errors, refactor inline max int32, fixed type check errors, format (black), and copyright notice
parent c09db518
This diff is collapsed.
# Copyright (c) 2019 ETH Zurich, SIS ID and HVL D-ITET
#
""" """
Tests for the Device SE ILS2T stepper motor. Tests for the Device SE ILS2T stepper motor.
""" """
...@@ -79,28 +81,58 @@ class MaskedModbusTcpCommunication(ModbusTcpCommunication): ...@@ -79,28 +81,58 @@ class MaskedModbusTcpCommunication(ModbusTcpCommunication):
return [i for i in range(count)] return [i for i in range(count)]
@pytest.fixture(scope='module') @pytest.fixture(scope="module")
def com_config(): def com_config():
return { return {
'host': '127.0.0.1', "host": "127.0.0.1",
'unit': 0, "unit": 0,
'port': 502, "port": 502,
}
@pytest.fixture(scope="module")
def dev_config():
return {
"wait_sec_post_enable": 0.01,
"wait_sec_post_relative_step": 0.01,
"wait_sec_post_absolute_position": 0.01,
} }
@pytest.fixture @pytest.fixture
def testdev(com_config): def testdev(com_config, dev_config):
modbus_tcp = MaskedModbusTcpCommunication(com_config) modbus_tcp = MaskedModbusTcpCommunication(com_config)
motor = ILS2T(modbus_tcp) motor = ILS2T(modbus_tcp, dev_config)
assert motor is not None assert motor is not None
motor.start() motor.start()
assert modbus_tcp.get_written() == (motor.RegAddr.ACCESS_ENABLE.value, [0, 1]) assert modbus_tcp.get_written() == (motor.RegAddr.ACCESS_ENABLE.value, [0, 1])
assert modbus_tcp.get_written() == ( assert modbus_tcp.get_written() == (
motor.RegAddr.RAMP_N_MAX.value, [0, ILS2TConfig.rpm_max_init]) motor.RegAddr.RAMP_N_MAX.value,
[0, ILS2TConfig.rpm_max_init],
)
yield motor yield motor
motor.stop() motor.stop()
@pytest.mark.parametrize(
"config_key,wrong_value",
[
("rpm_max_init", 0),
("rpm_max_init", -1),
("rpm_max_init", 3001),
("wait_sec_post_enable", 0),
("wait_sec_post_enable", -1),
("wait_sec_post_relative_step", 0),
("wait_sec_post_absolute_position", 0),
],
)
def test_wrong_config_value(com_config, dev_config, config_key, wrong_value):
wrong_config = dict(dev_config)
wrong_config[config_key] = wrong_value
with pytest.raises(ValueError):
ILS2T(com_config, wrong_config)
def test_clean_ioscanning_mode_values(testdev: ILS2T): def test_clean_ioscanning_mode_values(testdev: ILS2T):
default_values = testdev.DEFAULT_IO_SCANNING_CONTROL_VALUES default_values = testdev.DEFAULT_IO_SCANNING_CONTROL_VALUES
...@@ -109,71 +141,71 @@ def test_clean_ioscanning_mode_values(testdev: ILS2T): ...@@ -109,71 +141,71 @@ def test_clean_ioscanning_mode_values(testdev: ILS2T):
# missing key that gets loaded from defaults # missing key that gets loaded from defaults
other_values = dict(default_values) other_values = dict(default_values)
other_values.pop('mode') other_values.pop("mode")
assert testdev._clean_ioscanning_mode_values(other_values) == default_values assert testdev._clean_ioscanning_mode_values(other_values) == default_values
# additional key that is not needed # additional key that is not needed
other_values = dict(default_values) other_values = dict(default_values)
other_values['some_other_key'] = None other_values["some_other_key"] = None
with pytest.raises(ValueError): with pytest.raises(ValueError):
testdev._clean_ioscanning_mode_values(other_values) testdev._clean_ioscanning_mode_values(other_values)
# invalid mode # invalid mode
other_values = dict(default_values) other_values = dict(default_values)
other_values['mode'] = -1 other_values["mode"] = -1
with pytest.raises(IoScanningModeValueError): with pytest.raises(IoScanningModeValueError):
testdev._clean_ioscanning_mode_values(other_values) testdev._clean_ioscanning_mode_values(other_values)
# with mode 1: JOG # with mode 1: JOG
# ---------------- # ----------------
other_values = dict(default_values) other_values = dict(default_values)
other_values['mode'] = 1 other_values["mode"] = 1
# action has to be 0 # action has to be 0
other_values['action'] = -1 other_values["action"] = -1
with pytest.raises(IoScanningModeValueError): with pytest.raises(IoScanningModeValueError):
testdev._clean_ioscanning_mode_values(other_values) testdev._clean_ioscanning_mode_values(other_values)
other_values['action'] = 0 other_values["action"] = 0
# action is 0, but ref_16 is still 1500 # action is 0, but ref_16 is still 1500
with pytest.raises(IoScanningModeValueError): with pytest.raises(IoScanningModeValueError):
testdev._clean_ioscanning_mode_values(other_values) testdev._clean_ioscanning_mode_values(other_values)
# one of the valid values for ref_16 in JOG mode # one of the valid values for ref_16 in JOG mode
other_values['ref_16'] = 1 other_values["ref_16"] = 1
assert testdev._clean_ioscanning_mode_values(other_values) == other_values assert testdev._clean_ioscanning_mode_values(other_values) == other_values
# action and ref_16 valid, but ref_32 not `0` # action and ref_16 valid, but ref_32 not `0`
other_values['ref_32'] = 1 other_values["ref_32"] = 1
with pytest.raises(IoScanningModeValueError): with pytest.raises(IoScanningModeValueError):
testdev._clean_ioscanning_mode_values(other_values) testdev._clean_ioscanning_mode_values(other_values)
# with mode 3: Point to Point # with mode 3: Point to Point
# --------------------------- # ---------------------------
other_values = dict(default_values) other_values = dict(default_values)
other_values['mode'] = 3 other_values["mode"] = 3
# action has to be 1, 2 or 3 # action has to be 1, 2 or 3
other_values['action'] = 4 other_values["action"] = 4
with pytest.raises(IoScanningModeValueError): with pytest.raises(IoScanningModeValueError):
testdev._clean_ioscanning_mode_values(other_values) testdev._clean_ioscanning_mode_values(other_values)
other_values['action'] = 1 other_values["action"] = 1
# too high RPM # too high RPM
other_values = dict(default_values) other_values = dict(default_values)
other_values['ref_16'] = 4000 # too high RPM other_values["ref_16"] = 4000 # too high RPM
with pytest.raises(IoScanningModeValueError): with pytest.raises(IoScanningModeValueError):
testdev._clean_ioscanning_mode_values(other_values) testdev._clean_ioscanning_mode_values(other_values)
other_values['ref_16'] = 1500 other_values["ref_16"] = 1500
# needs to work # needs to work
assert testdev._clean_ioscanning_mode_values(other_values) == other_values assert testdev._clean_ioscanning_mode_values(other_values) == other_values
def test_32bit_decode(): def test_32bit_decode():
assert ILS2T._decode_32bit([2**15, 1], signed=False) == 2_147_483_649 assert ILS2T._decode_32bit([2 ** 15, 1], signed=False) == 2_147_483_649
assert ILS2T._decode_32bit([2**15, 1], signed=True) == -2_147_483_647 assert ILS2T._decode_32bit([2 ** 15, 1], signed=True) == -2_147_483_647
def test_toggle(testdev: ILS2T): def test_toggle(testdev: ILS2T):
...@@ -206,20 +238,18 @@ def test_get_position(testdev: ILS2T): ...@@ -206,20 +238,18 @@ def test_get_position(testdev: ILS2T):
def test_enable(testdev: ILS2T): def test_enable(testdev: ILS2T):
testdev.enable() testdev.enable()
assert ( assert testdev.com.get_written() == (
testdev.com.get_written() == testdev.RegAddr.IO_SCANNING.value,
(testdev.RegAddr.IO_SCANNING.value, testdev._generate_control_registers(enable_driver_en=1),
testdev._generate_control_registers(enable_driver_en=1))
) )
def test_disable(testdev: ILS2T): def test_disable(testdev: ILS2T):
testdev.com.simulate_running = False testdev.com.simulate_running = False
testdev.disable() testdev.disable()
assert ( assert testdev.com.get_written() == (
testdev.com.get_written() == testdev.RegAddr.IO_SCANNING.value,
(testdev.RegAddr.IO_SCANNING.value, testdev._generate_control_registers( testdev._generate_control_registers(disable_driver_di=1),
disable_driver_di=1))
) )
testdev.com.simulate_running = True testdev.com.simulate_running = True
...@@ -263,10 +293,7 @@ def test_absolute_position(testdev: ILS2T): ...@@ -263,10 +293,7 @@ def test_absolute_position(testdev: ILS2T):
def test_set_max_rpm(testdev: ILS2T): def test_set_max_rpm(testdev: ILS2T):
testdev.set_max_rpm(500) testdev.set_max_rpm(500)
assert ( assert testdev.com.get_written() == (testdev.RegAddr.RAMP_N_MAX.value, [0, 500])
testdev.com.get_written() == (
testdev.RegAddr.RAMP_N_MAX.value, [0, 500])
)
with pytest.raises(ILS2TException): with pytest.raises(ILS2TException):
testdev.set_max_rpm(-100) testdev.set_max_rpm(-100)
...@@ -275,18 +302,12 @@ def test_set_max_rpm(testdev: ILS2T): ...@@ -275,18 +302,12 @@ def test_set_max_rpm(testdev: ILS2T):
def test_set_ramp_type(testdev: ILS2T): def test_set_ramp_type(testdev: ILS2T):
testdev.set_ramp_type(0) testdev.set_ramp_type(0)
assert ( assert testdev.com.get_written() == (testdev.RegAddr.RAMP_TYPE.value, [0, 0])
testdev.com.get_written() == (
testdev.RegAddr.RAMP_TYPE.value, [0, 0])
)
def test_user_steps(testdev: ILS2T): def test_user_steps(testdev: ILS2T):
testdev.user_steps(10, 1) testdev.user_steps(10, 1)
assert ( assert testdev.com.get_written() == (testdev.RegAddr.SCALE.value, [0, 10, 0, 1])
testdev.com.get_written() == (
testdev.RegAddr.SCALE.value, [0, 10, 0, 1])
)
with pytest.raises(ScalingFactorValueError): with pytest.raises(ScalingFactorValueError):
testdev.user_steps(5.5, 3) testdev.user_steps(5.5, 3)
...@@ -321,27 +342,15 @@ def test_jog_run(testdev: ILS2T): ...@@ -321,27 +342,15 @@ def test_jog_run(testdev: ILS2T):
def test_set_jog_speed(testdev: ILS2T): def test_set_jog_speed(testdev: ILS2T):
testdev.set_jog_speed(1, 1000) testdev.set_jog_speed(1, 1000)
assert ( assert testdev.com.get_written() == (testdev.RegAddr.JOGN_SLOW.value, [0, 1])
testdev.com.get_written() == ( assert testdev.com.get_written() == (testdev.RegAddr.JOGN_FAST.value, [0, 1000])
testdev.RegAddr.JOGN_SLOW.value, [0, 1])
)
assert (
testdev.com.get_written() == (
testdev.RegAddr.JOGN_FAST.value, [0, 1000])
)
def test_set_max_acceleration(testdev: ILS2T): def test_set_max_acceleration(testdev: ILS2T):
testdev.set_max_acceleration(5000) testdev.set_max_acceleration(5000)
assert ( assert testdev.com.get_written() == (testdev.RegAddr.RAMP_ACC.value, [0, 5000])
testdev.com.get_written() == (
testdev.RegAddr.RAMP_ACC.value, [0, 5000])
)
def test_set_max_deceleration(testdev: ILS2T): def test_set_max_deceleration(testdev: ILS2T):
testdev.set_max_deceleration(5000) testdev.set_max_deceleration(5000)
assert ( assert testdev.com.get_written() == (testdev.RegAddr.RAMP_DECEL.value, [0, 5000])
testdev.com.get_written() == (
testdev.RegAddr.RAMP_DECEL.value, [0, 5000])
)
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