Skip to content

Commit f434a1e

Browse files
authored
Merge pull request #395 from simplefoc/dev
final v2.3.3
2 parents 4d33cd7 + e4b9f71 commit f434a1e

File tree

9 files changed

+780
-7
lines changed

9 files changed

+780
-7
lines changed

.github/workflows/ccpp.yml

+6-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ jobs:
2626
- esp32:esp32:esp32s2 # esp32s2
2727
- esp32:esp32:esp32s3 # esp32s3
2828
- STMicroelectronics:stm32:GenF1:pnum=BLUEPILL_F103C8 # stm32 bluepill
29-
- STMicroelectronics:stm32:Nucleo_64:pnum=NUCLEO_F411RE # stm32 nucleo
29+
- STMicroelectronics:stm32:Nucleo_64:pnum=NUCLEO_F411RE # stm32 nucleo
30+
- STMicroelectronics:stm32:Nucleo_144:pnum=NUCLEO_F746ZG # stm32 nucleo f746zg
3031
- STMicroelectronics:stm32:GenF4:pnum=GENERIC_F405RGTX # stm32f405 - odrive
3132
- STMicroelectronics:stm32:GenL4:pnum=GENERIC_L475RGTX # stm32l475
3233
- STMicroelectronics:stm32:Disco:pnum=B_G431B_ESC1 # B-G431-ESC1
@@ -92,6 +93,10 @@ jobs:
9293
platform-url: https://github.com/stm32duino/BoardManagerFiles/raw/main/package_stmicroelectronics_index.json
9394
sketch-names: single_full_control_example.ino, stm32_spi_alt_example.ino, double_full_control_example.ino, stm32_current_control_low_side.ino
9495

96+
- arduino-boards-fqbn: STMicroelectronics:stm32:Nucleo_144:pnum=NUCLEO_F746ZG # nucleo f7 one full example
97+
platform-url: https://github.com/stm32duino/BoardManagerFiles/raw/main/package_stmicroelectronics_index.json
98+
sketch-names: single_full_control_example.ino, stm32_spi_alt_example.ino, double_full_control_example.ino, stm32_current_control_low_side.ino
99+
95100

96101
# Do not cancel all jobs / architectures if one job fails
97102
fail-fast: false

.github/workflows/teensy.yml

+5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ jobs:
2929
run: pio ci --lib="." --board=teensy41 --board=teensy40
3030
env:
3131
PLATFORMIO_CI_SRC: examples/hardware_specific_examples/Teensy/Teensy4/open_loop_velocity_6pwm/open_loop_velocity_6pwm.ino
32+
33+
- name: PIO Run Teensy 4
34+
run: pio ci --lib="." --board=teensy41 --board=teensy40
35+
env:
36+
PLATFORMIO_CI_SRC: examples/hardware_specific_examples/DRV8302_driver/teensy4_current_control_low_side/teensy4_current_control_low_side.ino
3237

3338
- name: PIO Run Teensy 3
3439
run: pio ci --lib="." --board=teensy31 --board=teensy30 --board=teensy35 --board=teensy36

README.md

