From 5607a3975dde90a30ab7b1ea08a768fbcda0c9f6 Mon Sep 17 00:00:00 2001 From: NikolajDanger Date: Mon, 5 Sep 2022 14:07:34 +0200 Subject: [PATCH] :sparkles: --- encoder_free.py | 1 - robot.py | 25 +++++++++++++------------ test_arduino_encoders.py | 1 - 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/encoder_free.py b/encoder_free.py index e3e7800..6f7faca 100644 --- a/encoder_free.py +++ b/encoder_free.py @@ -1,4 +1,3 @@ -import time from time import sleep import robot diff --git a/robot.py b/robot.py index 6e2b8bb..1838742 100644 --- a/robot.py +++ b/robot.py @@ -16,7 +16,7 @@ class Robot(object): In case you experience trouble - consider using only commands that do not use the wheel encoders. """ - def __init__(self, port = '/dev/ttyACM0'): + def __init__(self, port: str = '/dev/ttyACM0'): """ The constructor port parameter can be changed from default value if you want to control the robot directly from your labtop (instead of from @@ -51,7 +51,7 @@ class Robot(object): print((self.send_command(cmd))) self.serial_read.close() - def send_command(self, cmd, sleep_ms=0.0): + def send_command(self, cmd : str, sleep_ms: float=0.0): """Sends a command to the Arduino robot controller""" self.serial_read.write(cmd.encode('ascii')) sleep(sleep_ms) @@ -59,14 +59,15 @@ class Robot(object): return str_val - def _power_checker(self, power): + def _power_checker(self, power: int): """ Checks if a power value is in the set {0, [40;127]}. This is an internal utility function. """ return (power == 0) or (power >=40 and power <=127) - def go_diff(self, power_left, power_right, dir_left, dir_right): + def go_diff(self, power_left : int, power_right : int, dir_left : int, + dir_right : int): """ Start left motor with motor power powerLeft (in {0, [40;127]} and the numbers must be integer) and direction dirLeft (0=reverse, 1=forward) @@ -85,13 +86,13 @@ class Robot(object): ): print("WARNING: Read the docstring of Robot.go_diff()!") return "" - else: - pl_int = int(power_left) - pr_int = int(power_right) - dl_int = int(dir_left) - dr_int = int(dir_right) - cmd = f"d{pl_int},{pr_int},{dl_int},{dr_int}\n" - return self.send_command(cmd) + + pl_int = int(power_left) + pr_int = int(power_right) + dl_int = int(dir_left) + dr_int = int(dir_right) + cmd = f"d{pl_int},{pr_int},{dl_int},{dr_int}\n" + return self.send_command(cmd) def stop(self): @@ -106,7 +107,7 @@ class Robot(object): - def read_sensor(self, sensorid): + def read_sensor(self, sensorid : int): """ Send a read sensor command with sensorid and return sensor value. diff --git a/test_arduino_encoders.py b/test_arduino_encoders.py index d208cef..964e071 100644 --- a/test_arduino_encoders.py +++ b/test_arduino_encoders.py @@ -1,4 +1,3 @@ -import time from time import sleep import robot