foc_simple/
lib.rs

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