|
| 1 | +#include "SimpleFOC.h" |
| 2 | + |
| 3 | +// BLDCMotor( int phA, int phB, int phC, int pp, int en) |
| 4 | +// - phA, phB, phC - motor A,B,C phase pwm pins |
| 5 | +// - pp - pole pair number |
| 6 | +// - enable pin - (optional input) |
| 7 | +BLDCMotor motor = BLDCMotor(9, 10, 11, 11); |
| 8 | + |
| 9 | +//Encoder(int encA, int encB , int cpr, int index) |
| 10 | +// - encA, encB - encoder A and B pins |
| 11 | +// - ppr - impulses per rotation (cpr=ppr*4) |
| 12 | +// - index pin - (optional input) |
| 13 | +Encoder encoder = Encoder(2, 3, 2048); |
| 14 | +// interrupt ruotine intialisation |
| 15 | +void doA(){encoder.handleA();} |
| 16 | +void doB(){encoder.handleB();} |
| 17 | + |
| 18 | +void setup() { |
| 19 | + // debugging port |
| 20 | + Serial.begin(115200); |
| 21 | + |
| 22 | + // initialise encoder sensor hardware |
| 23 | + encoder.init(); |
| 24 | + encoder.enableInterrupts(doA,doB); |
| 25 | + |
| 26 | + // power supply voltage |
| 27 | + // default 12V |
| 28 | + motor.voltage_power_supply = 12; |
| 29 | + |
| 30 | + // set control loop type to be used |
| 31 | + // ControlType::voltage |
| 32 | + // ControlType::velocity |
| 33 | + // ControlType::angle |
| 34 | + motor.controller = ControlType::angle; |
| 35 | + |
| 36 | + // contoller configuration based on the controll type |
| 37 | + // velocity PI controller parameters |
| 38 | + motor.PI_velocity.P = 0.2; |
| 39 | + motor.PI_velocity.I = 20; |
| 40 | + //defualt voltage_power_supply/2 |
| 41 | + motor.PI_velocity.voltage_limit = 6; |
| 42 | + // jerk control using voltage voltage ramp |
| 43 | + // default value is 300 volts per sec ~ 0.3V per millisecond |
| 44 | + motor.PI_velocity.voltage_ramp = 1000; |
| 45 | + |
| 46 | + // velocity low pass filtering |
| 47 | + // default 10ms - try different values to see what is the best. |
| 48 | + // the lower the less filtered |
| 49 | + motor.LPF_velocity.Tf = 0.0; |
| 50 | + |
| 51 | + // angle loop controller |
| 52 | + motor.P_angle.P = 3; |
| 53 | + motor.P_angle.velocity_limit = 10; |
| 54 | + |
| 55 | + // link the motor to the sensor |
| 56 | + motor.linkSensor(&encoder); |
| 57 | + |
| 58 | + // use debugging with serial for motor init |
| 59 | + // comment out if not needed |
| 60 | + motor.useDebugging(Serial); |
| 61 | + |
| 62 | + // intialise motor |
| 63 | + motor.init(); |
| 64 | + // align encoder and start FOC |
| 65 | + motor.initFOC(); |
| 66 | + |
| 67 | + // this serial print takes around 20% of arduino memory! |
| 68 | + Serial.println("\n\n"); |
| 69 | + Serial.println("PI controller parameters change:"); |
| 70 | + Serial.println("- P value : Prefix P (ex. P0.1)"); |
| 71 | + Serial.println("- I value : Prefix I (ex. I0.1)\n"); |
| 72 | + Serial.println("Velocity filter:"); |
| 73 | + Serial.println("- Tf value : Prefix F (ex. F0.001)\n"); |
| 74 | + Serial.println("Average loop execution time:"); |
| 75 | + Serial.println("- Type T\n"); |
| 76 | + Serial.println("Control loop type:"); |
| 77 | + Serial.println("- C0 - angle control"); |
| 78 | + Serial.println("- C1 - velocity control"); |
| 79 | + Serial.println("- C2 - voltage control\n"); |
| 80 | + Serial.println("Initial parameters:"); |
| 81 | + Serial.print("PI velocity P: "); |
| 82 | + Serial.print(motor.PI_velocity.P); |
| 83 | + Serial.print(",\t I: "); |
| 84 | + Serial.print(motor.PI_velocity.I); |
| 85 | + Serial.print(",\t Low passs filter Tf: "); |
| 86 | + Serial.println(motor.LPF_velocity.Tf,4); |
| 87 | + |
| 88 | + _delay(1000); |
| 89 | +} |
| 90 | + |
| 91 | +// target velocity variable |
| 92 | +float target = 0; |
| 93 | +// loop stats variables |
| 94 | +unsigned long t = 0; |
| 95 | +long timestamp = _micros(); |
| 96 | + |
| 97 | +void loop() { |
| 98 | + // iterative setting FOC pahse voltage |
| 99 | + motor.loopFOC(); |
| 100 | + |
| 101 | + // iterative function setting the outter loop target |
| 102 | + // velocity, position or voltage |
| 103 | + motor.move(target); |
| 104 | + |
| 105 | + // keep track of loop number |
| 106 | + t++; |
| 107 | +} |
| 108 | + |
| 109 | +// Serial communication callback |
| 110 | +void serialEvent() { |
| 111 | + // a string to hold incoming data |
| 112 | + static String inputString; |
| 113 | + while (Serial.available()) { |
| 114 | + // get the new byte: |
| 115 | + char inChar = (char)Serial.read(); |
| 116 | + // add it to the inputString: |
| 117 | + inputString += inChar; |
| 118 | + // if the incoming character is a newline |
| 119 | + // end of input |
| 120 | + if (inChar == '\n') { |
| 121 | + if(inputString.charAt(0) == 'P'){ |
| 122 | + motor.PI_velocity.P = inputString.substring(1).toFloat(); |
| 123 | + Serial.print("PI velocity P: "); |
| 124 | + Serial.print(motor.PI_velocity.P); |
| 125 | + Serial.print(",\t I: "); |
| 126 | + Serial.print(motor.PI_velocity.I); |
| 127 | + Serial.print(",\t Low passs filter Tf: "); |
| 128 | + Serial.println(motor.LPF_velocity.Tf,4); |
| 129 | + }else if(inputString.charAt(0) == 'I'){ |
| 130 | + motor.PI_velocity.I = inputString.substring(1).toFloat(); |
| 131 | + Serial.print("PI velocity P: "); |
| 132 | + Serial.print(motor.PI_velocity.P); |
| 133 | + Serial.print(",\t I: "); |
| 134 | + Serial.print(motor.PI_velocity.I); |
| 135 | + Serial.print(",\t Low passs filter Tf: "); |
| 136 | + Serial.println(motor.LPF_velocity.Tf,4); |
| 137 | + }else if(inputString.charAt(0) == 'F'){ |
| 138 | + motor.LPF_velocity.Tf = inputString.substring(1).toFloat(); |
| 139 | + Serial.print("PI velocity P: "); |
| 140 | + Serial.print(motor.PI_velocity.P); |
| 141 | + Serial.print(",\t I: "); |
| 142 | + Serial.print(motor.PI_velocity.I); |
| 143 | + Serial.print(",\t Low passs filter Tf: "); |
| 144 | + Serial.println(motor.LPF_velocity.Tf,4); |
| 145 | + }else if(inputString.charAt(0) == 'T'){ |
| 146 | + Serial.print("Average loop time is (microseconds): "); |
| 147 | + Serial.println((_micros() - timestamp)/t); |
| 148 | + t = 0; |
| 149 | + timestamp = _micros(); |
| 150 | + }else if(inputString.charAt(0) == 'C'){ |
| 151 | + Serial.print("Contoller type: "); |
| 152 | + int cnt = inputString.substring(1).toFloat(); |
| 153 | + if(cnt == 0){ |
| 154 | + Serial.println("angle!"); |
| 155 | + motor.controller = ControlType::angle; |
| 156 | + }else if(cnt == 1){ |
| 157 | + Serial.println("velocity!"); |
| 158 | + motor.controller = ControlType::velocity; |
| 159 | + }else if(cnt == 2){ |
| 160 | + Serial.println("volatge!"); |
| 161 | + motor.controller = ControlType::voltage; |
| 162 | + } |
| 163 | + Serial.println(); |
| 164 | + t = 0; |
| 165 | + timestamp = _micros(); |
| 166 | + }else{ |
| 167 | + target = inputString.toFloat(); |
| 168 | + Serial.print("Target : "); |
| 169 | + Serial.println(target); |
| 170 | + inputString = ""; |
| 171 | + } |
| 172 | + inputString = ""; |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments