Skip to content

Commit bc0f5a4

Browse files
committed
current sense better align + example refactoring
1 parent b4128e4 commit bc0f5a4

File tree

9 files changed

+358
-179
lines changed

9 files changed

+358
-179
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/**
2+
*
3+
* SimpleFOCMini motor control example
4+
*
5+
* For Arduino UNO, the most convenient way to use the board is to stack it to the pins:
6+
* - 12 - GND
7+
* - 11 - IN1
8+
* - 10 - IN2
9+
* - 9 - IN3
10+
* - 8 - EN
11+
*
12+
* For other boards with UNO headers but more PWM channles such as esp32, nucleo-64, samd51 metro etc, the best way to most convenient pinout is:
13+
* - GND - GND
14+
* - 13 - IN1
15+
* - 12 - IN2
16+
* - 11 - IN3
17+
* - 9 - EN
18+
*
19+
* For the boards without arduino uno headers, the choice of pinout is a lot less constrained.
20+
*
21+
*/
22+
#include <SimpleFOC.h>
23+
24+
25+
// BLDC motor & driver instance
26+
BLDCMotor motor = BLDCMotor(11);
27+
BLDCDriver3PWM driver = BLDCDriver3PWM(11, 10, 9, 8);
28+
29+
// encoder instance
30+
Encoder encoder = Encoder(2, 3, 500);
31+
// Interrupt routine intialisation
32+
// channel A and B callbacks
33+
void doA(){encoder.handleA();}
34+
void doB(){encoder.handleB();}
35+
36+
// instantiate the commander
37+
Commander command = Commander(Serial);
38+
void doMotor(char* cmd) { command.motor(&motor, cmd); }
39+
40+
void setup() {
41+
// if SimpleFOCMini is stacked in arduino headers
42+
// on pins 12,11,10,9,8
43+
// pin 12 is used as ground
44+
pinMode(12,OUTPUT);
45+
pinMode(12,LOW);
46+
47+
// initialize encoder sensor hardware
48+
encoder.init();
49+
encoder.enableInterrupts(doA, doB);
50+
// link the motor to the sensor
51+
motor.linkSensor(&encoder);
52+
53+
// driver config
54+
// power supply voltage [V]
55+
driver.voltage_power_supply = 12;
56+
driver.init();
57+
// link the motor and the driver
58+
motor.linkDriver(&driver);
59+
60+
// aligning voltage [V]
61+
motor.voltage_sensor_align = 3;
62+
63+
// set motion control loop to be used
64+
motor.controller = MotionControlType::angle;
65+
66+
// contoller configuration
67+
// default parameters in defaults.h
68+
69+
// velocity PI controller parameters
70+
motor.PID_velocity.P = 0.2f;
71+
motor.PID_velocity.I = 20;
72+
motor.PID_velocity.D = 0;
73+
// default voltage_power_supply
74+
motor.voltage_limit = 6;
75+
// jerk control using voltage voltage ramp
76+
// default value is 300 volts per sec ~ 0.3V per millisecond
77+
motor.PID_velocity.output_ramp = 1000;
78+
79+
// velocity low pass filtering time constant
80+
motor.LPF_velocity.Tf = 0.01f;
81+
82+
// angle P controller
83+
motor.P_angle.P = 20;
84+
// maximal velocity of the position control
85+
motor.velocity_limit = 4;
86+
87+
88+
// use monitoring with serial
89+
Serial.begin(115200);
90+
// comment out if not needed
91+
motor.useMonitoring(Serial);
92+
93+
// initialize motor
94+
motor.init();
95+
// align encoder and start FOC
96+
motor.initFOC();
97+
98+
// add target command M
99+
command.add('M', doMotor, "motor");
100+
101+
Serial.println(F("Motor ready."));
102+
Serial.println(F("Set the target angle using serial terminal:"));
103+
_delay(1000);
104+
}
105+
106+
void loop() {
107+
// main FOC algorithm function
108+
// the faster you run this function the better
109+
// Arduino UNO loop ~1kHz
110+
// Bluepill loop ~10kHz
111+
motor.loopFOC();
112+
113+
// Motion control function
114+
// velocity, position or voltage (defined in motor.controller)
115+
// this function can be run at much lower frequency than loopFOC() function
116+
// You can also use motor.move() and set the motor.target in the code
117+
motor.move();
118+
119+
// function intended to be used with serial plotter to monitor motor variables
120+
// significantly slowing the execution down!!!!
121+
// motor.monitor();
122+
123+
// user communication
124+
command.run();
125+
}

