From a868d08474a763ecf0f179419ec683f9a5fc9e79 Mon Sep 17 00:00:00 2001 From: Piotr Dec Date: Wed, 23 Aug 2023 20:45:54 +0200 Subject: [PATCH] Python test suite --- app/test.py | 31 ------------ {app => python}/main.py | 53 ++++++++------------- python/test.py | 103 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+), 65 deletions(-) delete mode 100644 app/test.py rename {app => python}/main.py (84%) create mode 100644 python/test.py diff --git a/app/test.py b/app/test.py deleted file mode 100644 index 10388cc..0000000 --- a/app/test.py +++ /dev/null @@ -1,31 +0,0 @@ -from numpy import uint8 - - -def send_hud(buf: list): - n = len(buf) - chars = [] - crc = uint8(0xeb + n + n) - chars.append(0x10) - chars.append(0x7b) - chars.append(n + 6) - if n == 0xa: - chars.append(0x10) - chars.append(n) - chars.append(0x00) - chars.append(0x00) - chars.append(0x00) - chars.append(0x55) - chars.append(0x15) - for char in buf: - crc = uint8(crc + char) - chars.append(char) - if char == 0x10: - chars.append(0x10) - chars.append((-crc) & 0xff) - chars.append(0x10) - chars.append(0x03) - print(chars) - print([bytes(chr(char), 'raw_unicode_escape') for char in chars]) - - -send_hud([0x04, 0x01]) diff --git a/app/main.py b/python/main.py similarity index 84% rename from app/main.py rename to python/main.py index 62a2a7a..73a55ab 100644 --- a/app/main.py +++ b/python/main.py @@ -52,11 +52,13 @@ class Lane(Enum): class Controller: - def __init__(self): + def __init__(self, device: str = None): self.gps = False - self.ser = None#serial.Serial('COM9', 9600) - time.sleep(2) - # print(self.ser.read_all()) + self.debug = device is None + if not self.debug: + self.ser = serial.Serial(device, 9600) + time.sleep(2) + print(self.ser.read_all()) def clear(self): self.send_hud([0x03, 0, 0, 0, 0x00, 0, 0]) @@ -123,8 +125,8 @@ class Controller: self.send_hud([0x07, 0x01]) self.gps = state - def set_speed_control(self): - self.send_hud([0x04, 0x01]) + def set_speed_control(self, on: bool = True): + self.send_hud([0x04, 0x01 if on else 0x00]) def set_compass(self, direction: float): if direction > 337.5 or direction <= 22.5: @@ -172,35 +174,18 @@ class Controller: self.send_packet(chars) def send_packet(self, buff): - print("raw", buff) encoded = [bytes(chr(char), 'raw_unicode_escape') for char in buff] - print("enc", encoded) - # for char in buff: - # self.ser.write(bytes(chr(char), 'raw_unicode_escape')) - # time.sleep(0.2) - # print(self.ser.read_all()) - - -def test(): - c.set_direction(OutAngle.Straight, OutType.Lane) - target = 100 - for i in range(10): - c.set_distance(target, Unit.Kilometres) - time.sleep(0.5) - target -= 1 - c.set_speed_control() - time.sleep(0.5) - for i in range(10): - c.set_distance(target, Unit.Kilometres) - time.sleep(0.5) - target -= 1 - c.set_direction(OutAngle.Right, OutType.LongerLane) - for i in range(10): - c.set_distance(target, Unit.Kilometres) - time.sleep(0.5) - target -= 1 + if self.debug: + print("raw", buff) + print("enc", encoded) + else: + for char in buff: + self.ser.write(bytes(chr(char), 'raw_unicode_escape')) + time.sleep(0.2) + print(self.ser.read_all()) if __name__ == '__main__': - c = Controller() - test() + import sys + device = sys.argv[1] if len(sys.argv) > 1 else None + c = Controller(device) diff --git a/python/test.py b/python/test.py new file mode 100644 index 0000000..2808632 --- /dev/null +++ b/python/test.py @@ -0,0 +1,103 @@ +from main import * +from time import sleep + +interval = 0.2 + + +def suite(controller: Controller): + lines(controller) + direction(controller) + distance(controller) + speed(controller) + time(controller) + control(controller) + + +def lines(controller: Controller): + controller.set_lines([Lane.DotsLeft], []) + controller.set_lines([Lane.OuterRight], [Lane.OuterLeft]) + controller.set_lines([Lane.MiddleRight], [Lane.MiddleLeft]) + controller.set_lines([Lane.InnerRight], [Lane.InnerLeft]) + controller.set_lines([Lane.InnerLeft], [Lane.InnerRight]) + controller.set_lines([Lane.MiddleLeft], [Lane.MiddleRight]) + controller.set_lines([Lane.OuterLeft], [Lane.OuterRight]) + controller.set_lines( + [Lane.DotsRight], + [Lane.OuterRight, Lane.MiddleRight, Lane.InnerRight, Lane.InnerLeft, Lane.MiddleLeft, Lane.OuterLeft] + ) + controller.set_lines([], []) + + +def direction(controller: Controller): + controller.set_direction(OutAngle.RightDown) + controller.set_direction(OutAngle.SharpRight) + controller.set_direction(OutAngle.Right) + controller.set_direction(OutAngle.EasyRight) + controller.set_direction(OutAngle.Straight) + controller.set_direction(OutAngle.EasyLeft) + controller.set_direction(OutAngle.Left) + controller.set_direction(OutAngle.SharpLeft) + controller.set_direction(OutAngle.LeftDown) + controller.set_direction(OutAngle.Down) + controller.set_direction(OutAngle.SharpRight, OutType.LongerLane) + controller.set_direction(OutAngle.Right, OutType.LongerLane) + controller.set_direction(OutAngle.EasyRight, OutType.LongerLane) + controller.set_direction(OutAngle.Straight, OutType.LongerLane) + controller.set_direction(OutAngle.EasyLeft, OutType.LongerLane) + controller.set_direction(OutAngle.Left, OutType.LongerLane) + controller.set_direction(OutAngle.SharpLeft, OutType.LongerLane) + controller.set_direction(OutAngle.SharpRight, OutType.RightRoundabout) + controller.set_direction(OutAngle.Right, OutType.RightRoundabout) + controller.set_direction(OutAngle.EasyRight, OutType.RightRoundabout) + controller.set_direction(OutAngle.Straight, OutType.RightRoundabout) + controller.set_direction(OutAngle.EasyLeft, OutType.RightRoundabout) + controller.set_direction(OutAngle.Left, OutType.RightRoundabout) + controller.set_direction(OutAngle.SharpLeft, OutType.RightRoundabout) + controller.set_direction(OutAngle.Left, OutType.Flag) + controller.set_direction(OutAngle.Right, OutType.Flag) + + +def distance(controller: Controller): + controller.set_distance(999) + controller.set_distance(999, Unit.Kilometres) + controller.set_distance(999, Unit.Metres) + controller.set_distance(999, Unit.Foot) + controller.set_distance(999, Unit.Miles) + pass + + +def speed(controller: Controller): + controller.set_gps(True) + controller.set_speed(50) + controller.set_speed(50, 100) + controller.set_speed(150, 100) + controller.set_speed(50, 100, True) + pass + + +def time(controller: Controller): + controller.set_time(22, 22) + controller.set_time(22, 22, traffic=True) + controller.set_time(22, 22, flag=True) + pass + + +def control(controller: Controller): + controller.set_speed_control() + controller.set_speed_control(False) + + +def compass(controller: Controller): + controller.set_compass(22.5) + controller.set_compass(67.5) + controller.set_compass(112.5) + controller.set_compass(157.5) + controller.set_compass(202.5) + controller.set_compass(247.5) + controller.set_compass(292.5) + controller.set_compass(337.5) + pass + + +if __name__ == '__main__': + instance = Controller('/dev/rfcomm0')