Skip to content
Snippets Groups Projects
Commit a5daac6a authored by Samuel Tardieu's avatar Samuel Tardieu
Browse files

Rename Python methods used to set speed

parent 0bf2943d
Branches
Tags
3 merge requests!16Resolve "Add README to the firmware package",!15Resolve "Remove bootloader.py",!14Resolve "Use automatic mode by default"
...@@ -299,7 +299,7 @@ dependencies = [ ...@@ -299,7 +299,7 @@ dependencies = [
[[package]] [[package]]
name = "controller" name = "controller"
version = "0.6.0" version = "0.6.1"
dependencies = [ dependencies = [
"bootloader-params", "bootloader-params",
"build-support", "build-support",
......
...@@ -4,7 +4,7 @@ description = "Firmware for DC Motor Driver Hat DFR0592" ...@@ -4,7 +4,7 @@ description = "Firmware for DC Motor Driver Hat DFR0592"
authors.workspace = true authors.workspace = true
edition.workspace = true edition.workspace = true
license.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] [lints]
workspace = true workspace = true
......
...@@ -64,18 +64,21 @@ class Controller: ...@@ -64,18 +64,21 @@ class Controller:
) )
raise WhoAmIMismatch(error) raise WhoAmIMismatch(error)
def set_motor_speed(self, left: Optional[float], right: Optional[float]): def set_raw_motor_speed(self, left: Optional[float], right: Optional[float]):
"""Set the motor speed between -100 and 100. None means not to """Set the motor speed between -127 and 127. None means not to
change the motor value. Using None for both motors will put 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): def convert(v: Optional[float], arg: str):
if v is None: if v is None:
return -128 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( raise ValueError(
f"{arg} motor speed " 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) return round(v)
...@@ -84,19 +87,20 @@ class Controller: ...@@ -84,19 +87,20 @@ class Controller:
list(struct.pack("bb", convert(left, "left"), convert(right, "right"))), list(struct.pack("bb", convert(left, "left"), convert(right, "right"))),
) )
def get_motor_speed(self) -> Optional[tuple[int, int]]: 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.""" """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") (left, right) = self._read(self.MOTOR_SPEED, 2, "bb")
return (left, right) if left != 0x80 and right != 0x80 else None return (left, right) if left != 0x80 and right != 0x80 else None
def set_automatic_motor_speed(self, left: int, right: int): def set_motor_speed(self, left: int, right: int):
"""Set the motor speed in ticks by 10th of seconds.""" """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): def check(v: int, arg: str):
if v < -32767 or v > 32767: if v < -32767 or v > 32767:
raise ValueError( raise ValueError(
f"{arg} automatic motor speed must be a number " f"{arg} motor speed must be a number " + "between -32767 and 32767"
+ "between -32767 and 32767"
) )
return v return v
...@@ -105,24 +109,18 @@ class Controller: ...@@ -105,24 +109,18 @@ class Controller:
list(struct.pack("<hh", check(left, "left"), check(right, "right"))), list(struct.pack("<hh", check(left, "left"), check(right, "right"))),
) )
def get_automatic_motor_speed(self): def get_motor_speed(self):
"""Get the left and right automatic motor speed as a tuple, or None if """Get the left and right motor speed as a tuple, or None if
the system is not in automatic mode.""" 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") (left, right) = self._read(self.AUTOMATIC_MOTOR_SPEED, 4, "<hh")
return (left, right) if left != -32768 else None 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): def standby(self):
"""Stop the motors by putting the controller board in standby """Stop the motors by putting the controller board in standby
mode.""" mode."""
self.set_motor_speed(None, None) self.set_raw_motor_speed(None, None)
def get_encoder_ticks(self) -> tuple[int, int]: def get_encoder_ticks(self) -> tuple[int, int]:
"""Retrieve the encoder ticks since the last time it was """Retrieve the encoder ticks since the last time it was
...@@ -134,9 +132,9 @@ class Controller: ...@@ -134,9 +132,9 @@ class Controller:
def get_status(self) -> dict[str, bool]: def get_status(self) -> dict[str, bool]:
"""Return a dict with status fields: """Return a dict with status fields:
- "moving": True if at least one motor is moving, False otherwise - "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") (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): def set_motor_shutdown_timeout(self, duration: float):
"""Set the duration in seconds after which the motors will """Set the duration in seconds after which the motors will
......
...@@ -77,5 +77,5 @@ fn build_speed_command( ...@@ -77,5 +77,5 @@ fn build_speed_command(
PidUnit::from_int(i32::from(measured_ticks)), PidUnit::from_int(i32::from(measured_ticks)),
) )
.round() .round()
.clamp(-100, 100) as i8 as u8 .clamp(-127, 127) as i8 as u8
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment