Skip to content

Commit 29bae8f

Browse files
committed
Merge branch 'dev'
2 parents 2066449 + 5934762 commit 29bae8f

File tree

19 files changed

+839
-607
lines changed

19 files changed

+839
-607
lines changed

README.md

Lines changed: 105 additions & 44 deletions
Large diffs are not rendered by default.

examples/HMBGC_example/HMBGC_example.ino

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ BLDCMotor motor = BLDCMotor(9, 10, 11, 11);
1212
// - encA, encB - encoder A and B pins
1313
// - ppr - impulses per rotation (cpr=ppr*4)
1414
// - index pin - (optional input)
15-
Encoder encoder = Encoder(A1, A0, 600);
15+
Encoder encoder = Encoder(A0, A1, 8192);
1616
// interrupt ruotine intialisation
1717
void doA(){encoder.handleA();}
1818
void doB(){encoder.handleB();}
@@ -25,22 +25,21 @@ void setup() {
2525
// debugging port
2626
Serial.begin(115200);
2727

28+
// by default for HMBGC
29+
encoder.quadrature = Quadrature::ENABLE;
30+
2831
// check if you need internal pullups
2932
// Pullup::EXTERN - external pullup added - dafault
3033
// Pullup::INTERN - needs internal arduino pullup
31-
encoder.pullup = Pullup::INTERN;
34+
encoder.pullup = Pullup::EXTERN;
35+
3236
// initialise encoder hardware
3337
encoder.init();
3438

3539
// interrupt intitialisation
3640
PciManager.registerListener(&listenerA);
3741
PciManager.registerListener(&listenerB);
3842

39-
// set driver type
40-
// DriverType::unipolar
41-
// DriverType::bipolar - default
42-
motor.driver = DriverType::unipolar;
43-
4443
// power supply voltage
4544
// default 12V
4645
motor.power_supply_voltage = 12;
@@ -54,8 +53,12 @@ void setup() {
5453

5554
// velocity PI controller parameters
5655
// default K=1.0 Ti = 0.003
57-
motor.PI_velocity.K = 1;
58-
motor.PI_velocity.Ti = 0.003;
56+
motor.PI_velocity.K = 0.3;
57+
motor.PI_velocity.Ti = 0.01;
58+
59+
// use debugging with serial for motor init
60+
// comment out if not needed
61+
motor.useDebugging(Serial);
5962

6063
// link the motor to the sensor
6164
motor.linkEncoder(&encoder);
@@ -66,10 +69,11 @@ void setup() {
6669
motor.initFOC();
6770

6871
Serial.println("Motor ready.");
69-
delay(1000);
72+
Serial.println("Set the target velocity using serial terminal:");
73+
_delay(1000);
7074
}
7175

72-
float velocity_sp;
76+
float target_velocity=0;
7377

7478
void loop() {
7579
// iterative state calculation calculating angle
@@ -80,18 +84,18 @@ void loop() {
8084
motor.loopFOC();
8185

8286
// 0.5 hertz sine weve
83-
velocity_sp = sin( micros()*1e-6 *2*M_PI * 0.5 );
87+
//target_velocity = sin( micros()*1e-6 *2*M_PI * 0.5 );
8488

8589
// iterative function setting the outter loop target
8690
// velocity, position or voltage
8791
// this funciton can be run at much lower frequency than loopFOC funciton
8892
// it can go as low as ~50Hz
89-
motor.move(velocity_sp);
93+
motor.move(target_velocity);
9094

9195

9296
// function intended to be used with serial plotter to monitor motor variables
9397
// significantly slowing the execution down!!!!
94-
motor_monitor();
98+
// motor_monitor();
9599
}
96100

97101
// utility function intended to be used with serial plotter to monitor motor variables
@@ -116,8 +120,31 @@ void motor_monitor() {
116120
case ControlType::voltage:
117121
Serial.print(motor.voltage_q);
118122
Serial.print("\t");
119-
Serial.println(motor.shaft_velocity);
123+
Serial.print(motor.shaft_velocity);
124+
Serial.print("\t");
125+
Serial.println(motor.shaft_angle);
120126
break;
121127
}
122128
}
123129

130+
131+
// Serial communication callback function
132+
// gets the target value from the user
133+
void serialEvent() {
134+
// a string to hold incoming data
135+
static String inputString;
136+
while (Serial.available()) {
137+
// get the new byte:
138+
char inChar = (char)Serial.read();
139+
// add it to the inputString:
140+
inputString += inChar;
141+
// if the incoming character is a newline
142+
// end of input
143+
if (inChar == '\n') {
144+
target_velocity = inputString.toFloat();
145+
Serial.print("Tagret velocity: ");
146+
Serial.println(target_velocity);
147+
inputString = "";
148+
}
149+
}
150+
}

examples/angle_control/angle_control.ino

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
#define arduinoInt1 2 // Arduino UNO interrupt 0
55
#define arduinoInt2 3 // Arduino UNO interrupt 1
66

7+
// angle set point variable
8+
float target_angle = 0;
9+
710
// BLDCMotor( int phA, int phB, int phC, int pp, int en)
811
// - phA, phB, phC - motor A,B,C phase pwm pins
912
// - pp - pole pair number
@@ -23,22 +26,31 @@ void setup() {
2326
Serial.begin(115200);
2427

2528
// check if you need internal pullups
26-
// Pullup::EXTERN - external pullup added - dafault
29+
// Quadrature::ENABLE - CPR = 4xPPR - default
30+
// Quadrature::DISABLE - CPR = PPR
31+
encoder.quadrature = Quadrature::ENABLE;
32+
33+
// check if you need internal pullups
34+
// Pullup::EXTERN - external pullup added - dafault
2735
// Pullup::INTERN - needs internal arduino pullup
2836
encoder.pullup = Pullup::EXTERN;
29-
37+
3038
// initialise encoder hardware
3139
encoder.init(doA, doB);
3240

33-
// set driver type
34-
// DriverType::unipolar
35-
// DriverType::bipolar - default
36-
motor.driver = DriverType::bipolar;
37-
3841
// power supply voltage
3942
// default 12V
4043
motor.power_supply_voltage = 12;
4144

45+
// index search velocity - default 1rad/s
46+
motor.index_search_velocity = 1;
47+
// index search PI contoller parameters
48+
// default K=0.5 Ti = 0.01
49+
motor.PI_velocity_index_search.K = 0.3;
50+
motor.PI_velocity_index_search.Ti = 0.01;
51+
motor.PI_velocity_index_search.u_limit = 3;
52+
53+
4254
// set FOC loop to be used
4355
// ControlType::voltage
4456
// ControlType::velocity
@@ -47,45 +59,44 @@ void setup() {
4759
motor.controller = ControlType::angle;
4860

4961
// velocity PI controller parameters
50-
// default K=1.0 Ti = 0.003
51-
motor.PI_velocity.K = 0.5;
52-
motor.PI_velocity.Ti = 0.01;
62+
// default K=0.5 Ti = 0.01
63+
motor.PI_velocity.K = 0.3;
64+
motor.PI_velocity.Ti = 0.007;
65+
motor.PI_velocity.u_limit = 5;
66+
5367
// angle P controller
5468
// default K=70
5569
motor.P_angle.K = 20;
5670
// maximal velocity of the poisiiton control
5771
// default 20
58-
motor.P_angle.velocity_limit = 10;
72+
motor.P_angle.velocity_limit = 4;
73+
74+
// use debugging with serial for motor init
75+
// comment out if not needed
76+
motor.useDebugging(Serial);
5977

6078
// link the motor to the sensor
6179
motor.linkEncoder(&encoder);
62-
80+
6381
// intialise motor
6482
motor.init();
6583
// align encoder and start FOC
6684
motor.initFOC();
6785

6886

6987
Serial.println("Motor ready.");
70-
delay(1000);
88+
Serial.println("Set the target angle using serial terminal:");
89+
_delay(1000);
7190
}
7291

73-
// angle target variable
74-
float target_angle;
75-
7692
void loop() {
77-
7893
// iterative state calculation calculating angle
7994
// and setting FOC pahse voltage
8095
// the faster you run this funciton the better
8196
// in arduino loop it should have ~1kHz
8297
// the best would be to be in ~10kHz range
8398
motor.loopFOC();
8499

85-
86-
// 0.5 hertz sine wave
87-
target_angle = sin( micros()*1e-6 *2*M_PI * 0.5 );
88-
89100
// iterative function setting the outter loop target
90101
// velocity, position or voltage
91102
// this funciton can be run at much lower frequency than loopFOC funciton
@@ -95,7 +106,7 @@ void loop() {
95106

96107
// function intended to be used with serial plotter to monitor motor variables
97108
// significantly slowing the execution down!!!!
98-
motor_monitor();
109+
// motor_monitor();
99110
}
100111

101112
// utility function intended to be used with serial plotter to monitor motor variables
@@ -125,3 +136,24 @@ void motor_monitor() {
125136
}
126137
}
127138

139+
// Serial communication callback function
140+
// gets the target value from the user
141+
void serialEvent() {
142+
// a string to hold incoming data
143+
static String inputString;
144+
while (Serial.available()) {
145+
// get the new byte:
146+
char inChar = (char)Serial.read();
147+
// add it to the inputString:
148+
inputString += inChar;
149+
// if the incoming character is a newline
150+
// end of input
151+
if (inChar == '\n') {
152+
target_angle = inputString.toFloat();
153+
Serial.print("Tagret angle: ");
154+
Serial.println(target_angle);
155+
inputString = "";
156+
}
157+
}
158+
}
159+

0 commit comments

Comments
 (0)