From 3aea844c292363c78863fe5a86a1d384b73a1ce7 Mon Sep 17 00:00:00 2001 From: Samuel Tardieu <sam@rfc1149.net> Date: Sat, 7 Sep 2024 08:08:27 +0200 Subject: [PATCH] Use 8 bits of fractional part for PID coefficients This is needed to set meaningful k_i and k_d coefficients. --- Cargo.lock | 2 +- controller/Cargo.toml | 2 +- controller/python/controller.py | 4 ++-- controller/src/logic/pid_control.rs | 9 ++++++--- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e0d8794..7616125 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -299,7 +299,7 @@ dependencies = [ [[package]] name = "controller" -version = "0.6.1" +version = "0.7.0" dependencies = [ "bootloader-params", "build-support", diff --git a/controller/Cargo.toml b/controller/Cargo.toml index aff2653..4bbb502 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.1" # Update in python/controller.py as well +version = "0.7.0" # Update in python/controller.py as well [lints] workspace = true diff --git a/controller/python/controller.py b/controller/python/controller.py index 42fa097..92e6f75 100644 --- a/controller/python/controller.py +++ b/controller/python/controller.py @@ -3,7 +3,7 @@ from numbers import Real from typing import Optional # Major and minor version of required firmware -_REQUIRED_FIRMWARE_VERSION = (0, 6) +_REQUIRED_FIRMWARE_VERSION = (0, 7) class FirmwareVersionMismatch(Exception): @@ -34,7 +34,7 @@ class Controller: DEVICE_ID = 0xF0 FIRMWARE_CAPABILITIES = 0xFE - PID_COEFFICIENTS_FACTOR = 1 << 4 + PID_COEFFICIENTS_FACTOR = 1 << 8 def __init__(self, i2c_bus=8): import smbus diff --git a/controller/src/logic/pid_control.rs b/controller/src/logic/pid_control.rs index 3eda827..6677e35 100644 --- a/controller/src/logic/pid_control.rs +++ b/controller/src/logic/pid_control.rs @@ -4,7 +4,12 @@ use core::sync::atomic::{AtomicBool, AtomicI16, AtomicI32, Ordering}; use embassy_time::{Duration, Ticker}; use pid::{Fixed, Pid}; -pub static K_P: AtomicI32 = AtomicI32::new(4); +type PidUnit = Fixed<8>; + +// Those coefficients are to be intepreted as `PidUnit` values. +// For example, if `PidUnit` is `Fixed<8>`, a value of 0x40 is +// in fact 0x40 รท (1 << 8) = 0.25. +pub static K_P: AtomicI32 = AtomicI32::new(0x40); pub static K_I: AtomicI32 = AtomicI32::new(0); pub static K_D: AtomicI32 = AtomicI32::new(0); @@ -12,8 +17,6 @@ static CONTROLLED_MODE: AtomicBool = AtomicBool::new(false); static LEFT_SPEED: AtomicI16 = AtomicI16::new(0); static RIGHT_SPEED: AtomicI16 = AtomicI16::new(0); -type PidUnit = Fixed<4>; - pub fn disable_controlled_mode() { CONTROLLED_MODE.store(false, Ordering::Relaxed); } -- GitLab