def test_serialize_deserialize(self):
        server_thread = threading.Thread(target=self.run_fake_server)
        server_thread.start()

        msg_length = get_simulation_settings()['exec_settings']['message_size']
        from ev3dev2simulator.connection.ClientSocket import get_client_socket
        sock = get_client_socket()

        command = RotateCommand('test_port', 500.0, 100.0, 'hold')
        sock.send_command(command)

        ser = sock._serialize(command)
        unpadded = (
            b'{"type": "RotateCommand", "address": "test_port", "speed": 500.0, "distance": 100.0, '
            b'"stop_action": "hold"}')

        expected = unpadded.ljust(msg_length, b'#')
        self.assertEqual(ser, expected)

        ser = b'{"value": 15}'
        deser = sock._deserialize(ser)
        self.assertEqual(deser, 15)

        sock.client.send(str.encode('close_test_server'))
        sock.client.close()
        server_thread.join()
Esempio n. 2
0
    def test_create_jobs_coast_center(self):
        coasting_sub = get_simulation_settings(
        )['motor_settings']['degree_coasting_subtraction']
        robot_sim = create_robot_sim()

        message_processor = MessageProcessor(0, robot_sim)
        message_processor.process_rotate_command(
            RotateCommand('ev3-ports:outB', 80, 200, 'coast'))

        for i in range(75):
            jobs = robot_sim.next_actuator_jobs()
            self.assertAlmostEqual(jobs[3][1], -2.667, 3)
            self.assertIsNone(jobs[0][1])
            self.assertIsNone(jobs[1][1])
            self.assertIsNone(jobs[2][1])

        ppf = 2.667 - coasting_sub
        for i in range(2):
            jobs = robot_sim.next_actuator_jobs()
            self.assertAlmostEqual(jobs[3][1], -ppf, 3)
            self.assertIsNone(jobs[0][1])
            self.assertIsNone(jobs[1][1])
            self.assertIsNone(jobs[2][1])
            ppf = max(ppf - coasting_sub, 0)

        jobs = robot_sim.next_actuator_jobs()
        self.assertEqual((jobs[0][1], jobs[1][1], jobs[2][1], jobs[3][1]),
                         (None, None, None, None))
Esempio n. 3
0
    def test_create_jobs_coast_left(self):
        coasting_sub = get_simulation_settings(
        )['motor_settings']['distance_coasting_subtraction']
        robot_sim = create_robot_sim()

        message_processor = MessageProcessor(0, robot_sim)
        message_processor.process_rotate_command(
            RotateCommand('ev3-ports:outA', 80, 200, 'coast'))

        frames_check = (200 / 80) * 30  # (1000/100) * 30 # distance * fps
        distance_in_mm = 200 / 360 * wheel_circumference
        dpf = distance_in_mm / frames_check

        for i in range(75):
            jobs = robot_sim.next_actuator_jobs()
            self.assertAlmostEqual(jobs[1][1], dpf, 3)
            self.assertIsNone(jobs[0][1])
            self.assertIsNone(jobs[2][1])
            self.assertIsNone(jobs[3][1])

        ppf = dpf - coasting_sub
        for i in range(2):
            jobs = robot_sim.next_actuator_jobs()
            self.assertAlmostEqual(jobs[1][1], ppf, 3)
            self.assertIsNone(jobs[0][1])
            self.assertIsNone(jobs[3][1])
            self.assertIsNone(jobs[2][1])
            ppf = max(ppf - coasting_sub, 0)

        jobs = robot_sim.next_actuator_jobs()
        self.assertEqual((jobs[0][1], jobs[1][1], jobs[2][1], jobs[3][1]),
                         (None, None, None, None))
    def test_process_drive_command_degrees_coast(self):
        creator = MotorCommandProcessor()
        command = RotateCommand('ev3-ports:outA', 500, 1000, 'coast')

        spf, frames, coast_frames, run_time = creator.process_drive_command_degrees(command)

        self.assertAlmostEqual(spf, 16.667, 3)
        self.assertEqual(frames, 60)
        self.assertEqual(coast_frames, 11)
        self.assertAlmostEqual(run_time, 2.367, 3)
