Skip to content

Commit 401ebe9

Browse files
author
Richard Unger
committed
Merge branch 'dev' of https://github.com/simplefoc/Arduino-FOC into dev
2 parents cac0484 + c2068a3 commit 401ebe9

File tree

42 files changed

+708
-537
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+708
-537
lines changed

.github/workflows/ccpp.yml

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@ jobs:
1414
- adafruit:samd:adafruit_metro_m4 # samd51
1515
- esp32:esp32:esp32doit-devkit-v1 # esp32
1616
- esp32:esp32:esp32s2 # esp32s2
17-
- STM32:stm32:GenF1:pnum=BLUEPILL_F103C8 # stm32 bluepill
18-
- STM32:stm32:Nucleo_64:pnum=NUCLEO_F411RE # stm32 nucleo
17+
- STMicroelectronics:stm32:GenF1:pnum=BLUEPILL_F103C8 # stm32 bluepill
18+
- STMicroelectronics:stm32:Nucleo_64:pnum=NUCLEO_F411RE # stm32 nucleo
19+
- STMicroelectronics:stm32:GenF4:pnum=GENERIC_F405RGTX # stm32f405 - odrive
20+
- STMicroelectronics:stm32:Disco:pnum=B_G431B_ESC1 # B-G431-ESC1
1921
- arduino:mbed_rp2040:pico # rpi pico
2022

2123
include:
2224
- arduino-boards-fqbn: arduino:avr:uno # arudino uno - compiling almost all examples
2325
sketch-names: '**.ino'
2426
required-libraries: PciManager
25-
sketches-exclude: bluepill_position_control, esp32_position_control, esp32_i2c_dual_bus_example, stm32_i2c_dual_bus_example, magnetic_sensor_spi_alt_example, osc_esp32_3pwm, osc_esp32_fullcontrol, nano33IoT_velocity_control, smartstepper_control,esp32_current_control_low_side, stm32_spi_alt_example, esp32_spi_alt_example, B_G431B_ESC1_position_control
27+
sketches-exclude: bluepill_position_control, esp32_position_control, esp32_i2c_dual_bus_example, stm32_i2c_dual_bus_example, magnetic_sensor_spi_alt_example, osc_esp32_3pwm, osc_esp32_fullcontrol, nano33IoT_velocity_control, smartstepper_control,esp32_current_control_low_side, stm32_spi_alt_example, esp32_spi_alt_example, B_G431B_ESC1, odrive_example
2628

2729
- arduino-boards-fqbn: arduino:sam:arduino_due_x # arduino due - one full example
2830
sketch-names: single_full_control_example.ino
@@ -45,12 +47,20 @@ jobs:
4547
platform-url: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_dev_index.json
4648
sketch-names: esp32_position_control.ino, esp32_i2c_dual_bus_example.ino, esp32_current_control_low_side.ino, esp32_spi_alt_example.ino
4749

48-
- arduino-boards-fqbn: STM32:stm32:GenF1:pnum=BLUEPILL_F103C8 # bluepill - hs examples
49-
platform-url: https://github.com/stm32duino/BoardManagerFiles/raw/master/STM32/package_stm_index.json
50+
- arduino-boards-fqbn: STMicroelectronics:stm32:GenF1:pnum=BLUEPILL_F103C8 # bluepill - hs examples
51+
platform-url: https://github.com/stm32duino/BoardManagerFiles/raw/main/package_stmicroelectronics_index.json
5052
sketch-names: bluepill_position_control.ino, stm32_i2c_dual_bus_example.ino, stm32_spi_alt_example.ino
53+
54+
- arduino-boards-fqbn: STMicroelectronics:stm32:Disco:pnum=B_G431B_ESC1 # B-G431-ESC1
55+
platform-url: https://github.com/stm32duino/BoardManagerFiles/raw/main/package_stmicroelectronics_index.json
56+
sketch-names: B_G431B_ESC1.ino
57+
58+
- arduino-boards-fqbn: STMicroelectronics:stm32:GenF4:pnum=GENERIC_F405RGTX # stm32f405 - odrive
59+
platform-url: https://github.com/stm32duino/BoardManagerFiles/raw/main/package_stmicroelectronics_index.json
60+
sketch-names: odrive_example.ino
5161