+5-6
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,18 @@ Additionally, most of the efforts at this moment are still channeled towards the
1919
Therefore this is an attempt to:
2020
- 🎯 Demystify FOC algorithm and make a robust but simple Arduino library: [Arduino *SimpleFOClibrary*](https://docs.simplefoc.com/arduino_simplefoc_library_showcase)
2121
- <i>Support as many <b>motor + sensor + driver + mcu</b> combinations out there</i>
22-
- 🎯 Develop a modular FOC supporting BLDC driver boards:
23-
- ***NEW*** 📢: *Minimalistic* BLDC driver (<3Amps) : [<span class="simple">Simple<b>FOC</b>Mini</span> ](https://github.com/simplefoc/SimpleFOCMini).
24-
- *Low-power* gimbal driver (<5Amps) : [*Arduino Simple**FOC**Shield*](https://docs.simplefoc.com/arduino_simplefoc_shield_showcase).
25-
- *Medium-power* BLDC driver (<30Amps): [Arduino <span class="simple">Simple<b>FOC</b>PowerShield</span> ](https://github.com/simplefoc/Arduino-SimpleFOC-PowerShield).
26-
- See also [@byDagor](https://github.com/byDagor)'s *fully-integrated* ESP32 based board: [Dagor Brushless Controller](https://github.com/byDagor/Dagor-Brushless-Controller)
22+
- 🎯 Develop modular and easy to use FOC supporting BLDC driver boards
23+
- For official driver boards see [<span class="simple">Simple<span class="foc">FOC</span>Boards</span>](https://docs.simplefoc.com/boards)
24+
- Many many more boards developed by the community members, see [<span class="simple">Simple<span class="foc">FOC</span>Community</span>](https://community.simplefoc.com/)
2725

2826
> NEW RELEASE 📢 : <span class="simple">Simple<span class="foc">FOC</span>library</span> v2.3.3
2927
> - Teensy4
3028
> - support for low-side current sensing [#392](https://github.com/simplefoc/Arduino-FOC/pull/392)
3129
> - support for center aligned 6pwm and 3pwm (optional) [#392](https://github.com/simplefoc/Arduino-FOC/pull/392)
3230
> - stm32
3331
> - support for center aligned pwm (even across multiple timers and motors/drivers) [#374](https://github.com/simplefoc/Arduino-FOC/pull/374), [#388](https://github.com/simplefoc/Arduino-FOC/pull/388)
34-
> - support for DMA based low-side current sensing: [#383](https://github.com/simplefoc/Arduino-FOC/pull/383),[#378](https://github.com/simplefoc/Arduino-FOC/pull/378),
32+
> - support for DMA based low-side current sensing: [#383](https://github.com/simplefoc/Arduino-FOC/pull/383),[#378](https://github.com/simplefoc/Arduino-FOC/pull/378)
33+
> - support for f7 architecture [#388](https://github.com/simplefoc/Arduino-FOC/pull/388),[#394](https://github.com/simplefoc/Arduino-FOC/pull/394)
3534
> - KV rating calculation fix [#347](https://github.com/simplefoc/Arduino-FOC/pull/347)
3635
> - Much more performant Space Vector PWM calculation [#340](https://github.com/simplefoc/Arduino-FOC/pull/340)
3736
> - And much more:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
/**
2+
* Comprehensive BLDC motor control example using encoder and the DRV8302 board
3+
*
4+
* Using serial terminal user can send motor commands and configure the motor and FOC in real-time:
5+
* - configure PID controller constants
6+
* - change motion control loops
7+
* - monitor motor variabels
8+
* - set target values
9+
* - check all the configuration values
10+
*
11+
* check the https://docs.simplefoc.com for full list of motor commands
12+
*
13+
*/
14+
#include <SimpleFOC.h>
15+
16+
// DRV8302 pins connections
17+
// don't forget to connect the common ground pin
18+
#define EN_GATE 11
19+
#define M_PWM 22
20+
#define GAIN 20
21+
#define M_OC 23
22+
#define OC_ADJ 19
23+
24+
#define INH_A 2
25+
#define INL_A 3
26+
#define INH_B 8
27+
#define INL_B 7
28+
#define INH_C 6
29+
#define INL_C 9
30+
31+
#define IOUTA 14
32+
#define IOUTB 15
33+
#define IOUTC 16
34+
35+
// Motor instance
36+
BLDCMotor motor = BLDCMotor(7);
37+
BLDCDriver3PWM driver = BLDCDriver3PWM(INH_A, INH_B, INH_C, EN_GATE);
38+
39+
// DRV8302 board has 0.005Ohm shunt resistors and the gain of 12.22 V/V
40+
LowsideCurrentSense cs = LowsideCurrentSense(0.005f, 12.22f, IOUTA, IOUTB);
41+
42+
// encoder instance
43+
Encoder encoder = Encoder(10, 11, 2048);
44+
45+
// Interrupt routine intialisation
46+
// channel A and B callbacks
47+
void doA(){encoder.handleA();}
48+
void doB(){encoder.handleB();}
49+
50+
51+
// commander interface
52+
Commander command = Commander(Serial);
53+
void onMotor(char* cmd){ command.motor(&motor, cmd); }
54+
55+
void setup() {
56+
57+
// initialize encoder sensor hardware
58+
encoder.init();
59+
encoder.enableInterrupts(doA, doB);
60+
// link the motor to the sensor
61+
motor.linkSensor(&encoder);
62+
63+
// DRV8302 specific code
64+
// M_OC - enable overcurrent protection
65+
pinMode(M_OC,OUTPUT);
66+
digitalWrite(M_OC,LOW);
67+
// M_PWM - enable 6pwm mode
68+
pinMode(M_PWM, OUTPUT);
69+
digitalWrite(M_PWM,LOW); // high for 3pwm
70+
// OD_ADJ - set the maximum overcurrent limit possible
71+
// Better option would be to use voltage divisor to set exact value
72+
pinMode(OC_ADJ,OUTPUT);
73+
digitalWrite(OC_ADJ,HIGH);
74+
75+
76+
// driver config
77+
// power supply voltage [V]
78+
driver.voltage_power_supply = 19;
79+
driver.pwm_frequency = 20000; // suggested not higher than 22khz
80+
driver.init();
81+
// link the motor and the driver
82+
motor.linkDriver(&driver);
83+
// link current sense and the driver
84+
cs.linkDriver(&driver);
85+
86+
// align voltage
87+
motor.voltage_sensor_align = 0.5;
88+
89+
// control loop type and torque mode
90+
motor.torque_controller = TorqueControlType::voltage;
91+
motor.controller = MotionControlType::torque;
92+
motor.motion_downsample = 0.0;
93+
94+
// velocity loop PID
95+
motor.PID_velocity.P = 0.2;
96+
motor.PID_velocity.I = 5.0;
97+
// Low pass filtering time constant
98+
motor.LPF_velocity.Tf = 0.02;
99+
// angle loop PID
100+
motor.P_angle.P = 20.0;
101+
// Low pass filtering time constant
102+
motor.LPF_angle.Tf = 0.0;
103+
// current q loop PID
104+
motor.PID_current_q.P = 3.0;
105+
motor.PID_current_q.I = 100.0;
106+
// Low pass filtering time constant
107+
motor.LPF_current_q.Tf = 0.02;
108+
// current d loop PID
109+
motor.PID_current_d.P = 3.0;
110+
motor.PID_current_d.I = 100.0;
111+
// Low pass filtering time constant
112+
motor.LPF_current_d.Tf = 0.02;
113+
114+
// Limits
115+
motor.velocity_limit = 100.0; // 100 rad/s velocity limit
116+
motor.voltage_limit = 12.0; // 12 Volt limit
117+
motor.current_limit = 2.0; // 2 Amp current limit
118+
119+
120+
// use monitoring with serial for motor init
121+
// monitoring port
122+
Serial.begin(115200);
123+
// comment out if not needed
124+
motor.useMonitoring(Serial);
125+
motor.monitor_variables = _MON_CURR_Q | _MON_CURR_D; // monitor the two currents d and q
126+
motor.monitor_downsample = 0;
127+
128+
// initialise motor
129+
motor.init();
130+
131+
cs.init();
132+
// driver 8302 has inverted gains on all channels
133+
cs.gain_a *=-1;
134+
cs.gain_b *=-1;
135+
cs.gain_c *=-1;
136+
motor.linkCurrentSense(&cs);
137+
138+
// align encoder and start FOC
139+
motor.initFOC();
140+
141+
// set the inital target value
142+
motor.target = 0;
143+
144+
// define the motor id
145+
command.add('M', onMotor, "motor");
146+
147+
Serial.println(F("Full control example: "));
148+
Serial.println(F("Run user commands to configure and the motor (find the full command list in docs.simplefoc.com) \n "));
149+
Serial.println(F("Initial motion control loop is voltage loop."));
150+
Serial.println(F("Initial target voltage 2V."));
151+
152+
_delay(1000);
153+
}
154+
155+
156+
void loop() {
157+
// iterative setting FOC phase voltage
158+
motor.loopFOC();
159+
160+
// iterative function setting the outter loop target
161+
motor.move();
162+
163+
// monitoring the state variables
164+
motor.monitor();
165+
166+
// user communication
167+
command.run();
168+
}

0 commit comments

Comments
 (0)