From a5daac6ac2c0a11d456b36c5e8783b0dc87c76a6 Mon Sep 17 00:00:00 2001 From: Samuel Tardieu <sam@rfc1149.net> Date: Fri, 6 Sep 2024 13:13:38 +0200 Subject: [PATCH] Rename Python methods used to set speed --- Cargo.lock | 2 +- controller/Cargo.toml | 2 +- controller/python/controller.py | 48 ++++++++++++++--------------- controller/src/logic/pid_control.rs | 2 +- 4 files changed, 26 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b105736..e0d8794 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -299,7 +299,7 @@ dependencies = [ [[package]] name = "controller" -version = "0.6.0" +version = "0.6.1" dependencies = [ "bootloader-params", "build-support", diff --git a/controller/Cargo.toml b/controller/Cargo.toml index 89ab7f8..aff2653 100644 --- a/controller/Cargo.toml +++ b/controller/Cargo.toml @@ -4,7 +4,7 @@ description = "Firmware for DC Motor Driver Hat DFR0592" authors.workspace = true edition.workspace = true license.workspace = true -version = "0.6.0" # Update in python/controller.py as well +version = "0.6.1" # Update in python/controller.py as well [lints] workspace = true diff --git a/controller/python/controller.py b/controller/python/controller.py index 35a0006..d2d45ee 100644 --- a/controller/python/controller.py +++ b/controller/python/controller.py @@ -64,18 +64,21 @@ class Controller: ) raise WhoAmIMismatch(error) - def set_motor_speed(self, left: Optional[float], right: Optional[float]): - """Set the motor speed between -100 and 100. None means not to + def set_raw_motor_speed(self, left: Optional[float], right: Optional[float]): + """Set the motor speed between -127 and 127. None means not to change the motor value. Using None for both motors will put - the controller board in standby mode and motors will stop.""" + the controller board in standby mode and motors will stop. + + The speed set through this method will not be regulated by + the builtin PID controller.""" def convert(v: Optional[float], arg: str): if v is None: return -128 - if not isinstance(v, Real) or v < -100 or v > 100: + if not isinstance(v, Real) or v < -127 or v > 127: raise ValueError( f"{arg} motor speed " - "must be a number between -100 and 100, or None" + "must be a number between -127 and 127, or None" ) return round(v) @@ -84,19 +87,20 @@ class Controller: list(struct.pack("bb", convert(left, "left"), convert(right, "right"))), ) - def get_motor_speed(self) -> Optional[tuple[int, int]]: - """Get the left and right motor speed as a tuple, or None if in standby.""" + def get_raw_motor_speed(self) -> Optional[tuple[int, int]]: + """Get the left and right motor speed as a tuple, or None if in standby. + Each speed will be between -127 and 127.""" (left, right) = self._read(self.MOTOR_SPEED, 2, "bb") return (left, right) if left != 0x80 and right != 0x80 else None - def set_automatic_motor_speed(self, left: int, right: int): - """Set the motor speed in ticks by 10th of seconds.""" + def set_motor_speed(self, left: int, right: int): + """Set the motor speed in ticks by 100th of seconds. Each motor speed + must be comprised between -32767 and 32767.""" def check(v: int, arg: str): if v < -32767 or v > 32767: raise ValueError( - f"{arg} automatic motor speed must be a number " - + "between -32767 and 32767" + f"{arg} motor speed must be a number " + "between -32767 and 32767" ) return v @@ -105,24 +109,18 @@ class Controller: list(struct.pack("<hh", check(left, "left"), check(right, "right"))), ) - def get_automatic_motor_speed(self): - """Get the left and right automatic motor speed as a tuple, or None if - the system is not in automatic mode.""" + def get_motor_speed(self): + """Get the left and right motor speed as a tuple, or None if + the speed has been set by the raw mode method or if the + motors have been put into standby mode. The speed is in + ticks by 100th of seconds.""" (left, right) = self._read(self.AUTOMATIC_MOTOR_SPEED, 4, "<hh") return (left, right) if left != -32768 else None - def set_left_motor_speed(self, speed: float): - """Set the left motor speed between -100 and 100.""" - self.set_motor_speed(speed, None) - - def set_right_motor_speed(self, speed: float): - """Set the right motor speed between -100 and 100.""" - self.set_motor_speed(None, speed) - def standby(self): """Stop the motors by putting the controller board in standby mode.""" - self.set_motor_speed(None, None) + self.set_raw_motor_speed(None, None) def get_encoder_ticks(self) -> tuple[int, int]: """Retrieve the encoder ticks since the last time it was @@ -134,9 +132,9 @@ class Controller: def get_status(self) -> dict[str, bool]: """Return a dict with status fields: - "moving": True if at least one motor is moving, False otherwise - - "automatic": True if the motors are in automatic mode, False otherwise""" + - "controlled": True if the motors are in controlled mode, False otherwise""" (status,) = self._read(self.STATUS, 1, "B") - return {"moving": (status & 1) != 0, "automatic": (status & 2) != 0} + return {"moving": (status & 1) != 0, "controlled": (status & 2) != 0} def set_motor_shutdown_timeout(self, duration: float): """Set the duration in seconds after which the motors will diff --git a/controller/src/logic/pid_control.rs b/controller/src/logic/pid_control.rs index 9fbc334..ef8ba8f 100644 --- a/controller/src/logic/pid_control.rs +++ b/controller/src/logic/pid_control.rs @@ -77,5 +77,5 @@ fn build_speed_command( PidUnit::from_int(i32::from(measured_ticks)), ) .round() - .clamp(-100, 100) as i8 as u8 + .clamp(-127, 127) as i8 as u8 } -- GitLab