Skip to content

Commit bf74c7c

Browse files
committed
Add Silabs drivers and examples
1 parent 0c86169 commit bf74c7c

File tree

9 files changed

+1802
-0
lines changed

9 files changed

+1802
-0
lines changed
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
/**
2+
* Silabs MG24 6PWM closed loop velocity control example with HALL sensor based rotor postion
3+
*
4+
* HARDWARE CONFIGURATION:
5+
* CPU Board: Arduino Nano Matter
6+
* Motor Driver Board: BOOSTXL-DRV8305EVM
7+
* BLDC Motor: Newark DF45M024053-A2
8+
*/
9+
10+
#include <SimpleFOC.h>
11+
12+
#define HALL_SENSOR_IRQ 1
13+
#define ENABLE_MONITOR 0
14+
15+
static bool allow_run = false;
16+
17+
// BLDC motor instance
18+
BLDCMotor *motor;
19+
20+
// BLDC driver instance
21+
BLDCDriver6PWM *driver;
22+
23+
// Commander instance
24+
Commander *command;
25+
26+
// Hall sensor instance
27+
HallSensor *sensor;
28+
29+
// Interrupt routine intialisation
30+
// channel A and B callbacks
31+
void doA() { sensor->handleA(); }
32+
void doB() { sensor->handleB(); }
33+
void doC() { sensor->handleC(); }
34+
35+
void doMotor(char* cmd) {
36+
if (!command) return;
37+
command->motor(motor, cmd);
38+
}
39+
40+
void setup() {
41+
// use monitoring with serial
42+
Serial.begin(115200);
43+
// enable more verbose output for debugging
44+
// comment out if not needed
45+
SimpleFOCDebug::enable(&Serial);
46+
47+
// Commander
48+
command = new Commander(Serial);
49+
if (!command) return;
50+
51+
// Driver
52+
driver = new BLDCDriver6PWM(6, 7, 8, 9, 10, 11, 12);
53+
if (!driver) return;
54+
55+
// Driver config
56+
// power supply voltage [V]
57+
driver->voltage_power_supply = 24;
58+
// pwm frequency to be used [Hz]
59+
driver->pwm_frequency = 20000; // 20 kHz
60+
// Max DC voltage allowed - default voltage_power_supply
61+
driver->voltage_limit = 12;
62+
// dead zone percentage of the duty cycle - default 0.02 - 2%
63+
// Can set value to 0 because the DRV8305 will provide the
64+
// required dead-time.
65+
driver->dead_zone = 0;
66+
67+
// init driver
68+
if (!driver->init()) {
69+
Serial.println("Driver init failed!");
70+
return;
71+
}
72+
driver->enable();
73+
74+
// HallSensor(int hallA, int hallB , int cpr, int index)
75+
// - hallA, hallB, hallC - HallSensor A, B and C pins
76+
// - pp - pole pairs
77+
sensor = new HallSensor(5, 4, 13, 8);
78+
if (!sensor) return;
79+
80+
// initialize sensor sensor hardware
81+
sensor->init();
82+
83+
#if HALL_SENSOR_IRQ
84+
sensor->enableInterrupts(doA, doB, doC);
85+
#else
86+
// Note: There is a bug when initializing HallSensor in heap, attribute
87+
// `use_interrupt` gets value not `false` even `enableInterrupts` has not been
88+
// called. So we initialize this attribute value `false` after creating a
89+
// `HallSensor` instance.
90+
sensor->use_interrupt = false;
91+
#endif
92+
93+
// Motor
94+
motor = new BLDCMotor(8);
95+
if (!motor) return;
96+
97+
// link the motor and the driver
98+
motor->linkDriver(driver);
99+
100+
// link the motor to the sensor
101+
motor->linkSensor(sensor);
102+
103+
// Set below the motor's max 5600 RPM limit = 586 rad/s
104+
motor->velocity_limit = 530.0f;
105+
106+
// set motion control loop to be used
107+
motor->controller = MotionControlType::velocity;
108+
109+
// choose FOC modulation (optional) - SinePWM or SpaceVectorPWM
110+
motor->foc_modulation = FOCModulationType::SpaceVectorPWM;
111+
112+
// controller configuration
113+
// velocity PI controller parameters
114+
motor->PID_velocity.P = 0.05f;
115+
motor->PID_velocity.I = 1;
116+
117+
// velocity low pass filtering time constant
118+
motor->LPF_velocity.Tf = 0.01f;
119+
120+
#if ENABLE_MONITOR
121+
motor->useMonitoring(Serial);
122+
motor->monitor_variables = _MON_TARGET | _MON_VEL;
123+
#endif
124+
125+
// initialize motor
126+
if (!motor->init()) {
127+
Serial.println("Motor init failed!");
128+
return;
129+
}
130+
131+
// align sensor and start FOC
132+
if (!motor->initFOC()) {
133+
Serial.println("FOC init failed!");
134+
return;
135+
}
136+
137+
// add target command M
138+
command->add('M', doMotor, "motor");
139+
140+
Serial.println("Motor ready!");
141+
Serial.println("Set target velocity [rad/s]");
142+
143+
allow_run = true;
144+
_delay(1000);
145+
}
146+
147+
void loop() {
148+
if (!allow_run) return;
149+
150+
// main FOC algorithm function
151+
// the faster you run this function the better
152+
motor->loopFOC();
153+
154+
// Motion control function
155+
// velocity, position or voltage (defined in motor.controller)
156+
// this function can be run at much lower frequency than loopFOC() function
157+
// You can also use motor.move() and set the motor.target in the code
158+
motor->move();
159+
160+
#if ENABLE_MONITOR
161+
// function intended to be used with serial plotter to monitor motor variables
162+
// significantly slowing the execution down!!!!
163+
motor->monitor();
164+
#endif
165+
166+
// user communication
167+
command->run();
168+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/**
2+
* Silabs MG24 6PWM open loop velocity control example
3+
*
4+
* HARDWARE CONFIGURATION:
5+
* CPU Board: Arduino Nano Matter
6+
* Motor Driver Board: BOOSTXL-DRV8305EVM
7+
* BLDC Motor: Newark DF45M024053-A2
8+
*/
9+
10+
#include <SimpleFOC.h>
11+
12+
static bool allow_run = false;
13+
14+
// BLDC motor instance
15+
BLDCMotor *motor;
16+
17+
// BLDC driver instance
18+
BLDCDriver6PWM *driver;
19+
20+
// Commander instance
21+
Commander *command;
22+
23+
void doMotor(char* cmd) {
24+
if (!command) return;
25+
command->motor(motor, cmd);
26+
}
27+
28+
void setup() {
29+
// use monitoring with serial
30+
Serial.begin(115200);
31+
// enable more verbose output for debugging
32+
// comment out if not needed
33+
SimpleFOCDebug::enable(&Serial);
34+
35+
// Commander
36+
command = new Commander(Serial);
37+
if (!command) return;
38+
39+
// Driver
40+
driver = new BLDCDriver6PWM(6, 7, 8, 9, 10, 11, 12);
41+
if (!driver) return;
42+
43+
// Driver config
44+
// power supply voltage [V]
45+
driver->voltage_power_supply = 24;
46+
// pwm frequency to be used [Hz]
47+
driver->pwm_frequency = 20000; // 20 kHz
48+
// Max DC voltage allowed - default voltage_power_supply
49+
driver->voltage_limit = 12;
50+
// dead zone percentage of the duty cycle - default 0.02 - 2%
51+
// Can set value to 0 because the DRV8305 will provide the
52+
// required dead-time.
53+
driver->dead_zone = 0;
54+
55+
// init driver
56+
if (!driver->init()) {
57+
Serial.println("Driver init failed!");
58+
return;
59+
}
60+
driver->enable();
61+
62+
// Motor
63+
motor = new BLDCMotor(8);
64+
if (!motor) return;
65+
66+
// link the motor and the driver
67+
motor->linkDriver(driver);
68+
69+
// default voltage_power_supply
70+
// motor->voltage_limit = 3;
71+
motor->voltage_limit = 0.8f;
72+
73+
// set motion control loop to be used
74+
motor->controller = MotionControlType::velocity_openloop;
75+
76+
// choose FOC modulation (optional) - SinePWM or SpaceVectorPWM
77+
motor->foc_modulation = FOCModulationType::SpaceVectorPWM;
78+
79+
// initialize motor
80+
if (!motor->init()) {
81+
Serial.println("Motor init failed!");
82+
return;
83+
}
84+
85+
// add target command M
86+
command->add('M', doMotor, "motor");
87+
88+
Serial.println("Motor ready!");
89+
Serial.println("Set target velocity [rad/s]");
90+
91+
allow_run = true;
92+
_delay(1000);
93+
}
94+
95+
96+
void loop() {
97+
if (!allow_run) return;
98+
99+
// open loop velocity movement
100+
// using motor.voltage_limit
101+
motor->move();
102+
103+
// user communication
104+
command->run();
105+
}

0 commit comments

Comments
 (0)