Jorn-Jan van de Beld
/
kin_PD_project
begin van episch avontuur
main.cpp@4:95bf97b44237, 2017-10-31 (annotated)
- Committer:
- Happyfield
- Date:
- Tue Oct 31 15:41:40 2017 +0000
- Revision:
- 4:95bf97b44237
- Parent:
- 3:329acce2487c
- Child:
- 5:21e846902e3e
Bas bezig, met 2 PI's erin zetten
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JornJan | 0:4c07bb5a9f9f | 1 | #include "mbed.h" |
JornJan | 0:4c07bb5a9f9f | 2 | #include "Serial.h" |
JornJan | 0:4c07bb5a9f9f | 3 | #include "math.h" |
JornJan | 0:4c07bb5a9f9f | 4 | #include "QEI.h" |
JornJan | 0:4c07bb5a9f9f | 5 | |
JornJan | 0:4c07bb5a9f9f | 6 | // Connecties |
JornJan | 0:4c07bb5a9f9f | 7 | Serial pc(USBTX, USBRX); //Serial PC connectie |
JornJan | 2:821ae727bf8c | 8 | DigitalOut led_g(LED_GREEN); //Groene led op k64f bord |
JornJan | 0:4c07bb5a9f9f | 9 | DigitalOut motor1DirectionPin(D4); //Motorrichting op D4 (connected op het bord) |
JornJan | 0:4c07bb5a9f9f | 10 | PwmOut motor1MagnitudePin(D5); //Motorkracht op D5 (connected op het bord) |
JornJan | 2:821ae727bf8c | 11 | DigitalOut motor2DirectionPin(D7); //Motorrichting op D4 (connected op het bord) |
JornJan | 2:821ae727bf8c | 12 | PwmOut motor2MagnitudePin(D6); //Motorkracht op D5 (connected op het bord) |
JornJan | 0:4c07bb5a9f9f | 13 | QEI q1_enc(D13, D12, NC, 32); //encoder motor 1 instellen |
JornJan | 0:4c07bb5a9f9f | 14 | QEI q2_enc(D11, D10, NC, 32); // encoder motor 2 instellen |
JornJan | 0:4c07bb5a9f9f | 15 | const double pi = 3.1415926535897; // waarde voor pi aanmaken |
JornJan | 0:4c07bb5a9f9f | 16 | |
JornJan | 0:4c07bb5a9f9f | 17 | // globale gegevens |
JornJan | 0:4c07bb5a9f9f | 18 | Ticker tick_sample; // ticker voor aanroepen aansturing |
JornJan | 2:821ae727bf8c | 19 | Ticker tick_wasd; //ticker voor toetsenbord aansturing |
JornJan | 0:4c07bb5a9f9f | 20 | char key; |
JornJan | 0:4c07bb5a9f9f | 21 | double ts=0.001; |
JornJan | 0:4c07bb5a9f9f | 22 | int q1_puls; |
JornJan | 0:4c07bb5a9f9f | 23 | int q2_puls; |
JornJan | 2:821ae727bf8c | 24 | int n = 0; |
JornJan | 0:4c07bb5a9f9f | 25 | |
JornJan | 0:4c07bb5a9f9f | 26 | // kinematica gegevens |
JornJan | 0:4c07bb5a9f9f | 27 | // lengte armen |
JornJan | 0:4c07bb5a9f9f | 28 | double L1 = 0.250; |
JornJan | 0:4c07bb5a9f9f | 29 | double L2 = 0.355; |
JornJan | 0:4c07bb5a9f9f | 30 | double L3 = 0.150; |
JornJan | 0:4c07bb5a9f9f | 31 | |
JornJan | 0:4c07bb5a9f9f | 32 | // reference position |
JornJan | 3:329acce2487c | 33 | double q1=0; // positie q1 in radialen |
JornJan | 0:4c07bb5a9f9f | 34 | double q2=0; // positie q2 in radialen |
JornJan | 0:4c07bb5a9f9f | 35 | double q1_pos; |
JornJan | 0:4c07bb5a9f9f | 36 | double q2_pos; |
JornJan | 0:4c07bb5a9f9f | 37 | |
JornJan | 0:4c07bb5a9f9f | 38 | // EMG Input_k |
JornJan | 0:4c07bb5a9f9f | 39 | double vx = 0; |
JornJan | 0:4c07bb5a9f9f | 40 | double vy = 0; |
JornJan | 0:4c07bb5a9f9f | 41 | |
JornJan | 0:4c07bb5a9f9f | 42 | |
JornJan | 0:4c07bb5a9f9f | 43 | // PID gegevens |
JornJan | 0:4c07bb5a9f9f | 44 | double pulses2rad; |
JornJan | 0:4c07bb5a9f9f | 45 | double position; |
JornJan | 0:4c07bb5a9f9f | 46 | double ref1; |
JornJan | 0:4c07bb5a9f9f | 47 | double ref2; |
JornJan | 0:4c07bb5a9f9f | 48 | double PD1; |
JornJan | 0:4c07bb5a9f9f | 49 | double PD2; |
JornJan | 2:821ae727bf8c | 50 | double error1=0; |
JornJan | 2:821ae727bf8c | 51 | double error1m=0; |
JornJan | 2:821ae727bf8c | 52 | double error2=0; |
JornJan | 2:821ae727bf8c | 53 | double error2m=0; |
JornJan | 0:4c07bb5a9f9f | 54 | |
JornJan | 0:4c07bb5a9f9f | 55 | double Kp; // proportional coefficient ( |
JornJan | 0:4c07bb5a9f9f | 56 | double Kd; // differential coefficient |
JornJan | 3:329acce2487c | 57 | double Ki; // integrating coefficient |
JornJan | 0:4c07bb5a9f9f | 58 | |
JornJan | 0:4c07bb5a9f9f | 59 | |
JornJan | 2:821ae727bf8c | 60 | void wasd() |
JornJan | 0:4c07bb5a9f9f | 61 | { |
JornJan | 2:821ae727bf8c | 62 | static char oldkey = 'p'; |
JornJan | 2:821ae727bf8c | 63 | static double oldvx = 0; |
JornJan | 2:821ae727bf8c | 64 | static double oldvy = 0; |
JornJan | 2:821ae727bf8c | 65 | |
JornJan | 0:4c07bb5a9f9f | 66 | if(pc.readable()==true) |
JornJan | 0:4c07bb5a9f9f | 67 | { key = pc.getc(); |
JornJan | 2:821ae727bf8c | 68 | |
JornJan | 0:4c07bb5a9f9f | 69 | if (key=='a') |
JornJan | 0:4c07bb5a9f9f | 70 | { |
JornJan | 1:b6a079ca16e0 | 71 | vx = 0.04; //referentie snelheid m/s |
JornJan | 0:4c07bb5a9f9f | 72 | vy = 0; |
JornJan | 0:4c07bb5a9f9f | 73 | } |
JornJan | 0:4c07bb5a9f9f | 74 | else if(key=='d') |
JornJan | 0:4c07bb5a9f9f | 75 | { |
JornJan | 1:b6a079ca16e0 | 76 | vx = -0.04; |
JornJan | 0:4c07bb5a9f9f | 77 | vy = 0; |
JornJan | 0:4c07bb5a9f9f | 78 | } |
JornJan | 0:4c07bb5a9f9f | 79 | else if(key=='w') |
JornJan | 0:4c07bb5a9f9f | 80 | { |
JornJan | 0:4c07bb5a9f9f | 81 | vx = 0; |
JornJan | 1:b6a079ca16e0 | 82 | vy = 0.04; |
JornJan | 0:4c07bb5a9f9f | 83 | } |
JornJan | 0:4c07bb5a9f9f | 84 | else if(key=='s') |
JornJan | 0:4c07bb5a9f9f | 85 | { |
JornJan | 0:4c07bb5a9f9f | 86 | vx = 0; |
JornJan | 1:b6a079ca16e0 | 87 | vy = -0.04; |
JornJan | 0:4c07bb5a9f9f | 88 | } |
JornJan | 2:821ae727bf8c | 89 | else |
JornJan | 0:4c07bb5a9f9f | 90 | { |
JornJan | 2:821ae727bf8c | 91 | key = oldkey; |
JornJan | 2:821ae727bf8c | 92 | vx = oldvx; |
JornJan | 2:821ae727bf8c | 93 | vy = oldvy; |
JornJan | 0:4c07bb5a9f9f | 94 | } |
JornJan | 2:821ae727bf8c | 95 | |
JornJan | 2:821ae727bf8c | 96 | } //einde eerste if statemnet |
JornJan | 2:821ae727bf8c | 97 | |
JornJan | 2:821ae727bf8c | 98 | else if (pc.readable()==false) |
JornJan | 0:4c07bb5a9f9f | 99 | { |
JornJan | 0:4c07bb5a9f9f | 100 | vx=0; |
JornJan | 2:821ae727bf8c | 101 | vy=0; |
JornJan | 2:821ae727bf8c | 102 | key='p'; |
JornJan | 0:4c07bb5a9f9f | 103 | } |
JornJan | 2:821ae727bf8c | 104 | oldkey = key; |
JornJan | 2:821ae727bf8c | 105 | oldvx = vx; |
JornJan | 2:821ae727bf8c | 106 | oldvy = vy; |
JornJan | 2:821ae727bf8c | 107 | |
JornJan | 0:4c07bb5a9f9f | 108 | } |
JornJan | 0:4c07bb5a9f9f | 109 | |
JornJan | 2:821ae727bf8c | 110 | void kinematics() |
JornJan | 0:4c07bb5a9f9f | 111 | { |
JornJan | 0:4c07bb5a9f9f | 112 | |
JornJan | 2:821ae727bf8c | 113 | q1 = ((-(L3 + L2*cos(q1 - q2))/(L1*L2*cos(2*q1 - q2) + L1*L3*cos(q1)))*vx + ((L2*sin(q1 - q2))/(L1*L2*cos(2*q1 - q2) + L1*L3*cos(q1)))*vy) * ts + q1_pos; |
JornJan | 2:821ae727bf8c | 114 | q2 = (((L3 - L1*sin(q1) + L2*cos(q1 - q2))/(L1*L2*cos(2*q1 - q2) + L1*L3*cos(q1))*vx) + ((L1*cos(q1) - L2*sin(q1 - q2))/(L1*L2*cos(2*q1 - q2) + L1*L3*cos(q1)))*vy) * ts + q2_pos; |
JornJan | 0:4c07bb5a9f9f | 115 | } |
JornJan | 0:4c07bb5a9f9f | 116 | |
JornJan | 2:821ae727bf8c | 117 | void controller(double q1ref, double q2ref) |
JornJan | 0:4c07bb5a9f9f | 118 | { |
JornJan | 0:4c07bb5a9f9f | 119 | // error = qset |
JornJan | 0:4c07bb5a9f9f | 120 | // referentie bepalen |
JornJan | 0:4c07bb5a9f9f | 121 | |
JornJan | 2:821ae727bf8c | 122 | error1 = q1ref - q1_pos; |
JornJan | 2:821ae727bf8c | 123 | error2 = q2ref - q2_pos; |
JornJan | 0:4c07bb5a9f9f | 124 | |
JornJan | 0:4c07bb5a9f9f | 125 | //PD |
JornJan | 2:821ae727bf8c | 126 | PD1 = Kp*(error1) + Kd*((error1m-error1)/ts) ; // PD sum; PD = proportianal + differential |
JornJan | 2:821ae727bf8c | 127 | error1m = error1; // waarde voor qset wordt opgeslagen (m = memory) |
JornJan | 2:821ae727bf8c | 128 | PD2 = Kp*(error2) + Kd*((error2m-error2)/ts) ; |
JornJan | 2:821ae727bf8c | 129 | error2m = error2; |
JornJan | 0:4c07bb5a9f9f | 130 | |
JornJan | 0:4c07bb5a9f9f | 131 | //Motor control |
JornJan | 0:4c07bb5a9f9f | 132 | if (PD1<0 && PD2<0 ) |
JornJan | 0:4c07bb5a9f9f | 133 | { |
JornJan | 0:4c07bb5a9f9f | 134 | motor1MagnitudePin = fabs(PD1); |
JornJan | 2:821ae727bf8c | 135 | motor1DirectionPin = 0; |
JornJan | 0:4c07bb5a9f9f | 136 | motor2MagnitudePin = fabs(PD2); |
JornJan | 0:4c07bb5a9f9f | 137 | motor2DirectionPin = 1; |
JornJan | 0:4c07bb5a9f9f | 138 | } |
JornJan | 0:4c07bb5a9f9f | 139 | else if (PD1>0 && PD2<0) |
JornJan | 0:4c07bb5a9f9f | 140 | { |
JornJan | 0:4c07bb5a9f9f | 141 | motor1MagnitudePin = fabs(PD1); |
JornJan | 2:821ae727bf8c | 142 | motor1DirectionPin = 1; |
JornJan | 0:4c07bb5a9f9f | 143 | motor2MagnitudePin = fabs(PD2); |
JornJan | 0:4c07bb5a9f9f | 144 | motor2DirectionPin = 1; |
JornJan | 0:4c07bb5a9f9f | 145 | } |
JornJan | 0:4c07bb5a9f9f | 146 | |
JornJan | 0:4c07bb5a9f9f | 147 | else if (PD1<0 && PD2>0) |
JornJan | 0:4c07bb5a9f9f | 148 | { |
JornJan | 0:4c07bb5a9f9f | 149 | motor1MagnitudePin = fabs(PD1); |
JornJan | 2:821ae727bf8c | 150 | motor1DirectionPin = 0; |
JornJan | 0:4c07bb5a9f9f | 151 | motor2MagnitudePin = fabs(PD2); |
JornJan | 0:4c07bb5a9f9f | 152 | motor2DirectionPin = 0; |
JornJan | 0:4c07bb5a9f9f | 153 | } |
JornJan | 0:4c07bb5a9f9f | 154 | |
JornJan | 0:4c07bb5a9f9f | 155 | else if (PD1>0 && PD2>0) |
JornJan | 0:4c07bb5a9f9f | 156 | { |
JornJan | 0:4c07bb5a9f9f | 157 | motor1MagnitudePin = fabs(PD1); |
JornJan | 2:821ae727bf8c | 158 | motor1DirectionPin = 1; |
JornJan | 0:4c07bb5a9f9f | 159 | motor2MagnitudePin = fabs(PD2); |
JornJan | 0:4c07bb5a9f9f | 160 | motor2DirectionPin = 0; |
JornJan | 0:4c07bb5a9f9f | 161 | } |
JornJan | 0:4c07bb5a9f9f | 162 | |
JornJan | 0:4c07bb5a9f9f | 163 | |
JornJan | 0:4c07bb5a9f9f | 164 | |
JornJan | 0:4c07bb5a9f9f | 165 | } |
Happyfield | 4:95bf97b44237 | 166 | |
Happyfield | 4:95bf97b44237 | 167 | void controller(double q1ref, double q2ref) //HIER WAS JE BEZIG@!#!@#!@#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ |
Happyfield | 4:95bf97b44237 | 168 | { |
Happyfield | 4:95bf97b44237 | 169 | pulses1 = q1_enc.getPulses(); |
Happyfield | 4:95bf97b44237 | 170 | error1_1 = ref1 - pulses1; |
Happyfield | 4:95bf97b44237 | 171 | error1_I = error1_I2 + ts*((error1_1 - error2_1)/2); |
Happyfield | 4:95bf97b44237 | 172 | |
Happyfield | 4:95bf97b44237 | 173 | PI = Kp*error1 + Ki*(error_I); |
Happyfield | 4:95bf97b44237 | 174 | |
Happyfield | 4:95bf97b44237 | 175 | error2 = error1; |
Happyfield | 4:95bf97b44237 | 176 | error_I2 = error_I; |
Happyfield | 4:95bf97b44237 | 177 | |
Happyfield | 4:95bf97b44237 | 178 | //Motor control |
Happyfield | 4:95bf97b44237 | 179 | if (PI<0 ) |
Happyfield | 4:95bf97b44237 | 180 | { |
Happyfield | 4:95bf97b44237 | 181 | motor2DirectionPin = 0; |
Happyfield | 4:95bf97b44237 | 182 | motor2MagnitudePin = fabs(PI); |
Happyfield | 4:95bf97b44237 | 183 | |
Happyfield | 4:95bf97b44237 | 184 | } |
Happyfield | 4:95bf97b44237 | 185 | else if (PI>0) |
Happyfield | 4:95bf97b44237 | 186 | { |
Happyfield | 4:95bf97b44237 | 187 | motor2DirectionPin = 1; |
Happyfield | 4:95bf97b44237 | 188 | motor2MagnitudePin = fabs(PI); |
Happyfield | 4:95bf97b44237 | 189 | } |
Happyfield | 4:95bf97b44237 | 190 | } |
JornJan | 0:4c07bb5a9f9f | 191 | |
JornJan | 0:4c07bb5a9f9f | 192 | void aansturing() |
JornJan | 0:4c07bb5a9f9f | 193 | { |
JornJan | 0:4c07bb5a9f9f | 194 | // encoders uitlezen |
JornJan | 0:4c07bb5a9f9f | 195 | q1_puls = q1_enc.getPulses(); |
JornJan | 0:4c07bb5a9f9f | 196 | q1_pos = q1_puls*pulses2rad; // berekent positie q1 in radialen |
JornJan | 0:4c07bb5a9f9f | 197 | q2_puls = q2_enc.getPulses(); |
JornJan | 0:4c07bb5a9f9f | 198 | q2_pos = q2_puls*pulses2rad; // berekent positie q2 in radialen |
JornJan | 2:821ae727bf8c | 199 | |
JornJan | 0:4c07bb5a9f9f | 200 | // kinematica bepaald gewenste q1 en q2 referenties afhankelijk van ingegeven vx en vy |
JornJan | 2:821ae727bf8c | 201 | kinematics(); |
JornJan | 2:821ae727bf8c | 202 | |
JornJan | 0:4c07bb5a9f9f | 203 | // PD controller gebruikt PD control en stuurt motor aan |
JornJan | 0:4c07bb5a9f9f | 204 | controller(q1, q2); |
JornJan | 2:821ae727bf8c | 205 | |
JornJan | 2:821ae727bf8c | 206 | if(n==500){ |
JornJan | 2:821ae727bf8c | 207 | printf("motor1 = %f motor2 = %f\n\r", PD1, PD2); |
JornJan | 2:821ae727bf8c | 208 | n=0; |
JornJan | 2:821ae727bf8c | 209 | } |
JornJan | 2:821ae727bf8c | 210 | else{ |
JornJan | 2:821ae727bf8c | 211 | n=n+1; |
JornJan | 2:821ae727bf8c | 212 | } |
JornJan | 0:4c07bb5a9f9f | 213 | } |
JornJan | 0:4c07bb5a9f9f | 214 | |
JornJan | 0:4c07bb5a9f9f | 215 | |
JornJan | 0:4c07bb5a9f9f | 216 | int main() |
JornJan | 0:4c07bb5a9f9f | 217 | { |
JornJan | 0:4c07bb5a9f9f | 218 | pc.baud(115200); |
JornJan | 0:4c07bb5a9f9f | 219 | pulses2rad=(2*pi)/4200; |
JornJan | 3:329acce2487c | 220 | Kp = 100; |
JornJan | 3:329acce2487c | 221 | Ki = ; |
JornJan | 2:821ae727bf8c | 222 | tick_sample.attach_us(&aansturing, 1000); //sample frequency 1000 Hz; |
JornJan | 2:821ae727bf8c | 223 | tick_wasd.attach(&wasd, 0.1); |
JornJan | 2:821ae727bf8c | 224 | led_g = 0; |
JornJan | 2:821ae727bf8c | 225 | |
JornJan | 0:4c07bb5a9f9f | 226 | while(true) |
JornJan | 0:4c07bb5a9f9f | 227 | { |
JornJan | 2:821ae727bf8c | 228 | |
JornJan | 0:4c07bb5a9f9f | 229 | } |
JornJan | 0:4c07bb5a9f9f | 230 | |
JornJan | 0:4c07bb5a9f9f | 231 | |
JornJan | 0:4c07bb5a9f9f | 232 | } |