52-
- arduino-boards-fqbn: STM32:stm32:Nucleo_64:pnum=NUCLEO_F411RE # nucleo one full example
53-
platform-url: https://github.com/stm32duino/BoardManagerFiles/raw/master/STM32/package_stm_index.json
62+
- arduino-boards-fqbn: STMicroelectronics:stm32:Nucleo_64:pnum=NUCLEO_F411RE # nucleo one full example
63+
platform-url: https://github.com/stm32duino/BoardManagerFiles/raw/main/package_stmicroelectronics_index.json
5464
sketch-names: single_full_control_example.ino, stm32_spi_alt_example.ino
5565

5666

README.md

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,16 @@ Therefore this is an attempt to:
1818
- See also [@byDagor](https://github.com/byDagor)'s *fully-integrated* ESP32 based board: [Dagor Brushless Controller](https://github.com/byDagor/Dagor-Brushless-Controller)
1919

2020

21+
> NEXT RELEASE 📢: <span class="simple">Simple<span class="foc">FOC</span>library</span> v2.2.2
22+
> - GenericCurrentSense bugfix and testing
23+
> - Odrive example code see `examples/hardware_specific/odrive_example`
24+
> - Low level API restructuring
25+
> - Driver API
26+
> - Current sense API
27+
> - Low-side current sensing
28+
> - ESP32 generic support for multiple motors
29+
2130

22-
<blockquote class="info">
23-
<p class="heading">NEW RELEASE 📢: <span class="simple">Simple<span class="foc">FOC</span>library</span> v2.2.1 <a href="https://github.com/simplefoc/Arduino-FOC/releases/tag/v2.2.1">see release</a></p>
24-
<ul>
25-
<li>Sensor class init bugfix <a href="https://github.com/simplefoc/Arduino-FOC/issues/121">#121</a></li>
26-
<li>Voltage/current limit handling bugs <a href="https://github.com/simplefoc/Arduino-FOC/issues/118">#118</a></li>
27-
<li>Added the new motion control interface to the commander <a href="https://docs.simplefoc.com/commander_target">see docs</a>
28-
<ul>
29-
<li>New target setting - possible to set the position, velocity and torque target at once</li>
30-
<li>Separated the motion control interface from full motor callback - only motion control and torque control type, enable disable and target setting</li>
31-
</ul>
32-
</li>
33-
<li>New MCU support <a href="https://docs.simplefoc.com/microcontrollers">see docs</a>
34-
<ul>
35-
<li>NRF52 series mcus support by <a href="https://github.com/Polyphe">@Polyphe</a></li>
36-
<li><b>esp32 arduino package transfer to v2.0.1+</b> - helpful <a href="https://github.com/simplefoc/Arduino-FOC/pull/92/149">PR#149</a> by <a href="https://github.com/samguns">samguns</a></li>
37-
<li>Initial support for esp32s2 and esp32s3 - separation of the esp32 mcpwm and led implementation</li>
38-
</ul>
39-
</li>
40-
<li>Generic sensor class - to implement a new sensor only implement one function <a href="https://docs.simplefoc.com/generic_sensor">see docs</a></li>
41-
</ul>
42-
</blockquote>
4331

4432
## Arduino *SimpleFOClibrary* v2.2
4533

examples/hardware_specific_examples/B_G431B_ESC1/B_G431B_ESC1.ino

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,22 @@
66

77
// Motor instance
88
BLDCMotor motor = BLDCMotor(11);
9-
BLDCDriver6PWM driver = BLDCDriver6PWM(PHASE_UH, PHASE_UL, PHASE_VH, PHASE_VL, PHASE_WH, PHASE_WL);
10-
LowsideCurrentSense currentSense = LowsideCurrentSense(0.003, -64.0/7.0, OP1_OUT, OP2_OUT, OP3_OUT);
9+
BLDCDriver6PWM driver = BLDCDriver6PWM(A_PHASE_UH, A_PHASE_UL, A_PHASE_VH, A_PHASE_VL, A_PHASE_WH, A_PHASE_WL);
10+
LowsideCurrentSense currentSense = LowsideCurrentSense(0.003, -64.0/7.0, A_OP1_OUT, A_OP2_OUT, A_OP3_OUT);
1111

1212

1313
// encoder instance
14-
Encoder encoder = Encoder(HALL2, HALL3, 2048, HALL1);
14+
Encoder encoder = Encoder(A_HALL2, A_HALL3, 2048, A_HALL1);
1515

1616
// Interrupt routine intialisation
1717
// channel A and B callbacks
1818
void doA(){encoder.handleA();}
1919
void doB(){encoder.handleB();}
2020
void doIndex(){encoder.handleIndex();}
2121

22-
// angle set point variable
23-
float target_angle = 0;
2422
// instantiate the commander
2523
Commander command = Commander(Serial);
26-
void doTarget(char* cmd) { command.scalar(&target_angle, cmd); }
24+
void doTarget(char* cmd) { command.motion(&motor, cmd); }
2725

2826
void setup() {
2927

@@ -40,6 +38,8 @@ void setup() {
4038
driver.init();
4139
// link the motor and the driver
4240
motor.linkDriver(&driver);
41+
// link current sense and the driver
42+
currentSense.linkDriver(&driver);
4343

4444
// current sensing
4545
currentSense.init();
@@ -94,14 +94,11 @@ void setup() {
9494
_delay(1000);
9595
}
9696

97-
// angle set point variable
98-
float target_angle = 0;
99-
10097
void loop() {
10198
// main FOC algorithm function
10299

103100
// Motion control function
104-
motor.move(target_angle);
101+
motor.move();
105102

106103
// function intended to be used with serial plotter to monitor motor variables
107104
// significantly slowing the execution down!!!!

examples/hardware_specific_examples/DRV8302_driver/esp32_current_control_low_side/esp32_current_control_low_side.ino

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ void setup() {
7979
driver.init();
8080
// link the motor and the driver
8181
motor.linkDriver(&driver);
82+
// link current sense and the driver
83+
cs.linkDriver(&driver);
84+
85+
// align voltage
8286
motor.voltage_sensor_align = 0.5;
8387

8488
// control loop type and torque mode
@@ -124,7 +128,6 @@ void setup() {
124128
motor.init();
125129

126130
cs.init();
127-
cs.driverSync(&driver);
128131
// driver 8302 has inverted gains on all channels
129132
cs.gain_a *=-1;
130133
cs.gain_b *=-1;

examples/hardware_specific_examples/SimpleFOC-PowerShield/version_v02/single_full_control_example/single_full_control_example.ino

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ void setup() {
3333
driver.init();
3434
// link driver
3535
motor.linkDriver(&driver);
36+
// link current sense and the driver
37+
current_sense.linkDriver(&driver);
3638

3739
motor.voltage_sensor_align = 1;
3840
// set control loop type to be used

examples/hardware_specific_examples/SimpleFOCShield/version_v2/double_full_control_example/double_full_control_example.ino

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,16 @@ void setup() {
5656
driver1.init();
5757
// link driver
5858
motor1.linkDriver(&driver1);
59+
// link current sense and the driver
60+
current_sense1.linkDriver(&driver1);
61+
5962
// power supply voltage [V]
6063
driver2.voltage_power_supply = 12;
6164
driver2.init();
6265
// link driver
6366
motor2.linkDriver(&driver2);
67+
// link current sense and the driver
68+
current_sense2.linkDriver(&driver2);
6469

6570
// set control loop type to be used
6671
motor1.controller = MotionControlType::torque;

examples/hardware_specific_examples/SimpleFOCShield/version_v2/single_full_control_example/single_full_control_example.ino

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ void setup() {
3434
driver.init();
3535
// link driver
3636
motor.linkDriver(&driver);
37+
// link current sense and the driver
38+
current_sense.linkDriver(&driver);
3739

3840
// set control loop type to be used
3941
motor.controller = MotionControlType::torque;
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
Odrive robotics' hardware is one of the best BLDC motor foc supporting hardware out there.
3+
4+
This is an example code that can be directly uploaded to the Odrive usind the SWD programmer.
5+
This code uses an encoder with 500 cpr and a BLDC motor with 7 pole pairs connected to the M0 interface of the Odrive.
6+
7+
This is a short template code and the idea is that you are able to adapt to your needs not to be a complete solution. :D
8+
*/
9+
#include <SimpleFOC.h>
10+
11+
// Odrive M0 motor pinout
12+
#define M0_INH_A PA8
13+
#define M0_INH_B PA9
14+
#define M0_INH_C PA10
15+
#define M0_INL_A PB13
16+
#define M0_INL_B PB14
17+
#define M0_INL_C PB15
18+
// M0 currnets
19+
#define M0_IB PC0
20+
#define M0_IC PC1
21+
// Odrive M0 encoder pinout
22+
#define M0_ENC_A PB4
23+
#define M0_ENC_B PB5
24+
25+
26+
// Odrive M1 motor pinout
27+
#define M1_INH_A PC6
28+
#define M1_INH_B PC7
29+
#define M1_INH_C PC8
30+
#define M1_INL_A PA7
31+
#define M1_INL_B PB0
32+
#define M1_INL_C PB1
33+
// M0 currnets
34+
#define M1_IB PC2
35+
#define M1_IC PC3
36+
// Odrive M1 encoder pinout
37+
#define M1_ENC_A PB6
38+
#define M1_ENC_B PB7
39+
40+
// M1 & M2 common enable pin
41+
#define EN_GATE PB12
42+
43+
44+
// Motor instance
45+
BLDCMotor motor = BLDCMotor(7);
46+
BLDCDriver6PWM driver = BLDCDriver6PWM(M0_INH_A,M0_INL_A, M0_INH_B,M0_INL_B, M0_INH_C,M0_INL_C, EN_GATE);
47+
48+
// instantiate the commander
49+
Commander command = Commander(Serial);
50+
void doMotor(char* cmd) { command.motor(&motor, cmd); }
51+
52+
53+
Encoder encoder = Encoder(M0_ENC_A, M0_ENC_B, 500);
54+
// Interrupt routine intialisation
55+
// channel A and B callbacks
56+
void doA(){encoder.handleA();}
57+
void doB(){encoder.handleB();}
58+
59+
void setup(){
60+
61+
// pwm frequency to be used [Hz]
62+
driver.pwm_frequency = 20000;
63+
// power supply voltage [V]
64+
driver.voltage_power_supply = 20;
65+
// Max DC voltage allowed - default voltage_power_supply
66+
driver.voltage_limit = 20;
67+
// driver init
68+
driver.init();
69+
// link the motor and the driver
70+
motor.linkDriver(&driver);
71+
72+
// initialize encoder sensor hardware
73+
encoder.init();
74+
encoder.enableInterrupts(doA, doB);
75+
// link the motor to the sensor
76+
motor.linkSensor(&encoder);
77+
78+
// control loop type and torque mode
79+
motor.torque_controller = TorqueControlType::voltage;
80+
motor.controller = MotionControlType::torque;
81+
82+
// max voltage allowed for motion control
83+
motor.voltage_limit = 8.0;
84+
// alignment voltage limit
85+
motor.voltage_sensor_align = 0.5;
86+
87+
88+
Serial.begin(115200);
89+
// comment out if not needed
90+
motor.useMonitoring(Serial);
91+
motor.monitor_downsample = 0; // disable monitoring at start
92+
93+
// add target command T
94+
command.add('M', doMotor, "motor M0");
95+
96+
// initialise motor
97+
motor.init();
98+
// init FOC
99+
motor.initFOC();
100+
delay(1000);
101+
}
102+
void loop(){
103+
// foc loop
104+
motor.loopFOC();
105+
// motion control
106+
motor.move();
107+
// monitoring
108+
motor.monitor();
109+
// user communication
110+
command.run();
111+
}

examples/motion_control/torque_control/encoder/current_control/current_control.ino

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ void setup() {
3939
driver.init();
4040
// link driver
4141
motor.linkDriver(&driver);
42+
// link current sense and the driver
43+
current_sense.linkDriver(&driver);
4244

4345
// current sense init hardware
4446
current_sense.init();

keywords.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ getFOCCurrents KEYWORD2
9191
getDCCurrent KEYWORD2
9292
setPwm KEYWORD2
9393
driverAlign KEYWORD2
94-
driverSync KEYWORD2
94+
linkDriver KEYWORD2
9595
add KEYWORD2
9696
run KEYWORD2
9797
attach KEYWORD2

0 commit comments

Comments
 (0)