foc_simple/
lib.rs

1#![no_std]
2#![allow(dead_code)]
3use core::future::Future;
4
5use fixed::types::I16F16;
6
7pub use crate::tools::foc_command::FocCommand;
8use crate::tools::shaft_position::ShaftPosition;
9
10pub mod foc;
11pub mod tools;
12
13#[derive(Clone, Debug, Copy, PartialEq)]
14pub enum EFocCommand {
15  FocMode(foc::EFocMode),
16  Speed(I16F16),
17  SpeedAcc(I16F16),
18  Angle(I16F16),
19  Torque(I16F16),
20  ShaftPosition(ShaftPosition),
21  TorqueLimit(I16F16),
22  ErrorCount,
23}
24
25#[derive(Clone, Debug, Copy, PartialEq)]
26pub enum EFocSimpleError {
27  Uninitialized,
28  NoMagWarning,
29  DataError,
30  SerialError,
31  AngleSensorError,
32  MotorNrError,
33  NoMotorMovement,
34  NoAngleSensor,
35  NoCurrentSensor,
36  NotImplementedYet,
37  NoData,
38  Error,
39}
40pub type Result<T> = core::result::Result<T, EFocSimpleError>;
41
42/// The trait for getting the shaft angle of the motor from some sensor
43pub trait SensorAngle {
44  /// Retrieve the angle from the sensor, in a synchrounous way
45  /// Return on success the raw angle from the sensor, in radians from 0 .. 2 pi
46  fn get_angle(&mut self) -> Result<I16F16>;
47}
48
49#[derive(Clone, Debug, Copy)]
50pub enum EFocAngleSensor<A> {
51  SensorLess,
52  ShaftSensor(A), // contains the implementation of the SensorAngle trait
53  HallSensor,
54}
55
56#[derive(Clone, Debug, Copy)]
57pub enum EFocCurrentSensor<C> {
58  SensorLess,
59  ThreePhase(C), // contains the implementation of the SensorCurrent Trait
60}
61
62#[derive(Clone, Debug, Copy)]
63pub enum EFocAngle {
64  SensorLess,
65  SensorValue(I16F16), // actual measured sensor value
66  Interpolate,         // interpolate actual value
67}
68
69// The trait for getting the current from the motor
70pub trait SensorCurrent {
71  /// Retrieve the current from the sensor.
72  /// Return on success the current array from the sensor, in amperes
73  /// In case 2 phases are sample set the third phase to zero here
74  fn get_current(&mut self) -> Result<[I16F16; 3]>;
75}
76
77pub type MessageRx = [u8; 64];
78pub struct MessageTx {
79  pub data: [u8; 64],
80  pub len: usize,
81}
82
83pub trait FocSerial {
84  fn receive(&mut self, buffer: &mut [u8]) -> impl Future<Output = Result<usize>>;
85  fn send(&mut self, buffer: &[u8]) -> Result<()>;
86}
87
88#[derive(Clone, Debug, Copy, PartialEq)]
89pub struct FocParam {
90  p: I16F16,
91  i: I16F16,
92  d: I16F16,
93}
94impl FocParam {
95  pub fn new_fp(fp: f32, fi: f32, fd: f32) -> Self {
96    FocParam {
97      p: I16F16::from_num(fp),
98      i: I16F16::from_num(fi),
99      d: I16F16::from_num(fd),
100    }
101  }
102  pub fn new(p: I16F16, i: I16F16, d: I16F16) -> Self {
103    FocParam { p, i, d }
104  }
105  pub fn set_p(&mut self, p: I16F16) {
106    self.p = p
107  }
108  pub fn set_i(&mut self, i: I16F16) {
109    self.i = i
110  }
111  pub fn set_d(&mut self, d: I16F16) {
112    self.d = d
113  }
114  pub fn get_p(&mut self) -> I16F16 {
115    self.p
116  }
117  pub fn get_i(&mut self) -> I16F16 {
118    self.i
119  }
120  pub fn get_d(&mut self) -> I16F16 {
121    self.d
122  }
123}