async def test_dispatch_debug_log(protobuf_router): """ Test low level log message. """ bus_message = BusMessage(debugLog=DebugLog(content='Hello, world!')) msg_bytes = bus_message.SerializeToString() await protobuf_router.decode_message(msg_bytes, 'motor_board')
async def movement_done(self) -> None: """ Send the "movement done" signal to the robot. """ bus_message = BusMessage(movementEnded=MovementEndedMsg(blocked=False)) msg_bytes = bus_message.SerializeToString() await self.motor_board_adapter.send(msg_bytes)
async def test_move_rotation(simulation_handler, event_queue_mock): """ Happy path for rotation. """ bus_message = BusMessage(rotate=RotateMsg(ticks=3)) msg_bytes = bus_message.SerializeToString() await simulation_handler.handle_movement_order(msg_bytes) event_queue_mock.push.assert_any_call( EventOrder(type=EventType.MOVE_WHEEL, payload={ 'left': -1, 'right': 1 }), 62) event_queue_mock.push.assert_any_call( EventOrder(type=EventType.MOVE_WHEEL, payload={ 'left': -1, 'right': 1 }), 188) event_queue_mock.push.assert_any_call( EventOrder(type=EventType.MOVE_WHEEL, payload={ 'left': -1, 'right': 1 }), 313) event_queue_mock.push.assert_any_call( EventOrder(type=EventType.MOVEMENT_DONE, payload=None), 397) assert event_queue_mock.push.call_count == 4
async def rotate(self, ticks: int) -> None: """ Rotate counter-clockwise if ticks > 0, clockwise if ticks < 0. """ LOGGER.get().debug('gateway_rotate', ticks=ticks) message = BusMessage(rotate=RotateMsg(ticks=ticks)) payload = message.SerializeToString() await self.motor_board_adapter.send(payload)
async def translate(self, ticks: int) -> None: """ Move forward if ticks > 0, backward if ticks < 0. """ LOGGER.get().debug('gateway_translate', ticks=ticks) message = BusMessage(translate=TranslateMsg(ticks=ticks)) payload = message.SerializeToString() await self.motor_board_adapter.send(payload)
async def test_movement_ended(protobuf_handler, localization_controller_mock, blocked): """ Route movement ended messages to localization controller. """ bus_message = BusMessage(movementEnded=MovementEndedMsg(blocked=blocked)) msg_bytes = bus_message.SerializeToString() await protobuf_handler.translate_message(msg_bytes) localization_controller_mock.movement_done.assert_called_once_with(blocked)
async def encoder_position(self, left_tick: int, right_tick: int) -> None: """ Send encoder positions. """ bus_message = BusMessage(encoderPosition=EncoderPositionMsg( left_tick=left_tick, right_tick=right_tick, )) msg_bytes = bus_message.SerializeToString() await self.motor_board_adapter.send(msg_bytes)
async def test_move_wheels_zero_unit(simulation_handler, event_queue_mock): """ If we receive a packet to move wheels by 0 unit, do nothing. """ bus_message = BusMessage(translate=TranslateMsg(ticks=0)) msg_bytes = bus_message.SerializeToString() await simulation_handler.handle_movement_order(msg_bytes) event_queue_mock.push.assert_called_with( EventOrder(type=EventType.MOVEMENT_DONE, payload=None), 0)
async def _send_message(self, adapter_index: int, message: BusMessage) -> None: """ Serializes and sends sends a protobuf BusMessage through an adapter. """ if adapter_index >= len(self._servo_board_adapters): raise RuntimeError( f"Board ID out of bounds:" f"{adapter_index}>={len(self._servo_board_adapters)}") payload = message.SerializeToString() await self._servo_board_adapters[adapter_index].send(payload)
async def test_speed_order(simulation_router, simulation_state_mock): """ Happy path for translation. """ bus_message = BusMessage(moveWheelAtSpeed=MoveWheelAtSpeedMsg( left_tick_per_sec=100, right_tick_per_sec=200)) msg_bytes = bus_message.SerializeToString() await simulation_router.handle_movement_order(msg_bytes, 'test') assert simulation_state_mock.queue_speed_left[-1] == 100 assert simulation_state_mock.queue_speed_right[-1] == 200
async def test_dispatch_laser_sensor(protobuf_handler, match_action_controller_mock): """ If LaserSensorMsg provided, should call match_action_controller.set_laser_distances once. """ bus_message = BusMessage( laserSensor=LaserSensorMsg(distance_front_left=10, distance_front_right=10, distance_back_left=10, distance_back_right=10)) msg_bytes = bus_message.SerializeToString() await protobuf_handler.translate_message(msg_bytes) match_action_controller_mock.set_laser_distances.assert_called_once_with()
async def test_dispatch_encoder_position(protobuf_router, position_controller_mock): """ Dispatch encoder position to position controller. """ bus_message = BusMessage(encoderPosition=EncoderPositionMsg( left_tick=1, right_tick=-2, )) msg_bytes = bus_message.SerializeToString() await protobuf_router.decode_message(msg_bytes, 'test') position_controller_mock.update_odometry.assert_called_once_with(1, -2)
async def test_dispatch_pressure_sensor(protobuf_handler, match_action_controller_mock): """ If PressureSensorMsg provided, should call match_action_controller.set_pressures once. """ bus_message = BusMessage( pressureSensor=PressureSensorMsg(on_left=10, on_center_left=10, on_center=10, on_center_right=10, on_right=10)) msg_bytes = bus_message.SerializeToString() await protobuf_handler.translate_message(msg_bytes) match_action_controller_mock.set_pressures.assert_called_once_with()
async def test_dispatch_encoder_position(protobuf_handler, localization_controller_mock): """ Dispatch encoder position to localization controller. """ bus_message = BusMessage(encoderPosition=EncoderPositionMsg( left_tick=1, right_tick=-2, )) msg_bytes = bus_message.SerializeToString() await protobuf_handler.translate_message(msg_bytes) localization_controller_mock.update_odometry_position.assert_called_once_with( 1, -2)
async def _send_message(self, message: BusMessage) -> None: """ Serializes and sends sends a protobuf BusMessage through an adapter. """ payload = message.SerializeToString() await self.motor_board_adapter.send(payload)