Skip to content

Commit 972369f

Browse files
authored
Merge branch 'dev' into foc_refactoring
2 parents 72cdd21 + 0caaf03 commit 972369f

File tree

21 files changed

+129
-61
lines changed

21 files changed

+129
-61
lines changed

.github/workflows/ccpp.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ jobs:
5858
sketch-names: single_full_control_example.ino
5959

6060
- arduino-boards-fqbn: esp32:esp32:esp32s2 # esp32s2
61-
platform-url: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_dev_index.json
62-
sketch-names: bldc_driver_3pwm_standalone.ino, stepper_driver_2pwm_standalone.ino, stepper_driver_4pwm_standalone
61+
platform-url: https://espressif.github.io/arduino-esp32/package_esp32_index.json
62+
sketch-names: bldc_driver_3pwm_standalone.ino,stepper_driver_2pwm_standalone.ino,stepper_driver_4pwm_standalone.ino
6363

6464
- arduino-boards-fqbn: esp32:esp32:esp32s3 # esp32s3
65-
platform-url: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_dev_index.json
65+
platform-url: https://espressif.github.io/arduino-esp32/package_esp32_index.json
6666
sketch-names: esp32_position_control.ino, esp32_i2c_dual_bus_example.ino
6767

6868
- arduino-boards-fqbn: esp32:esp32:esp32doit-devkit-v1 # esp32
69-
platform-url: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_dev_index.json
69+
platform-url: https://espressif.github.io/arduino-esp32/package_esp32_index.json
7070
sketch-names: esp32_position_control.ino, esp32_i2c_dual_bus_example.ino, esp32_current_control_low_side.ino, esp32_spi_alt_example.ino
7171

7272
- arduino-boards-fqbn: STMicroelectronics:stm32:GenF1:pnum=BLUEPILL_F103C8 # bluepill - hs examples

.github/workflows/teensy.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: PlatformIO - Teensy build
2+
3+
on: [push]
4+
5+
jobs:
6+
build:
7+
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v3
11+
- uses: actions/cache@v3
12+
with:
13+
path: |
14+
~/.cache/pip
15+
~/.platformio/.cache
16+
key: ${{ runner.os }}-pio
17+
- uses: actions/setup-python@v4
18+
with:
19+
python-version: '3.9'
20+
- name: Install PlatformIO Core
21+
run: pip install --upgrade platformio
22+
23+
- name: PIO Run Teensy 4
24+
run: pio ci --lib="." --board=teensy41 --board=teensy40
25+
env:
26+
PLATFORMIO_CI_SRC: examples/hardware_specific_examples/Teensy/Teensy4/bldc_driver_6pwm_standalone/bldc_driver_6pwm_standalone.ino
27+
28+
- name: PIO Run Teensy 4
29+
run: pio ci --lib="." --board=teensy41 --board=teensy40
30+
env:
31+
PLATFORMIO_CI_SRC: examples/hardware_specific_examples/Teensy/Teensy4/open_loop_velocity_6pwm/open_loop_velocity_6pwm.ino
32+
33+
- name: PIO Run Teensy 3
34+
run: pio ci --lib="." --board=teensy31 --board=teensy30 --board=teensy35 --board=teensy36
35+
env:
36+
PLATFORMIO_CI_SRC: examples/hardware_specific_examples/Teensy/Teensy3/bldc_driver_6pwm_standalone/bldc_driver_6pwm_standalone.ino
37+
38+
- name: PIO Run Teensy 3
39+
run: pio ci --lib="." --board=teensy31 --board=teensy30 --board=teensy35 --board=teensy36
40+
env:
41+
PLATFORMIO_CI_SRC: examples/hardware_specific_examples/Teensy/Teensy3/open_loop_velocity_6pwm/open_loop_velocity_6pwm.ino

README.md

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# SimpleFOClibrary - **Simple** Field Oriented Control (FOC) **library** <br>
22
### A Cross-Platform FOC implementation for BLDC and Stepper motors<br> based on the Arduino IDE and PlatformIO
33

