def test_sync_run(mbrick, msyncmotor): msyncmotor.run(50) assert mbrick.mock_calls == [ call.reset_motor_position(Port.A, True), call.reset_motor_position(Port.B, True), call.set_output_state( Port.A, 50, Mode.ON | Mode.REGULATED, RegulationMode.SYNC, # TODO: why reversed? This does not seems right. -50, RunState.RUNNING, 0, ), call.set_output_state( Port.B, 50, Mode.ON | Mode.REGULATED, RegulationMode.SYNC, -50, RunState.RUNNING, 0, ), ]
def test_sync_idle(mbrick, msyncmotor): msyncmotor.idle() assert mbrick.mock_calls == [ call.set_output_state(Port.A, 0, Mode.IDLE, RegulationMode.IDLE, 0, RunState.IDLE, 0), call.set_output_state(Port.B, 0, Mode.IDLE, RegulationMode.IDLE, 0, RunState.IDLE, 0), ]
def test_turn_stopped(mbrick, mmotor, mtime): # Test if skipping get_tacho() while not "slept enough" # Test motor gets stopped once stop_turn is set to True mbrick.get_output_state.side_effect = [ (Port.A, 0, Mode.IDLE, RegulationMode.IDLE, 0, RunState.IDLE, 0, 0, 0, 0), ( Port.A, 50, Mode.ON | Mode.REGULATED, RegulationMode.SPEED, 0, RunState.RUNNING, 0, 90, 90, 90, ), ] mtime.time.side_effect = [0, 0, 0.1, 0.2, 0.8, 0.9] stop_motor_mock = MagicMock(side_effect=lambda: False) stop_motor_mock.side_effect = [False, False, False, False, True] mmotor.turn(50, 360, stop_turn=stop_motor_mock) assert mbrick.mock_calls == [ call.get_output_state(Port.A), call.set_output_state( Port.A, 50, Mode.ON | Mode.REGULATED, RegulationMode.SPEED, 0, RunState.RUNNING, 0, ), call.get_output_state(Port.A), call.set_output_state( Port.A, 0, Mode.ON | Mode.REGULATED | Mode.BRAKE, RegulationMode.SPEED, 0, RunState.RUNNING, 0, ), ]
def test_run(mbrick, mmotor): mmotor.run() assert mbrick.mock_calls == [ # TODO: should be RegulationMode.IDLE. call.set_output_state(Port.A, 100, Mode.ON, RegulationMode.SPEED, 0, RunState.RUNNING, 0), ]
def test_turn_blocked(mbrick, mmotor, mtime): mtime.time.side_effect = [0, 0, 1, 2] with pytest.raises(nxt.motor.BlockedException): mmotor.turn(50, 360, brake=False, timeout=0.1, emulate=False) assert mbrick.mock_calls == [ call.get_output_state(Port.A), call.set_output_state( Port.A, 50, Mode.ON | Mode.REGULATED, RegulationMode.SPEED, 0, RunState.RUNNING, 360, ), call.get_output_state(Port.A), call.set_output_state(Port.A, 0, Mode.IDLE, RegulationMode.IDLE, 0, RunState.IDLE, 0), ]
def test_sync_brake(mbrick, msyncmotor): msyncmotor.brake() assert mbrick.mock_calls == [ # TODO: this should be possible to make it simpler. call.set_output_state(Port.A, 0, Mode.IDLE, RegulationMode.IDLE, 0, RunState.IDLE, 0), call.set_output_state(Port.B, 0, Mode.IDLE, RegulationMode.IDLE, 0, RunState.IDLE, 0), call.reset_motor_position(Port.A, True), call.reset_motor_position(Port.B, True), call.set_output_state( Port.A, 0, Mode.ON | Mode.BRAKE | Mode.REGULATED, RegulationMode.SYNC, -50, RunState.RUNNING, 0, ), call.set_output_state( Port.B, 0, Mode.ON | Mode.BRAKE | Mode.REGULATED, RegulationMode.SYNC, -50, RunState.RUNNING, 0, ), call.set_output_state(Port.A, 0, Mode.IDLE, RegulationMode.IDLE, -50, RunState.IDLE, 0), call.set_output_state(Port.B, 0, Mode.IDLE, RegulationMode.IDLE, -50, RunState.IDLE, 0), call.set_output_state( Port.A, 0, Mode.ON | Mode.BRAKE | Mode.REGULATED, RegulationMode.SPEED, -50, RunState.RUNNING, 0, ), call.set_output_state( Port.B, 0, Mode.ON | Mode.BRAKE | Mode.REGULATED, RegulationMode.SPEED, -50, RunState.RUNNING, 0, ), ]
def test_turn(mbrick, mmotor, mtime): mbrick.get_output_state.side_effect = [ (Port.A, 0, Mode.IDLE, RegulationMode.IDLE, 0, RunState.IDLE, 0, 0, 0, 0), # Test overshoot. ( Port.A, 50, Mode.ON | Mode.REGULATED, RegulationMode.SPEED, 0, RunState.RUNNING, 0, 720, 720, 720, ), ] mmotor.turn(50, 360) assert mbrick.mock_calls == [ call.get_output_state(Port.A), call.set_output_state( Port.A, 50, Mode.ON | Mode.REGULATED, RegulationMode.SPEED, 0, RunState.RUNNING, 0, ), call.get_output_state(Port.A), call.set_output_state( Port.A, 0, Mode.ON | Mode.REGULATED | Mode.BRAKE, RegulationMode.SPEED, 0, RunState.RUNNING, 0, ), ]
def test_brake(mbrick, mmotor): mmotor.brake() assert mbrick.mock_calls == [ call.set_output_state( Port.A, 0, Mode.ON | Mode.BRAKE | Mode.REGULATED, RegulationMode.SPEED, 0, RunState.RUNNING, 0, ), ]
def test_run_regulated(mbrick, mmotor): mmotor.run(50, regulated=True) assert mbrick.mock_calls == [ call.set_output_state( Port.A, 50, Mode.ON | Mode.REGULATED, RegulationMode.SPEED, 0, RunState.RUNNING, 0, ), ]
def test_sync_turn(mbrick, msyncmotor): mbrick.get_output_state.side_effect = [ (Port.A, 0, Mode.IDLE, RegulationMode.IDLE, 0, RunState.IDLE, 0, 0, 0, 0), (Port.B, 0, Mode.IDLE, RegulationMode.IDLE, 0, RunState.IDLE, 0, 0, 0, 0), # Test overshoot. ( Port.A, 50, Mode.ON | Mode.REGULATED, RegulationMode.SYNC, 0, RunState.RUNNING, 0, 720, 720, 720, ), ( Port.B, 0, Mode.ON | Mode.REGULATED, RegulationMode.SYNC, 0, RunState.RUNNING, 0, 0, 0, 0, ), ] msyncmotor.turn(50, 360, brake=False) assert mbrick.mock_calls == [ call.reset_motor_position(Port.A, True), call.reset_motor_position(Port.B, True), call.get_output_state(Port.A), call.get_output_state(Port.B), call.set_output_state( Port.A, 50, Mode.ON | Mode.REGULATED, RegulationMode.SYNC, -50, RunState.RUNNING, 0, ), call.set_output_state( Port.B, 50, Mode.ON | Mode.REGULATED, RegulationMode.SYNC, -50, RunState.RUNNING, 0, ), call.get_output_state(Port.A), call.get_output_state(Port.B), call.set_output_state(Port.A, 0, Mode.IDLE, RegulationMode.IDLE, 0, RunState.IDLE, 0), call.set_output_state(Port.B, 0, Mode.IDLE, RegulationMode.IDLE, 0, RunState.IDLE, 0), ]
def test_weak_turn(mbrick, mmotor): mmotor.weak_turn(50, 360) assert mbrick.mock_calls == [ call.set_output_state(Port.A, 50, Mode.ON, RegulationMode.IDLE, 0, RunState.RUNNING, 360), ]