class Powertrain: def __init__(self) -> None: self._joystick = Joystick(FORWARD_PIN, REVERSE_PIN, LEFT_PIN, RIGHT_PIN) self._left_ramp = Ramp(RAMP_SIZE, RAMP_BREAK_FACTOR) self._right_ramp = Ramp(RAMP_SIZE, RAMP_BREAK_FACTOR) self._left_motor_controler = MotorControler(LEFT_MOTOR_PWM, LEFT_MOTOR_A, LEFT_MOTOR_B) self._right_motor_controler = MotorControler(RIGHT_MOTOR_PWM, RIGHT_MOTOR_A, RIGHT_MOTOR_B) def loop(self): start_time = time.ticks_ms() while True: try: # delta = time.ticks_diff(time.ticks_ms(), start_time) # print("delta", delta) delta = 10 left_direction, right_direction = self._joystick.get() print("direction", left_direction, right_direction) self._left_ramp.update(left_direction, delta) self._right_ramp.update(right_direction, delta) left_consign = self._left_ramp.get_normalized(750) self._left_motor_controler.set(left_consign) right_consign = self._right_ramp.get_normalized(750) self._right_motor_controler.set(right_consign) print("") time.sleep_ms(10) except Exception as e: print(e) start_time = time.ticks_ms()
class TestRamp(unittest.TestCase): def setUp(self) -> None: self.r = Ramp(10) def test_forward_1(self): self.r.update(1, 1) result = self.r.get() expected = 1 self.assertEqual(result, expected) def test_forward_2(self): self.r.update(1, 1) self.r.update(1, 10) result = self.r.get() expected = 10 self.assertEqual(result, expected) def test_forward_release_1(self): self.r.update(1, 5) self.r.update(0, 3) result = self.r.get() expected = 2 self.assertEqual(result, expected) def test_forward_release_2(self): self.r.update(1, 5) self.r.update(0, 6) result = self.r.get() expected = 0 self.assertEqual(result, expected) def test_forward_brake_1(self): self.r.update(1, 7) self.r.update(-1, 3) result = self.r.get() expected = 1 self.assertEqual(result, expected) def test_forward_brake_2(self): self.r.update(1, 5) self.r.update(-1, 6) result = self.r.get() expected = -7 self.assertEqual(result, expected) def test_reverse_1(self): self.r.update(-1, 1) result = self.r.get() expected = -1 self.assertEqual(result, expected) def test_reverse_2(self): self.r.update(-11, 1) self.r.update(-1, 10) result = self.r.get() expected = -10 self.assertEqual(result, expected) def test_reverse_release_1(self): self.r.update(-1, 5) self.r.update(0, 3) result = self.r.get() expected = -2 self.assertEqual(result, expected) def test_reverse_release_2(self): self.r.update(-1, 5) self.r.update(0, 6) result = self.r.get() expected = 0 self.assertEqual(result, expected) def test_reverse_brake_1(self): self.r.update(-1, 7) self.r.update(1, 3) result = self.r.get() expected = -1 self.assertEqual(result, expected) def test_reverse_brake_2(self): self.r.update(-1, 5) self.r.update(1, 6) result = self.r.get() expected = 7 self.assertEqual(result, expected) def test_get_normalized(self): self.r.update(1, 10) result = self.r.get_normalized() expected = 1023 self.assertEqual(result, expected)