Skip to content

Commit 424aaec

Browse files
committed
FEAT library version 1.4.1 - support for ESP32 few bugfixes
1 parent 62d22d4 commit 424aaec

File tree

7 files changed

+265
-5
lines changed

7 files changed

+265
-5
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ Therefore this is an attempt to:
1010
- Demystify FOC algorithm and make a robust but simple Arduino library: [Arduino *SimpleFOClibrary*](https://docs.simplefoc.com/arduino_simplefoc_library_showcase)
1111
- Develop a modular BLDC driver board: [Arduino *SimpleFOCShield*](https://docs.simplefoc.com/arduino_simplefoc_shield_showcase).
1212

13+
> ***NEWS:*** 📢 Arduino *SimpleFOClibrary* now supports ESP32 boards! Check out the library [docs](https://docs.simplefoc.com/microcontrollers) to see how to use them with your project. You can also find full configuration in the library examples.
14+
1315
> ***NEWS:*** 📢 Arduino *SimpleFOClibrary* now supports magnetic sensors using I2C communication! Check out the library [docs](https://docs.simplefoc.com/sensors) to see how to use them with your project. You can also find full configuration in the library examples. <br>
1416
> ***BEWARE:*** 📢 The `MagneticSensor` class has been renamed and divided to `MagneticSensorSPI` and `MagneticSensorI2C` classes.
1517
16-
> ***NEWS:*** 📢 Arduino *SimpleFOClibrary* now supports high performance DRV8302 driver boards (60V, 15A, 30$). [YouTube link](https://www.youtube.com/watch?v=RI4nNMF608I).
17-
> You can find the board by clicking on [Aliexpress link](https://bit.ly/2BZZ5fG) and the Arduino code example in the examples folder!
1818

1919

2020
## Arduino *SimpleFOCShield*
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/**
2+
*
3+
* STM32 Bluepill position motion control example with encoder
4+
*
5+
* The same example can be ran with any STM32 board - just make sure that put right pin numbers.
6+
*
7+
*/
8+
#include <SimpleFOC.h>
9+
10+
// motor instance
11+
BLDCMotor motor = BLDCMotor(25, 26, 27, 7);
12+
13+
// encoder instance
14+
Encoder encoder = Encoder(4, 2, 1024);
15+
16+
// Interrupt routine intialisation
17+
// channel A and B callbacks
18+
void doA(){encoder.handleA();}
19+
void doB(){encoder.handleB();}
20+
21+
void setup() {
22+
23+
// initialize encoder sensor hardware
24+
encoder.init();
25+
encoder.enableInterrupts(doA, doB);
26+
27+
// link the motor to the sensor
28+
motor.linkSensor(&encoder);
29+
30+
// power supply voltage [V]
31+
motor.voltage_power_supply = 12;
32+
// aligning voltage [V]
33+
motor.voltage_sensor_align = 3;
34+
// index search velocity [rad/s]
35+
motor.velocity_index_search = 3;
36+
37+
// set motion control loop to be used
38+
motor.controller = ControlType::velocity;
39+
40+
// contoller configuration
41+
// default parameters in defaults.h
42+
43+
// velocity PI controller parameters
44+
motor.PI_velocity.P = 0.2;
45+
motor.PI_velocity.I = 20;
46+
// default voltage_power_supply
47+
motor.PI_velocity.voltage_limit = 6;
48+
// jerk control using voltage voltage ramp
49+
// default value is 300 volts per sec ~ 0.3V per millisecond
50+
motor.PI_velocity.voltage_ramp = 1000;
51+
52+
// velocity low pass filtering time constant
53+
motor.LPF_velocity.Tf = 0.01;
54+
55+
// angle P controller
56+
motor.P_angle.P = 20;
57+
// maximal velocity of the position control
58+
motor.P_angle.velocity_limit = 4;
59+
60+
61+
// use monitoring with serial
62+
Serial.begin(115200);
63+
// comment out if not needed
64+
motor.useMonitoring(Serial);
65+
66+
// initialize motor
67+
motor.init();
68+
// align encoder and start FOC
69+
motor.initFOC();
70+
71+
72+
Serial.println("Motor ready.");
73+
Serial.println("Set the target angle using serial terminal:");
74+
_delay(1000);
75+
}
76+
77+
// angle set point variable
78+
float target_angle = 0;
79+
80+
void loop() {
81+
// main FOC algorithm function
82+
// the faster you run this function the better
83+
// Arduino UNO loop ~1kHz
84+
// Bluepill loop ~10kHz
85+
motor.loopFOC();
86+
87+
// Motion control function
88+
// velocity, position or voltage (defined in motor.controller)
89+
// this function can be run at much lower frequency than loopFOC() function
90+
// You can also use motor.move() and set the motor.target in the code
91+
motor.move(target_angle);
92+
93+
// function intended to be used with serial plotter to monitor motor variables
94+
// significantly slowing the execution down!!!!
95+
// motor.monitor();
96+
97+
// user communication
98+
serialReceiveUserCommand();
99+
}
100+
101+
// utility function enabling serial communication with the user to set the target values
102+
// this function can be implemented in serialEvent function as well
103+
void serialReceiveUserCommand() {
104+
105+
// a string to hold incoming data
106+
static String received_chars;
107+
108+
while (Serial.available()) {
109+
// get the new byte:
110+
char inChar = (char)Serial.read();
111+
// add it to the string buffer:
112+
received_chars += inChar;
113+
// end of user input
114+
if (inChar == '\n') {
115+
116+
// change the motor target
117+
target_angle = received_chars.toFloat();
118+
Serial.print("Target angle: ");
119+
Serial.println(target_angle);
120+
121+
// reset the command buffer
122+
received_chars = "";
123+
}
124+
}
125+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/**
2+
*
3+
* STM32 Bluepill position motion control example with magnetic sensor
4+
*
5+
* The same example can be ran with any STM32 board - just make sure that put right pin numbers.
6+
*
7+
*/
8+
#include <SimpleFOC.h>
9+
10+
// SPI Magnetic sensor instance (AS5047U example)
11+
// MISO 12
12+
// MOSI 9
13+
// SCK 14
14+
MagneticSensorSPI sensor = MagneticSensorSPI(10, 14, 0x3FFF);
15+
16+
// I2C Magnetic sensor instance (AS5600 example)
17+
// make sure to use the pull-ups!!
18+
// SDA 21
19+
// SCL 22
20+
//MagneticSensorI2C sensor = MagneticSensorI2C(0x36, 12, 0x0E, 4);
21+
22+
// Motor instance
23+
BLDCMotor motor = BLDCMotor(25, 26, 27, 7);
24+
25+
void setup() {
26+
27+
// initialise magnetic sensor hardware
28+
sensor.init();
29+
// link the motor to the sensor
30+
motor.linkSensor(&sensor);
31+
32+
// power supply voltage
33+
// default 12V
34+
motor.voltage_power_supply = 12;
35+
36+
// choose FOC modulation (optional)
37+
motor.foc_modulation = FOCModulationType::SpaceVectorPWM;
38+
39+
// set motion control loop to be used
40+
motor.controller = ControlType::angle;
41+
42+
// contoller configuration
43+
// default parameters in defaults.h
44+
45+
// velocity PI controller parameters
46+
motor.PI_velocity.P = 0.2;
47+
motor.PI_velocity.I = 20;
48+
// maximal voltage to be set to the motor
49+
motor.PI_velocity.voltage_limit = 6;
50+
51+
// velocity low pass filtering time constant
52+
// the lower the less filtered
53+
motor.LPF_velocity.Tf = 0.01;
54+
55+
// angle P controller
56+
motor.P_angle.P = 20;
57+
// maximal velocity of the position control
58+
motor.P_angle.velocity_limit = 40;
59+
60+
// use monitoring with serial
61+
Serial.begin(115200);
62+
// comment out if not needed
63+
motor.useMonitoring(Serial);
64+
65+
66+
// initialize motor
67+
motor.init();
68+
// align sensor and start FOC
69+
motor.initFOC();
70+
71+
72+
Serial.println("Motor ready.");
73+
Serial.println("Set the target angle using serial terminal:");
74+
_delay(1000);
75+
}
76+
77+
// angle set point variable
78+
float target_angle = 0;
79+
80+
void loop() {
81+
82+
// main FOC algorithm function
83+
// the faster you run this function the better
84+
// Arduino UNO loop ~1kHz
85+
// Bluepill loop ~10kHz
86+
motor.loopFOC();
87+
88+
// Motion control function
89+
// velocity, position or voltage (defined in motor.controller)
90+
// this function can be run at much lower frequency than loopFOC() function
91+
// You can also use motor.move() and set the motor.target in the code
92+
motor.move(target_angle);
93+
94+
95+
// function intended to be used with serial plotter to monitor motor variables
96+
// significantly slowing the execution down!!!!
97+
// motor.monitor();
98+
99+
// user communication
100+
serialReceiveUserCommand();
101+
}
102+
103+
// utility function enabling serial communication with the user to set the target values
104+
// this function can be implemented in serialEvent function as well
105+
void serialReceiveUserCommand() {
106+
107+
// a string to hold incoming data
108+
static String received_chars;
109+
110+
while (Serial.available()) {
111+
// get the new byte:
112+
char inChar = (char)Serial.read();
113+
// add it to the string buffer:
114+
received_chars += inChar;
115+
// end of user input
116+
if (inChar == '\n') {
117+
118+
// change the motor target
119+
target_angle = received_chars.toFloat();
120+
Serial.print("Target angle: ");
121+
Serial.println(target_angle);
122+
123+
// reset the command buffer
124+
received_chars = "";
125+
}
126+
}
127+
}
128+
129+

examples/motion_control/torque_voltage_control/encoder/voltage_control/voltage_control.ino

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ void setup() {
3131
// power supply voltage
3232
// default 12V
3333
motor.voltage_power_supply = 12;
34+
// aligning voltage
35+
motor.voltage_sensor_align = 5;
3436

3537
// choose FOC modulation (optional)
3638
motor.foc_modulation = FOCModulationType::SpaceVectorPWM;

examples/motion_control/torque_voltage_control/magnetic_sensor/voltage_control/voltage_control.ino

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ void setup() {
2626
// power supply voltage
2727
// default 12V
2828
motor.voltage_power_supply = 12;
29+
// aligning voltage
30+
motor.voltage_sensor_align = 5;
2931

3032
// choose FOC modulation (optional)
3133
motor.foc_modulation = FOCModulationType::SpaceVectorPWM;

keywords.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ _micros KEYWORD3
1414
_sin KEYWORD3
1515
_cos KEYWORD3
1616
_setPwmFrequency KEYWORD3
17+
_writeDutyCycle KEYWORD3
18+
_round KEYWORD3
1719
monitor KEYWORD3
1820
command KEYWORD3
1921

@@ -37,6 +39,8 @@ setPhaseVoltage KEYWORD2
3739
getAngle KEYWORD2
3840
initRelativeZero KEYWORD2
3941
initAbsoluteZero KEYWORD2
42+
hasAbsoluteZero KEYWORD2
43+
needsAbsoluteZeroSearch KEYWORD2
4044
useMonitoring KEYWORD2
4145

4246
voltage_q KEYWORD2
@@ -52,8 +56,6 @@ foc_modulation KEYWORD2
5256
target KEYWORD2
5357

5458

55-
56-
5759
voltage KEYWORD2
5860
velocity KEYWORD2
5961
velocity_ultra_slow 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=1.4.0
2+
version=1.4.1
33
author=Antun Skuric <[email protected]>
44
maintainer=Antun Skuric <[email protected]>
55
sentence=A library demistifying FOC for BLDC motors

0 commit comments

Comments
 (0)