This commit is contained in:
NikolajDanger
2022-09-05 14:07:34 +02:00
parent 4f0bb76e55
commit 5607a3975d
3 changed files with 13 additions and 14 deletions

View File

@ -1,4 +1,3 @@
import time
from time import sleep
import robot

View File

@ -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.

View File

@ -1,4 +1,3 @@
import time
from time import sleep
import robot