Changed over to async functions
changed over all the classes in these files to use async based methods rather than non. This SHOULD make the system more reliable with embassy in the future, and hopefully prevent issues with blocking code and the way embassy works.
This commit is contained in:
@@ -25,25 +25,26 @@ where
|
||||
///Controller Impl for the adding the PID logic for handling the motor compiston
|
||||
impl<T: motor::Driver + motor::Sensor> Controller<T> {
|
||||
///Uses the given set_speed and then will handle the rest of the control logic to accurately* hit the requested speed
|
||||
pub fn control(&mut self, set_speed: Speed) {
|
||||
pub async fn control(&mut self, set_speed: Speed) {
|
||||
let max_speed_rads = self.max_speed.rads();
|
||||
let set_speed_rads = set_speed.rads().clamp(-max_speed_rads, max_speed_rads);
|
||||
|
||||
self.pid.set_point(set_speed_rads);
|
||||
let pid_output = self.pid.pid_step(self.motor.get_speed().rads());
|
||||
self.pid.set_point(set_speed_rads).await;
|
||||
let pid_output = self.pid.pid_step(self.motor.get_speed().await.rads()).await;
|
||||
|
||||
self.current_direction.dir_from_f32(pid_output);
|
||||
|
||||
let motor_command = (pid_output * 65535.0) as u16;
|
||||
|
||||
self.motor
|
||||
.set_speed_and_direction(motor_command, self.current_direction);
|
||||
.set_speed_and_direction(motor_command, self.current_direction)
|
||||
.await;
|
||||
}
|
||||
|
||||
///Retrieve the speed from the motor inside the controller.
|
||||
/// This allows you to get a speed value from behind the move.
|
||||
pub fn retrieve(&self) -> motor::Speed {
|
||||
self.motor.get_speed()
|
||||
pub async fn retrieve(&self) -> motor::Speed {
|
||||
self.motor.get_speed().await
|
||||
}
|
||||
|
||||
///Creates a new Controller with a motor (of type T), a max speed and a pid::Config
|
||||
|
||||
@@ -61,7 +61,7 @@ impl PID {
|
||||
///Will output a float between -1.0 and 1.0
|
||||
///Current value is the current "position" of the system NOT the goal
|
||||
/// Use set_point() method to change the "goal position" for the system
|
||||
pub fn pid_step(&mut self, current_value: f32) -> f32 {
|
||||
pub async fn pid_step(&mut self, current_value: f32) -> f32 {
|
||||
//If the values
|
||||
let error = self.set_point - current_value;
|
||||
|
||||
@@ -117,7 +117,7 @@ impl PID {
|
||||
return self.output;
|
||||
}
|
||||
|
||||
pub fn set_point(&mut self, set_point: f32) {
|
||||
pub async fn set_point(&mut self, set_point: f32) {
|
||||
if set_point != self.set_point {
|
||||
//Update all internal fields
|
||||
self.integral = 0.0;
|
||||
|
||||
+18
-14
@@ -111,62 +111,66 @@ impl Display for Speed {
|
||||
}
|
||||
}
|
||||
|
||||
///This trait is implemented on a motor struct to implement the motor DRIVER (like an ESC) capabalities in how it should control the motor.
|
||||
/// This will then let other systems use this generic trait and system to control your motor without needing to understand the hardware as much.
|
||||
/// # Motor Driver
|
||||
///
|
||||
/// This trait is implemented on a motor struct to implement the motor DRIVER (like an ESC) capabalities in how it should control the motor.
|
||||
/// This will then let other systems use this trait and system to control your motor without needing to understand the hardware as much.
|
||||
#[allow(async_fn_in_trait)]
|
||||
pub trait Driver {
|
||||
///Spins in the provided direction
|
||||
/// Properly map each direction to CounterClockWise and Clockwise when LOOKING AT THE MOTOR SHAFT. This is VITAL for the way the systems within core::logic will work.
|
||||
///This is best not called manually, use set_speed_and_direction_raw instead
|
||||
fn spin(&mut self, dir: Direction);
|
||||
async fn spin(&mut self, dir: Direction);
|
||||
|
||||
///Commands the motor to stop
|
||||
/// This should stop the motor in what ever way you see fit.
|
||||
/// Some hardware may have a proper "Stop" or "Off" Signal, some will not.
|
||||
/// If there is no proper hardware stop, verify that 0 speed is actually a stopping command and you can easily just set speed to 0 via set_speed_raw in this method.
|
||||
/// No default implementation (though this is easily something I could have done) because it SHOULD be something you notice and implement PROPERLY if applicable
|
||||
fn stop(&mut self);
|
||||
async fn stop(&mut self);
|
||||
|
||||
///This method uses a full u16 to set the motor speed. This does not change spin direction.
|
||||
/// You need to map 0 to 65535 properly to your specific hardware range, just because PWM is commonly 0 to 65535 do not assume that ALL ways of controlling a motor cleanly takes a value of 0 to 65535,
|
||||
/// you need to verify and map properly.
|
||||
/// 65535 should be the max possible speed, while 0 should be stopped.
|
||||
///This is best to not call manually, use set_speed_and_direction_raw instead
|
||||
fn set_speed(&mut self, speed: u16);
|
||||
async fn set_speed(&mut self, speed: u16);
|
||||
|
||||
///Commands the motors to use the speed and spin in the same command.
|
||||
///Uses a number between 0 and 65535
|
||||
///This should be used in most cases, unless the motor has the core::logic::motor::controller Trait, then the the methods from that should be used instead.
|
||||
fn set_speed_and_direction(&mut self, speed: u16, dir: Direction) {
|
||||
async fn set_speed_and_direction(&mut self, speed: u16, dir: Direction) {
|
||||
//Set direction before speed, so it won't start spinning in one direction, then snap to the other.
|
||||
//Most MCUs should execute these two lines of code so fast, that it should be neglible regardless.
|
||||
self.spin(dir);
|
||||
self.set_speed(speed);
|
||||
self.spin(dir).await;
|
||||
self.set_speed(speed).await;
|
||||
}
|
||||
}
|
||||
|
||||
///This trait is implemented on motors that have some form of sensory feedback. Note that this does not require the motor ITSELF provide feedback, just that the setup the motor is in supports it. IE, a Current draw based ESC
|
||||
/// # Motor Sensor
|
||||
/// This trait is implemented on motors that have some form of sensory feedback. Note that this does not require the motor ITSELF provide feedback, just that the setup the motor is in supports it. IE, a Current draw based ESC
|
||||
/// Speed Feedback is REQUIRED, if your device does not provide speed feedback directly,
|
||||
/// Implement the other conditions and use them to then implement the speed method.
|
||||
///
|
||||
/// Some motors/sensors might have a direct "Current Angle" being reported, the option is there in case yours can, so the communication system can sync it to the host for you.
|
||||
/// Some sensors may also provide current and voltage feedback, if implemented the communication system can sync them to the host you for.
|
||||
/// Can also be used for other custom logic. They are unused by RNavP, entirely optional.
|
||||
#[allow(async_fn_in_trait)]
|
||||
pub trait Sensor {
|
||||
///Returns the speed struct value from the motors sensor.
|
||||
/// This should be non-blocking, there should be some form of cached value ready to go as soon as the method is called
|
||||
///
|
||||
/// IF properly returned in the correct Enum Data field, all other systems will use the value propely, being unit agnostic later.
|
||||
fn get_speed(&self) -> Speed;
|
||||
async fn get_speed(&self) -> Speed;
|
||||
|
||||
///Returns the current from the sensor, if it capable.
|
||||
/// This should be non-blocking. Keep a cached value ready.
|
||||
fn get_current(&self) -> Option<Current>;
|
||||
async fn get_current(&self) -> Option<Current>;
|
||||
|
||||
///Returns the current voltage from the sensro, if it is capable.
|
||||
/// This should be non-blocking. Keep a cached value ready.
|
||||
fn get_voltage(&self) -> Option<Voltage>;
|
||||
async fn get_voltage(&self) -> Option<Voltage>;
|
||||
|
||||
///Returns the current angle from the sensor if it is capable.
|
||||
/// This should be non-blocking. Keep a cached value ready.
|
||||
fn get_angle(&self) -> Option<Angle>;
|
||||
async fn get_angle(&self) -> Option<Angle>;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user