Esempio n. 5
0
    def _process_drive_command(self, d: dict) -> Any:
        """
        Deserialize the given dictionary into a RotateCommand and send it to the MessageProcessor.
        :param d: to process.
        """
        command = RotateCommand(d['address'], d['speed'], d['distance'],
                                d['stop_action'])
        value = self.message_processor.process_rotate_command(command)

        return self._serialize_response(value)
Esempio n. 6
0
    def test_process_drive_command_pixels_break(self):
        creator = MotorCommandProcessor()
        command = RotateCommand('ev3-ports:outA', 500, -1000, 'break')

        spf, frames, coast_frames, run_time = creator.process_drive_command_pixels(command)

        self.assertAlmostEqual(spf, -5.206, 3)
        self.assertEqual(frames, 60)
        self.assertEqual(coast_frames, 0)
        self.assertEqual(run_time, 2)
Esempio n. 7
0
    def test_process_drive_command_pixels_hold(self):
        creator = MotorCommandProcessor()
        command = RotateCommand('ev3-ports:outA', 100, 1000, 'hold')

        spf, frames, coast_frames, run_time = creator.process_drive_command_pixels(command)

        self.assertAlmostEqual(spf, 1.041, 3)
        self.assertEqual(frames, 300)
        self.assertEqual(coast_frames, 0)
        self.assertEqual(run_time, 10)
    def test_process_drive_command_degrees_hold(self):
        creator = MotorCommandProcessor()
        command = RotateCommand('ev3-ports:outA', 100, 1000, 'hold')
        # 1000 degrees, 100 per second

        spf, frames, coast_frames, run_time = creator.process_drive_command_degrees(command)

        self.assertAlmostEqual(spf, 3.333, 3)
        self.assertEqual(frames, 300)
        self.assertEqual(coast_frames, 0)
        self.assertEqual(run_time, 10)
    def test_process_drive_command_distance_hold(self):
        creator = MotorCommandProcessor()
        command = RotateCommand('ev3-ports:outA', 100, 1000, 'hold')
        spf, frames, coast_frames, run_time = creator.process_drive_command_distance(command)

        frames_check = 10 * 30  # (1000/100) * 30 # distance * fps
        distance_in_mm = 1000/360 * wheel_circumference
        dpf = distance_in_mm / frames_check
        self.assertAlmostEqual(spf, dpf, 3)
        self.assertEqual(frames, 300)
        self.assertEqual(coast_frames, 0)
        self.assertEqual(run_time, 10)
Esempio n. 10
0
    def test_create_jobs_right(self):
        robot_state = RobotState()

        message_processor = MessageProcessor('left_brick', robot_state)
        message_processor.process_rotate_command(RotateCommand('ev3-ports:outD', 10, 100, 'hold'))

        for i in range(300):
            c, l, r = robot_state.next_motor_jobs()
            self.assertIsNone(c)
            self.assertIsNone(l)
            self.assertAlmostEqual(r, 0.104, 3)

        self.assertEqual((None, None, None), robot_state.next_motor_jobs())
Esempio n. 11
0
    def test_create_jobs_center(self):
        robot_state = RobotState()

        message_processor = MessageProcessor('left_brick', robot_state)
        message_processor.process_rotate_command(RotateCommand('ev3-ports:outB', 20, 100, 'hold'))

        for i in range(150):
            c, l, r = robot_state.next_motor_jobs()
            self.assertAlmostEqual(c, -0.667, 3)
            self.assertIsNone(l)
            self.assertIsNone(r)

        self.assertEqual((None, None, None), robot_state.next_motor_jobs())
