Skip to content

Commit aded286

Browse files
committed
FEAT added support for 6pwm for arduino and stm32 + refactured examples
1 parent 3da53b9 commit aded286

File tree

67 files changed

+1420
-566
lines changed

Some content is hidden

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

67 files changed

+1420
-566
lines changed

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77
*/
88
#include <SimpleFOC.h>
99

10-
// motor instance
11-
BLDCMotor motor = BLDCMotor(PB6, PB7, PB8, 11, PB5);
10+
// Motor instance
11+
BLDCMotor motor = BLDCMotor(11);
12+
// BLDCDriver3PWM(IN1, IN2, IN3, enable(optional))
13+
BLDCDriver3PWM driver = BLDCDriver3PWM(PB6, PB7, PB8, PB5);
14+
// BLDCDriver6PWM(IN1_H, IN1_L, IN2_H, IN2_L, IN3_H, IN3_L, enable(optional))
15+
//BLDCDriver6PWM driver = BLDCDriver6PWM(PA8, PB13, PA9, PB14, PA10, PB15, PB12);
1216

1317
// encoder instance
1418
Encoder encoder = Encoder(PA8, PA9, 8192, PA10);
@@ -24,12 +28,16 @@ void setup() {
2428
// initialize encoder sensor hardware
2529
encoder.init();
2630
encoder.enableInterrupts(doA, doB, doI);
27-
2831
// link the motor to the sensor
2932
motor.linkSensor(&encoder);
3033

34+
// driver config
3135
// power supply voltage [V]
32-
motor.voltage_power_supply = 12;
36+
driver.voltage_power_supply = 12;
37+
driver.init();
38+
// link the motor and the driver
39+
motor.linkDriver(&driver);
40+
3341
// aligning voltage [V]
3442
motor.voltage_sensor_align = 3;
3543
// index search velocity [rad/s]

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ MagneticSensorSPI sensor = MagneticSensorSPI(PA4, 14, 0x3FFF);
2020
//MagneticSensorI2C sensor = MagneticSensorI2C(0x36, 12, 0x0E, 4);
2121

2222
// Motor instance
23-
BLDCMotor motor = BLDCMotor(PA3, PA2, PA1, 11, PA0);
23+
BLDCMotor motor = BLDCMotor(11);
24+
// BLDCDriver3PWM(IN1, IN2, IN3, enable(optional))
25+
BLDCDriver3PWM driver = BLDCDriver3PWM(PB6, PB7, PB8, PB5);
26+
// BLDCDriver6PWM(IN1_H, IN1_L, IN2_H, IN2_L, IN3_H, IN3_L, enable(optional))
27+
//BLDCDriver6PWM driver = BLDCDriver6PWM(PA8, PB13, PA9, PB14, PA10, PB15, PB12);
2428

2529
void setup() {
2630

@@ -29,9 +33,12 @@ void setup() {
2933
// link the motor to the sensor
3034
motor.linkSensor(&sensor);
3135

32-
// power supply voltage
33-
// default 12V
34-
motor.voltage_power_supply = 12;
36+
// driver config
37+
// power supply voltage [V]
38+
driver.voltage_power_supply = 12;
39+
driver.init();
40+
// link the motor and the driver
41+
motor.linkDriver(&driver);
3542

3643
// choose FOC modulation (optional)
3744
motor.foc_modulation = FOCModulationType::SpaceVectorPWM;
Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818
#define INH_A 9
1919
#define INH_B 10
2020
#define INH_C 11
21+
2122
#define EN_GATE 7
2223
#define M_PWM A1
2324
#define M_OC A2
2425
#define OC_ADJ A3
2526

26-
// motor instance
27-
BLDCMotor motor = BLDCMotor(INH_A, INH_B, INH_C, 11, EN_GATE);
27+
// Motor instance
28+
BLDCMotor motor = BLDCMotor(11);
29+
BLDCDriver3PWM driver = BLDCDriver3PWM(INH_A, INH_B, INH_C, EN_GATE);
2830

2931
// encoder instance
3032
Encoder encoder = Encoder(2, 3, 8192);
@@ -54,11 +56,17 @@ void setup() {
5456
pinMode(OC_ADJ,OUTPUT);
5557
digitalWrite(OC_ADJ,HIGH);
5658

57-
// choose FOC modulation
58-
motor.foc_modulation = FOCModulationType::SpaceVectorPWM;
5959

60+
// driver config
6061
// power supply voltage [V]
61-
motor.voltage_power_supply = 12;
62+
driver.voltage_power_supply = 12;
63+
driver.init();
64+
// link the motor and the driver
65+
motor.linkDriver(&driver);
66+
67+
68+
// choose FOC modulation
69+
motor.foc_modulation = FOCModulationType::SpaceVectorPWM;
6270

6371
// set control loop type to be used
6472
motor.controller = ControlType::voltage;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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 3
19+
#define INH_B 5
20+
#define INH_C 9
21+
#define INL_A 11
22+
#define INL_B 6
23+
#define INL_C 10
24+
25+
#define EN_GATE 7
26+
#define M_PWM A1
27+
#define M_OC A2
28+
#define OC_ADJ A3
29+
30+
// Motor instance
31+
BLDCMotor motor = BLDCMotor(11);
32+
BLDCDriver6PWM driver = BLDCDriver6PWM(INH_A,INH_A, INH_B,INH_B, INH_C,INL_C, EN_GATE);
33+
34+
// encoder instance
35+
Encoder encoder = Encoder(2, 3, 8192);
36+
37+
// Interrupt routine intialisation
38+
// channel A and B callbacks
39+
void doA(){encoder.handleA();}
40+
void doB(){encoder.handleB();}
41+
42+
void setup() {
43+
44+
// initialize encoder sensor hardware
45+
encoder.init();
46+
encoder.enableInterrupts(doA, doB);
47+
// link the motor to the sensor
48+
motor.linkSensor(&encoder);
49+
50+
// DRV8302 specific code
51+
// M_OC - enable overcurrent protection
52+
pinMode(M_OC,OUTPUT);
53+
digitalWrite(M_OC,LOW);
54+
// M_PWM - disable 3pwm mode
55+
pinMode(M_PWM,OUTPUT);
56+
digitalWrite(M_PWM, LOW);
57+
// OD_ADJ - set the maximum overcurrent limit possible
58+
// Better option would be to use voltage divisor to set exact value
59+
pinMode(OC_ADJ,OUTPUT);
60+
digitalWrite(OC_ADJ,HIGH);
61+
62+
// driver config
63+
// power supply voltage [V]
64+
driver.voltage_power_supply = 12;
65+
driver.init();
66+
// link the motor and the driver
67+
motor.linkDriver(&driver);
68+
69+
// choose FOC modulation
70+
motor.foc_modulation = FOCModulationType::SpaceVectorPWM;
71+
72+
// set control loop type to be used
73+
motor.controller = ControlType::voltage;
74+
75+
// contoller configuration based on the controll type
76+
motor.PID_velocity.P = 0.2;
77+
motor.PID_velocity.I = 20;
78+
// default voltage_power_supply
79+
motor.voltage_limit = 12;
80+
81+
// velocity low pass filtering time constant
82+
motor.LPF_velocity.Tf = 0.01;
83+
84+
// angle loop controller
85+
motor.P_angle.P = 20;
86+
// angle loop velocity limit
87+
motor.velocity_limit = 50;
88+
89+
// use monitoring with serial for motor init
90+
// monitoring port
91+
Serial.begin(115200);
92+
// comment out if not needed
93+
motor.useMonitoring(Serial);
94+
95+
// initialise motor
96+
motor.init();
97+
// align encoder and start FOC
98+
motor.initFOC();
99+
100+
// set the inital target value
101+
motor.target = 2;
102+
103+
104+
Serial.println("Full control example: ");
105+
Serial.println("Run user commands to configure and the motor (find the full command list in docs.simplefoc.com) \n ");
106+
Serial.println("Initial motion control loop is voltage loop.");
107+
Serial.println("Initial target voltage 2V.");
108+
109+
_delay(1000);
110+
}
111+
112+
113+
void loop() {
114+
// iterative setting FOC phase voltage
115+
motor.loopFOC();
116+
117+
// iterative function setting the outter loop target
118+
// velocity, position or voltage
119+
// if tatget not set in parameter uses motor.target variable
120+
motor.move();
121+
122+
// user communication
123+
motor.command(serialReceiveUserCommand());
124+
}
125+
126+
// utility function enabling serial communication the user
127+
String serialReceiveUserCommand() {
128+
129+
// a string to hold incoming data
130+
static String received_chars;
131+
132+
String command = "";
133+
134+
while (Serial.available()) {
135+
// get the new byte:
136+
char inChar = (char)Serial.read();
137+
// add it to the string buffer:
138+
received_chars += inChar;
139+
140+
// end of user input
141+
if (inChar == '\n') {
142+
143+
// execute the user command
144+
command = received_chars;
145+
146+
// reset the command buffer
147+
received_chars = "";
148+
}
149+
}
150+
return command;
151+
}
152+

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
*/
55
#include <SimpleFOC.h>
66

7-
// motor instance
8-
BLDCMotor motor = BLDCMotor(25, 26, 27, 7);
7+
// Motor instance
8+
BLDCMotor motor = BLDCMotor(11);
9+
BLDCDriver3PWM driver = BLDCDriver3PWM(25, 26, 27, 7);
910

1011
// encoder instance
1112
Encoder encoder = Encoder(4, 2, 1024);
@@ -23,9 +24,14 @@ void setup() {
2324

2425
// link the motor to the sensor
2526
motor.linkSensor(&encoder);
26-
27+
28+
// driver config
2729
// power supply voltage [V]
28-
motor.voltage_power_supply = 12;
30+
driver.voltage_power_supply = 12;
31+
driver.init();
32+
// link the motor and the driver
33+
motor.linkDriver(&driver);
34+
2935
// aligning voltage [V]
3036
motor.voltage_sensor_align = 3;
3137
// index search velocity [rad/s]

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ MagneticSensorSPI sensor = MagneticSensorSPI(10, 14, 0x3FFF);
1919
// MagneticSensorAnalog sensor = MagneticSensorAnalog(A1, 14, 1020);
2020

2121
// Motor instance
22-
BLDCMotor motor = BLDCMotor(25, 26, 27, 7);
22+
BLDCMotor motor = BLDCMotor(11);
23+
BLDCDriver3PWM driver = BLDCDriver3PWM(25, 26, 27, 7);
2324

2425
void setup() {
2526

@@ -28,9 +29,12 @@ void setup() {
2829
// link the motor to the sensor
2930
motor.linkSensor(&sensor);
3031

31-
// power supply voltage
32-
// default 12V
33-
motor.voltage_power_supply = 12;
32+
// driver config
33+
// power supply voltage [V]
34+
driver.voltage_power_supply = 12;
35+
driver.init();
36+
// link the motor and the driver
37+
motor.linkDriver(&driver);
3438

3539
// choose FOC modulation (optional)
3640
motor.foc_modulation = FOCModulationType::SpaceVectorPWM;

examples/hardware_specific_examples/HMBGC_example/position_control/position_control.ino

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
#include <PciListenerImp.h>
1818

1919

20-
// motor instance
21-
BLDCMotor motor = BLDCMotor(9, 10, 11, 11);
20+
// BLDC motor & driver instance
21+
BLDCMotor motor = BLDCMotor(11);
22+
BLDCDriver3PWM driver = BLDCDriver3PWM(9, 10, 11);
2223

2324
// encoder instance
2425
Encoder encoder = Encoder(A0, A1, 2048);
@@ -41,9 +42,14 @@ void setup() {
4142
PciManager.registerListener(&listenerB);
4243
// link the motor to the sensor
4344
motor.linkSensor(&encoder);
44-
45+
46+
// driver config
4547
// power supply voltage [V]
46-
motor.voltage_power_supply = 12;
48+
driver.voltage_power_supply = 12;
49+
driver.init();
50+
// link the motor and the driver
51+
motor.linkDriver(&driver);
52+
4753
// aligning voltage [V]
4854
motor.voltage_sensor_align = 3;
4955
// index search velocity [rad/s]

examples/hardware_specific_examples/HMBGC_example/voltage_control/voltage_control.ino

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323
#include <PciListenerImp.h>
2424

2525

26-
// motor instance
27-
BLDCMotor motor = BLDCMotor(9, 10, 11, 11);
26+
// BLDC motor & driver instance
27+
BLDCMotor motor = BLDCMotor(11);
28+
BLDCDriver3PWM driver = BLDCDriver3PWM(9, 10, 11);
2829

2930
// encoder instance
3031
Encoder encoder = Encoder(A0, A1, 8192);
@@ -48,9 +49,12 @@ void setup() {
4849
// link the motor to the sensor
4950
motor.linkSensor(&encoder);
5051

51-
// power supply voltage
52-
// default 12V
53-
motor.voltage_power_supply = 12;
52+
// driver config
53+
// power supply voltage [V]
54+
driver.voltage_power_supply = 12;
55+
driver.init();
56+
// link the motor and the driver
57+
motor.linkDriver(&driver);
5458

5559
// choose FOC modulation (optional)
5660
motor.foc_modulation = FOCModulationType::SpaceVectorPWM;

examples/motion_control/open_loop_motor_control/open_loop_position_example/open_loop_position_example.ino

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
// Open loop motor control example
22
#include <SimpleFOC.h>
33

4-
// motor instance
5-
// BLDCMotor( phA, phB, phC, pp, (en optional))
6-
BLDCMotor motor = BLDCMotor(9, 5, 6, 1, 8);
7-
// StepperMotor(ph1A,ph1B,ph2A,ph2B,pp,( en1, en2 optional))
8-
//StepperMotor motor = StepperMotor(9, 5, 10, 6, 50, 8);
4+
5+
// BLDC motor & driver instance
6+
BLDCMotor motor = BLDCMotor(11);
7+
BLDCDriver3PWM driver = BLDCDriver3PWM(9, 5, 6, 8);
8+
// Stepper motor & driver instance
9+
//StepperMotor motor = StepperMotor(50);
10+
//StepperDriver4PWM driver = StepperDriver4PWM(9, 5, 10, 6, 8);
911

1012
void setup() {
1113

12-
// power supply voltage
13-
// default 12V
14-
motor.voltage_power_supply = 12;
14+
// driver config
15+
// power supply voltage [V]
16+
driver.voltage_power_supply = 12;
17+
driver.init();
18+
// link the motor and the driver
19+
motor.linkDriver(&driver);
1520

1621
// limiting motor movements
1722
motor.voltage_limit = 3; // rad/s

0 commit comments

Comments
 (0)