Skip to content

Commit 0400c38

Browse files
committed
added odrive examples
1 parent 0505c73 commit 0400c38

File tree

3 files changed

+263
-2
lines changed

3 files changed

+263
-2
lines changed

.github/workflows/ccpp.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
- arduino-boards-fqbn: arduino:avr:uno # arudino uno - compiling almost all examples
2525
sketch-names: '**.ino'
2626
required-libraries: PciManager
27-
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,esp32_current_control_low_side, stm32_spi_alt_example, esp32_spi_alt_example, B_G431B_ESC1, odrive_example
27+
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,esp32_current_control_low_side, stm32_spi_alt_example, esp32_spi_alt_example, B_G431B_ESC1, odrive_example_spi, odrive_example_encoder
2828

2929
- arduino-boards-fqbn: arduino:sam:arduino_due_x # arduino due - one full example
3030
sketch-names: single_full_control_example.ino
@@ -57,7 +57,7 @@ jobs:
5757

5858
- arduino-boards-fqbn: STMicroelectronics:stm32:GenF4:pnum=GENERIC_F405RGTX # stm32f405 - odrive
5959
platform-url: https://github.com/stm32duino/BoardManagerFiles/raw/main/package_stmicroelectronics_index.json
60-
sketch-names: odrive_example.ino
60+
sketch-names: odrive_example_encoder.ino odrive_example_spi.ino
6161

