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-
7692void 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