SimpleFOClibrary 2.4.0
Loading...
Searching...
No Matches
CurrentSense.h
Go to the documentation of this file.
1#ifndef CURRENTSENSE_H
2#define CURRENTSENSE_H
3
4#include "FOCDriver.h"
5#include "../foc_utils.h"
6#include "../time_utils.h"
7#include "StepperDriver.h"
8#include "BLDCDriver.h"
9
10/**
11 * Current sensing abstract class defintion
12 * Each current sensing implementation needs to extend this interface
13 */
15 public:
16
17 /**
18 * Function intialising the CurrentSense class
19 * - All the necessary intialisations of adc and sync should be implemented here
20 *
21 * @returns - 0 - for failure & 1 - for success
22 */
23 virtual int init() = 0;
24
25 /**
26 * Linking the current sense with the motor driver
27 * Only necessary if synchronisation in between the two is required
28 */
30
31 // variables
32 bool skip_align = false; //!< variable signaling that the phase current direction should be verified during initFOC()
33
34 FOCDriver* driver = nullptr; //!< driver link
35 bool initialized = false; // true if current sense was successfully initialized
36 void* params = 0; //!< pointer to hardware specific parameters of current sensing
37 DriverType driver_type = DriverType::UnknownDriver; //!< driver type (BLDC or Stepper)
38
39
40 // ADC measurement gain for each phase
41 // support for different gains for different phases of more commonly - inverted phase currents
42 // this should be automated later
43 float gain_a; //!< phase A gain
44 float gain_b; //!< phase B gain
45 float gain_c; //!< phase C gain
46
47 float offset_ia; //!< zero current A voltage value (center of the adc reading)
48 float offset_ib; //!< zero current B voltage value (center of the adc reading)
49 float offset_ic; //!< zero current C voltage value (center of the adc reading)
50
51 // hardware variables
52 int pinA; //!< pin A analog pin for current measurement
53 int pinB; //!< pin B analog pin for current measurement
54 int pinC; //!< pin C analog pin for current measurement
55
56 /**
57 * Function intended to verify if:
58 * - phase current are oriented properly
59 * - if their order is the same as driver phases
60 *
61 * This function corrects the alignment errors if possible ans if no such thing is needed it can be left empty (return 1)
62 * @returns -
63 0 - failure
64 1 - success and nothing changed
65 2 - success but pins reconfigured
66 3 - success but gains inverted
67 4 - success but pins reconfigured and gains inverted
68 *
69 * IMPORTANT: Default implementation provided in the CurrentSense class, but can be overriden in the child classes
70 */
71 virtual int driverAlign(float align_voltage, bool modulation_centered = false);
72
73 /**
74 * Function rading the phase currents a, b and c
75 * This function will be used with the foc control throught the function
76 * CurrentSense::getFOCCurrents(electrical_angle)
77 * - it returns current c equal to 0 if only two phase measurements available
78 *
79 * @return PhaseCurrent_s current values
80 */
82 /**
83 * Function reading the magnitude of the current set to the motor
84 * It returns the absolute or signed magnitude if possible
85 * It can receive the motor electrical angle to help with calculation
86 * This function is used with the current control (not foc)
87 *
88 * @param angle_el - electrical angle of the motor (optional)
89 */
90 virtual float getDCCurrent(float angle_el = 0);
91
92 /**
93 * Function used for FOC control, it reads the DQ currents of the motor
94 * It uses the function getPhaseCurrents internally
95 *
96 * @param angle_el - motor electrical angle
97 */
98 DQCurrent_s getFOCCurrents(float angle_el);
99
100 /**
101 * Function used for Clarke transform in FOC control
102 * It reads the phase currents of the motor
103 * It returns the alpha and beta currents
104 *
105 * @param current - phase current
106 */
108
109 /**
110 * Function used for Park transform in FOC control
111 * It reads the Alpha Beta currents and electrical angle of the motor
112 * It returns the D and Q currents
113 *
114 * @param current - phase current
115 */
116 DQCurrent_s getDQCurrents(ABCurrent_s current,float angle_el);
117
118 /**
119 * enable the current sense. default implementation does nothing, but you can
120 * override it to do something useful.
121 */
122 virtual void enable();
123
124 /**
125 * disable the current sense. default implementation does nothing, but you can
126 * override it to do something useful.
127 */
128 virtual void disable();
129
130 /**
131 * Function used to align the current sense with the BLDC motor driver
132 */
133 int alignBLDCDriver(float align_voltage, BLDCDriver* driver, bool modulation_centered);
134 /**
135 * Function used to align the current sense with the Stepper motor driver
136 */
137 int alignStepperDriver(float align_voltage, StepperDriver* driver, bool modulation_centered);
138 /**
139 * Function used to align the current sense with the Hybrid motor driver
140 */
141 int alignHybridDriver(float align_voltage, BLDCDriver* driver, bool modulation_centered);
142
143 /**
144 * Function used to read the average current values over N samples
145 */
147
148};
149
150#endif
DriverType
Definition FOCDriver.h:15
@ UnknownDriver
Definition FOCDriver.h:16
float gain_b
phase B gain
float offset_ic
zero current C voltage value (center of the adc reading)
virtual int driverAlign(float align_voltage, bool modulation_centered=false)
PhaseCurrent_s readAverageCurrents(int N=100)
DriverType driver_type
driver type (BLDC or Stepper)
int pinC
pin C analog pin for current measurement
int alignBLDCDriver(float align_voltage, BLDCDriver *driver, bool modulation_centered)
FOCDriver * driver
driver link
int alignStepperDriver(float align_voltage, StepperDriver *driver, bool modulation_centered)
virtual void enable()
int alignHybridDriver(float align_voltage, BLDCDriver *driver, bool modulation_centered)
bool skip_align
variable signaling that the phase current direction should be verified during initFOC()
DQCurrent_s getDQCurrents(ABCurrent_s current, float angle_el)
float gain_c
phase C gain
virtual PhaseCurrent_s getPhaseCurrents()=0
float gain_a
phase A gain
int pinB
pin B analog pin for current measurement
int pinA
pin A analog pin for current measurement
virtual float getDCCurrent(float angle_el=0)
virtual void disable()
float offset_ib
zero current B voltage value (center of the adc reading)
void * params
pointer to hardware specific parameters of current sensing
ABCurrent_s getABCurrents(PhaseCurrent_s current)
virtual int init()=0
float offset_ia
zero current A voltage value (center of the adc reading)
DQCurrent_s getFOCCurrents(float angle_el)
void linkDriver(FOCDriver *driver)