Skip to content

Commit 75f2974

Browse files
committed
Merge branch 'dev' into FEAT_refactor_hardware_specific
2 parents fca0d0c + e33782a commit 75f2974

File tree

7 files changed

+219
-14
lines changed

7 files changed

+219
-14
lines changed

.github/workflows/ccpp.yml

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,62 @@ jobs:
44
build:
55
name: Test compiling
66
runs-on: ubuntu-latest
7+
8+
strategy:
9+
matrix:
10+
arduino-boards-fqbn:
11+
- arduino:avr:uno # arudino uno
12+
- arduino:sam:arduino_due_x # arduino due
13+
- arduino:samd:nano_33_iot # samd21
14+
- adafruit:samd:adafruit_metro_m4 # samd51
15+
- esp32:esp32:esp32doit-devkit-v1 # esm32
16+
- STM32:stm32:GenF1:pnum=BLUEPILL_F103C8 # stm32 bluepill
17+
- STM32:stm32:Nucleo_64:pnum=NUCLEO_F411RE # stm32 nucleo
18+
- arduino:mbed_rp2040:pico # rpi pico
19+
20+
include:
21+
- arduino-boards-fqbn: arduino:avr:uno # arudino uno - compiling almost all examples
22+
sketch-names: '**.ino'
23+
required-libraries: PciManager
24+
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
25+
26+
- arduino-boards-fqbn: arduino:sam:arduino_due_x # arduino due - one full example
27+
sketch-names: single_full_control_example.ino
28+
29+
- arduino-boards-fqbn: arduino:samd:nano_33_iot # samd21
30+
sketch-names: nano33IoT_velocity_control.ino, smartstepper_control.ino
31+
32+
- arduino-boards-fqbn: arduino:mbed_rp2040:pico # raspberry pi pico - one example
33+
sketch-names: open_loop_position_example.ino
34+
35+
- arduino-boards-fqbn: adafruit:samd:adafruit_metro_m4 # samd51 - one full example
36+
platform-url: https://adafruit.github.io/arduino-board-index/package_adafruit_index.json
37+
sketch-names: single_full_control_example.ino
38+
39+
- arduino-boards-fqbn: esp32:esp32:esp32doit-devkit-v1 # esp32
40+
platform-url: https://dl.espressif.com/dl/package_esp32_index.json
41+
sketch-names: esp32_position_control.ino, esp32_i2c_dual_bus_example.ino
42+
43+
- arduino-boards-fqbn: STM32:stm32:GenF1:pnum=BLUEPILL_F103C8 # bluepill - hs examples
44+
platform-url: https://github.com/stm32duino/BoardManagerFiles/raw/master/STM32/package_stm_index.json
45+
sketch-names: bluepill_position_control.ino, stm32_i2c_dual_bus_example.ino, magnetic_sensor_spi_alt_example.ino
46+
47+
- arduino-boards-fqbn: STM32:stm32:Nucleo_64:pnum=NUCLEO_F411RE # one full example
48+
platform-url: https://github.com/stm32duino/BoardManagerFiles/raw/master/STM32/package_stm_index.json
49+
sketch-names: single_full_control_example.ino
50+
51+
52+
53+
# Do not cancel all jobs / architectures if one job fails
54+
fail-fast: false
755
steps:
856
- name: Checkout
957
uses: actions/checkout@master
1058
- name: Compile all examples
11-
uses: ArminJo/arduino-test-compile@v1.0.0
59+
uses: ArminJo/arduino-test-compile@master
1260
with:
13-
libraries: PciManager
14-
examples-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
61+
arduino-board-fqbn: ${{ matrix.arduino-boards-fqbn }}
62+
required-libraries: ${{ matrix.required-libraries }}
63+
platform-url: ${{ matrix.platform-url }}
64+
sketch-names: ${{ matrix.sketch-names }}
65+
sketches-exclude: ${{ matrix.sketches-exclude }}

