Jochem Brouwer / Mbed 2 deprecated PID_controller_Motor2

Dependencies:   HIDScope MODSERIAL QEI biquadFilter mbed

Fork of PID_controller_Motor by BioBitches TN

Committer:
MBroek
Date:
Mon Oct 17 06:58:57 2016 +0000
Revision:
6:e0857842e8cd
Parent:
5:c510ab61af28
Child:
7:435f984781ab
Dit is de PID-controller ding, waarbij de PID-stuk wordt getest. Van het Biorobotic project.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MBroek 0:9e053ed05c69 1
MBroek 0:9e053ed05c69 2
MBroek 0:9e053ed05c69 3 // HET DEFINIEREN VAN ALLES ==========================================================================================
MBroek 0:9e053ed05c69 4
MBroek 0:9e053ed05c69 5 // Includen van alle libraries ---------------------------------------
MBroek 0:9e053ed05c69 6 #include "mbed.h"
MBroek 0:9e053ed05c69 7 #include "MODSERIAL.h"
MBroek 0:9e053ed05c69 8 #include "QEI.h"
MBroek 0:9e053ed05c69 9 #include "math.h"
MBroek 3:581c5918b590 10 #include "HIDScope.h"
MBroek 3:581c5918b590 11
MBroek 3:581c5918b590 12 // Definieren van de HIDscope ----------------------------------------
MBroek 4:6524b198721f 13 HIDScope scope(1);
MBroek 0:9e053ed05c69 14
MBroek 0:9e053ed05c69 15
MBroek 0:9e053ed05c69 16 // Definieren van de Serial en Encoder -------------------------------
MBroek 0:9e053ed05c69 17 MODSERIAL pc(USBTX, USBRX);
MBroek 0:9e053ed05c69 18 #define SERIALBAUD 115200
MBroek 0:9e053ed05c69 19
MBroek 0:9e053ed05c69 20 QEI encoder_motor1(D10,D11,NC,64);
MBroek 0:9e053ed05c69 21 QEI encoder_motor2(D12,D13,NC,64);
MBroek 0:9e053ed05c69 22
MBroek 0:9e053ed05c69 23
MBroek 0:9e053ed05c69 24 // Definieren van de Motorpins ---------------------------------------
MBroek 0:9e053ed05c69 25 DigitalOut motor1_direction_pin(D7);
MBroek 0:9e053ed05c69 26 PwmOut motor1_speed_pin(D6);
MBroek 0:9e053ed05c69 27
MBroek 0:9e053ed05c69 28 DigitalOut motor2_direction_pin(D4);
MBroek 0:9e053ed05c69 29 PwmOut motor2_speed_pin(D5);
MBroek 0:9e053ed05c69 30
MBroek 0:9e053ed05c69 31
MBroek 0:9e053ed05c69 32 //Definieren van bord-elementen --------------------------------------
MBroek 3:581c5918b590 33 InterruptIn kill_switch(D8);
MBroek 3:581c5918b590 34 InterruptIn test_button(D9);
MBroek 1:5b3fa8e47e8b 35 AnalogIn pot1(A1); // Dit is de gesimuleerde emg1
MBroek 1:5b3fa8e47e8b 36 AnalogIn pot2(A2); // Dit is de gesimuleerde emg2
MBroek 0:9e053ed05c69 37
MBroek 0:9e053ed05c69 38
MBroek 4:6524b198721f 39 //Definieren van de tickers ------------------------------------------
MBroek 4:6524b198721f 40 Ticker test_ticker;
MBroek 4:6524b198721f 41 Ticker hidscope_ticker;
MBroek 4:6524b198721f 42
MBroek 4:6524b198721f 43
MBroek 0:9e053ed05c69 44
MBroek 0:9e053ed05c69 45 // HET VASTSTELLEN VAN ALLE TE GEBRUIKEN VARIABELEN EN CONSTANTEN ======================================================
MBroek 4:6524b198721f 46
MBroek 0:9e053ed05c69 47 volatile bool safe = true; // Conditie voor de while-loop in main
MBroek 0:9e053ed05c69 48
MBroek 4:6524b198721f 49 double position_motor1;
MBroek 4:6524b198721f 50
MBroek 4:6524b198721f 51 int counts1; // Pulses van motoren
MBroek 4:6524b198721f 52 int counts2;
MBroek 0:9e053ed05c69 53
MBroek 0:9e053ed05c69 54 const double pi = 3.14159265358979323846264338327950288419716939937510; // pi
MBroek 4:6524b198721f 55 const double rad_per_count = pi/8400.0; // Aantal rad per puls uit encoder
MBroek 0:9e053ed05c69 56
MBroek 1:5b3fa8e47e8b 57 const double meter_per_count = 1; // Het aantal meter dat het karretje aflegt per puls, DIT IS NOG ONBEKEND!!!
MBroek 1:5b3fa8e47e8b 58
MBroek 4:6524b198721f 59 double error1_int = 0.00000; // Initiele error integral waardes
MBroek 4:6524b198721f 60 double error2_int = 0.00000;
MBroek 0:9e053ed05c69 61
MBroek 0:9e053ed05c69 62 const double T_getpos = 0.01; // Periode van de frequentie van het aanroepen van de positiechecker (get_position)
MBroek 0:9e053ed05c69 63
MBroek 4:6524b198721f 64 volatile bool flag1 = false, flag2 = false; // De flags voor de functies aangeroepen door de tickers
MBroek 4:6524b198721f 65
MBroek 6:e0857842e8cd 66 const double Kp_1 = 1.0000000; // De PID variablen voor motor 1
MBroek 6:e0857842e8cd 67 const double Kd_1 = 1.0000000;
MBroek 6:e0857842e8cd 68 const double Ki_1 = 1.0000000;
MBroek 4:6524b198721f 69
MBroek 5:c510ab61af28 70 const double vrijheid_m1_rad = 0.5*pi; // Dit is de uiterste postitie vd arm in radialen
MBroek 5:c510ab61af28 71
MBroek 5:c510ab61af28 72 double reference_position_motor1 = 0.5*pi; // Dit is de eerste positie waar de motor heen wil, dit moet dezelfde zijn als de startpositie!
MBroek 5:c510ab61af28 73
MBroek 6:e0857842e8cd 74 double PID_output = 0; // De eerste PID_output, deze is nul anders gaan de motoren draaien
MBroek 5:c510ab61af28 75
MBroek 6:e0857842e8cd 76 double error_prev = 0; // De initiele previous error
MBroek 6:e0857842e8cd 77 double error_int = 0; // De initiele intergral error
MBroek 5:c510ab61af28 78
MBroek 0:9e053ed05c69 79
MBroek 0:9e053ed05c69 80
MBroek 0:9e053ed05c69 81
MBroek 0:9e053ed05c69 82
MBroek 0:9e053ed05c69 83 // ALLE FUNCTIES BUITEN DE MAIN-LOOP ====================================================================================
MBroek 0:9e053ed05c69 84
MBroek 0:9e053ed05c69 85 // De emergency break ------------------------------------------------
MBroek 0:9e053ed05c69 86 void emergency_stop(){
MBroek 0:9e053ed05c69 87 safe = false;
MBroek 0:9e053ed05c69 88 pc.printf("Emergency stop!!! Please reset your K64F board\r\n");
MBroek 0:9e053ed05c69 89 }
MBroek 0:9e053ed05c69 90
MBroek 1:5b3fa8e47e8b 91
MBroek 1:5b3fa8e47e8b 92 // Functie voor het vinden van de positie van motor 1 ---------------------
MBroek 1:5b3fa8e47e8b 93 double get_position_m1(const double radpercount){ //returned de positie van de motor in radialen (positie vd arm in radialen)
MBroek 0:9e053ed05c69 94 counts1 = encoder_motor1.getPulses(); // Leest aantal pulses uit encoder af
MBroek 1:5b3fa8e47e8b 95 return radpercount * counts1; // rekent positie motor1 uit en geeft deze als output vd functie
MBroek 1:5b3fa8e47e8b 96 }
MBroek 1:5b3fa8e47e8b 97
MBroek 1:5b3fa8e47e8b 98
MBroek 1:5b3fa8e47e8b 99 // Functie voor vinden van de positie van motor 2 -----------------------------
MBroek 1:5b3fa8e47e8b 100 double get_position_m2(const double meterpercount){ // returned de positie van het karretje in meter
MBroek 1:5b3fa8e47e8b 101 counts2 = encoder_motor2.getPulses(); // leest aantal pulses vd encoder af
MBroek 1:5b3fa8e47e8b 102 return meterpercount * counts2; // rekent de positie van het karretje uit en geeft dit als output vd functie
MBroek 1:5b3fa8e47e8b 103 }
MBroek 1:5b3fa8e47e8b 104
MBroek 1:5b3fa8e47e8b 105
MBroek 5:c510ab61af28 106 // Functie voor het vinden van de reference position van motor 1 --------------------
MBroek 5:c510ab61af28 107 double get_reference_position_m1(const double aantal_rad){
MBroek 4:6524b198721f 108 double ref_pos = pot1.read() - pot2.read(); // De reference position als output (zit tussen -1 en 1, -1 is maximaal links en 1 is maximaal rechts)
MBroek 5:c510ab61af28 109 return ref_pos * aantal_rad; // Aantal rad is de uiterste positie vd arm in radialen
MBroek 5:c510ab61af28 110 }
MBroek 5:c510ab61af28 111
MBroek 5:c510ab61af28 112
MBroek 5:c510ab61af28 113 // Functie voor het vinden van de reference position van motor 2 --------------------
MBroek 5:c510ab61af28 114 double get_reference_position_m2(const double aantal_meter){
MBroek 5:c510ab61af28 115 double ref_pos = pot1.read() - pot2.read(); // De reference position als output (zit tussen -1 en 1, -1 is maximaal links en 1 is maximaal rechts)
MBroek 5:c510ab61af28 116 return ref_pos * aantal_meter; // Aantal meter is hoeveelheid radialen van middelpunt tot minima.
MBroek 1:5b3fa8e47e8b 117 }
MBroek 0:9e053ed05c69 118
MBroek 3:581c5918b590 119
MBroek 3:581c5918b590 120 // HIDScope functie ----------------------------------------------------------------------
MBroek 3:581c5918b590 121 void set_scope(double input){
MBroek 3:581c5918b590 122 scope.set(0, input);
MBroek 3:581c5918b590 123 scope.send();
MBroek 3:581c5918b590 124 }
MBroek 3:581c5918b590 125
MBroek 0:9e053ed05c69 126
MBroek 1:5b3fa8e47e8b 127 // De PID-controller voor de motors -------------------------------------------------
MBroek 6:e0857842e8cd 128 double PID_controller(double ref_pos, double mot_pos, double &e_prev, double &e_int, const double Kp, const double Kd, const double Ki){
MBroek 0:9e053ed05c69 129 double error = ref_pos - mot_pos; // error uitrekenen
MBroek 6:e0857842e8cd 130 double error_dif = (error-e_prev)/T_getpos; // De error differentieren
MBroek 1:5b3fa8e47e8b 131 //error_dif = ..... // Filter biquad poep
MBroek 6:e0857842e8cd 132 e_prev = error; // Hier wordt de error opgeslagen voor de volgende keer
MBroek 6:e0857842e8cd 133 e_int = e_int + T_getpos*error; // De error integreren
MBroek 6:e0857842e8cd 134 return Kp*error + Ki*e_int + Kd*error_dif; // De uiteindelijke PID output
MBroek 0:9e053ed05c69 135 }
MBroek 0:9e053ed05c69 136
MBroek 0:9e053ed05c69 137
MBroek 2:6bef5397e8a9 138 // Motor 1 besturen ---------------------------------------------------------------------
MBroek 2:6bef5397e8a9 139 void set_motor1(double motor_input){ // functie die de motor aanstuurt mt als input de PID-output
MBroek 2:6bef5397e8a9 140 if (motor_input >= 0){ // Dit checkt welke kant de motor op moet draaien
MBroek 2:6bef5397e8a9 141 motor1_direction_pin = 1; // Clockwise
MBroek 2:6bef5397e8a9 142 }
MBroek 2:6bef5397e8a9 143 else {
MBroek 2:6bef5397e8a9 144 motor1_direction_pin = 0; // CounterClockwise
MBroek 2:6bef5397e8a9 145 }
MBroek 2:6bef5397e8a9 146 if (fabs(motor_input)>1){ // Dit fixed de PwmOutput op maximaal 1
MBroek 2:6bef5397e8a9 147 motor_input = 1;
MBroek 2:6bef5397e8a9 148 }
MBroek 2:6bef5397e8a9 149 motor1_speed_pin = fabs(motor_input); // Dit geeft de uiteindelijke input door aan de motor
MBroek 2:6bef5397e8a9 150 }
MBroek 2:6bef5397e8a9 151
MBroek 2:6bef5397e8a9 152
MBroek 2:6bef5397e8a9 153 // Motor 2 besturen ---------------------------------------------------------------------
MBroek 2:6bef5397e8a9 154 void set_motor2(double motor_input){ // functie die de motor aanstuurt mt als input de PID-output
MBroek 2:6bef5397e8a9 155 if (motor_input >= 0){ // Dit checkt welke kant de motor op moet draaien
MBroek 2:6bef5397e8a9 156 motor2_direction_pin = 1; // Clockwise
MBroek 2:6bef5397e8a9 157 }
MBroek 2:6bef5397e8a9 158 else {
MBroek 2:6bef5397e8a9 159 motor2_direction_pin = 0; // CounterClockwise
MBroek 2:6bef5397e8a9 160 }
MBroek 2:6bef5397e8a9 161 if (fabs(motor_input)>1){ // Dit fixed de PwmOutput op maximaal 1
MBroek 2:6bef5397e8a9 162 motor_input = 1;
MBroek 2:6bef5397e8a9 163 }
MBroek 2:6bef5397e8a9 164 motor2_speed_pin = fabs(motor_input); // Dit geeft de uiteindelijke input door aan de motor
MBroek 2:6bef5397e8a9 165 }
MBroek 2:6bef5397e8a9 166
MBroek 0:9e053ed05c69 167
MBroek 4:6524b198721f 168 // De go-flag functies -----------------------------------------------------------------
MBroek 4:6524b198721f 169 void flag1_activate(){flag1=true;} // Activeert de flag
MBroek 4:6524b198721f 170 void flag2_activate(){flag2=true;}
MBroek 4:6524b198721f 171
MBroek 4:6524b198721f 172
MBroek 0:9e053ed05c69 173
MBroek 0:9e053ed05c69 174 // DE MAIN =================================================================================================================
MBroek 0:9e053ed05c69 175 int main()
MBroek 0:9e053ed05c69 176 {
MBroek 0:9e053ed05c69 177 pc.baud(SERIALBAUD);
MBroek 0:9e053ed05c69 178 pc.printf("Starting");
MBroek 0:9e053ed05c69 179
MBroek 4:6524b198721f 180 //test_button.fall(&); // Activeert test button
MBroek 0:9e053ed05c69 181 kill_switch.fall(&emergency_stop); // Activeert kill switch
MBroek 0:9e053ed05c69 182
MBroek 4:6524b198721f 183 test_ticker.attach(&flag1_activate,0.1); // Activeert de go-flag van motor positie
MBroek 4:6524b198721f 184 hidscope_ticker.attach(&flag2_activate,0.01);
MBroek 4:6524b198721f 185
MBroek 0:9e053ed05c69 186 while(safe){ // Draait loop zolang alles veilig is.
MBroek 4:6524b198721f 187 if (flag1){
MBroek 4:6524b198721f 188 flag1 = false;
MBroek 6:e0857842e8cd 189 PID_output = PID_controller(get_reference_position_m1(vrijheid_m1_rad), get_position_m1(rad_per_count), error_prev, error_int, Kp_1, Kd_1, Ki_1);
MBroek 5:c510ab61af28 190 //pc.printf("%f\r\n",get_reference_position_m1);
MBroek 4:6524b198721f 191 }
MBroek 4:6524b198721f 192 if (flag2){
MBroek 4:6524b198721f 193 flag2 = false;
MBroek 6:e0857842e8cd 194 set_scope(PID_output);
MBroek 4:6524b198721f 195 }
MBroek 0:9e053ed05c69 196 }
MBroek 0:9e053ed05c69 197
MBroek 0:9e053ed05c69 198 motor1_speed_pin = 0; //Dit zet de motoren uit nadat de kill switch is gebruikt
MBroek 0:9e053ed05c69 199 motor2_speed_pin = 0;
MBroek 0:9e053ed05c69 200 }
MBroek 0:9e053ed05c69 201
MBroek 0:9e053ed05c69 202
MBroek 0:9e053ed05c69 203
MBroek 0:9e053ed05c69 204
MBroek 0:9e053ed05c69 205
MBroek 0:9e053ed05c69 206
MBroek 0:9e053ed05c69 207
MBroek 0:9e053ed05c69 208
MBroek 0:9e053ed05c69 209
MBroek 0:9e053ed05c69 210
MBroek 0:9e053ed05c69 211
MBroek 0:9e053ed05c69 212
MBroek 0:9e053ed05c69 213
MBroek 0:9e053ed05c69 214
MBroek 0:9e053ed05c69 215
MBroek 0:9e053ed05c69 216
MBroek 0:9e053ed05c69 217
MBroek 0:9e053ed05c69 218
MBroek 0:9e053ed05c69 219
MBroek 0:9e053ed05c69 220
MBroek 0:9e053ed05c69 221
MBroek 0:9e053ed05c69 222
MBroek 0:9e053ed05c69 223
MBroek 0:9e053ed05c69 224
MBroek 0:9e053ed05c69 225
MBroek 0:9e053ed05c69 226
MBroek 0:9e053ed05c69 227
MBroek 0:9e053ed05c69 228
MBroek 0:9e053ed05c69 229
MBroek 0:9e053ed05c69 230
MBroek 0:9e053ed05c69 231
MBroek 0:9e053ed05c69 232
MBroek 0:9e053ed05c69 233
MBroek 0:9e053ed05c69 234
MBroek 0:9e053ed05c69 235
MBroek 0:9e053ed05c69 236
MBroek 0:9e053ed05c69 237
MBroek 0:9e053ed05c69 238
MBroek 0:9e053ed05c69 239
MBroek 0:9e053ed05c69 240
MBroek 0:9e053ed05c69 241
MBroek 0:9e053ed05c69 242
MBroek 0:9e053ed05c69 243
MBroek 0:9e053ed05c69 244
MBroek 0:9e053ed05c69 245
MBroek 0:9e053ed05c69 246
MBroek 0:9e053ed05c69 247
MBroek 0:9e053ed05c69 248
MBroek 0:9e053ed05c69 249
MBroek 0:9e053ed05c69 250
MBroek 0:9e053ed05c69 251
MBroek 0:9e053ed05c69 252
MBroek 0:9e053ed05c69 253
MBroek 0:9e053ed05c69 254