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-
107// BLDCMotor( int phA, int phB, int phC, int pp, int en)
118// - phA, phB, phC - motor A,B,C phase pwm pins
129// - pp - pole pair number
1310// - enable pin - (optional input)
14- BLDCMotor motor = BLDCMotor(9 , 10 , 11 , 11 , 8 );
11+ BLDCMotor motor = BLDCMotor(9 , 5 , 6 , 11 , 8 );
1512// Encoder(int encA, int encB , int cpr, int index)
1613// - encA, encB - encoder A and B pins
1714// - ppr - impulses per rotation (cpr=ppr*4)
18- // - index pin - (optional input)
19- Encoder encoder = Encoder(arduinoInt1, arduinoInt2, 8192 , 4 );
20- // interrupt ruotine intialisation
15+ // - index pin - (optional input)
16+ Encoder encoder = Encoder(arduinoInt1, arduinoInt2, 8192 , A0);
17+
18+ // Interrupt rutine intialisation
19+ // channel A and B callbacks
2120void doA (){encoder.handleA ();}
2221void doB (){encoder.handleB ();}
22+ // index calback interrupt code
23+ // please set the right PCINT(0,1,2)_vect parameter
24+ // PCINT0_vect - index pin in between D8 and D13
25+ // PCINT1_vect - index pin in between A0 and A5 (recommended)
26+ // PCINT2_vect - index pin in between D0 and D7
27+ ISR (PCINT1_vect) { encoder.handleIndex (); }
2328
2429void setup () {
2530 // debugging port
@@ -40,15 +45,18 @@ void setup() {
4045
4146 // power supply voltage
4247 // default 12V
43- motor.power_supply_voltage = 12 ;
48+ motor.voltage_power_supply = 12 ;
4449
4550 // index search velocity - default 1rad/s
4651 motor.index_search_velocity = 1 ;
4752 // index search PI contoller parameters
4853 // default K=0.5 Ti = 0.01
49- motor.PI_velocity_index_search .K = 0.3 ;
54+ motor.PI_velocity_index_search .K = 0.1 ;
5055 motor.PI_velocity_index_search .Ti = 0.01 ;
51- motor.PI_velocity_index_search .u_limit = 3 ;
56+ // motor.PI_velocity_index_search.voltage_limit = 3;
57+ // jerk control using voltage voltage ramp
58+ // default value is 100
59+ motor.PI_velocity_index_search .voltage_ramp = 100 ;
5260
5361
5462 // set FOC loop to be used
@@ -58,12 +66,18 @@ void setup() {
5866 // ControlType::angle
5967 motor.controller = ControlType::angle;
6068
69+
70+ // contoller configuration based on the controll type
6171 // velocity PI controller parameters
62- // default K=0.5 Ti = 0.01
72+ // default K=1.0 Ti = 0.003
6373 motor.PI_velocity .K = 0.3 ;
64- motor.PI_velocity .Ti = 0.007 ;
65- motor.PI_velocity .u_limit = 5 ;
66-
74+ motor.PI_velocity .Ti = 0.003 ;
75+ // defualt voltage_power_supply/2
76+ motor.PI_velocity .voltage_limit = 6 ;
77+ // jerk control using voltage voltage ramp
78+ // default value is 300 volts per sec ~ 0.3V per millisecond
79+ motor.PI_velocity .voltage_ramp = 300 ;
80+
6781 // angle P controller
6882 // default K=70
6983 motor.P_angle .K = 20 ;
@@ -89,6 +103,9 @@ void setup() {
89103 _delay (1000 );
90104}
91105
106+ // angle set point variable
107+ float target_angle = 0 ;
108+
92109void loop () {
93110 // iterative state calculation calculating angle
94111 // and setting FOC pahse voltage
@@ -131,6 +148,8 @@ void motor_monitor() {
131148 case ControlType::voltage:
132149 Serial.print (motor.voltage_q );
133150 Serial.print (" \t " );
151+ Serial.print (motor.shaft_angle );
152+ Serial.print (" \t " );
134153 Serial.println (motor.shaft_velocity );
135154 break ;
136155 }
0 commit comments