MODSERIAL PID controller edit
Dependencies: HIDScope MODSERIAL QEI biquadFilter mbed
Fork of PID_controller_Motor by
main.cpp@24:ea6de9237cff, 2016-10-25 (annotated)
- Committer:
- MBroek
- Date:
- Tue Oct 25 18:15:05 2016 +0000
- Revision:
- 24:ea6de9237cff
- Parent:
- 23:3902ee714281
- Child:
- 25:553b0ca967fc
EMG signaal geimplemteerd, doet nog een beetje raar.
Who changed what in which revision?
User | Revision | Line number | New 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 | 24:ea6de9237cff | 11 | #include "BiQuad.h" |
MBroek | 3:581c5918b590 | 12 | |
MBroek | 3:581c5918b590 | 13 | // Definieren van de HIDscope ---------------------------------------- |
MBroek | 24:ea6de9237cff | 14 | HIDScope scope(5); |
MBroek | 0:9e053ed05c69 | 15 | |
MBroek | 0:9e053ed05c69 | 16 | |
MBroek | 0:9e053ed05c69 | 17 | // Definieren van de Serial en Encoder ------------------------------- |
MBroek | 0:9e053ed05c69 | 18 | MODSERIAL pc(USBTX, USBRX); |
MBroek | 0:9e053ed05c69 | 19 | #define SERIALBAUD 115200 |
MBroek | 0:9e053ed05c69 | 20 | |
MBroek | 0:9e053ed05c69 | 21 | QEI encoder_motor1(D10,D11,NC,64); |
MBroek | 0:9e053ed05c69 | 22 | QEI encoder_motor2(D12,D13,NC,64); |
MBroek | 0:9e053ed05c69 | 23 | |
MBroek | 0:9e053ed05c69 | 24 | |
MBroek | 0:9e053ed05c69 | 25 | // Definieren van de Motorpins --------------------------------------- |
MBroek | 0:9e053ed05c69 | 26 | DigitalOut motor1_direction_pin(D7); |
MBroek | 0:9e053ed05c69 | 27 | PwmOut motor1_speed_pin(D6); |
MBroek | 0:9e053ed05c69 | 28 | |
MBroek | 0:9e053ed05c69 | 29 | DigitalOut motor2_direction_pin(D4); |
MBroek | 0:9e053ed05c69 | 30 | PwmOut motor2_speed_pin(D5); |
MBroek | 0:9e053ed05c69 | 31 | |
MBroek | 0:9e053ed05c69 | 32 | |
MBroek | 0:9e053ed05c69 | 33 | //Definieren van bord-elementen -------------------------------------- |
MBroek | 19:35f3da6c6969 | 34 | InterruptIn motor_switch_button(D3); |
MBroek | 19:35f3da6c6969 | 35 | InterruptIn state_switch_button(D2); |
MBroek | 17:525b785f007a | 36 | DigitalIn EMG_sim1(SW2); |
MBroek | 17:525b785f007a | 37 | DigitalIn EMG_sim2(SW3); |
MBroek | 17:525b785f007a | 38 | |
MBroek | 1:5b3fa8e47e8b | 39 | AnalogIn pot1(A1); // Dit is de gesimuleerde emg1 |
MBroek | 1:5b3fa8e47e8b | 40 | AnalogIn pot2(A2); // Dit is de gesimuleerde emg2 |
MBroek | 0:9e053ed05c69 | 41 | |
MBroek | 13:b0d3c547bf2f | 42 | |
MBroek | 13:b0d3c547bf2f | 43 | DigitalOut ledred(LED_RED, 1); |
MBroek | 13:b0d3c547bf2f | 44 | DigitalOut ledgreen(LED_GREEN, 1); |
MBroek | 16:5a749b319276 | 45 | DigitalOut ledblue(LED_BLUE, 1); |
MBroek | 13:b0d3c547bf2f | 46 | |
MBroek | 24:ea6de9237cff | 47 | AnalogIn emg0( A0 ); // right biceps |
MBroek | 24:ea6de9237cff | 48 | AnalogIn emg1( A1 ); // left biceps |
MBroek | 24:ea6de9237cff | 49 | |
MBroek | 0:9e053ed05c69 | 50 | |
MBroek | 4:6524b198721f | 51 | //Definieren van de tickers ------------------------------------------ |
MBroek | 17:525b785f007a | 52 | Ticker motor_ticker; // Deze ticker activeert het motor_runs programma, dit leest motor pos en ref pos uit, rekent de PID uit en stuurt met dit de motoren |
MBroek | 4:6524b198721f | 53 | Ticker hidscope_ticker; |
MBroek | 4:6524b198721f | 54 | |
MBroek | 4:6524b198721f | 55 | |
MBroek | 24:ea6de9237cff | 56 | // Het definieren van de filters ---------------------------------------- |
MBroek | 24:ea6de9237cff | 57 | BiQuadChain bqc1; // Combined filter: high-pass (10Hz) and notch (50Hz) |
MBroek | 24:ea6de9237cff | 58 | BiQuad bq1( 9.21171e-01, -1.84234e+00, 9.21171e-01, -1.88661e+00, 8.90340e-01 ); |
MBroek | 24:ea6de9237cff | 59 | BiQuad bq2( 1.00000e+00, -2.00000e+00, 1.00000e+00, -1.94922e+00, 9.53070e-01 ); |
MBroek | 24:ea6de9237cff | 60 | BiQuad bq3( 9.93756e-01, -1.89024e+00, 9.93756e-01, -1.89024e+00, 9.87512e-01 ); |
MBroek | 24:ea6de9237cff | 61 | |
MBroek | 24:ea6de9237cff | 62 | BiQuadChain bqc2; |
MBroek | 24:ea6de9237cff | 63 | BiQuad bq4 = bq1; |
MBroek | 24:ea6de9237cff | 64 | BiQuad bq5 = bq2; |
MBroek | 24:ea6de9237cff | 65 | BiQuad bq6 = bq3; |
MBroek | 24:ea6de9237cff | 66 | |
MBroek | 24:ea6de9237cff | 67 | BiQuadChain bqc3; // Low-pass filter (5Hz) |
MBroek | 24:ea6de9237cff | 68 | BiQuad bq7( 5.84514e-08, 1.16903e-07, 5.84514e-08, -1.94264e+00, 9.43597e-01 ); |
MBroek | 24:ea6de9237cff | 69 | BiQuad bq8( 1.00000e+00, 2.00000e+00, 1.00000e+00, -1.97527e+00, 9.76245e-01 ); |
MBroek | 24:ea6de9237cff | 70 | |
MBroek | 24:ea6de9237cff | 71 | BiQuadChain bqc4; |
MBroek | 24:ea6de9237cff | 72 | BiQuad bq9 = bq7; |
MBroek | 24:ea6de9237cff | 73 | BiQuad bq10 = bq9; |
MBroek | 24:ea6de9237cff | 74 | |
MBroek | 24:ea6de9237cff | 75 | |
MBroek | 0:9e053ed05c69 | 76 | |
MBroek | 0:9e053ed05c69 | 77 | // HET VASTSTELLEN VAN ALLE TE GEBRUIKEN VARIABELEN EN CONSTANTEN ====================================================== |
MBroek | 4:6524b198721f | 78 | |
MBroek | 21:3db3f2d56d56 | 79 | enum states_enum{off, init, run}; // De states waar de main loop doorheen loopt, off is uit, init is intialiseren en run is het aanturen vd motoren met emg. |
MBroek | 14:f51d51395803 | 80 | states_enum states = off; |
MBroek | 14:f51d51395803 | 81 | |
MBroek | 21:3db3f2d56d56 | 82 | double ref_pos_prev_m1 = 0.0; // De initiele ref_pos_pref |
MBroek | 21:3db3f2d56d56 | 83 | double ref_pos_prev_m2 = 0.0; |
MBroek | 21:3db3f2d56d56 | 84 | |
MBroek | 4:6524b198721f | 85 | int counts1; // Pulses van motoren |
MBroek | 4:6524b198721f | 86 | int counts2; |
MBroek | 0:9e053ed05c69 | 87 | |
MBroek | 0:9e053ed05c69 | 88 | const double pi = 3.14159265358979323846264338327950288419716939937510; // pi |
MBroek | 15:3f3d87513a6b | 89 | const double rad_per_count = 2.0*pi/8400.0; // Aantal rad per puls uit encoder |
MBroek | 0:9e053ed05c69 | 90 | |
MBroek | 23:3902ee714281 | 91 | const double radius_tandwiel = 0.008; |
MBroek | 9:f735baee0c2b | 92 | const double meter_per_count = rad_per_count * radius_tandwiel; // Het aantal meter dat het karretje aflegt per puls, DIT IS NOG ONBEKEND!!! |
MBroek | 1:5b3fa8e47e8b | 93 | |
MBroek | 23:3902ee714281 | 94 | const double v_max_karretje = 8.4*radius_tandwiel; // Maximale snelheid karretje, gelimiteerd door de v_max vd motor |
MBroek | 23:3902ee714281 | 95 | |
MBroek | 4:6524b198721f | 96 | double error1_int = 0.00000; // Initiele error integral waardes |
MBroek | 4:6524b198721f | 97 | double error2_int = 0.00000; |
MBroek | 0:9e053ed05c69 | 98 | |
MBroek | 21:3db3f2d56d56 | 99 | const double T_motor_function = 0.01; // Periode van de frequentie van het aanroepen van onder andere positiechecker (get_position) en de rest vd motor functie |
MBroek | 0:9e053ed05c69 | 100 | |
MBroek | 4:6524b198721f | 101 | volatile bool flag1 = false, flag2 = false; // De flags voor de functies aangeroepen door de tickers |
MBroek | 4:6524b198721f | 102 | |
MBroek | 21:3db3f2d56d56 | 103 | const double Kp_1 = 0.5; // De PID variablen voor motor 1 |
MBroek | 21:3db3f2d56d56 | 104 | const double Kd_1 = 0.0; |
MBroek | 21:3db3f2d56d56 | 105 | const double Ki_1 = 0.0; |
MBroek | 4:6524b198721f | 106 | |
MBroek | 23:3902ee714281 | 107 | const double Kp_2 = 1.0; // De PID variablen voor motor 2 |
MBroek | 23:3902ee714281 | 108 | const double Kd_2 = 0.0; |
MBroek | 23:3902ee714281 | 109 | const double Ki_2 = 0.0; |
MBroek | 5:c510ab61af28 | 110 | |
MBroek | 9:f735baee0c2b | 111 | const double vrijheid_m1_rad = 0.5*pi; // Dit is de uiterste postitie vd arm in radialen |
MBroek | 23:3902ee714281 | 112 | const double vrijheid_m2_meter = 0.5; // Dit is de helft vd arm, dit is wat het karretje heen en weer kan bewegen |
MBroek | 5:c510ab61af28 | 113 | |
MBroek | 21:3db3f2d56d56 | 114 | const double initial_pos_m1 = vrijheid_m1_rad; // Startin posities van de motoren |
MBroek | 23:3902ee714281 | 115 | const double initial_pos_m2 = 0; |
MBroek | 23:3902ee714281 | 116 | |
MBroek | 23:3902ee714281 | 117 | double position_motor2; |
MBroek | 23:3902ee714281 | 118 | double position_motor2_prev = initial_pos_m2; |
MBroek | 9:f735baee0c2b | 119 | |
MBroek | 13:b0d3c547bf2f | 120 | double PID_output_1 = 0.0; // De eerste PID_output, deze is nul anders gaan de motoren draaien |
MBroek | 13:b0d3c547bf2f | 121 | double PID_output_2 = 0.0; |
MBroek | 5:c510ab61af28 | 122 | |
MBroek | 13:b0d3c547bf2f | 123 | double error_prev_1 = 0.0; // De initiele previous error |
MBroek | 13:b0d3c547bf2f | 124 | double error_int_1 = 0.0; // De initiele intergral error |
MBroek | 10:755b9228cc42 | 125 | |
MBroek | 13:b0d3c547bf2f | 126 | double error_prev_2 = 0.0; |
MBroek | 13:b0d3c547bf2f | 127 | double error_int_2 = 0.0; |
MBroek | 5:c510ab61af28 | 128 | |
MBroek | 20:2fdb069ffcae | 129 | double reference_pos_m2; // De reference positie waar de motor heen wil afhankelijk van het EMG signaal |
MBroek | 20:2fdb069ffcae | 130 | double reference_pos_m1; |
MBroek | 20:2fdb069ffcae | 131 | |
MBroek | 17:525b785f007a | 132 | enum which_motor{motor1, motor2}; // enum van de motoren |
MBroek | 15:3f3d87513a6b | 133 | which_motor motor_that_runs = motor1; |
MBroek | 15:3f3d87513a6b | 134 | |
MBroek | 24:ea6de9237cff | 135 | int digital0; // Define digital signals fot EMG |
MBroek | 24:ea6de9237cff | 136 | int digital1; |
MBroek | 24:ea6de9237cff | 137 | |
MBroek | 24:ea6de9237cff | 138 | double threshold0 = 0.3; // Define thresholds (signal is normalized, so 0.3 = 30% of maximum value) fot EMG |
MBroek | 24:ea6de9237cff | 139 | double threshold1 = 0.3; |
MBroek | 24:ea6de9237cff | 140 | |
MBroek | 24:ea6de9237cff | 141 | double max_out0 = 0; // Set initial maximum values of the signals to 0, the code will check if there's a new maximum value every iteration |
MBroek | 24:ea6de9237cff | 142 | double max_out1 = 0; |
MBroek | 24:ea6de9237cff | 143 | |
MBroek | 24:ea6de9237cff | 144 | int final_signal; // Dit is waarmee de reference position gezocht gaat worden. |
MBroek | 24:ea6de9237cff | 145 | |
MBroek | 9:f735baee0c2b | 146 | |
MBroek | 0:9e053ed05c69 | 147 | |
MBroek | 0:9e053ed05c69 | 148 | |
MBroek | 0:9e053ed05c69 | 149 | |
MBroek | 0:9e053ed05c69 | 150 | |
MBroek | 0:9e053ed05c69 | 151 | // ALLE FUNCTIES BUITEN DE MAIN-LOOP ==================================================================================== |
MBroek | 0:9e053ed05c69 | 152 | |
MBroek | 13:b0d3c547bf2f | 153 | |
MBroek | 15:3f3d87513a6b | 154 | // De statechanger ----------------------------------------------------------- |
MBroek | 15:3f3d87513a6b | 155 | void state_changer(){ |
MBroek | 15:3f3d87513a6b | 156 | if(states == off){ |
MBroek | 21:3db3f2d56d56 | 157 | states = init; |
MBroek | 16:5a749b319276 | 158 | } |
MBroek | 21:3db3f2d56d56 | 159 | else if(states == init){ |
MBroek | 16:5a749b319276 | 160 | states = run; |
MBroek | 16:5a749b319276 | 161 | } |
MBroek | 16:5a749b319276 | 162 | else{ |
MBroek | 16:5a749b319276 | 163 | states = off; |
MBroek | 16:5a749b319276 | 164 | } |
MBroek | 16:5a749b319276 | 165 | } |
MBroek | 16:5a749b319276 | 166 | |
MBroek | 13:b0d3c547bf2f | 167 | |
MBroek | 9:f735baee0c2b | 168 | // Functie voor het switchen tussen de motors ------------------------------ |
MBroek | 9:f735baee0c2b | 169 | void motor_switch(){ |
MBroek | 15:3f3d87513a6b | 170 | if(motor_that_runs == motor1){ |
MBroek | 15:3f3d87513a6b | 171 | motor_that_runs = motor2; |
MBroek | 15:3f3d87513a6b | 172 | } |
MBroek | 15:3f3d87513a6b | 173 | else{ |
MBroek | 15:3f3d87513a6b | 174 | motor_that_runs = motor1; |
MBroek | 15:3f3d87513a6b | 175 | } |
MBroek | 16:5a749b319276 | 176 | } |
MBroek | 15:3f3d87513a6b | 177 | |
MBroek | 9:f735baee0c2b | 178 | |
MBroek | 1:5b3fa8e47e8b | 179 | // Functie voor het vinden van de positie van motor 1 --------------------- |
MBroek | 13:b0d3c547bf2f | 180 | double get_position_m1(const double radpercount){ //returned de positie van de motor in radialen (positie vd arm in radialen) |
MBroek | 0:9e053ed05c69 | 181 | counts1 = encoder_motor1.getPulses(); // Leest aantal pulses uit encoder af |
MBroek | 21:3db3f2d56d56 | 182 | return radpercount * counts1 + initial_pos_m1; // rekent positie motor1 uit en geeft deze als output vd functie |
MBroek | 1:5b3fa8e47e8b | 183 | } |
MBroek | 1:5b3fa8e47e8b | 184 | |
MBroek | 1:5b3fa8e47e8b | 185 | |
MBroek | 1:5b3fa8e47e8b | 186 | // Functie voor vinden van de positie van motor 2 ----------------------------- |
MBroek | 23:3902ee714281 | 187 | double get_position_m2(const double meterpercount, double mot_pos_prev){ // returned de positie van het karretje in meter |
MBroek | 23:3902ee714281 | 188 | double mot_pos; |
MBroek | 1:5b3fa8e47e8b | 189 | counts2 = encoder_motor2.getPulses(); // leest aantal pulses vd encoder af |
MBroek | 23:3902ee714281 | 190 | mot_pos = meterpercount * counts2 + mot_pos_prev; |
MBroek | 23:3902ee714281 | 191 | encoder_motor2.reset(); |
MBroek | 23:3902ee714281 | 192 | mot_pos_prev = mot_pos; |
MBroek | 23:3902ee714281 | 193 | return mot_pos_prev; // rekent de positie van het karretje uit en geeft dit als output vd functie |
MBroek | 1:5b3fa8e47e8b | 194 | } |
MBroek | 1:5b3fa8e47e8b | 195 | |
MBroek | 1:5b3fa8e47e8b | 196 | |
MBroek | 24:ea6de9237cff | 197 | // Functie voor het uitlezen van het EMG signaal ------------------------------------------ |
MBroek | 24:ea6de9237cff | 198 | int get_EMG() |
MBroek | 24:ea6de9237cff | 199 | { |
MBroek | 24:ea6de9237cff | 200 | double in0 = emg0.read(); // EMG signal 0 (right biceps) |
MBroek | 24:ea6de9237cff | 201 | double in1 = emg1.read(); // EMG signal 1 (left biceps) |
MBroek | 24:ea6de9237cff | 202 | |
MBroek | 24:ea6de9237cff | 203 | double filtered0 = bqc1.step(in0); // Filter signals (high pass and notch) |
MBroek | 24:ea6de9237cff | 204 | double filtered1 = bqc2.step(in1); |
MBroek | 24:ea6de9237cff | 205 | double abs_filtered0 = abs(filtered0); // Absolute value of filtered signals |
MBroek | 24:ea6de9237cff | 206 | double abs_filtered1 = abs(filtered1); |
MBroek | 24:ea6de9237cff | 207 | double out0 = bqc3.step(abs_filtered0); // Filter the signals again to remove trend (low pass) |
MBroek | 24:ea6de9237cff | 208 | double out1 = bqc4.step(abs_filtered1); |
MBroek | 24:ea6de9237cff | 209 | |
MBroek | 24:ea6de9237cff | 210 | if (out0 > max_out0) // Check if the signals have a new maximum value |
MBroek | 24:ea6de9237cff | 211 | { max_out0 = out0; } |
MBroek | 24:ea6de9237cff | 212 | if (out1 > max_out1) |
MBroek | 24:ea6de9237cff | 213 | { max_out1 = out1; } |
MBroek | 24:ea6de9237cff | 214 | |
MBroek | 24:ea6de9237cff | 215 | double normalized_out0 = out0 / max_out0; // Normalize the signals (divide by maximum value to scale from 0 to 1) |
MBroek | 24:ea6de9237cff | 216 | double normalized_out1 = out1 / max_out1; |
MBroek | 24:ea6de9237cff | 217 | |
MBroek | 24:ea6de9237cff | 218 | if (normalized_out0 > threshold0) // If the signal value is above the threshold, give an output of 1 |
MBroek | 24:ea6de9237cff | 219 | { digital0 = 1; } |
MBroek | 24:ea6de9237cff | 220 | else // If the signal value is below the threshold, give an output of 0 |
MBroek | 24:ea6de9237cff | 221 | { digital0 = 0; } |
MBroek | 24:ea6de9237cff | 222 | |
MBroek | 24:ea6de9237cff | 223 | if (normalized_out1 > threshold1) // Do the same for the second EMG signal |
MBroek | 24:ea6de9237cff | 224 | { digital1 = 1; } |
MBroek | 24:ea6de9237cff | 225 | else |
MBroek | 24:ea6de9237cff | 226 | { digital1 = 0; } |
MBroek | 24:ea6de9237cff | 227 | |
MBroek | 24:ea6de9237cff | 228 | scope.set(0,in0); // EMG signal 0 (right biceps) |
MBroek | 24:ea6de9237cff | 229 | scope.set(1,in1); // EMG signal 1 (left biceps) |
MBroek | 24:ea6de9237cff | 230 | scope.set(2,digital0); // Signal 0 after filtering, detrending, rectification, digitalization |
MBroek | 24:ea6de9237cff | 231 | scope.set(3,digital1); // Signal 1 after filtering, detrending, rectification, digitalization |
MBroek | 24:ea6de9237cff | 232 | scope.set(4,digital0 - digital1); // Final output signal |
MBroek | 24:ea6de9237cff | 233 | |
MBroek | 24:ea6de9237cff | 234 | scope.send(); // Send all channels to the PC at once |
MBroek | 24:ea6de9237cff | 235 | |
MBroek | 24:ea6de9237cff | 236 | return digital0 - digital1; // Subtract the digital signals (right arm gives positive values, left arm give negative values) |
MBroek | 24:ea6de9237cff | 237 | } |
MBroek | 24:ea6de9237cff | 238 | |
MBroek | 24:ea6de9237cff | 239 | |
MBroek | 24:ea6de9237cff | 240 | |
MBroek | 5:c510ab61af28 | 241 | // Functie voor het vinden van de reference position van motor 1 -------------------- |
MBroek | 21:3db3f2d56d56 | 242 | double get_reference_position_m1(double &ref_pos_prev, const double vrijheid_rad){ |
MBroek | 22:6dfe5554b96e | 243 | double ref_pos; |
MBroek | 24:ea6de9237cff | 244 | final_signal = get_EMG(); // Het uiteindelijke signaal uit de emg als output zit is -1, 0 of 1. |
MBroek | 21:3db3f2d56d56 | 245 | switch(final_signal){ |
MBroek | 21:3db3f2d56d56 | 246 | case 0 : |
MBroek | 22:6dfe5554b96e | 247 | ref_pos = ref_pos_prev; |
MBroek | 21:3db3f2d56d56 | 248 | break; |
MBroek | 21:3db3f2d56d56 | 249 | case 1 : |
MBroek | 23:3902ee714281 | 250 | ref_pos = ref_pos_prev + T_motor_function*vrijheid_rad; // Dit dwingt af dat de ref pos veranderd met ong 0.5 rad/s |
MBroek | 21:3db3f2d56d56 | 251 | break; |
MBroek | 21:3db3f2d56d56 | 252 | case -1 : |
MBroek | 22:6dfe5554b96e | 253 | ref_pos = ref_pos_prev - T_motor_function*vrijheid_rad; |
MBroek | 21:3db3f2d56d56 | 254 | break; |
MBroek | 21:3db3f2d56d56 | 255 | } |
MBroek | 22:6dfe5554b96e | 256 | if(ref_pos >= vrijheid_rad){ |
MBroek | 22:6dfe5554b96e | 257 | ref_pos = vrijheid_rad; |
MBroek | 21:3db3f2d56d56 | 258 | } |
MBroek | 22:6dfe5554b96e | 259 | if(ref_pos <= -vrijheid_rad){ |
MBroek | 22:6dfe5554b96e | 260 | ref_pos = -vrijheid_rad; |
MBroek | 21:3db3f2d56d56 | 261 | } |
MBroek | 22:6dfe5554b96e | 262 | ref_pos_prev = ref_pos; |
MBroek | 21:3db3f2d56d56 | 263 | return ref_pos_prev; // vrijheid_rad is de uiterste positie vd arm in radialen |
MBroek | 5:c510ab61af28 | 264 | } |
MBroek | 5:c510ab61af28 | 265 | |
MBroek | 5:c510ab61af28 | 266 | |
MBroek | 5:c510ab61af28 | 267 | // Functie voor het vinden van de reference position van motor 2 -------------------- |
MBroek | 23:3902ee714281 | 268 | double get_reference_position_m2(double &ref_pos_prev, const double vrijheid_meter){ |
MBroek | 23:3902ee714281 | 269 | double ref_pos; |
MBroek | 23:3902ee714281 | 270 | int final_signal = EMG_sim1.read() - EMG_sim2.read(); // Het uiteindelijke signaal uit de emg als output zit is -1, 0 of 1. |
MBroek | 23:3902ee714281 | 271 | switch(final_signal){ |
MBroek | 23:3902ee714281 | 272 | case 0 : |
MBroek | 23:3902ee714281 | 273 | ref_pos = ref_pos_prev; |
MBroek | 23:3902ee714281 | 274 | break; |
MBroek | 23:3902ee714281 | 275 | case 1 : |
MBroek | 23:3902ee714281 | 276 | ref_pos = ref_pos_prev + T_motor_function*v_max_karretje; // Hierdoor veranderd de ref_pos met de maximale snelheid vd motor (karretje) |
MBroek | 23:3902ee714281 | 277 | break; |
MBroek | 23:3902ee714281 | 278 | case -1 : |
MBroek | 23:3902ee714281 | 279 | ref_pos = ref_pos_prev - T_motor_function*v_max_karretje; |
MBroek | 23:3902ee714281 | 280 | break; |
MBroek | 23:3902ee714281 | 281 | } |
MBroek | 23:3902ee714281 | 282 | if(ref_pos >= vrijheid_meter){ |
MBroek | 23:3902ee714281 | 283 | ref_pos = vrijheid_meter; |
MBroek | 23:3902ee714281 | 284 | } |
MBroek | 23:3902ee714281 | 285 | if(ref_pos <= 0){ |
MBroek | 23:3902ee714281 | 286 | ref_pos = 0; |
MBroek | 23:3902ee714281 | 287 | } |
MBroek | 23:3902ee714281 | 288 | ref_pos_prev = ref_pos; |
MBroek | 23:3902ee714281 | 289 | return ref_pos_prev; |
MBroek | 1:5b3fa8e47e8b | 290 | } |
MBroek | 23:3902ee714281 | 291 | |
MBroek | 3:581c5918b590 | 292 | |
MBroek | 3:581c5918b590 | 293 | // HIDScope functie ---------------------------------------------------------------------- |
MBroek | 7:435f984781ab | 294 | void set_scope(double input1, double input2, double input3){ |
MBroek | 7:435f984781ab | 295 | scope.set(0, input1); |
MBroek | 7:435f984781ab | 296 | scope.set(1, input2); |
MBroek | 7:435f984781ab | 297 | scope.set(2, input3); |
MBroek | 3:581c5918b590 | 298 | scope.send(); |
MBroek | 3:581c5918b590 | 299 | } |
MBroek | 3:581c5918b590 | 300 | |
MBroek | 0:9e053ed05c69 | 301 | |
MBroek | 1:5b3fa8e47e8b | 302 | // De PID-controller voor de motors ------------------------------------------------- |
MBroek | 6:e0857842e8cd | 303 | 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 | 304 | double error = ref_pos - mot_pos; // error uitrekenen |
MBroek | 21:3db3f2d56d56 | 305 | double error_dif = (error-e_prev)/T_motor_function; // De error differentieren |
MBroek | 1:5b3fa8e47e8b | 306 | //error_dif = ..... // Filter biquad poep |
MBroek | 6:e0857842e8cd | 307 | e_prev = error; // Hier wordt de error opgeslagen voor de volgende keer |
MBroek | 21:3db3f2d56d56 | 308 | e_int = e_int + T_motor_function*error; // De error integreren |
MBroek | 6:e0857842e8cd | 309 | return Kp*error + Ki*e_int + Kd*error_dif; // De uiteindelijke PID output |
MBroek | 0:9e053ed05c69 | 310 | } |
MBroek | 0:9e053ed05c69 | 311 | |
MBroek | 0:9e053ed05c69 | 312 | |
MBroek | 2:6bef5397e8a9 | 313 | // Motor 1 besturen --------------------------------------------------------------------- |
MBroek | 2:6bef5397e8a9 | 314 | void set_motor1(double motor_input){ // functie die de motor aanstuurt mt als input de PID-output |
MBroek | 2:6bef5397e8a9 | 315 | if (motor_input >= 0){ // Dit checkt welke kant de motor op moet draaien |
MBroek | 2:6bef5397e8a9 | 316 | motor1_direction_pin = 1; // Clockwise |
MBroek | 2:6bef5397e8a9 | 317 | } |
MBroek | 2:6bef5397e8a9 | 318 | else { |
MBroek | 2:6bef5397e8a9 | 319 | motor1_direction_pin = 0; // CounterClockwise |
MBroek | 2:6bef5397e8a9 | 320 | } |
MBroek | 2:6bef5397e8a9 | 321 | if (fabs(motor_input)>1){ // Dit fixed de PwmOutput op maximaal 1 |
MBroek | 2:6bef5397e8a9 | 322 | motor_input = 1; |
MBroek | 2:6bef5397e8a9 | 323 | } |
MBroek | 2:6bef5397e8a9 | 324 | motor1_speed_pin = fabs(motor_input); // Dit geeft de uiteindelijke input door aan de motor |
MBroek | 2:6bef5397e8a9 | 325 | } |
MBroek | 2:6bef5397e8a9 | 326 | |
MBroek | 2:6bef5397e8a9 | 327 | |
MBroek | 2:6bef5397e8a9 | 328 | // Motor 2 besturen --------------------------------------------------------------------- |
MBroek | 2:6bef5397e8a9 | 329 | void set_motor2(double motor_input){ // functie die de motor aanstuurt mt als input de PID-output |
MBroek | 2:6bef5397e8a9 | 330 | if (motor_input >= 0){ // Dit checkt welke kant de motor op moet draaien |
MBroek | 2:6bef5397e8a9 | 331 | motor2_direction_pin = 1; // Clockwise |
MBroek | 2:6bef5397e8a9 | 332 | } |
MBroek | 2:6bef5397e8a9 | 333 | else { |
MBroek | 2:6bef5397e8a9 | 334 | motor2_direction_pin = 0; // CounterClockwise |
MBroek | 2:6bef5397e8a9 | 335 | } |
MBroek | 2:6bef5397e8a9 | 336 | if (fabs(motor_input)>1){ // Dit fixed de PwmOutput op maximaal 1 |
MBroek | 2:6bef5397e8a9 | 337 | motor_input = 1; |
MBroek | 2:6bef5397e8a9 | 338 | } |
MBroek | 2:6bef5397e8a9 | 339 | motor2_speed_pin = fabs(motor_input); // Dit geeft de uiteindelijke input door aan de motor |
MBroek | 2:6bef5397e8a9 | 340 | } |
MBroek | 2:6bef5397e8a9 | 341 | |
MBroek | 0:9e053ed05c69 | 342 | |
MBroek | 4:6524b198721f | 343 | // De go-flag functies ----------------------------------------------------------------- |
MBroek | 4:6524b198721f | 344 | void flag1_activate(){flag1=true;} // Activeert de flag |
MBroek | 14:f51d51395803 | 345 | void flag2_activate(){flag2=true;} |
MBroek | 14:f51d51395803 | 346 | |
MBroek | 14:f51d51395803 | 347 | |
MBroek | 21:3db3f2d56d56 | 348 | // Dit doet de robot als hij niets doet ------------------------------------------------------ |
MBroek | 21:3db3f2d56d56 | 349 | void robot_is_off(){ |
MBroek | 21:3db3f2d56d56 | 350 | ledgreen.write(1); |
MBroek | 21:3db3f2d56d56 | 351 | ledred.write(0); // Indicator ik ben uit |
MBroek | 21:3db3f2d56d56 | 352 | motor1_speed_pin = 0; // Uitzetten vd motoren |
MBroek | 16:5a749b319276 | 353 | motor2_speed_pin = 0; |
MBroek | 16:5a749b319276 | 354 | } |
MBroek | 16:5a749b319276 | 355 | |
MBroek | 16:5a749b319276 | 356 | |
MBroek | 21:3db3f2d56d56 | 357 | // De initialiseren functie ------------------------------------------------------------ |
MBroek | 21:3db3f2d56d56 | 358 | void initialise(){ // Deze functie laat de motor van een vaste uiterste positie (fysisch limiet) naar het middelpunt draaien. |
MBroek | 21:3db3f2d56d56 | 359 | ref_pos_prev_m1 = initial_pos_m1; |
MBroek | 21:3db3f2d56d56 | 360 | ref_pos_prev_m2 = initial_pos_m2; |
MBroek | 16:5a749b319276 | 361 | ledgreen.write(0); |
MBroek | 21:3db3f2d56d56 | 362 | encoder_motor1.reset(); |
MBroek | 21:3db3f2d56d56 | 363 | encoder_motor2.reset(); |
MBroek | 16:5a749b319276 | 364 | motor1_speed_pin = 0; |
MBroek | 21:3db3f2d56d56 | 365 | } |
MBroek | 16:5a749b319276 | 366 | |
MBroek | 16:5a749b319276 | 367 | |
MBroek | 15:3f3d87513a6b | 368 | // De functie die de motoren aanstuurt ------------------------------------------------- |
MBroek | 17:525b785f007a | 369 | void running_motors(){ // Deze functie kijkt welke motor moet draaien en rekent dan de PID uit en geeft dit door aan de motor input functie |
MBroek | 15:3f3d87513a6b | 370 | if (flag1){ |
MBroek | 15:3f3d87513a6b | 371 | flag1 = false; |
MBroek | 16:5a749b319276 | 372 | ledred.write(1); |
MBroek | 15:3f3d87513a6b | 373 | switch (motor_that_runs){ |
MBroek | 15:3f3d87513a6b | 374 | case motor1 : // In deze case draait alleen motor 1 |
MBroek | 21:3db3f2d56d56 | 375 | reference_pos_m1 = get_reference_position_m1(ref_pos_prev_m1, vrijheid_m1_rad); |
MBroek | 20:2fdb069ffcae | 376 | PID_output_1 = PID_controller(reference_pos_m1, get_position_m1(rad_per_count), error_prev_1, error_int_1, Kp_1, Kd_1, Ki_1); |
MBroek | 20:2fdb069ffcae | 377 | PID_output_2 = PID_controller(-reference_pos_m1, get_position_m1(rad_per_count), error_prev_1, error_int_1, Kp_1, Kd_1, Ki_1); // Zorgt er voor dat motor2 meedraait met motor1 |
MBroek | 15:3f3d87513a6b | 378 | set_motor1(PID_output_1); |
MBroek | 20:2fdb069ffcae | 379 | set_motor2(PID_output_2); |
MBroek | 23:3902ee714281 | 380 | encoder_motor2.reset(); |
MBroek | 15:3f3d87513a6b | 381 | break; |
MBroek | 15:3f3d87513a6b | 382 | case motor2 : |
MBroek | 23:3902ee714281 | 383 | reference_pos_m2 = get_reference_position_m2(ref_pos_prev_m2, vrijheid_m2_meter); |
MBroek | 23:3902ee714281 | 384 | position_motor2 = get_position_m2(meter_per_count, position_motor2_prev); |
MBroek | 23:3902ee714281 | 385 | PID_output_2 = PID_controller(reference_pos_m2, position_motor2, error_prev_2, error_int_2, Kp_2, Kd_2, Ki_2); |
MBroek | 20:2fdb069ffcae | 386 | PID_output_1 = PID_controller(reference_pos_m1, get_position_m1(rad_per_count), error_prev_1, error_int_1, Kp_1, Kd_1, Ki_1); // Zorgt ervoor dat motor1 op de dezelfde positie blijft als motor 2 draait. |
MBroek | 15:3f3d87513a6b | 387 | set_motor2(PID_output_2); |
MBroek | 20:2fdb069ffcae | 388 | set_motor1(PID_output_1); |
MBroek | 23:3902ee714281 | 389 | position_motor2_prev = position_motor2; |
MBroek | 15:3f3d87513a6b | 390 | break; |
MBroek | 15:3f3d87513a6b | 391 | } |
MBroek | 15:3f3d87513a6b | 392 | } |
MBroek | 15:3f3d87513a6b | 393 | } |
MBroek | 15:3f3d87513a6b | 394 | |
MBroek | 15:3f3d87513a6b | 395 | |
MBroek | 15:3f3d87513a6b | 396 | // De HIDscope debug functie ---------------------------------------------------------------- |
MBroek | 17:525b785f007a | 397 | void call_HIDscope(double input_1, double input_2, double input_3){ // Deze functie roept de HIDscope aan |
MBroek | 15:3f3d87513a6b | 398 | if (flag2){ |
MBroek | 15:3f3d87513a6b | 399 | flag2 = false; |
MBroek | 15:3f3d87513a6b | 400 | set_scope(input_1, input_2, input_3); |
MBroek | 15:3f3d87513a6b | 401 | } |
MBroek | 15:3f3d87513a6b | 402 | } |
MBroek | 15:3f3d87513a6b | 403 | |
MBroek | 15:3f3d87513a6b | 404 | |
MBroek | 15:3f3d87513a6b | 405 | |
MBroek | 0:9e053ed05c69 | 406 | |
MBroek | 0:9e053ed05c69 | 407 | // DE MAIN ================================================================================================================= |
MBroek | 0:9e053ed05c69 | 408 | int main() |
MBroek | 0:9e053ed05c69 | 409 | { |
MBroek | 0:9e053ed05c69 | 410 | pc.baud(SERIALBAUD); |
MBroek | 0:9e053ed05c69 | 411 | |
MBroek | 16:5a749b319276 | 412 | state_switch_button.fall(&state_changer); // Switcht states |
MBroek | 17:525b785f007a | 413 | motor_switch_button.fall(&motor_switch); // Switcht motors |
MBroek | 16:5a749b319276 | 414 | |
MBroek | 17:525b785f007a | 415 | |
MBroek | 13:b0d3c547bf2f | 416 | |
MBroek | 21:3db3f2d56d56 | 417 | motor_ticker.attach(&flag1_activate, T_motor_function); // Activeert de go-flag van motor positie |
MBroek | 17:525b785f007a | 418 | hidscope_ticker.attach(&flag2_activate,0.01); // Leest ref pos en mot pos uit, rekent PID uit en stuurt motoren aan. |
MBroek | 13:b0d3c547bf2f | 419 | |
MBroek | 16:5a749b319276 | 420 | while(1){ |
MBroek | 14:f51d51395803 | 421 | switch(states){ |
MBroek | 16:5a749b319276 | 422 | case off : // Dan staat het programma stil en gebeurt er niks |
MBroek | 16:5a749b319276 | 423 | robot_is_off(); |
MBroek | 14:f51d51395803 | 424 | break; |
MBroek | 21:3db3f2d56d56 | 425 | case init : // Hier voert hij alleen het initialiseren van motor 1 uit |
MBroek | 21:3db3f2d56d56 | 426 | initialise(); |
MBroek | 16:5a749b319276 | 427 | break; |
MBroek | 15:3f3d87513a6b | 428 | case run : |
MBroek | 17:525b785f007a | 429 | running_motors(); // Hier voert hij het programma voor het aansturen vd motors uit |
MBroek | 15:3f3d87513a6b | 430 | break; |
MBroek | 14:f51d51395803 | 431 | |
MBroek | 15:3f3d87513a6b | 432 | } |
MBroek | 24:ea6de9237cff | 433 | //call_HIDscope(final_signal, reference_pos_m1, get_position_m1(rad_per_count)); |
MBroek | 15:3f3d87513a6b | 434 | } |
MBroek | 15:3f3d87513a6b | 435 | } |
MBroek | 14:f51d51395803 | 436 | |
MBroek | 14:f51d51395803 | 437 | |
MBroek | 14:f51d51395803 | 438 | |
MBroek | 14:f51d51395803 | 439 | |
MBroek | 14:f51d51395803 | 440 | |
MBroek | 14:f51d51395803 | 441 | |
MBroek | 14:f51d51395803 | 442 | |
MBroek | 14:f51d51395803 | 443 | |
MBroek | 0:9e053ed05c69 | 444 | |
MBroek | 0:9e053ed05c69 | 445 | |
MBroek | 0:9e053ed05c69 | 446 | |
MBroek | 0:9e053ed05c69 | 447 | |
MBroek | 0:9e053ed05c69 | 448 | |
MBroek | 0:9e053ed05c69 | 449 | |
MBroek | 0:9e053ed05c69 | 450 | |
MBroek | 0:9e053ed05c69 | 451 | |
MBroek | 0:9e053ed05c69 | 452 | |
MBroek | 0:9e053ed05c69 | 453 | |
MBroek | 0:9e053ed05c69 | 454 | |
MBroek | 0:9e053ed05c69 | 455 | |
MBroek | 0:9e053ed05c69 | 456 | |
MBroek | 0:9e053ed05c69 | 457 | |
MBroek | 0:9e053ed05c69 | 458 | |
MBroek | 0:9e053ed05c69 | 459 | |
MBroek | 0:9e053ed05c69 | 460 | |
MBroek | 0:9e053ed05c69 | 461 | |
MBroek | 0:9e053ed05c69 | 462 | |
MBroek | 0:9e053ed05c69 | 463 | |
MBroek | 0:9e053ed05c69 | 464 | |
MBroek | 0:9e053ed05c69 | 465 | |
MBroek | 0:9e053ed05c69 | 466 | |
MBroek | 0:9e053ed05c69 | 467 | |
MBroek | 0:9e053ed05c69 | 468 | |
MBroek | 0:9e053ed05c69 | 469 | |
MBroek | 0:9e053ed05c69 | 470 | |
MBroek | 0:9e053ed05c69 | 471 | |
MBroek | 0:9e053ed05c69 | 472 | |
MBroek | 0:9e053ed05c69 | 473 | |
MBroek | 0:9e053ed05c69 | 474 | |
MBroek | 0:9e053ed05c69 | 475 | |
MBroek | 0:9e053ed05c69 | 476 | |
MBroek | 0:9e053ed05c69 | 477 | |
MBroek | 0:9e053ed05c69 | 478 | |
MBroek | 0:9e053ed05c69 | 479 | |
MBroek | 0:9e053ed05c69 | 480 | |
MBroek | 0:9e053ed05c69 | 481 | |
MBroek | 0:9e053ed05c69 | 482 | |
MBroek | 0:9e053ed05c69 | 483 | |
MBroek | 0:9e053ed05c69 | 484 | |
MBroek | 0:9e053ed05c69 | 485 | |
MBroek | 0:9e053ed05c69 | 486 | |
MBroek | 0:9e053ed05c69 | 487 | |
MBroek | 0:9e053ed05c69 | 488 | |
MBroek | 0:9e053ed05c69 | 489 | |
MBroek | 0:9e053ed05c69 | 490 | |
MBroek | 0:9e053ed05c69 | 491 | |
MBroek | 0:9e053ed05c69 | 492 | |
MBroek | 0:9e053ed05c69 | 493 | |
MBroek | 0:9e053ed05c69 | 494 | |
MBroek | 0:9e053ed05c69 | 495 | |
MBroek | 0:9e053ed05c69 | 496 | |
MBroek | 0:9e053ed05c69 | 497 |