SimpleFOClibrary 2.4.0
Loading...
Searching...
No Matches
InlineCurrentSense.cpp
Go to the documentation of this file.
3// InlineCurrentSensor constructor
4// - shunt_resistor - shunt resistor value
5// - gain - current-sense op-amp gain
6// - phA - A phase adc pin
7// - phB - B phase adc pin
8// - phC - C phase adc pin (optional)
9InlineCurrentSense::InlineCurrentSense(float _shunt_resistor, float _gain, int _pinA, int _pinB, int _pinC){
10 pinA = _pinA;
11 pinB = _pinB;
12 pinC = _pinC;
13
14 shunt_resistor = _shunt_resistor;
15 amp_gain = _gain;
16 volts_to_amps_ratio = 1.0f /_shunt_resistor / _gain; // volts to amps
17 // gains for each phase
18 gain_a = volts_to_amps_ratio;
19 gain_b = volts_to_amps_ratio;
20 gain_c = volts_to_amps_ratio;
21};
22
23
24InlineCurrentSense::InlineCurrentSense(float _mVpA, int _pinA, int _pinB, int _pinC){
25 pinA = _pinA;
26 pinB = _pinB;
27 pinC = _pinC;
28
29 volts_to_amps_ratio = 1000.0f / _mVpA; // mV to amps
30 // gains for each phase
31 gain_a = volts_to_amps_ratio;
32 gain_b = volts_to_amps_ratio;
33 gain_c = volts_to_amps_ratio;
34};
35
36
37
38// Inline sensor init function
40 // if no linked driver its fine in this case
41 // at least for init()
42 void* drv_params = driver ? driver->params : nullptr;
43 // configure ADC variables
45 // if init failed return fail
47 // set the center pwm (0 voltage vector)
50 // calibrate zero offsets
51 calibrateOffsets();
52 // set zero voltage to all phases
54 static_cast<BLDCDriver*>(driver)->setPwm(0,0,0);
55 // set the initialized flag
57 // return success
58 return 1;
59}
60// Function finding zero offsets of the ADC
61void InlineCurrentSense::calibrateOffsets(){
62 const int calibration_rounds = 1000;
63
64 // find adc offset = zero current voltage
65 offset_ia = 0;
66 offset_ib = 0;
67 offset_ic = 0;
68 // read the adc voltage 1000 times ( arbitrary number )
69 for (int i = 0; i < calibration_rounds; i++) {
73 _delay(1);
74 }
75 // calculate the mean offsets
76 if(_isset(pinA)) offset_ia = offset_ia / calibration_rounds;
77 if(_isset(pinB)) offset_ib = offset_ib / calibration_rounds;
78 if(_isset(pinC)) offset_ic = offset_ic / calibration_rounds;
79}
80
81// read all three phase currents (if possible 2 or 3)
83 PhaseCurrent_s current;
84 current.a = (!_isset(pinA)) ? 0 : (_readADCVoltageInline(pinA, params) - offset_ia)*gain_a;// amps
85 current.b = (!_isset(pinB)) ? 0 : (_readADCVoltageInline(pinB, params) - offset_ib)*gain_b;// amps
86 current.c = (!_isset(pinC)) ? 0 : (_readADCVoltageInline(pinC, params) - offset_ic)*gain_c; // amps
87 return current;
88}
@ BLDC
Definition FOCDriver.h:17
float gain_b
phase B gain
float offset_ic
zero current C voltage value (center of the adc reading)
DriverType driver_type
driver type (BLDC or Stepper)
int pinC
pin C analog pin for current measurement
FOCDriver * driver
driver link
float gain_c
phase C gain
float gain_a
phase A gain
int pinB
pin B analog pin for current measurement
int pinA
pin A analog pin for current measurement
float offset_ib
zero current B voltage value (center of the adc reading)
void * params
pointer to hardware specific parameters of current sensing
float offset_ia
zero current A voltage value (center of the adc reading)
float voltage_limit
limiting voltage set to the motor
Definition FOCDriver.h:37
void * params
pointer to hardware specific parameters of driver
Definition FOCDriver.h:40
InlineCurrentSense(float shunt_resistor, float gain, int pinA, int pinB, int pinC=NOT_SET)
PhaseCurrent_s getPhaseCurrents() override
void * _configureADCInline(const void *driver_params, const int pinA, const int pinB, const int pinC=NOT_SET)
float _readADCVoltageInline(const int pinA, const void *cs_params)
#define SIMPLEFOC_CURRENT_SENSE_INIT_FAILED
#define _isset(a)
Definition foc_utils.h:13
float c
Definition foc_utils.h:68
float a
Definition foc_utils.h:66
float b
Definition foc_utils.h:67
void _delay(unsigned long ms)
Definition time_utils.cpp:5