SimpleFOClibrary 2.4.0
Loading...
Searching...
No Matches
LowsideCurrentSense.cpp
Go to the documentation of this file.
3// LowsideCurrentSensor 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)
9LowsideCurrentSense::LowsideCurrentSense(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
24LowsideCurrentSense::LowsideCurrentSense(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// Lowside sensor init function
39
40 if (driver==nullptr) {
41 SIMPLEFOC_DEBUG("CUR: Driver not linked!");
42 return 0;
43 }
44
45 // configure ADC variables
47 // if init failed return fail
49 // sync the driver
52 // set the center pwm (0 voltage vector)
55 // calibrate zero offsets
56 calibrateOffsets();
57 // set zero voltage to all phases
59 static_cast<BLDCDriver*>(driver)->setPwm(0,0,0);
60 // set the initialized flag
62 // return success
63 return 1;
64}
65// Function finding zero offsets of the ADC
66void LowsideCurrentSense::calibrateOffsets(){
67 const int calibration_rounds = 2000;
68
69 // find adc offset = zero current voltage
70 offset_ia = 0;
71 offset_ib = 0;
72 offset_ic = 0;
73 // read the adc voltage 1000 times ( arbitrary number )
74 for (int i = 0; i < calibration_rounds; i++) {
79 _delay(1);
80 }
81 // calculate the mean offsets
82 if(_isset(pinA)) offset_ia = offset_ia / calibration_rounds;
83 if(_isset(pinB)) offset_ib = offset_ib / calibration_rounds;
84 if(_isset(pinC)) offset_ic = offset_ic / calibration_rounds;
85}
86
87// read all three phase currents (if possible 2 or 3)
89 PhaseCurrent_s current;
91 current.a = (!_isset(pinA)) ? 0 : (_readADCVoltageLowSide(pinA, params) - offset_ia)*gain_a;// amps
92 current.b = (!_isset(pinB)) ? 0 : (_readADCVoltageLowSide(pinB, params) - offset_ib)*gain_b;// amps
93 current.c = (!_isset(pinC)) ? 0 : (_readADCVoltageLowSide(pinC, params) - offset_ic)*gain_c; // amps
94 return current;
95}
@ BLDC
Definition FOCDriver.h:17
#define SIMPLEFOC_DEBUG(msg,...)
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
LowsideCurrentSense(float shunt_resistor, float gain, int pinA, int pinB, int pinC=_NC)
PhaseCurrent_s getPhaseCurrents() override
void * _driverSyncLowSide(void *driver_params, void *cs_params)
#define SIMPLEFOC_CURRENT_SENSE_INIT_FAILED
void * _configureADCLowSide(const void *driver_params, const int pinA, const int pinB, const int pinC=NOT_SET)
void _startADC3PinConversionLowSide()
float _readADCVoltageLowSide(const int pinA, const void *cs_params)
float r
Definition foc_utils.cpp:63
#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