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