Skip to content

Commit a6daa97

Browse files
authored
Merge pull request #11 from Candas1/dev
Dev
2 parents 70ab24c + 030508f commit a6daa97

File tree

18 files changed

+164
-64
lines changed

18 files changed

+164
-64
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,16 @@ Therefore this is an attempt to:
2727

2828
> NEW RELEASE 📢 : <span class="simple">Simple<span class="foc">FOC</span>library</span> v2.3.2
2929
> - Improved [space vector modulation code](https://github.com/simplefoc/Arduino-FOC/pull/309) thanks to [@Candas1](https://github.com/Candas1)
30+
> - Bugfix for stepper motor initialization
31+
> - Bugfix for current sensing when only 2 phase currents available - please re-check your current sense PID tuning
3032
> - Bugfix for teensy3.2 - [#321](https://github.com/simplefoc/Arduino-FOC/pull/321)
3133
> - Added teensy3/4 compile to the github CI using platformio
32-
> - And more bugfixes - see the [complete list of 2.3.2 fixes here](https://github.com/simplefoc/Arduino-FOC/issues?q=is%3Aissue+milestone%3A2.3.2_Release)
34+
> - Fix compile issues with recent versions of ESP32 framework
35+
> - Add ADC calibration on STM32 MCUs
36+
> - Bugfix for crash when using ADC2 on ESP32s - [thanks to @mcells](https://github.com/simplefoc/Arduino-FOC/pull/346)
37+
> - Bugfix for renesas PWM on UNO R4 WiFi - [thanks to @facchinm](https://github.com/simplefoc/Arduino-FOC/pull/322)
38+
> - And more bugfixes - see the complete list of 2.3.2 [fixes and PRs](https://github.com/simplefoc/Arduino-FOC/milestone/9?closed=1)
39+
3340

3441
## Arduino *SimpleFOClibrary* v2.3.2
3542

examples/hardware_specific_examples/B_G431B_ESC1/B_G431B_ESC1.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ void setup() {
9696

9797
void loop() {
9898
// main FOC algorithm function
99+
motor.loopFOC();
99100

100101
// Motion control function
101102
motor.move();

examples/hardware_specific_examples/DRV8302_driver/6pwm_example/encoder/full_control_serial/full_control_serial.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
// Motor instance
3131
BLDCMotor motor = BLDCMotor(11);
32-
BLDCDriver6PWM driver = BLDCDriver6PWM(INH_A,INH_A, INH_B,INH_B, INH_C,INL_C, EN_GATE);
32+
BLDCDriver6PWM driver = BLDCDriver6PWM(INH_A,INL_A, INH_B,INL_B, INH_C,INL_C, EN_GATE);
3333

3434
// encoder instance
3535
Encoder encoder = Encoder(2, 3, 8192);

src/common/base_classes/CurrentSense.cpp

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,12 @@
77
float CurrentSense::getDCCurrent(float motor_electrical_angle){
88
// read current phase currents
99
PhaseCurrent_s current = getPhaseCurrents();
10-
// currnet sign - if motor angle not provided the magnitude is always positive
11-
float sign = 1;
12-
10+
1311
// calculate clarke transform
14-
float i_alpha, i_beta;
15-
if(!current.c){
16-
// if only two measured currents
17-
i_alpha = current.a;
18-
i_beta = _1_SQRT3 * current.a + _2_SQRT3 * current.b;
19-
}if(!current.a){
20-
// if only two measured currents
21-
float a = -current.c - current.b;
22-
i_alpha = a;
23-
i_beta = _1_SQRT3 * a + _2_SQRT3 * current.b;
24-
}if(!current.b){
25-
// if only two measured currents
26-
float b = -current.a - current.c;
27-
i_alpha = current.a;
28-
i_beta = _1_SQRT3 * current.a + _2_SQRT3 * b;
29-
}else{
30-
// signal filtering using identity a + b + c = 0. Assumes measurement error is normally distributed.
31-
float mid = (1.f/3) * (current.a + current.b + current.c);
32-
float a = current.a - mid;
33-
float b = current.b - mid;
34-
i_alpha = a;
35-
i_beta = _1_SQRT3 * a + _2_SQRT3 * b;
36-
}
12+
ABCurrent_s ABcurrent = getABCurrents(current);
13+
14+
// current sign - if motor angle not provided the magnitude is always positive
15+
float sign = 1;
3716

3817
// if motor angle provided function returns signed value of the current
3918
// determine the sign of the current
@@ -42,32 +21,46 @@ float CurrentSense::getDCCurrent(float motor_electrical_angle){
4221
float ct;
4322
float st;
4423
_sincos(motor_electrical_angle, &st, &ct);
45-
sign = (i_beta*ct - i_alpha*st) > 0 ? 1 : -1;
24+
sign = (ABcurrent.beta*ct - ABcurrent.alpha*st) > 0 ? 1 : -1;
4625
}
4726
// return current magnitude
48-
return sign*_sqrt(i_alpha*i_alpha + i_beta*i_beta);
27+
return sign*_sqrt(ABcurrent.alpha*ABcurrent.alpha + ABcurrent.beta*ABcurrent.beta);
4928
}
5029

5130
// function used with the foc algorihtm
5231
// calculating DQ currents from phase currents
5332
// - function calculating park and clarke transform of the phase currents
54-
// - using getPhaseCurrents internally
33+
// - using getPhaseCurrents and getABCurrents internally
5534
DQCurrent_s CurrentSense::getFOCCurrents(float angle_el){
5635
// read current phase currents
5736
PhaseCurrent_s current = getPhaseCurrents();
5837

38+
// calculate clarke transform
39+
ABCurrent_s ABcurrent = getABCurrents(current);
40+
41+
// calculate park transform
42+
DQCurrent_s return_current = getDQCurrents(ABcurrent,angle_el);
43+
44+
return return_current;
45+
}
46+
47+
// function used with the foc algorihtm
48+
// calculating Alpha Beta currents from phase currents
49+
// - function calculating Clarke transform of the phase currents
50+
ABCurrent_s CurrentSense::getABCurrents(PhaseCurrent_s current){
51+
5952
// calculate clarke transform
6053
float i_alpha, i_beta;
6154
if(!current.c){
6255
// if only two measured currents
6356
i_alpha = current.a;
6457
i_beta = _1_SQRT3 * current.a + _2_SQRT3 * current.b;
65-
}if(!current.a){
58+
}else if(!current.a){
6659
// if only two measured currents
6760
float a = -current.c - current.b;
6861
i_alpha = a;
6962
i_beta = _1_SQRT3 * a + _2_SQRT3 * current.b;
70-
}if(!current.b){
63+
}else if(!current.b){
7164
// if only two measured currents
7265
float b = -current.a - current.c;
7366
i_alpha = current.a;
@@ -81,13 +74,23 @@ DQCurrent_s CurrentSense::getFOCCurrents(float angle_el){
8174
i_beta = _1_SQRT3 * a + _2_SQRT3 * b;
8275
}
8376

84-
// calculate park transform
77+
ABCurrent_s return_ABcurrent;
78+
return_ABcurrent.alpha = i_alpha;
79+
return_ABcurrent.beta = i_beta;
80+
return return_ABcurrent;
81+
}
82+
83+
// function used with the foc algorihtm
84+
// calculating D and Q currents from Alpha Beta currents and electrical angle
85+
// - function calculating Clarke transform of the phase currents
86+
DQCurrent_s CurrentSense::getDQCurrents(ABCurrent_s current, float angle_el){
87+
// calculate park transform
8588
float ct;
8689
float st;
8790
_sincos(angle_el, &st, &ct);
8891
DQCurrent_s return_current;
89-
return_current.d = i_alpha * ct + i_beta * st;
90-
return_current.q = i_beta * ct - i_alpha * st;
92+
return_current.d = current.alpha * ct + current.beta * st;
93+
return_current.q = current.beta * ct - current.alpha * st;
9194
return return_current;
9295
}
9396

@@ -96,4 +99,4 @@ DQCurrent_s CurrentSense::getFOCCurrents(float angle_el){
9699
*/
97100
void CurrentSense::linkDriver(BLDCDriver* _driver) {
98101
driver = _driver;
99-
}
102+
}

src/common/base_classes/CurrentSense.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class CurrentSense{
5353
virtual PhaseCurrent_s getPhaseCurrents() = 0;
5454
/**
5555
* Function reading the magnitude of the current set to the motor
56-
* It returns the abosolute or signed magnitude if possible
56+
* It returns the absolute or signed magnitude if possible
5757
* It can receive the motor electrical angle to help with calculation
5858
* This function is used with the current control (not foc)
5959
*
@@ -62,13 +62,31 @@ class CurrentSense{
6262
virtual float getDCCurrent(float angle_el = 0);
6363

6464
/**
65-
* Function used for FOC contorl, it reads the DQ currents of the motor
65+
* Function used for FOC control, it reads the DQ currents of the motor
6666
* It uses the function getPhaseCurrents internally
6767
*
6868
* @param angle_el - motor electrical angle
6969
*/
7070
DQCurrent_s getFOCCurrents(float angle_el);
7171

72+
/**
73+
* Function used for Clarke transform in FOC control
74+
* It reads the phase currents of the motor
75+
* It returns the alpha and beta currents
76+
*
77+
* @param current - phase current
78+
*/
79+
ABCurrent_s getABCurrents(PhaseCurrent_s current);
80+
81+
/**
82+
* Function used for Park transform in FOC control
83+
* It reads the Alpha Beta currents and electircal angle of the motor
84+
* It returns the D and Q currents
85+
*
86+
* @param current - phase current
87+
*/
88+
DQCurrent_s getDQCurrents(ABCurrent_s current,float angle_el);
89+
7290

7391
};
7492

src/common/base_classes/Sensor.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
void Sensor::update() {
88
float val = getSensorAngle();
9+
if (val<0) // sensor angles are strictly non-negative. Negative values are used to signal errors.
10+
return; // TODO signal error, e.g. via a flag and counter
911
angle_prev_ts = _micros();
1012
float d_angle = val - angle_prev;
1113
// if overflow happened track it as full rotation

src/common/foc_utils.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ struct DQVoltage_s
5656
float d;
5757
float q;
5858
};
59+
// alpha beta current structure
60+
struct ABCurrent_s
61+
{
62+
float alpha;
63+
float beta;
64+
};
5965

6066

6167
/**

src/communication/Commander.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ Commander::Commander(char eol, bool echo){
1111
}
1212

1313

14-
void Commander::add(char id, CommandCallback onCommand, char* label ){
14+
void Commander::add(char id, CommandCallback onCommand, const char* label ){
1515
call_list[call_count] = onCommand;
1616
call_ids[call_count] = id;
17-
call_label[call_count] = label;
17+
call_label[call_count] = (char*)label;
1818
call_count++;
1919
}
2020

src/communication/Commander.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class Commander
9191
* @param onCommand - function pointer void function(char*)
9292
* @param label - string label to be displayed when scan command sent
9393
*/
94-
void add(char id , CommandCallback onCommand, char* label = nullptr);
94+
void add(char id , CommandCallback onCommand, const char* label = nullptr);
9595

9696
// printing variables
9797
VerboseMode verbose = VerboseMode::user_friendly; //!< flag signaling that the commands should output user understanable text

src/current_sense/hardware_specific/esp32/esp32_adc_driver.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,8 @@ bool IRAM_ATTR __adcStart(uint8_t pin){
154154
}
155155

156156
if(channel > 9){
157-
channel -= 10;
158157
CLEAR_PERI_REG_MASK(SENS_SAR_MEAS_START2_REG, SENS_MEAS2_START_SAR_M);
159-
SET_PERI_REG_BITS(SENS_SAR_MEAS_START2_REG, SENS_SAR2_EN_PAD, (1 << channel), SENS_SAR2_EN_PAD_S);
158+
SET_PERI_REG_BITS(SENS_SAR_MEAS_START2_REG, SENS_SAR2_EN_PAD, (1 << (channel - 10)), SENS_SAR2_EN_PAD_S);
160159
SET_PERI_REG_MASK(SENS_SAR_MEAS_START2_REG, SENS_MEAS2_START_SAR_M);
161160
} else {
162161
CLEAR_PERI_REG_MASK(SENS_SAR_MEAS_START1_REG, SENS_MEAS1_START_SAR_M);
@@ -225,9 +224,8 @@ uint16_t IRAM_ATTR adcRead(uint8_t pin)
225224
__analogInit();
226225

227226
if(channel > 9){
228-
channel -= 10;
229227
CLEAR_PERI_REG_MASK(SENS_SAR_MEAS_START2_REG, SENS_MEAS2_START_SAR_M);
230-
SET_PERI_REG_BITS(SENS_SAR_MEAS_START2_REG, SENS_SAR2_EN_PAD, (1 << channel), SENS_SAR2_EN_PAD_S);
228+
SET_PERI_REG_BITS(SENS_SAR_MEAS_START2_REG, SENS_SAR2_EN_PAD, (1 << (channel - 10)), SENS_SAR2_EN_PAD_S);
231229
SET_PERI_REG_MASK(SENS_SAR_MEAS_START2_REG, SENS_MEAS2_START_SAR_M);
232230
} else {
233231
CLEAR_PERI_REG_MASK(SENS_SAR_MEAS_START1_REG, SENS_MEAS1_START_SAR_M);
@@ -257,4 +255,4 @@ uint16_t IRAM_ATTR adcRead(uint8_t pin)
257255
}
258256

259257

260-
#endif
258+
#endif

0 commit comments

Comments
 (0)