4-
![Library Compile](https://github.com/simplefoc/Arduino-FOC/workflows/Library%20Compile/badge.svg)
4+
![Library Compile](https://github.com/simplefoc/Arduino-FOC/workflows/Library%20Compile/badge.svg) [![PlatformIO - Teensy build](https://github.com/simplefoc/Arduino-FOC/actions/workflows/teensy.yml/badge.svg)](https://github.com/simplefoc/Arduino-FOC/actions/workflows/teensy.yml)
5+
56
![GitHub release (latest by date)](https://img.shields.io/github/v/release/simplefoc/arduino-foc)
67
![GitHub Release Date](https://img.shields.io/github/release-date/simplefoc/arduino-foc?color=blue)
78
![GitHub commits since tagged version](https://img.shields.io/github/commits-since/simplefoc/arduino-foc/latest/dev)
@@ -24,20 +25,13 @@ Therefore this is an attempt to:
2425
- *Medium-power* BLDC driver (<30Amps): [Arduino <span class="simple">Simple<b>FOC</b>PowerShield</span> ](https://github.com/simplefoc/Arduino-SimpleFOC-PowerShield).
2526
- See also [@byDagor](https://github.com/byDagor)'s *fully-integrated* ESP32 based board: [Dagor Brushless Controller](https://github.com/byDagor/Dagor-Brushless-Controller)
2627

27-
> NEW RELEASE 📢 : <span class="simple">Simple<span class="foc">FOC</span>library</span> v2.3.1
28-
> - Support for Arduino UNO R4 Minima (Renesas R7FA4M1 MCU - note UNO R4 WiFi is not yet supported)
29-
> - Support setting PWM polarity on ESP32 (thanks to [@mcells](https://github.com/mcells))
30-
> - Expose I2C errors in MagneticSensorI2C (thanks to [@padok](https://github.com/padok))
31-
> - Improved default trig functions (sine, cosine) - faster, smaller
32-
> - Overridable trig functions - plug in your own optimized versions
33-
> - Bugfix: microseconds overflow in velocity mode [#287](https://github.com/simplefoc/Arduino-FOC/issues/287)
34-
> - Bugfix: KV initialization ([5fc3128](https://github.com/simplefoc/Arduino-FOC/commit/5fc3128d282b65c141ca486327c6235089999627))
35-
> - And more bugfixes - see the [complete list of 2.3.1 fixes here](https://github.com/simplefoc/Arduino-FOC/issues?q=is%3Aissue+milestone%3A2.3.1_Release)
36-
> - Change: simplify initFOC() API ([d57d32d](https://github.com/simplefoc/Arduino-FOC/commit/d57d32dd8715dbed4e476469bc3de0c052f1d531). [5231e5e](https://github.com/simplefoc/Arduino-FOC/commit/5231e5e1d044b0cc33ede67664b6ef2f9d0a8cdf), [10c5b87](https://github.com/simplefoc/Arduino-FOC/commit/10c5b872672cab72df16ddd738bbf09bcce95d28))
37-
> - Change: check for linked driver in currentsense and exit gracefully ([5ef4d9d](https://github.com/simplefoc/Arduino-FOC/commit/5ef4d9d5a92e03da0dd5af7f624243ab30f1b688))
38-
> - Compatibility with newest versions of Arduino framework for STM32, Renesas, ESP32, Atmel SAM, Atmel AVR, nRF52 and RP2040
39-
40-
## Arduino *SimpleFOClibrary* v2.3.1
28+
> NEW RELEASE 📢 : <span class="simple">Simple<span class="foc">FOC</span>library</span> v2.3.2
29+
> - Improved [space vector modulation code](https://github.com/simplefoc/Arduino-FOC/pull/309) thanks to [@Candas1](https://github.com/Candas1)
30+
> - Bugfix for teensy3.2 - [#321](https://github.com/simplefoc/Arduino-FOC/pull/321)
31+
> - 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)
33+
34+
## Arduino *SimpleFOClibrary* v2.3.2
4135

4236
<p align="">
4337
<a href="https://youtu.be/Y5kLeqTc6Zk">

keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ sensor_direction KEYWORD2
133133
sensor_offset KEYWORD2
134134
zero_electric_angle KEYWORD2
135135
verbose KEYWORD2
136+
verboseMode KEYWORD1
136137
decimal_places KEYWORD2
137138
phase_resistance KEYWORD2
138139
phase_inductance KEYWORD2

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Simple FOC
2-
version=2.3.1
2+
version=2.3.2
33
author=Simplefoc <[email protected]>
44
maintainer=Simplefoc <[email protected]>
55
sentence=A library demistifying FOC for BLDC motors

src/BLDCMotor.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ void BLDCMotor::move(float new_target) {
422422
shaft_angle_sp = target;
423423
// calculate velocity set point
424424
shaft_velocity_sp = feed_forward_velocity + P_angle( shaft_angle_sp - shaft_angle );
425-
shaft_angle_sp = _constrain(shaft_angle_sp,-velocity_limit, velocity_limit);
425+
shaft_velocity_sp = _constrain(shaft_velocity_sp,-velocity_limit, velocity_limit);
426426
// calculate the torque command - sensor precision: this calculation is ok, but based on bad value from previous calculation
427427
current_sp = PID_velocity(shaft_velocity_sp - shaft_velocity); // if voltage torque control
428428
// if torque controlled through voltage
@@ -561,6 +561,8 @@ void BLDCMotor::setPhaseVoltage(float Uq, float Ud, float angle_el) {
561561

562562
center = driver->voltage_limit/2;
563563
if (foc_modulation == FOCModulationType::SpaceVectorPWM){
564+
// discussed here: https://community.simplefoc.com/t/embedded-world-2023-stm32-cordic-co-processor/3107/165?u=candas1
565+
// a bit more info here: https://microchipdeveloper.com/mct5001:which-zsm-is-best
564566
// Midpoint Clamp
565567
float Umin = min(Ua, min(Ub, Uc));
566568
float Umax = max(Ua, max(Ub, Uc));

src/StepperMotor.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ int StepperMotor::alignSensor() {
134134
SIMPLEFOC_DEBUG("MOT: Align sensor.");
135135

136136
// if unknown natural direction
137-
if(!_isset(sensor_direction)){
137+
if(sensor_direction == Direction::UNKNOWN){
138138
// check if sensor needs zero search
139139
if(sensor->needsSearch()) exit_flag = absoluteZeroSearch();
140140
// stop init if not found index
@@ -311,7 +311,7 @@ void StepperMotor::move(float new_target) {
311311
shaft_angle_sp = target;
312312
// calculate velocity set point
313313
shaft_velocity_sp = feed_forward_velocity + P_angle( shaft_angle_sp - shaft_angle );
314-
shaft_angle_sp = _constrain(shaft_angle_sp, -velocity_limit, velocity_limit);
314+
shaft_velocity_sp = _constrain(shaft_velocity_sp, -velocity_limit, velocity_limit);
315315
// calculate the torque command
316316
current_sp = PID_velocity(shaft_velocity_sp - shaft_velocity); // if voltage torque control
317317
// if torque controlled through voltage
@@ -440,4 +440,4 @@ float StepperMotor::angleOpenloop(float target_angle){
440440
open_loop_timestamp = now_us;
441441

442442
return Uq;
443-
}
443+
}

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.cpp

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,33 @@ __attribute__((weak)) void _sincos(float a, float* s, float* c){
4444
*c = _cos(a);
4545
}
4646

47+
// fast_atan2 based on https://math.stackexchange.com/a/1105038/81278
48+
// Via Odrive project
49+
// https://github.com/odriverobotics/ODrive/blob/master/Firmware/MotorControl/utils.cpp
50+
// This function is MIT licenced, copyright Oskar Weigl/Odrive Robotics
51+
// The origin for Odrive atan2 is public domain. Thanks to Odrive for making
52+
// it easy to borrow.
53+
__attribute__((weak)) float _atan2(float y, float x) {
54+
// a := min (|x|, |y|) / max (|x|, |y|)
55+
float abs_y = fabsf(y);
56+
float abs_x = fabsf(x);
57+
// inject FLT_MIN in denominator to avoid division by zero
58+
float a = min(abs_x, abs_y) / (max(abs_x, abs_y));
59+
// s := a * a
60+
float s = a * a;
61+
// r := ((-0.0464964749 * s + 0.15931422) * s - 0.327622764) * s * a + a
62+
float r =
63+
((-0.0464964749f * s + 0.15931422f) * s - 0.327622764f) * s * a + a;
64+
// if |y| > |x| then r := 1.57079637 - r
65+
if (abs_y > abs_x) r = 1.57079637f - r;
66+
// if x < 0 then r := 3.14159274 - r
67+
if (x < 0.0f) r = 3.14159274f - r;
68+
// if y < 0 then r := -r
69+
if (y < 0.0f) r = -r;
70+
71+
return r;
72+
}
73+
4774

4875
// normalizing radian angle to [0,2PI]
4976
__attribute__((weak)) float _normalizeAngle(float angle){
@@ -60,14 +87,10 @@ float _electricalAngle(float shaft_angle, int pole_pairs) {
6087
// https://reprap.org/forum/read.php?147,219210
6188
// https://en.wikipedia.org/wiki/Fast_inverse_square_root
6289
__attribute__((weak)) float _sqrtApprox(float number) {//low in fat
63-
// float x;
64-
// const float f = 1.5F; // better precision
65-
66-
// x = number * 0.5F;
67-
float y = number;
68-
long i = * ( long * ) &y;
69-
i = 0x5f375a86 - ( i >> 1 );
70-
y = * ( float * ) &i;
71-
// y = y * ( f - ( x * y * y ) ); // better precision
72-
return number * y;
90+
union {
91+
float f;
92+
uint32_t i;
93+
} y = { .f = number };
94+
y.i = 0x5f375a86 - ( y.i >> 1 );
95+
return number * y.f;
7396
}

src/common/foc_utils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ float _cos(float a);
8585
*/
8686
void _sincos(float a, float* s, float* c);
8787

88+
/**
89+
* Function approximating atan2
90+
*
91+
*/
92+
float _atan2(float y, float x);
8893

8994
/**
9095
* normalizing radian angle to [0,2PI]

0 commit comments

Comments
 (0)