Skip to content

Commit

Permalink
Update multiple_motors_controller.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
pierre-rouanet committed Aug 18, 2023
1 parent bc5e6b6 commit 219f461
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/multiple_motors_controller.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::{coherency::CoherentResult, motor_controller::Result, MotorController, PID};

pub trait MultipleMotorsController<const N: usize> {
/// Name of the controller (used for Debug trait)
fn name(&self) -> &'static str;

/// Check if the motor is ON or OFF
fn is_torque_on(&self) -> Result<bool>;
/// Enable/Disable the torque
Expand Down Expand Up @@ -50,6 +53,14 @@ pub trait MultipleMotorsController<const N: usize> {
fn set_pid_gains(&mut self, pid: [PID; N]) -> Result<()>;
}

impl<const N: usize> std::fmt::Debug for dyn MultipleMotorsController<N> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("MultipleMotorsController")
.field("name", &self.name())
.finish()
}
}

#[derive(Debug)]
pub struct MultipleMotorsControllerWrapper<const N: usize> {
controllers: [Box<dyn MotorController>; N],
Expand All @@ -62,6 +73,10 @@ impl<const N: usize> MultipleMotorsControllerWrapper<N> {
}

impl<const N: usize> MultipleMotorsController<N> for MultipleMotorsControllerWrapper<N> {
fn name(&self) -> &'static str {
"MultipleMotorsController"
}

fn is_torque_on(&self) -> Result<bool> {
self.controllers.iter().map(|c| c.is_torque_on()).coherent()
}
Expand Down

0 comments on commit 219f461

Please sign in to comment.