Skip to content

Commit bd9973c

Browse files
committed
fead refactored examples
1 parent 27f5215 commit bd9973c

File tree

35 files changed

+911
-673
lines changed

35 files changed

+911
-673
lines changed

examples/hardware_specific_examples/Bluepill_examples/encoder/bluepill_position_control/bluepill_position_control.ino

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ void doA(){encoder.handleA();}
2323
void doB(){encoder.handleB();}
2424
void doI(){encoder.handleIndex();}
2525

26+
27+
// angle set point variable
28+
float target_angle = 0;
29+
// instantiate the commander
30+
Commander command = Commander(Serial);
31+
void doTarget(char* cmd) { command.variable(&target_angle, cmd); }
32+
33+
2634
void setup() {
2735

2836
// initialize encoder sensor hardware
@@ -77,15 +85,14 @@ void setup() {
7785
// align encoder and start FOC
7886
motor.initFOC();
7987

88+
// add target command T
89+
command.add('T', doTarget);
8090

8191
Serial.println(F("Motor ready."));
8292
Serial.println(F(("Set the target angle using serial terminal:"));
8393
_delay(1000);
8494
}
8595

86-
// angle set point variable
87-
float target_angle = 0;
88-
8996
void loop() {
9097
// main FOC algorithm function
9198
// the faster you run this function the better
@@ -104,31 +111,5 @@ void loop() {
104111
// motor.monitor();
105112

106113
// user communication
107-
serialReceiveUserCommand();
108-
}
109-
110-
// utility function enabling serial communication with the user to set the target values
111-
// this function can be implemented in serialEvent function as well
112-
void serialReceiveUserCommand() {
113-
114-
// a string to hold incoming data
115-
static String received_chars;
116-
117-
while (Serial.available()) {
118-
// get the new byte:
119-
char inChar = (char)Serial.read();
120-
// add it to the string buffer:
121-
received_chars += inChar;
122-
// end of user input
123-
if (inChar == '\n') {
124-
125-
// change the motor target
126-
target_angle = received_chars.toFloat();
127-
Serial.print("Target angle: ");
128-
Serial.println(target_angle);
129-
130-
// reset the command buffer
131-
received_chars = "";
132-
}
133-
}
114+
command.run();
134115
}

examples/hardware_specific_examples/Bluepill_examples/magnetic_sensor/bluepill_position_control/bluepill_position_control.ino

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ BLDCDriver3PWM driver = BLDCDriver3PWM(PB6, PB7, PB8, PB5);
2626
// BLDCDriver6PWM(IN1_H, IN1_L, IN2_H, IN2_L, IN3_H, IN3_L, enable(optional))
2727
//BLDCDriver6PWM driver = BLDCDriver6PWM(PA8, PB13, PA9, PB14, PA10, PB15, PB12);
2828

29+
30+
// angle set point variable
31+
float target_angle = 0;
32+
// instantiate the commander
33+
Commander command = Commander(Serial);
34+
void doTarget(char* cmd) { command.variable(&target_angle, cmd); }
35+
36+
2937
void setup() {
3038

3139
// initialise magnetic sensor hardware
@@ -75,6 +83,8 @@ void setup() {
7583
// align sensor and start FOC
7684
motor.initFOC();
7785

86+
// add target command T
87+
command.add('T', doTarget);
7888

7989
Serial.println(F("Motor ready."));
8090
Serial.println(F("Set the target angle using serial terminal:"));
@@ -104,33 +114,5 @@ void loop() {
104114
// motor.monitor();
105115

106116
// user communication
107-
serialReceiveUserCommand();
108-
}
109-
110-
// utility function enabling serial communication with the user to set the target values
111-
// this function can be implemented in serialEvent function as well
112-
void serialReceiveUserCommand() {
113-
114-
// a string to hold incoming data
115-
static String received_chars;
116-
117-
while (Serial.available()) {
118-
// get the new byte:
119-
char inChar = (char)Serial.read();
120-
// add it to the string buffer:
121-
received_chars += inChar;
122-
// end of user input
123-
if (inChar == '\n') {
124-
125-
// change the motor target
126-
target_angle = received_chars.toFloat();
127-
Serial.print("Target angle: ");
128-
Serial.println(target_angle);
129-
130-
// reset the command buffer
131-
received_chars = "";
132-
}
133-
}
134-
}
135-
136-
117+
command.run();
118+
}

examples/hardware_specific_examples/ESP32/encoder/esp32_position_control/esp32_position_control.ino

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ Encoder encoder = Encoder(4, 2, 1024);
1616
void doA(){encoder.handleA();}
1717
void doB(){encoder.handleB();}
1818

19+
// angle set point variable
20+
float target_angle = 0;
21+
// instantiate the commander
22+
Commander command = Commander(Serial);
23+
void doTarget(char* cmd) { command.variable(&target_angle, cmd); }
24+
1925
void setup() {
2026

2127
// initialize encoder sensor hardware
@@ -71,6 +77,8 @@ void setup() {
7177
// align encoder and start FOC
7278
motor.initFOC();
7379

80+
// add target command T
81+
command.add('T', doTarget);
7482

7583
Serial.println(F("Motor ready."));
7684
Serial.println(F("Set the target angle using serial terminal:"));
@@ -98,31 +106,5 @@ void loop() {
98106
// motor.monitor();
99107

100108
// user communication
101-
serialReceiveUserCommand();
102-
}
103-
104-
// utility function enabling serial communication with the user to set the target values
105-
// this function can be implemented in serialEvent function as well
106-
void serialReceiveUserCommand() {
107-
108-
// a string to hold incoming data
109-
static String received_chars;
110-
111-
while (Serial.available()) {
112-
// get the new byte:
113-
char inChar = (char)Serial.read();
114-
// add it to the string buffer:
115-
received_chars += inChar;
116-
// end of user input
117-
if (inChar == '\n') {
118-
119-
// change the motor target
120-
target_angle = received_chars.toFloat();
121-
Serial.print("Target angle: ");
122-
Serial.println(target_angle);
123-
124-
// reset the command buffer
125-
received_chars = "";
126-
}
127-
}
109+
command.run();
128110
}

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

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ MagneticSensorSPI sensor = MagneticSensorSPI(AS5147_SPI, 10);
2424
BLDCMotor motor = BLDCMotor(11);
2525
BLDCDriver3PWM driver = BLDCDriver3PWM(25, 26, 27, 7);
2626

27+
28+
// angle set point variable
29+
float target_angle = 0;
30+
// instantiate the commander
31+
Commander command = Commander(Serial);
32+
void doTarget(char* cmd) { command.variable(&target_angle, cmd); }
33+
2734
void setup() {
2835

2936
// initialise magnetic sensor hardware
@@ -73,6 +80,8 @@ void setup() {
7380
// align sensor and start FOC
7481
motor.initFOC();
7582

83+
// add target command T
84+
command.add('T', doTarget);
7685

7786
Serial.println(F("Motor ready."));
7887
Serial.println(F("Set the target angle using serial terminal:"));
@@ -102,33 +111,5 @@ void loop() {
102111
// motor.monitor();
103112

104113
// user communication
105-
serialReceiveUserCommand();
106-
}
107-
108-
// utility function enabling serial communication with the user to set the target values
109-
// this function can be implemented in serialEvent function as well
110-
void serialReceiveUserCommand() {
111-
112-
// a string to hold incoming data
113-
static String received_chars;
114-
115-
while (Serial.available()) {
116-
// get the new byte:
117-
char inChar = (char)Serial.read();
118-
// add it to the string buffer:
119-
received_chars += inChar;
120-
// end of user input
121-
if (inChar == '\n') {
122-
123-
// change the motor target
124-
target_angle = received_chars.toFloat();
125-
Serial.print("Target angle: ");
126-
Serial.println(target_angle);
127-
128-
// reset the command buffer
129-
received_chars = "";
130-
}
131-
}
132-
}
133-
134-
114+
command.run();
115+
}

examples/hardware_specific_examples/HMBGC_example/position_control/position_control.ino

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ void doB(){encoder.handleB();}
3333
PciListenerImp listenerA(encoder.pinA, doA);
3434
PciListenerImp listenerB(encoder.pinB, doB);
3535

36+
37+
// angle set point variable
38+
float target_angle = 0;
39+
// instantiate the commander
40+
Commander command = Commander(Serial);
41+
void doTarget(char* cmd) { command.variable(&target_angle, cmd); }
42+
3643
void setup() {
3744

3845
// initialise encoder hardware
@@ -89,14 +96,14 @@ void setup() {
8996
// align encoder and start FOC
9097
motor.initFOC();
9198

99+
// add target command T
100+
command.add('T', doTarget);
92101

93102
Serial.println(F("Motor ready."));
94103
Serial.println(F("Set the target angle using serial terminal:"));
95104
_delay(1000);
96105
}
97106

98-
// angle set point variable
99-
float target_angle = 0;
100107

101108
void loop() {
102109
// main FOC algorithm function
@@ -116,31 +123,5 @@ void loop() {
116123
// motor.monitor();
117124

118125
// user communication
119-
serialReceiveUserCommand();
120-
}
121-
122-
// utility function enabling serial communication with the user to set the target values
123-
// this function can be implemented in serialEvent function as well
124-
void serialReceiveUserCommand() {
125-
126-
// a string to hold incoming data
127-
static String received_chars;
128-
129-
while (Serial.available()) {
130-
// get the new byte:
131-
char inChar = (char)Serial.read();
132-
// add it to the string buffer:
133-
received_chars += inChar;
134-
// end of user input
135-
if (inChar == '\n') {
136-
137-
// change the motor target
138-
target_angle = received_chars.toFloat();
139-
Serial.print("Target angle: ");
140-
Serial.println(target_angle);
141-
142-
// reset the command buffer
143-
received_chars = "";
144-
}
145-
}
126+
command.run();
146127
}

examples/hardware_specific_examples/HMBGC_example/voltage_control/voltage_control.ino

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ void doB(){encoder.handleB();}
3939
PciListenerImp listenerA(encoder.pinA, doA);
4040
PciListenerImp listenerB(encoder.pinB, doB);
4141

42+
43+
// voltage set point variable
44+
float target_voltage = 2;
45+
// instantiate the commander
46+
Commander command = Commander(Serial);
47+
void doTarget(char* cmd) { command.variable(&target_voltage, cmd); }
48+
4249
void setup() {
4350

4451
// initialize encoder sensor hardware
@@ -75,15 +82,15 @@ void setup() {
7582
motor.init();
7683
// align sensor and start FOC
7784
motor.initFOC();
85+
86+
// add target command T
87+
command.add('T', doTarget);
7888

7989
Serial.println(F("Motor ready."));
8090
Serial.println(F("Set the target voltage using serial terminal:"));
8191
_delay(1000);
8292
}
8393

84-
// target voltage to be set to the motor
85-
float target_voltage = 2;
86-
8794
void loop() {
8895

8996
// main FOC algorithm function
@@ -98,33 +105,6 @@ void loop() {
98105
// You can also use motor.move() and set the motor.target in the code
99106
motor.move(target_voltage);
100107

101-
// communicate with the user
102-
serialReceiveUserCommand();
103-
}
104-
105-
106-
// utility function enabling serial communication with the user to set the target values
107-
// this function can be implemented in serialEvent function as well
108-
void serialReceiveUserCommand() {
109-
110-
// a string to hold incoming data
111-
static String received_chars;
112-
113-
while (Serial.available()) {
114-
// get the new byte:
115-
char inChar = (char)Serial.read();
116-
// add it to the string buffer:
117-
received_chars += inChar;
118-
// end of user input
119-
if (inChar == '\n') {
120-
121-
// change the motor target
122-
target_voltage = received_chars.toFloat();
123-
Serial.print("Target voltage: ");
124-
Serial.println(target_voltage);
125-
126-
// reset the command buffer
127-
received_chars = "";
128-
}
129-
}
108+
// user communication
109+
command.run();
130110
}

0 commit comments

Comments
 (0)