Skip to content

Commit 1361c6e

Browse files
author
Richard Unger
committed
Merge branch 'dev' of https://github.com/simplefoc/Arduino-FOC into dev
2 parents 4dd59b6 + 091a98d commit 1361c6e

File tree

14 files changed

+408
-58
lines changed

14 files changed

+408
-58
lines changed

README.md

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,31 @@ Therefore this is an attempt to:
2424
- *Medium-power* BLDC driver (<30Amps): [Arduino <span class="simple">Simple<b>FOC</b>PowerShield</span> ](https://github.com/simplefoc/Arduino-SimpleFOC-PowerShield).
2525
- See also [@byDagor](https://github.com/byDagor)'s *fully-integrated* ESP32 based board: [Dagor Brushless Controller](https://github.com/byDagor/Dagor-Brushless-Controller)
2626

27-
<h3>NEWS 📢: SimpleFOClibrary has been published in the Journal of Open Source Software</h3>
28-
<p>
29-
<b>SimpleFOC</b>: A Field Oriented Control (FOC) Library for Controlling Brushless Direct Current (BLDC) and Stepper Motors.<br>
30-
A. Skuric, HS. Bank, R. Unger, O. Williams, D. González-Reyes<br>
31-
Journal of Open Source Software, 7(74), 4232, https://doi.org/10.21105/joss.04232
32-
</p>
33-
34-
> FUTURE RELEASE : <span class="simple">Simple<span class="foc">FOC</span>library</span> v2.2.3
27+
> NEW RELEASE 📢 : <span class="simple">Simple<span class="foc">FOC</span>library</span> v2.2.3
3528
> - stm32 low-side current sensing
3629
> - g4 supported
37-
> - thoroughly tested f1/f4/g4
30+
> - thoroughly tested f1/f4/g4 - [#187](https://github.com/simplefoc/Arduino-FOC/issues/187)
31+
> - bg431b: added support for VBAT and TEMPERATURE readings [#222](https://github.com/simplefoc/Arduino-FOC/pull/222)
3832
> - bugfixing
3933
> - leonardo
40-
> - mega2560
41-
> - inline current sense without driver #188
42-
> - `initFOC` fails if current sense not initialised
43-
> - driver and cs have to be well initialised for `initFOC` to start
44-
> - `cs.init()` and `driver.init()` return `1` if well initialised and `0` if failed
45-
## Arduino *SimpleFOClibrary* v2.2.2
34+
> - mega2560 [#190](https://github.com/simplefoc/Arduino-FOC/issues/190)
35+
> - inline current sense without driver [#188](https://github.com/simplefoc/Arduino-FOC/issues/188)
36+
> - bg431b support current sense fix [#210](https://github.com/simplefoc/Arduino-FOC/pull/210)
37+
> - StepperDriver4PWM wrong init [#182](https://github.com/simplefoc/Arduino-FOC/issues/182)
38+
> - open loop back-emf vlotage issue [#219](https://github.com/simplefoc/Arduino-FOC/issues/219)
39+
> - SAMD51 compile issue [#217](https://github.com/simplefoc/Arduino-FOC/issues/217)
40+
> - ESP32-S3 compile issue [#198](https://github.com/simplefoc/Arduino-FOC/issues/198)
41+
> - ESP32 compile issue [#208](https://github.com/simplefoc/Arduino-FOC/issues/208), [#207](https://github.com/simplefoc/Arduino-FOC/issues/207)
42+
> - magnetic sensor direction finding more robust [#173](https://github.com/simplefoc/Arduino-FOC/issues/173), [#164](https://github.com/simplefoc/Arduino-FOC/pull/164)
43+
> - `StepDirListener` improved timing [#169](https://github.com/simplefoc/Arduino-FOC/issues/169), [#209](https://github.com/simplefoc/Arduino-FOC/pull/209)
44+
> - API changes
45+
> - `setPhaseVoltage` is now public function
46+
> - `getVelocity` can now be called as many times as necessary (it recalculates the velocity if the time between calls is longer then `minDeltaT` - default 0.1ms)
47+
> - BG431 board can be used only with `LowsideCurrentSense` class!
48+
> - `initFOC` fails if current sense not initialised
49+
> - driver and curent sense have to be well initialised for `initFOC` to start
50+
> - `cs.init()` and `driver.init()` return `1` if well initialised and `0` if failed
51+
## Arduino *SimpleFOClibrary* v2.2.3
4652

4753
<p align="">
4854
<a href="https://youtu.be/Y5kLeqTc6Zk">

src/BLDCMotor.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ int BLDCMotor::alignSensor() {
185185
for (int i = 0; i <=500; i++ ) {
186186
float angle = _3PI_2 + _2PI * i / 500.0f;
187187
setPhaseVoltage(voltage_sensor_align, 0, angle);
188-
sensor->update();
188+
sensor->update();
189189
_delay(2);
190190
}
191191
// take and angle in the middle
@@ -195,7 +195,7 @@ int BLDCMotor::alignSensor() {
195195
for (int i = 500; i >=0; i-- ) {
196196
float angle = _3PI_2 + _2PI * i / 500.0f ;
197197
setPhaseVoltage(voltage_sensor_align, 0, angle);
198-
sensor->update();
198+
sensor->update();
199199
_delay(2);
200200
}
201201
sensor->update();
@@ -638,9 +638,11 @@ float BLDCMotor::velocityOpenloop(float target_velocity){
638638

639639
// use voltage limit or current limit
640640
float Uq = voltage_limit;
641-
if(_isset(phase_resistance))
642-
Uq = _constrain(current_limit*phase_resistance + voltage_bemf,-voltage_limit, voltage_limit);
643-
641+
if(_isset(phase_resistance)){
642+
Uq = _constrain(current_limit*phase_resistance + fabs(voltage_bemf),-voltage_limit, voltage_limit);
643+
// recalculate the current
644+
current.q = (Uq - fabs(voltage_bemf))/phase_resistance;
645+
}
644646
// set the maximal allowed voltage (voltage_limit) with the necessary angle
645647
setPhaseVoltage(Uq, 0, _electricalAngle(shaft_angle, pole_pairs));
646648

@@ -676,8 +678,11 @@ float BLDCMotor::angleOpenloop(float target_angle){
676678

677679
// use voltage limit or current limit
678680
float Uq = voltage_limit;
679-
if(_isset(phase_resistance))
680-
Uq = _constrain(current_limit*phase_resistance + voltage_bemf,-voltage_limit, voltage_limit);
681+
if(_isset(phase_resistance)){
682+
Uq = _constrain(current_limit*phase_resistance + fabs(voltage_bemf),-voltage_limit, voltage_limit);
683+
// recalculate the current
684+
current.q = (Uq - fabs(voltage_bemf))/phase_resistance;
685+
}
681686
// set the maximal allowed voltage (voltage_limit) with the necessary angle
682687
// sensor precision: this calculation is OK due to the normalisation
683688
setPhaseVoltage(Uq, 0, _electricalAngle(_normalizeAngle(shaft_angle), pole_pairs));

src/BLDCMotor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class BLDCMotor: public FOCMotor
7878
* @param Ud Current voltage in d axis to set to the motor
7979
* @param angle_el current electrical angle of the motor
8080
*/
81-
void setPhaseVoltage(float Uq, float Ud, float angle_el);
81+
void setPhaseVoltage(float Uq, float Ud, float angle_el) override;
8282

8383
private:
8484
// FOC methods

src/StepperMotor.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ int StepperMotor::alignSensor() {
141141
for (int i = 0; i <=500; i++ ) {
142142
float angle = _3PI_2 + _2PI * i / 500.0f;
143143
setPhaseVoltage(voltage_sensor_align, 0, angle);
144+
sensor->update();
144145
_delay(2);
145146
}
146147
// take and angle in the middle
@@ -150,6 +151,7 @@ int StepperMotor::alignSensor() {
150151
for (int i = 500; i >=0; i-- ) {
151152
float angle = _3PI_2 + _2PI * i / 500.0f ;
152153
setPhaseVoltage(voltage_sensor_align, 0, angle);
154+
sensor->update();
153155
_delay(2);
154156
}
155157
sensor->update();
@@ -377,8 +379,11 @@ float StepperMotor::velocityOpenloop(float target_velocity){
377379

378380
// use voltage limit or current limit
379381
float Uq = voltage_limit;
380-
if(_isset(phase_resistance))
381-
Uq = _constrain(current_limit*phase_resistance + voltage_bemf,-voltage_limit, voltage_limit);
382+
if(_isset(phase_resistance)){
383+
Uq = _constrain(current_limit*phase_resistance + fabs(voltage_bemf),-voltage_limit, voltage_limit);
384+
// recalculate the current
385+
current.q = (Uq - fabs(voltage_bemf))/phase_resistance;
386+
}
382387

383388
// set the maximal allowed voltage (voltage_limit) with the necessary angle
384389
setPhaseVoltage(Uq, 0, _electricalAngle(shaft_angle, pole_pairs));
@@ -412,8 +417,11 @@ float StepperMotor::angleOpenloop(float target_angle){
412417

413418
// use voltage limit or current limit
414419
float Uq = voltage_limit;
415-
if(_isset(phase_resistance))
416-
Uq = _constrain(current_limit*phase_resistance + voltage_bemf,-voltage_limit, voltage_limit);
420+
if(_isset(phase_resistance)){
421+
Uq = _constrain(current_limit*phase_resistance + fabs(voltage_bemf),-voltage_limit, voltage_limit);
422+
// recalculate the current
423+
current.q = (Uq - fabs(voltage_bemf))/phase_resistance;
424+
}
417425
// set the maximal allowed voltage (voltage_limit) with the necessary angle
418426
setPhaseVoltage(Uq, 0, _electricalAngle((shaft_angle), pole_pairs));
419427

src/StepperMotor.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,18 @@ class StepperMotor: public FOCMotor
7878

7979
float Ualpha,Ubeta; //!< Phase voltages U alpha and U beta used for inverse Park and Clarke transform
8080

81-
private:
82-
83-
// FOC methods
84-
/**
81+
/**
8582
* Method using FOC to set Uq to the motor at the optimal angle
8683
* Heart of the FOC algorithm
8784
*
8885
* @param Uq Current voltage in q axis to set to the motor
8986
* @param Ud Current voltage in d axis to set to the motor
9087
* @param angle_el current electrical angle of the motor
9188
*/
92-
void setPhaseVoltage(float Uq, float Ud , float angle_el);
89+
void setPhaseVoltage(float Uq, float Ud, float angle_el) override;
9390

91+
private:
92+
9493
/** Sensor alignment to electrical 0 angle of the motor */
9594
int alignSensor();
9695
/** Motor and sensor alignment to the sensors absolute 0 angle */

src/common/base_classes/FOCMotor.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,16 @@ class FOCMotor
127127
*/
128128
virtual void move(float target = NOT_SET)=0;
129129

130+
/**
131+
* Method using FOC to set Uq to the motor at the optimal angle
132+
* Heart of the FOC algorithm
133+
*
134+
* @param Uq Current voltage in q axis to set to the motor
135+
* @param Ud Current voltage in d axis to set to the motor
136+
* @param angle_el current electrical angle of the motor
137+
*/
138+
virtual void setPhaseVoltage(float Uq, float Ud, float angle_el)=0;
139+
130140
// State calculation methods
131141
/** Shaft angle calculation in radians [rad] */
132142
float shaftAngle();
@@ -137,6 +147,7 @@ class FOCMotor
137147
float shaftVelocity();
138148

139149

150+
140151
/**
141152
* Electrical angle calculation
142153
*/

src/common/foc_utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
// sign function
77
#define _sign(a) ( ( (a) < 0 ) ? -1 : ( (a) > 0 ) )
8+
#ifndef _round
89
#define _round(x) ((x)>=0?(long)((x)+0.5f):(long)((x)-0.5f))
10+
#endif
911
#define _constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
1012
#define _sqrt(a) (_sqrtApprox(a))
1113
#define _isset(a) ( (a) != (NOT_SET) )

src/current_sense/hardware_specific/esp32/esp32_adc_driver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "esp32_adc_driver.h"
22

3-
#if defined(ESP_H) && defined(ARDUINO_ARCH_ESP32) && defined(SOC_MCPWM_SUPPORTED)
3+
#if defined(ESP_H) && defined(ARDUINO_ARCH_ESP32) && defined(SOC_MCPWM_SUPPORTED) && !defined(CONFIG_IDF_TARGET_ESP32S2) && !defined(CONFIG_IDF_TARGET_ESP32S3)
44

55
#include "freertos/FreeRTOS.h"
66
#include "freertos/task.h"

src/current_sense/hardware_specific/esp32/esp32_mcu.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ int adc_read_index[2]={0};
6868

6969
// function reading an ADC value and returning the read voltage
7070
float _readADCVoltageLowSide(const int pin, const void* cs_params){
71-
7271
mcpwm_unit_t unit = ((ESP32MCPWMCurrentSenseParams*)cs_params)->mcpwm_unit;
7372
int buffer_index = ((ESP32MCPWMCurrentSenseParams*)cs_params)->buffer_index;
7473
float adc_voltage_conv = ((ESP32MCPWMCurrentSenseParams*)cs_params)->adc_voltage_conv;

0 commit comments

Comments
 (0)