Esempio n. 12
0
    def _run(self) -> float:
        """
        Run the motor at a speed for a distance.
        :return a floating point value representing the number of seconds
        the given run operation will take.
        """

        if self.speed == 0 or self.distance == 0:
            command = StopCommand(self.address, 0, 'hold')
        else:
            command = RotateCommand(self.address,
                                    self.speed,
                                    self.distance,
                                    self.stop_action)

        return self.client_socket.send_motor_command(command)
Esempio n. 13
0
    def test_create_jobs_center(self):
        robot_sim = create_robot_sim()

        message_processor = MessageProcessor(0, robot_sim)
        message_processor.process_rotate_command(
            RotateCommand('ev3-ports:outB', 20, 100, 'hold'))

        for i in range(150):
            jobs = robot_sim.next_actuator_jobs()
            self.assertAlmostEqual(jobs[3][1], -0.667, 3)
            self.assertIsNone(jobs[0][1])
            self.assertIsNone(jobs[1][1])
            self.assertIsNone(jobs[2][1])

        jobs = robot_sim.next_actuator_jobs()
        self.assertEqual((jobs[0][1], jobs[1][1], jobs[2][1], jobs[3][1]),
                         (None, None, None, None))
Esempio n. 14
0
    def test_create_jobs_right(self):
        robot_sim = create_robot_sim()

        message_processor = MessageProcessor(0, robot_sim)
        message_processor.process_rotate_command(
            RotateCommand('ev3-ports:outD', 10, 100, 'hold'))

        frames_check = 10 * 30  # (1000/100) * 30 # distance * fps
        distance_in_mm = 100 / 360 * wheel_circumference
        dpf = distance_in_mm / frames_check

        for i in range(frames_check):
            jobs = robot_sim.next_actuator_jobs()
            self.assertAlmostEqual(jobs[2][1], dpf, 3)
            self.assertIsNone(jobs[0][1])
            self.assertIsNone(jobs[1][1])
            self.assertIsNone(jobs[3][1])
        jobs = robot_sim.next_actuator_jobs()
        self.assertEqual((jobs[0][1], jobs[1][1], jobs[2][1], jobs[3][1]),
                         (None, None, None, None))
Esempio n. 15
0
    def test_create_jobs_coast_left(self):
        coasting_sub = apply_scaling(get_config().get_data()['motor_settings']['pixel_coasting_subtraction'])
        robot_state = RobotState()

        message_processor = MessageProcessor('left_brick', robot_state)
        message_processor.process_rotate_command(RotateCommand('ev3-ports:outA', 80, 200, 'coast'))

        for i in range(75):
            c, l, r = robot_state.next_motor_jobs()
            self.assertIsNone(c)
            self.assertAlmostEqual(l, 0.833, 3)
            self.assertIsNone(r)

        ppf = 0.833 - coasting_sub
        for i in range(2):
            c, l, r = robot_state.next_motor_jobs()
            self.assertIsNone(c)
            self.assertAlmostEqual(l, ppf, 3)
            self.assertIsNone(r)
            ppf = max(ppf - coasting_sub, 0)

        self.assertEqual((None, None, None), robot_state.next_motor_jobs())
Esempio n. 16
0
    def test_create_jobs_coast_center(self):
        coasting_sub = get_config().get_data()['motor_settings']['degree_coasting_subtraction']
        robot_state = RobotState()

        message_processor = MessageProcessor('left_brick', robot_state)
        message_processor.process_rotate_command(RotateCommand('ev3-ports:outB', 80, 200, 'coast'))

        for i in range(75):
            c, l, r = robot_state.next_motor_jobs()
            self.assertAlmostEqual(c, -2.667, 3)
            self.assertIsNone(l)
            self.assertIsNone(r)

        ppf = 2.667 - coasting_sub
        for i in range(2):
            c, l, r = robot_state.next_motor_jobs()
            self.assertAlmostEqual(c, -ppf, 3)
            self.assertIsNone(l)
            self.assertIsNone(r)
            ppf = max(ppf - coasting_sub, 0)

        self.assertEqual((None, None, None), robot_state.next_motor_jobs())