examples/hardware_specific_examples/Bluepill_examples/encoder/bluepill_position_control/bluepill_position_control.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ void setup() {
8989
command.add('T', doTarget, "target angle");
9090

9191
Serial.println(F("Motor ready."));
92-
Serial.println(F(("Set the target angle using serial terminal:"));
92+
Serial.println(F("Set the target angle using serial terminal:"));
9393
_delay(1000);
9494
}
9595

examples/hardware_specific_examples/Bluepill_examples/magnetic_sensor/bluepill_position_control/bluepill_position_control.ino

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,6 @@ void setup() {
9191
_delay(1000);
9292
}
9393

94-
// angle set point variable
95-
float target_angle = 0;
96-
9794
void loop() {
9895

9996
// main FOC algorithm function
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
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 INH_A 21
19+
#define INH_B 19
20+
#define INH_C 18
21+
22+
#define EN_GATE 5
23+
#define M_PWM 25
24+
#define M_OC 26
25+
#define OC_ADJ 12
26+
#define OC_GAIN 14
27+
28+
#define IOUTA 34
29+
#define IOUTB 35
30+
#define IOUTC 32
31+
32+
// Motor instance
33+
BLDCMotor motor = BLDCMotor(7);
34+
BLDCDriver3PWM driver = BLDCDriver3PWM(INH_A, INH_B, INH_C, EN_GATE);
35+
36+
// DRV8302 board has 0.005Ohm shunt resistors and the gain of 12.22 V/V
37+
LowsideCurrentSense cs = LowsideCurrentSense(0.005f, 12.22f, IOUTA, IOUTB, IOUTC);
38+
39+
// encoder instance
40+
Encoder encoder = Encoder(22, 23, 500);
41+
42+
// Interrupt routine intialisation
43+
// channel A and B callbacks
44+
void doA(){encoder.handleA();}
45+
void doB(){encoder.handleB();}
46+
47+
48+
// commander interface
49+
Commander command = Commander(Serial);
50+
void onMotor(char* cmd){ command.motor(&motor, cmd); }
51+
52+
void setup() {
53+
54+
// initialize encoder sensor hardware
55+
encoder.init();
56+
encoder.enableInterrupts(doA, doB);
57+
// link the motor to the sensor
58+
motor.linkSensor(&encoder);
59+
60+
// DRV8302 specific code
61+
// M_OC - enable overcurrent protection
62+
pinMode(M_OC,OUTPUT);
63+
digitalWrite(M_OC,LOW);
64+
// M_PWM - enable 3pwm mode
65+
pinMode(M_PWM,OUTPUT);
66+
digitalWrite(M_PWM,HIGH);
67+
// OD_ADJ - set the maximum overcurrent limit possible
68+
// Better option would be to use voltage divisor to set exact value
69+
pinMode(OC_ADJ,OUTPUT);
70+
digitalWrite(OC_ADJ,HIGH);
71+
pinMode(OC_GAIN,OUTPUT);
72+
digitalWrite(OC_GAIN,LOW);
73+
74+
75+
// driver config
76+
// power supply voltage [V]
77+
driver.voltage_power_supply = 12;
78+
driver.pwm_frequency = 15000;
79+
driver.init();
80+
// link the motor and the driver
81+
motor.linkDriver(&driver);
82+
motor.voltage_sensor_align = 0.5;
83+
84+
// control loop type and torque mode
85+
motor.torque_controller = TorqueControlType::voltage;
86+
motor.controller = MotionControlType::torque;
87+
motor.motion_downsample = 0.0;
88+
89+
// velocity loop PID
90+
motor.PID_velocity.P = 0.2;
91+
motor.PID_velocity.I = 5.0;
92+
// Low pass filtering time constant
93+
motor.LPF_velocity.Tf = 0.02;
94+
// angle loop PID
95+
motor.P_angle.P = 20.0;
96+
// Low pass filtering time constant
97+
motor.LPF_angle.Tf = 0.0;
98+
// current q loop PID
99+
motor.PID_current_q.P = 3.0;
100+
motor.PID_current_q.I = 100.0;
101+
// Low pass filtering time constant
102+
motor.LPF_current_q.Tf = 0.02;
103+
// current d loop PID
104+
motor.PID_current_d.P = 3.0;
105+
motor.PID_current_d.I = 100.0;
106+
// Low pass filtering time constant
107+
motor.LPF_current_d.Tf = 0.02;
108+
109+
// Limits
110+
motor.velocity_limit = 100.0; // 100 rad/s velocity limit
111+
motor.voltage_limit = 12.0; // 12 Volt limit
112+
motor.current_limit = 2.0; // 2 Amp current limit
113+
114+
115+
// use monitoring with serial for motor init
116+
// monitoring port
117+
Serial.begin(115200);
118+
// comment out if not needed
119+
motor.useMonitoring(Serial);
120+
motor.monitor_variables = _MON_CURR_Q | _MON_CURR_D; // monitor the two currents d and q
121+
motor.monitor_downsample = 1000;
122+
123+
// initialise motor
124+
motor.init();
125+
126+
cs.init();
127+
cs.driverSync(&driver);
128+
// driver 8302 has inverted gains on all channels
129+
cs.gain_a *=-1;
130+
cs.gain_b *=-1;
131+
cs.gain_c *=-1;
132+
motor.linkCurrentSense(&cs);
133+
134+
// align encoder and start FOC
135+
motor.initFOC();
136+
137+
// set the inital target value
138+
motor.target = 0;
139+
140+
// define the motor id
141+
command.add('M', onMotor, "motor");
142+
143+
Serial.println(F("Full control example: "));
144+
Serial.println(F("Run user commands to configure and the motor (find the full command list in docs.simplefoc.com) \n "));
145+
Serial.println(F("Initial motion control loop is voltage loop."));
146+
Serial.println(F("Initial target voltage 2V."));
147+
148+
_delay(1000);
149+
}
150+
151+
152+
void loop() {
153+
// iterative setting FOC phase voltage
154+
motor.loopFOC();
155+
156+
// iterative function setting the outter loop target
157+
motor.move();
158+
159+
// monitoring the state variables
160+
motor.monitor();
161+
162+
// user communication
163+
command.run();
164+
}

examples/hardware_specific_examples/ESP32/encoder/esp32_position_control/esp32_position_control.ino

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,6 @@ void setup() {
8585
_delay(1000);
8686
}
8787

88-
// angle set point variable
89-
float target_angle = 0;
90-
9188
void loop() {
9289
// main FOC algorithm function
9390
// the faster you run this function the better

examples/hardware_specific_examples/ESP32/magnetic_sensor/esp32_position_control/esp32_position_control.ino

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,6 @@ void setup() {
8888
_delay(1000);
8989
}
9090

91-
// angle set point variable
92-
float target_angle = 0;
93-
9491
void loop() {
9592

9693
// main FOC algorithm function

examples/hardware_specific_examples/SAMD_examples/nano33IoT/nano33IoT_velocity_control/nano33IoT_velocity_control.ino

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include "Arduino.h"
77
#include <Wire.h>
88
#include <SimpleFOC.h>
9-
#include <Math.h>
109

1110
// this is for an AS5048B absolute magnetic encoder on I2C address 0x41
1211
MagneticSensorI2C sensor = MagneticSensorI2C(0x41, 14, 0xFE, 8);

0 commit comments

Comments
 (0)