6262
- arduino-boards-fqbn: STMicroelectronics:stm32:Nucleo_64:pnum=NUCLEO_F411RE # nucleo one full example
6363
platform-url: https://github.com/stm32duino/BoardManagerFiles/raw/main/package_stmicroelectronics_index.json
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
Odrive robotics' hardware is one of the best BLDC motor foc supporting hardware out there.
3+
4+
This is an example code that can be directly uploaded to the Odrive using the SWD programmer.
5+
This code uses an encoder with 500 cpr and a BLDC motor with 7 pole pairs connected to the M0 interface of the Odrive.
6+
7+
This is a short template code and the idea is that you are able to adapt to your needs not to be a complete solution. :D
8+
*/
9+
#include <SimpleFOC.h>
10+
11+
// Odrive M0 motor pinout
12+
#define M0_INH_A PA8
13+
#define M0_INH_B PA9
14+
#define M0_INH_C PA10
15+
#define M0_INL_A PB13
16+
#define M0_INL_B PB14
17+
#define M0_INL_C PB15
18+
// M0 currnets
19+
#define M0_IB PC0
20+
#define M0_IC PC1
21+
// Odrive M0 encoder pinout
22+
#define M0_ENC_A PB4
23+
#define M0_ENC_B PB5
24+
25+
26+
// Odrive M1 motor pinout
27+
#define M1_INH_A PC6
28+
#define M1_INH_B PC7
29+
#define M1_INH_C PC8
30+
#define M1_INL_A PA7
31+
#define M1_INL_B PB0
32+
#define M1_INL_C PB1
33+
// M0 currnets
34+
#define M1_IB PC2
35+
#define M1_IC PC3
36+
// Odrive M1 encoder pinout
37+
#define M1_ENC_A PB6
38+
#define M1_ENC_B PB7
39+
40+
// M1 & M2 common enable pin
41+
#define EN_GATE PB12
42+
43+
// SPI pinout
44+
#define SPI3_SCL PC10
45+
#define SPI3_MISO PC11
46+
#define SPI3_MOSO PC12
47+
48+
// Motor instance
49+
BLDCMotor motor = BLDCMotor(7);
50+
BLDCDriver6PWM driver = BLDCDriver6PWM(M0_INH_A,M0_INL_A, M0_INH_B,M0_INL_B, M0_INH_C,M0_INL_C, EN_GATE);
51+
52+
// instantiate the commander
53+
Commander command = Commander(Serial);
54+
void doMotor(char* cmd) { command.motor(&motor, cmd); }
55+
56+
// low side current sensing define
57+
// 0.0005 Ohm resistor
58+
// gain of 10x
59+
// current sensing on B and C phases, phase A not connected
60+
LowsideCurrentSense current_sense = LowsideCurrentSense(0.0005f, 10.0f, _NC, M0_IB, M0_IC);
61+
62+
Encoder encoder = Encoder(M0_ENC_A, M0_ENC_B, 500);
63+
// Interrupt routine intialisation
64+
// channel A and B callbacks
65+
void doA(){encoder.handleA();}
66+
void doB(){encoder.handleB();}
67+
68+
void setup(){
69+
70+
// pwm frequency to be used [Hz]
71+
driver.pwm_frequency = 20000;
72+
// power supply voltage [V]
73+
driver.voltage_power_supply = 20;
74+
// Max DC voltage allowed - default voltage_power_supply
75+
driver.voltage_limit = 20;
76+
// driver init
77+
driver.init();
78+
// link the motor and the driver
79+
motor.linkDriver(&driver);
80+
81+
// initialize encoder sensor hardware
82+
encoder.init();
83+
encoder.enableInterrupts(doA, doB);
84+
// link the motor to the sensor
85+
motor.linkSensor(&encoder);
86+
87+
// control loop type and torque mode
88+
motor.torque_controller = TorqueControlType::voltage;
89+
motor.controller = MotionControlType::torque;
90+
91+
// max voltage allowed for motion control
92+
motor.voltage_limit = 8.0;
93+
// alignment voltage limit
94+
motor.voltage_sensor_align = 0.5;
95+
96+
97+
Serial.begin(115200);
98+
// comment out if not needed
99+
motor.useMonitoring(Serial);
100+
motor.monitor_variables = _MON_CURR_Q | _MON_CURR_D;
101+
motor.monitor_downsample = 1000;
102+
103+
// add target command T
104+
command.add('M', doMotor, "motor M0");
105+
106+
// initialise motor
107+
motor.init();
108+
109+
// link the driver
110+
current_sense.linkDriver(&driver);
111+
// init the current sense
112+
current_sense.init();
113+
current_sense.skip_align = true;
114+
motor.linkCurrentSense(&current_sense);
115+
116+
// init FOC
117+
motor.initFOC();
118+
delay(1000);
119+
}
120+
121+
void loop(){
122+
123+
// foc loop
124+
motor.loopFOC();
125+
// motion control
126+
motor.move();
127+
// monitoring
128+
motor.monitor();
129+
// user communication
130+
command.run();
131+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
Odrive robotics' hardware is one of the best BLDC motor foc supporting hardware out there.
3+
4+
This is an example code that can be directly uploaded to the Odrive using the SWD programmer.
5+
This code uses an magnetic spi sensor AS5047 and a BLDC motor with 11 pole pairs connected to the M0 interface of the Odrive.
6+
7+
This is a short template code and the idea is that you are able to adapt to your needs not to be a complete solution. :D
8+
*/
9+
#include <SimpleFOC.h>
10+
11+
// Odrive M0 motor pinout
12+
#define M0_INH_A PA8
13+
#define M0_INH_B PA9
14+
#define M0_INH_C PA10
15+
#define M0_INL_A PB13
16+
#define M0_INL_B PB14
17+
#define M0_INL_C PB15
18+
// M0 currnets
19+
#define M0_IB PC0
20+
#define M0_IC PC1
21+
// Odrive M0 encoder pinout
22+
#define M0_ENC_A PB4
23+
#define M0_ENC_B PB5
24+
25+
26+
// Odrive M1 motor pinout
27+
#define M1_INH_A PC6
28+
#define M1_INH_B PC7
29+
#define M1_INH_C PC8
30+
#define M1_INL_A PA7
31+
#define M1_INL_B PB0
32+
#define M1_INL_C PB1
33+
// M0 currnets
34+
#define M1_IB PC2
35+
#define M1_IC PC3
36+
// Odrive M1 encoder pinout
37+
#define M1_ENC_A PB6
38+
#define M1_ENC_B PB7
39+
40+
// M1 & M2 common enable pin
41+
#define EN_GATE PB12
42+
43+
// SPI pinout
44+
#define SPI3_SCL PC10
45+
#define SPI3_MISO PC11
46+
#define SPI3_MOSO PC12
47+
48+
// Motor instance
49+
BLDCMotor motor = BLDCMotor(11);
50+
BLDCDriver6PWM driver = BLDCDriver6PWM(M0_INH_A,M0_INL_A, M0_INH_B,M0_INL_B, M0_INH_C,M0_INL_C, EN_GATE);
51+
52+
// instantiate the commander
53+
Commander command = Commander(Serial);
54+
void doMotor(char* cmd) { command.motor(&motor, cmd); }
55+
56+
// low side current sensing define
57+
// 0.0005 Ohm resistor
58+
// gain of 10x
59+
// current sensing on B and C phases, phase A not connected
60+
LowsideCurrentSense current_sense = LowsideCurrentSense(0.0005f, 10.0f, _NC, M0_IB, M0_IC);
61+
62+
// MagneticSensorSPI(int cs, float _cpr, int _angle_register)
63+
// config - SPI config
64+
// cs - SPI chip select pin
65+
MagneticSensorSPI sensor = MagneticSensorSPI(AS5147_SPI, M0_ENC_A);
66+
SPIClass SPI_3(SPI3_MOSO, SPI3_MISO, SPI3_SCL);
67+
68+
void setup(){
69+
70+
// pwm frequency to be used [Hz]
71+
driver.pwm_frequency = 20000;
72+
// power supply voltage [V]
73+
driver.voltage_power_supply = 20;
74+
// Max DC voltage allowed - default voltage_power_supply
75+
driver.voltage_limit = 20;
76+
// driver init
77+
driver.init();
78+
// link the motor and the driver
79+
motor.linkDriver(&driver);
80+
81+
// initialise magnetic sensor hardware
82+
sensor.init(&SPI_3);
83+
// link the motor to the sensor
84+
motor.linkSensor(&sensor);
85+
86+
// control loop type and torque mode
87+
motor.torque_controller = TorqueControlType::voltage;
88+
motor.controller = MotionControlType::torque;
89+
90+
// max voltage allowed for motion control
91+
motor.voltage_limit = 8.0;
92+
// alignment voltage limit
93+
motor.voltage_sensor_align = 0.5;
94+
95+
96+
Serial.begin(115200);
97+
// comment out if not needed
98+
motor.useMonitoring(Serial);
99+
motor.monitor_variables = _MON_CURR_Q | _MON_CURR_D;
100+
motor.monitor_downsample = 1000;
101+
102+
// add target command T
103+
command.add('M', doMotor, "motor M0");
104+
105+
// initialise motor
106+
motor.init();
107+
108+
// link the driver
109+
current_sense.linkDriver(&driver);
110+
// init the current sense
111+
current_sense.init();
112+
current_sense.skip_align = true;
113+
motor.linkCurrentSense(&current_sense);
114+
115+
// init FOC
116+
motor.initFOC();
117+
delay(1000);
118+
}
119+
120+
void loop(){
121+
122+
// foc loop
123+
motor.loopFOC();
124+
// motion control
125+
motor.move();
126+
// monitoring
127+
motor.monitor();
128+
// user communication
129+
command.run();
130+
}

0 commit comments

Comments
 (0)