examples/motion_control/position_motion_control/encoder/angle_control/angle_control.ino

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@
2424
*
2525
*/
2626
#include <SimpleFOC.h>
27-
// software interrupt library
28-
#include <PciManager.h>
29-
#include <PciListenerImp.h>
3027

3128
// BLDC motor & driver instance
3229
BLDCMotor motor = BLDCMotor(11);
@@ -36,15 +33,12 @@ BLDCDriver3PWM driver = BLDCDriver3PWM(9, 5, 6, 8);
3633
//StepperDriver4PWM driver = StepperDriver4PWM(9, 5, 10, 6, 8);
3734

3835
// encoder instance
39-
Encoder encoder = Encoder(2, 3, 8192, A0);
36+
Encoder encoder = Encoder(2, 3, 8192);
4037

4138
// Interrupt routine intialisation
4239
// channel A and B callbacks
4340
void doA(){encoder.handleA();}
4441
void doB(){encoder.handleB();}
45-
void doIndex(){encoder.handleIndex();}
46-
// If no available hadware interrupt pins use the software interrupt
47-
PciListenerImp listenerIndex(encoder.index_pin, doIndex);
4842

4943
// angle set point variable
5044
float target_angle = 0;
@@ -57,8 +51,6 @@ void setup() {
5751
// initialize encoder sensor hardware
5852
encoder.init();
5953
encoder.enableInterrupts(doA, doB);
60-
// software interrupts
61-
PciManager.registerListener(&listenerIndex);
6254
// link the motor to the sensor
6355
motor.linkSensor(&encoder);
6456

@@ -71,8 +63,6 @@ void setup() {
7163

7264
// aligning voltage [V]
7365
motor.voltage_sensor_align = 3;
74-
// index search velocity [rad/s]
75-
motor.velocity_index_search = 3;
7666

7767
// set motion control loop to be used
7868
motor.controller = MotionControlType::angle;

examples/utils/calibration/find_kv_rating/encoder/find_kv_rating/find_kv_rating.ino

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ void setup() {
4949
motor.linkSensor(&sensor);
5050

5151
// driver config
52-
// power supply voltage [V]
52+
// IMPORTANT!
53+
// make sure to set the correct power supply voltage [V]
5354
driver.voltage_power_supply = 12;
5455
driver.init();
5556
// link driver
@@ -58,9 +59,6 @@ void setup() {
5859
// aligning voltage
5960
motor.voltage_sensor_align = 3;
6061

61-
// choose FOC modulation (optional)
62-
motor.foc_modulation = FOCModulationType::SpaceVectorPWM;
63-
6462
// set motion control loop to be used
6563
motor.controller = MotionControlType::torque;
6664

examples/utils/calibration/find_kv_rating/hall_sensor/find_kv_rating/find_kv_rating.ino

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,15 @@ void setup() {
4646
motor.linkSensor(&sensor);
4747

4848
// driver config
49-
// power supply voltage [V]
49+
// IMPORTANT!
50+
// make sure to set the correct power supply voltage [V]
5051
driver.voltage_power_supply = 12;
5152
driver.init();
5253
// link driver
5354
motor.linkDriver(&driver);
5455

5556
// aligning voltage
5657
motor.voltage_sensor_align = 3;
57-
58-
// choose FOC modulation (optional)
59-
motor.foc_modulation = FOCModulationType::SpaceVectorPWM;
6058

6159
// set motion control loop to be used
6260
motor.controller = MotionControlType::torque;

examples/utils/calibration/find_kv_rating/magnetic_sensor/find_kv_rating/find_kv_rating.ino

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,15 @@ void setup() {
4343
motor.linkSensor(&sensor);
4444

4545
// driver config
46-
// power supply voltage [V]
46+
// IMPORTANT!
47+
// make sure to set the correct power supply voltage [V]
4748
driver.voltage_power_supply = 12;
4849
driver.init();
4950
// link driver
5051
motor.linkDriver(&driver);
5152

5253
// aligning voltage
5354
motor.voltage_sensor_align = 3;
54-
55-
// choose FOC modulation (optional)
56-
motor.foc_modulation = FOCModulationType::SpaceVectorPWM;
5755

5856
// set motion control loop to be used
5957
motor.controller = MotionControlType::torque;

src/BLDCMotor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class BLDCMotor: public FOCMotor
1919
BLDCMotor class constructor
2020
@param pp pole pairs number
2121
@param R motor phase resistance
22-
@param KV motor KV rating (1/K_bemf)
22+
@param KV motor KV rating (1/K_bemf) - rpm/V
2323
*/
2424
BLDCMotor(int pp, float R = NOT_SET, float KV = NOT_SET);
2525

0 commit comments

Comments
 (0)