Skip to content

Commit ae934b7

Browse files
committed
FEAT added initial support for open-loop motor control
1 parent 9e7ed9e commit ae934b7

File tree

7 files changed

+173
-38
lines changed

7 files changed

+173
-38
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Open loop motor control example
2+
#include <SimpleFOC.h>
3+
4+
// motor instance
5+
BLDCMotor motor = BLDCMotor(3, 10, 6, 11, 7);
6+
7+
void setup() {
8+
9+
// power supply voltage
10+
// default 12V
11+
motor.voltage_power_supply = 12;
12+
13+
// init motor hardware
14+
motor.init();
15+
16+
Serial.begin(115200);
17+
Serial.println("Motor ready!");
18+
_delay(1000);
19+
}
20+
21+
float target_position = 0; // rad/s
22+
float target_velocity= 10; // rad/s
23+
float target_voltage = 3; // V
24+
25+
void loop() {
26+
// set angle in open loop
27+
// - angle - rad
28+
// - velocity - rad/s
29+
// - voltage - V
30+
motor.angleOpenloop(target_position, target_velocity, target_voltage);
31+
32+
// a bit of delay
33+
_delay(1);
34+
35+
// receive the used commands from serial
36+
serialReceiveUserCommand();
37+
}
38+
39+
// utility function enabling serial communication with the user to set the target values
40+
// this function can be implemented in serialEvent function as well
41+
void serialReceiveUserCommand() {
42+
43+
// a string to hold incoming data
44+
static String received_chars;
45+
46+
while (Serial.available()) {
47+
// get the new byte:
48+
char inChar = (char)Serial.read();
49+
// add it to the string buffer:
50+
received_chars += inChar;
51+
// end of user input
52+
if (inChar == '\n') {
53+
54+
// change the motor target
55+
target_position = received_chars.toFloat();
56+
Serial.print("Target position: ");
57+
Serial.println(target_position);
58+
59+
// reset the command buffer
60+
received_chars = "";
61+
}
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Open loop motor control example
2+
#include <SimpleFOC.h>
3+
4+
// motor instance
5+
BLDCMotor motor = BLDCMotor(3, 10, 6, 11, 7);
6+
7+
void setup() {
8+
9+
// power supply voltage
10+
// default 12V
11+
motor.voltage_power_supply = 12;
12+
13+
// init motor hardware
14+
motor.init();
15+
16+
Serial.begin(115200);
17+
Serial.println("Motor ready!");
18+
_delay(1000);
19+
}
20+
21+
float target_velocity= 2; // rad/s
22+
float target_voltage = 3; // V
23+
void loop() {
24+
// set velocity in open loop
25+
// - velocity - rad/s
26+
// - voltage - V
27+
motor.velocityOpenloop(target_velocity, target_voltage);
28+
29+
// a bit of delay
30+
_delay(1);
31+
32+
// receive the used commands from serial
33+
serialReceiveUserCommand();
34+
}
35+
36+
// utility function enabling serial communication with the user to set the target values
37+
// this function can be implemented in serialEvent function as well
38+
void serialReceiveUserCommand() {
39+
40+
// a string to hold incoming data
41+
static String received_chars;
42+
43+
while (Serial.available()) {
44+
// get the new byte:
45+
char inChar = (char)Serial.read();
46+
// add it to the string buffer:
47+
received_chars += inChar;
48+
// end of user input
49+
if (inChar == '\n') {
50+
51+
// change the motor target
52+
target_velocity = received_chars.toFloat();
53+
Serial.print("Target velocity: ");
54+
Serial.println(target_velocity);
55+
56+
// reset the command buffer
57+
received_chars = "";
58+
}
59+
}
60+
}

examples/utils/open_loop_motor_test/open_loop_motor_test.ino

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

keywords.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ initAbsoluteZero KEYWORD2
4646
hasAbsoluteZero KEYWORD2
4747
needsAbsoluteZeroSearch KEYWORD2
4848
useMonitoring KEYWORD2
49+
angleOpenloop KEYWORD2
50+
velocityOpenloop KEYWORD2
4951

5052
voltage_q KEYWORD2
5153
shaft_angle_sp KEYWORD2

src/BLDCMotor.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,4 +612,36 @@ int BLDCMotor::command(String user_command) {
612612
}
613613
// return 0 if error and 1 if ok
614614
return errorFlag;
615+
}
616+
617+
618+
// set velocity in open loop
619+
// - velocity - rad/s
620+
// - voltage - V
621+
void BLDCMotor::velocityOpenloop(float vel, float voltage){
622+
float Ts = (_micros() - open_loop_timestamp) * 1e-6;
623+
624+
shaft_angle += vel*Ts;
625+
626+
setPhaseVoltage(voltage, electricAngle(shaft_angle));
627+
628+
open_loop_timestamp = _micros();
629+
}
630+
631+
// set angle in open loop
632+
// - angle - rad
633+
// - velocity - rad/s
634+
// - voltage - V
635+
void BLDCMotor::angleOpenloop(float angle, float vel, float voltage){
636+
float Ts = (_micros() - open_loop_timestamp) * 1e-6;
637+
638+
if(abs(angle- shaft_angle) > abs(vel*Ts)){
639+
shaft_angle += _sign(angle - shaft_angle) * abs(vel)*Ts;
640+
}else{
641+
shaft_angle = angle;
642+
}
643+
644+
setPhaseVoltage(voltage, electricAngle(shaft_angle));
645+
646+
open_loop_timestamp = _micros();
615647
}

src/BLDCMotor.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
enum ControlType{
2121
voltage,//!< Torque control using voltage
2222
velocity,//!< Velocity motion control
23-
angle//!< Position/angle motion control
23+
angle,//!< Position/angle motion control
2424
};
2525

2626
/**
@@ -121,7 +121,17 @@ class BLDCMotor
121121
* This function doesn't need to be run upon each loop execution - depends of the use case
122122
*/
123123
void move(float target = NOT_SET);
124+
125+
// set velocity in open loop
126+
// - velocity - rad/s
127+
// - voltage - V
128+
void velocityOpenloop(float vel, float voltage);
124129

130+
// set angle in open loop
131+
// - angle - rad
132+
// - velocity - rad/s
133+
// - voltage - V
134+
void angleOpenloop(float angle, float vel, float voltage);
125135

126136
// hardware variables
127137
int pwmA; //!< phase A pwm pin number
@@ -284,6 +294,10 @@ class BLDCMotor
284294
// phase voltages
285295
float Ualpha,Ubeta; //!< Phase voltages U alpha and U beta used for inverse Park and Clarke transform
286296

297+
298+
// open loop variables
299+
long open_loop_timestamp;
300+
287301
};
288302

289303

src/FOCutils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#endif
1414

1515
// sign function
16-
#define sign(a) ( ( (a) < 0 ) ? -1 : ( (a) > 0 ) )
16+
#define _sign(a) ( ( (a) < 0 ) ? -1 : ( (a) > 0 ) )
1717
#define _round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
1818

1919
// utility defines

0 commit comments

Comments
 (0)