Skip to content

Commit e8421ed

Browse files
committed
added teesny low-side example, and complted the readme with f7
1 parent af2c7c0 commit e8421ed

File tree

2 files changed

+175
-6
lines changed

2 files changed

+175
-6
lines changed

README.md

Lines changed: 5 additions & 6 deletions
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>](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,170 @@
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+
pinMode(OC_GAIN,OUTPUT);
75+
digitalWrite(OC_GAIN,LOW);
76+
77+
78+
// driver config
79+
// power supply voltage [V]
80+
driver.voltage_power_supply = 19;
81+
driver.pwm_frequency = 20000; // suggested not higher than 22khz
82+
driver.init();
83+
// link the motor and the driver
84+
motor.linkDriver(&driver);
85+
// link current sense and the driver
86+
cs.linkDriver(&driver);
87+
88+
// align voltage
89+
motor.voltage_sensor_align = 0.5;
90+
91+
// control loop type and torque mode
92+
motor.torque_controller = TorqueControlType::voltage;
93+
motor.controller = MotionControlType::torque;
94+
motor.motion_downsample = 0.0;
95+
96+
// velocity loop PID
97+
motor.PID_velocity.P = 0.2;
98+
motor.PID_velocity.I = 5.0;
99+
// Low pass filtering time constant
100+
motor.LPF_velocity.Tf = 0.02;
101+
// angle loop PID
102+
motor.P_angle.P = 20.0;
103+
// Low pass filtering time constant
104+
motor.LPF_angle.Tf = 0.0;
105+
// current q loop PID
106+
motor.PID_current_q.P = 3.0;
107+
motor.PID_current_q.I = 100.0;
108+
// Low pass filtering time constant
109+
motor.LPF_current_q.Tf = 0.02;
110+
// current d loop PID
111+
motor.PID_current_d.P = 3.0;
112+
motor.PID_current_d.I = 100.0;
113+
// Low pass filtering time constant
114+
motor.LPF_current_d.Tf = 0.02;
115+
116+
// Limits
117+
motor.velocity_limit = 100.0; // 100 rad/s velocity limit
118+
motor.voltage_limit = 12.0; // 12 Volt limit
119+
motor.current_limit = 2.0; // 2 Amp current limit
120+
121+
122+
// use monitoring with serial for motor init
123+
// monitoring port
124+
Serial.begin(115200);
125+
// comment out if not needed
126+
motor.useMonitoring(Serial);
127+
motor.monitor_variables = _MON_CURR_Q | _MON_CURR_D; // monitor the two currents d and q
128+
motor.monitor_downsample = 0;
129+
130+
// initialise motor
131+
motor.init();
132+
133+
cs.init();
134+
// driver 8302 has inverted gains on all channels
135+
cs.gain_a *=-1;
136+
cs.gain_b *=-1;
137+
cs.gain_c *=-1;
138+
motor.linkCurrentSense(&cs);
139+
140+
// align encoder and start FOC
141+
motor.initFOC();
142+
143+
// set the inital target value
144+
motor.target = 0;
145+
146+
// define the motor id
147+
command.add('M', onMotor, "motor");
148+
149+
Serial.println(F("Full control example: "));
150+
Serial.println(F("Run user commands to configure and the motor (find the full command list in docs.simplefoc.com) \n "));
151+
Serial.println(F("Initial motion control loop is voltage loop."));
152+
Serial.println(F("Initial target voltage 2V."));
153+
154+
_delay(1000);
155+
}
156+
157+
158+
void loop() {
159+
// iterative setting FOC phase voltage
160+
motor.loopFOC();
161+
162+
// iterative function setting the outter loop target
163+
motor.move();
164+
165+
// monitoring the state variables
166+
motor.monitor();
167+
168+
// user communication
169+
command.run();
170+
}

0 commit comments

Comments
 (0)