Skip to content

Commit fdaf9e2

Browse files
committed
FEAT refactoring owen's sensor implementations
1 parent c0ed33b commit fdaf9e2

File tree

19 files changed

+316
-123
lines changed

19 files changed

+316
-123
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ MagneticSensorSPI sensor = MagneticSensorSPI(10, 14, 0x3FFF);
1515
// SCL 22
1616
//MagneticSensorI2C sensor = MagneticSensorI2C(0x36, 12, 0x0E, 4);
1717

18+
// Analog output Magnetic sensor instance (AS5600)
19+
// MagneticSensorAnalog sensor = MagneticSensorAnalog(A1, 14, 1020);
20+
1821
// Motor instance
1922
BLDCMotor motor = BLDCMotor(25, 26, 27, 7);
2023

examples/motion_control/position_motion_control/hall_sensor/angle_control/angle_control.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ void doA(){sensor.handleA();}
3333
void doB(){sensor.handleB();}
3434
void doC(){sensor.handleC();}
3535
// If no available hadware interrupt pins use the software interrupt
36-
PciListenerImp listenerIndex(sensor.pinC, doC);
36+
PciListenerImp listenC(sensor.pinC, doC);
3737

3838

3939
void setup() {
@@ -42,7 +42,7 @@ void setup() {
4242
sensor.init();
4343
sensor.enableInterrupts(doA, doB); //, doC);
4444
// software interrupts
45-
PciManager.registerListener(&listenerIndex);
45+
PciManager.registerListener(&listenC);
4646
// link the motor to the sensor
4747
motor.linkSensor(&sensor);
4848

examples/motion_control/position_motion_control/magnetic_sensor/angle_control/angle_control.ino

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
*/
1010
#include <SimpleFOC.h>
1111

12-
// magnetic sensor instance
13-
MagneticSensorSPI sensor = MagneticSensorSPI(10, 16384, 0x3FFF);
14-
// magnetic sensor instance
12+
// magnetic sensor instance - SPI
13+
MagneticSensorSPI sensor = MagneticSensorSPI(10, 14, 0x3FFF);
14+
// magnetic sensor instance - I2C
1515
//MagneticSensorI2C sensor = MagneticSensorI2C(0x36, 12, 0x0E, 4);
16+
// magnetic sensor instance - analog output
17+
// MagneticSensorAnalog sensor = MagneticSensorAnalog(A1, 14, 1020);
1618

1719
// Motor instance
1820
BLDCMotor motor = BLDCMotor(9, 5, 6, 11, 8);

examples/motion_control/torque_voltage_control/analog/voltage_control.ino

Lines changed: 0 additions & 107 deletions
This file was deleted.

examples/motion_control/torque_voltage_control/magnetic_sensor/voltage_control/voltage_control.ino

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

11-
// magnetic sensor instance
12-
MagneticSensorSPI sensor = MagneticSensorSPI(10, 16384, 0x3FFF);
13-
// magnetic sensor instance
11+
// magnetic sensor instance - SPI
12+
MagneticSensorSPI sensor = MagneticSensorSPI(10, 14, 0x3FFF);
13+
// magnetic sensor instance - I2C
1414
//MagneticSensorI2C sensor = MagneticSensorI2C(0x36, 12, 0x0E, 4);
15+
// magnetic sensor instance - analog output
16+
// MagneticSensorAnalog sensor = MagneticSensorAnalog(A1, 14, 1020);
1517

1618
// Motor instance
1719
BLDCMotor motor = BLDCMotor(9, 5, 6, 11, 8);

examples/motion_control/velocity_motion_control/magnetic_sensor/velocity_control/velocity_control.ino

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
*/
1313
#include <SimpleFOC.h>
1414

15-
// magnetic sensor instance
15+
// magnetic sensor instance - SPI
1616
MagneticSensorSPI sensor = MagneticSensorSPI(10, 16384, 0x3FFF);
17-
// magnetic sensor instance
17+
// magnetic sensor instance - I2C
1818
//MagneticSensorI2C sensor = MagneticSensorI2C(0x36, 12, 0x0E, 4);
19+
// magnetic sensor instance - analog output
20+
// MagneticSensorAnalog sensor = MagneticSensorAnalog(A1, 14, 1020);
1921

2022
// Motor instance
2123
BLDCMotor motor = BLDCMotor(9, 5, 6, 11, 8);
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/**
2+
* Comprehensive BLDC motor control example using Hall sensor
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+
* To check the config value just enter the command letter.
12+
* For example: - to read velocity PI controller P gain run: P
13+
* - to set velocity PI controller P gain to 1.2 run: P1.2
14+
*
15+
* To change the target value just enter a number in the terminal:
16+
* For example: - to change the target value to -0.1453 enter: -0.1453
17+
* - to get the current target value enter: V3
18+
*
19+
* List of commands:
20+
* - P: velocity PI controller P gain
21+
* - I: velocity PI controller I gain
22+
* - L: velocity PI controller voltage limit
23+
* - R: velocity PI controller voltage ramp
24+
* - F: velocity Low pass filter time constant
25+
* - K: angle P controller P gain
26+
* - N: angle P controller velocity limit
27+
* - C: control loop
28+
* - 0: voltage
29+
* - 1: velocity
30+
* - 2: angle
31+
* - V: get motor variables
32+
* - 0: currently set voltage
33+
* - 1: current velocity
34+
* - 2: current angle
35+
* - 3: current target value
36+
*
37+
*/
38+
#include <SimpleFOC.h>
39+
// software interrupt library
40+
#include <PciManager.h>
41+
#include <PciListenerImp.h>
42+
43+
// motor instance
44+
BLDCMotor motor = BLDCMotor(9, 10, 11, 11, 7);
45+
46+
// hall sensor instance
47+
HallSensor sensor = HallSensor(2, 3, 4, 11);
48+
49+
// Interrupt routine intialisation
50+
// channel A, B and C callbacks
51+
void doA(){sensor.handleA();}
52+
void doB(){sensor.handleB();}
53+
void doC(){sensor.handleC();}
54+
// If no available hadware interrupt pins use the software interrupt
55+
PciListenerImp listenC(sensor.pinC, doC);
56+
57+
void setup() {
58+
59+
// initialize encoder sensor hardware
60+
sensor.init();
61+
sensor.enableInterrupts(doA, doB); //, doC);
62+
// software interrupts
63+
PciManager.registerListener(&listenC);
64+
65+
// link the motor to the sensor
66+
motor.linkSensor(&sensor);
67+
68+
// choose FOC modulation
69+
motor.foc_modulation = FOCModulationType::SpaceVectorPWM;
70+
71+
// power supply voltage [V]
72+
motor.voltage_power_supply = 12;
73+
74+
// set control loop type to be used
75+
motor.controller = ControlType::voltage;
76+
77+
// contoller configuration based on the controll type
78+
motor.PI_velocity.P = 0.2;
79+
motor.PI_velocity.I = 20;
80+
// default voltage_power_supply
81+
motor.PI_velocity.voltage_limit = 12;
82+
83+
// velocity low pass filtering time constant
84+
motor.LPF_velocity.Tf = 0.01;
85+
86+
// angle loop controller
87+
motor.P_angle.P = 20;
88+
// angle loop velocity limit
89+
motor.P_angle.velocity_limit = 50;
90+
91+
// use monitoring with serial for motor init
92+
// monitoring port
93+
Serial.begin(115200);
94+
// comment out if not needed
95+
motor.useMonitoring(Serial);
96+
97+
// initialise motor
98+
motor.init();
99+
// align encoder and start FOC
100+
motor.initFOC();
101+
102+
// set the inital target value
103+
motor.target = 2;
104+
105+
106+
// Run user commands to configure and the motor (find the full command list in docs.simplefoc.com)
107+
Serial.println("Motor commands sketch | Initial motion control > torque/voltage : target 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+
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@
3737
*/
3838
#include <SimpleFOC.h>
3939

40-
// SPI magnetic sensor instance
40+
// magnetic sensor instance - SPI
4141
MagneticSensorSPI sensor = MagneticSensorSPI(10, 16384, 0x3FFF);
42-
// I2C magnetic sensor instance
42+
// magnetic sensor instance - I2C
4343
//MagneticSensorI2C sensor = MagneticSensorI2C(0x36, 12, 0x0E, 4);
44+
// magnetic sensor instance - analog output
45+
// MagneticSensorAnalog sensor = MagneticSensorAnalog(A1, 14, 1020);
4446

4547
// motor instance
4648
BLDCMotor motor = BLDCMotor(9, 5, 6, 11, 8);

examples/utils/find_pole_pair_number/magnetic_sensor/find_pole_pairs_number/find_pole_pairs_number.ino

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
// its not important how many pole pairs do you set, the progam will find it alone
2020
BLDCMotor motor = BLDCMotor(9, 5, 6, 0, 8);
2121

22-
// magnetic sensor instance
23-
MagneticSensorSPI sensor = MagneticSensorSPI(10, 16384, 0x3FFF);
24-
// magnetic sensor instance
22+
// magnetic sensor instance - SPI
23+
MagneticSensorSPI sensor = MagneticSensorSPI(10, 14, 0x3FFF);
24+
// magnetic sensor instance - I2C
2525
//MagneticSensorI2C sensor = MagneticSensorI2C(0x36, 12, 0x0E, 4);
26+
// magnetic sensor instance - analog output
27+
// MagneticSensorAnalog sensor = MagneticSensorAnalog(A1, 14, 1020);
2628

2729
void setup() {
2830

0 commit comments

Comments
 (0)