![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
begin van episch avontuur
main.cpp@6:c97264a28123, 2017-10-31 (annotated)
- Committer:
- Happyfield
- Date:
- Tue Oct 31 21:37:31 2017 +0000
- Revision:
- 6:c97264a28123
- Parent:
- 5:21e846902e3e
- Child:
- 7:9e965efc430e
Alles samen, maar iets is mis
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) |
Happyfield | 5:21e846902e3e | 13 | QEI q1_enc(D13, D12, NC, 32); //encoder motor 1 instellen |
Happyfield | 5:21e846902e3e | 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; |
Happyfield | 6:c97264a28123 | 45 | double rad2pulses; |
JornJan | 0:4c07bb5a9f9f | 46 | double position; |
JornJan | 0:4c07bb5a9f9f | 47 | double ref1; |
JornJan | 0:4c07bb5a9f9f | 48 | double ref2; |
Happyfield | 6:c97264a28123 | 49 | double PI1; |
Happyfield | 6:c97264a28123 | 50 | double PI2; |
Happyfield | 5:21e846902e3e | 51 | double error1_1=0; |
Happyfield | 5:21e846902e3e | 52 | double error1_2=0; |
Happyfield | 5:21e846902e3e | 53 | double error2_1=0; |
Happyfield | 5:21e846902e3e | 54 | double error2_2=0; |
Happyfield | 5:21e846902e3e | 55 | double error_I_1=0; |
Happyfield | 5:21e846902e3e | 56 | double error_I2_1=0; |
Happyfield | 5:21e846902e3e | 57 | double error_I_2=0; |
Happyfield | 5:21e846902e3e | 58 | double error_I2_2=0; |
JornJan | 0:4c07bb5a9f9f | 59 | |
Happyfield | 6:c97264a28123 | 60 | double Kp1 = 0.0001; // proportional coefficient motor 1 |
Happyfield | 6:c97264a28123 | 61 | double Ki1 = 0.0002; // integrating coefficient motor 1 |
JornJan | 0:4c07bb5a9f9f | 62 | |
Happyfield | 6:c97264a28123 | 63 | double Kp2 = 0.00015; // proportional coefficient motor 2 |
Happyfield | 6:c97264a28123 | 64 | double Ki2 = 0.0035; // integrating coefficient motor 2 |
JornJan | 0:4c07bb5a9f9f | 65 | |
JornJan | 2:821ae727bf8c | 66 | void wasd() |
JornJan | 0:4c07bb5a9f9f | 67 | { |
JornJan | 2:821ae727bf8c | 68 | static char oldkey = 'p'; |
JornJan | 2:821ae727bf8c | 69 | static double oldvx = 0; |
JornJan | 2:821ae727bf8c | 70 | static double oldvy = 0; |
JornJan | 2:821ae727bf8c | 71 | |
JornJan | 0:4c07bb5a9f9f | 72 | if(pc.readable()==true) |
JornJan | 0:4c07bb5a9f9f | 73 | { key = pc.getc(); |
JornJan | 2:821ae727bf8c | 74 | |
JornJan | 0:4c07bb5a9f9f | 75 | if (key=='a') |
JornJan | 0:4c07bb5a9f9f | 76 | { |
JornJan | 1:b6a079ca16e0 | 77 | vx = 0.04; //referentie snelheid m/s |
JornJan | 0:4c07bb5a9f9f | 78 | vy = 0; |
JornJan | 0:4c07bb5a9f9f | 79 | } |
JornJan | 0:4c07bb5a9f9f | 80 | else if(key=='d') |
JornJan | 0:4c07bb5a9f9f | 81 | { |
JornJan | 1:b6a079ca16e0 | 82 | vx = -0.04; |
JornJan | 0:4c07bb5a9f9f | 83 | vy = 0; |
JornJan | 0:4c07bb5a9f9f | 84 | } |
JornJan | 0:4c07bb5a9f9f | 85 | else if(key=='w') |
JornJan | 0:4c07bb5a9f9f | 86 | { |
JornJan | 0:4c07bb5a9f9f | 87 | vx = 0; |
JornJan | 1:b6a079ca16e0 | 88 | vy = 0.04; |
JornJan | 0:4c07bb5a9f9f | 89 | } |
JornJan | 0:4c07bb5a9f9f | 90 | else if(key=='s') |
JornJan | 0:4c07bb5a9f9f | 91 | { |
JornJan | 0:4c07bb5a9f9f | 92 | vx = 0; |
JornJan | 1:b6a079ca16e0 | 93 | vy = -0.04; |
JornJan | 0:4c07bb5a9f9f | 94 | } |
JornJan | 2:821ae727bf8c | 95 | else |
JornJan | 0:4c07bb5a9f9f | 96 | { |
JornJan | 2:821ae727bf8c | 97 | key = oldkey; |
JornJan | 2:821ae727bf8c | 98 | vx = oldvx; |
JornJan | 2:821ae727bf8c | 99 | vy = oldvy; |
JornJan | 0:4c07bb5a9f9f | 100 | } |
JornJan | 2:821ae727bf8c | 101 | |
JornJan | 2:821ae727bf8c | 102 | } //einde eerste if statemnet |
JornJan | 2:821ae727bf8c | 103 | |
JornJan | 2:821ae727bf8c | 104 | else if (pc.readable()==false) |
JornJan | 0:4c07bb5a9f9f | 105 | { |
JornJan | 0:4c07bb5a9f9f | 106 | vx=0; |
JornJan | 2:821ae727bf8c | 107 | vy=0; |
JornJan | 2:821ae727bf8c | 108 | key='p'; |
JornJan | 0:4c07bb5a9f9f | 109 | } |
JornJan | 2:821ae727bf8c | 110 | oldkey = key; |
JornJan | 2:821ae727bf8c | 111 | oldvx = vx; |
JornJan | 2:821ae727bf8c | 112 | oldvy = vy; |
JornJan | 2:821ae727bf8c | 113 | |
JornJan | 0:4c07bb5a9f9f | 114 | } |
JornJan | 0:4c07bb5a9f9f | 115 | |
JornJan | 2:821ae727bf8c | 116 | void kinematics() |
JornJan | 0:4c07bb5a9f9f | 117 | { |
JornJan | 0:4c07bb5a9f9f | 118 | |
Happyfield | 5:21e846902e3e | 119 | q1 = ((-(L3 + L2*cos(q1_pos - q2_pos))/(L1*L2*cos(2*q1_pos - q2_pos) + L1*L3*cos(q1_pos)))*vx + ((L2*sin(q1_pos - q2_pos))/(L1*L2*cos(2*q1_pos - q2_pos) + L1*L3*cos(q1_pos)))*vy) * ts + q1_pos; |
Happyfield | 5:21e846902e3e | 120 | q2 = (((L3 - L1*sin(q1_pos) + L2*cos(q1_pos - q2_pos))/(L1*L2*cos(2*q1_pos - q2_pos) + L1*L3*cos(q1_pos))*vx) + ((L1*cos(q1_pos) - L2*sin(q1_pos - q2_pos))/(L1*L2*cos(2*q1_pos - q2_pos) + L1*L3*cos(q1_pos)))*vy) * ts + q2_pos; |
Happyfield | 5:21e846902e3e | 121 | |
Happyfield | 5:21e846902e3e | 122 | ref1 = q1*rad2pulses; |
Happyfield | 5:21e846902e3e | 123 | ref2 = q2*rad2pulses; |
JornJan | 0:4c07bb5a9f9f | 124 | } |
JornJan | 0:4c07bb5a9f9f | 125 | |
Happyfield | 5:21e846902e3e | 126 | void controller() |
Happyfield | 4:95bf97b44237 | 127 | { |
Happyfield | 5:21e846902e3e | 128 | //PID 1 |
Happyfield | 5:21e846902e3e | 129 | error1_1 = ref1 - q1_puls; |
Happyfield | 5:21e846902e3e | 130 | error_I_1 = error_I2_1 + ts*((error1_1 - error2_1)/2); |
Happyfield | 5:21e846902e3e | 131 | |
Happyfield | 5:21e846902e3e | 132 | PI1 = Kp1*error1_1 + Ki1*(error_I_1); |
Happyfield | 5:21e846902e3e | 133 | |
Happyfield | 5:21e846902e3e | 134 | error2_1 = error1_1; |
Happyfield | 5:21e846902e3e | 135 | error_I2_1 = error_I_1; |
Happyfield | 5:21e846902e3e | 136 | |
Happyfield | 5:21e846902e3e | 137 | //PID 2 |
Happyfield | 5:21e846902e3e | 138 | error1_2 = ref2 - q2_puls; |
Happyfield | 5:21e846902e3e | 139 | error_I_2 = error_I2_2 + ts*((error1_2 - error2_2)/2); |
Happyfield | 5:21e846902e3e | 140 | |
Happyfield | 5:21e846902e3e | 141 | PI2 = Kp2*error1_2 + Ki2*(error_I_2); |
Happyfield | 4:95bf97b44237 | 142 | |
Happyfield | 5:21e846902e3e | 143 | error2_2 = error1_2; |
Happyfield | 5:21e846902e3e | 144 | error_I2_2 = error_I_2; |
Happyfield | 5:21e846902e3e | 145 | |
Happyfield | 6:c97264a28123 | 146 | //Motor control |
Happyfield | 6:c97264a28123 | 147 | if (PI1<0 && PI2<0) |
Happyfield | 5:21e846902e3e | 148 | { |
Happyfield | 5:21e846902e3e | 149 | motor1DirectionPin = 0; |
Happyfield | 5:21e846902e3e | 150 | motor1MagnitudePin = fabs(PI1); |
Happyfield | 4:95bf97b44237 | 151 | motor2DirectionPin = 0; |
Happyfield | 5:21e846902e3e | 152 | motor2MagnitudePin = fabs(PI2); |
Happyfield | 4:95bf97b44237 | 153 | |
Happyfield | 4:95bf97b44237 | 154 | } |
Happyfield | 6:c97264a28123 | 155 | else if (PI1>0 && PI2<0) |
Happyfield | 6:c97264a28123 | 156 | { |
Happyfield | 6:c97264a28123 | 157 | motor1DirectionPin = 1; |
Happyfield | 6:c97264a28123 | 158 | motor1MagnitudePin = fabs(PI1); |
Happyfield | 6:c97264a28123 | 159 | motor2DirectionPin = 0; |
Happyfield | 6:c97264a28123 | 160 | motor2MagnitudePin = fabs(PI2); |
Happyfield | 6:c97264a28123 | 161 | } |
Happyfield | 6:c97264a28123 | 162 | |
Happyfield | 6:c97264a28123 | 163 | else if (PI1<0 && PI2>0) |
Happyfield | 4:95bf97b44237 | 164 | { |
Happyfield | 6:c97264a28123 | 165 | motor1DirectionPin = 0; |
Happyfield | 6:c97264a28123 | 166 | motor1MagnitudePin = fabs(PI1); |
Happyfield | 6:c97264a28123 | 167 | motor2DirectionPin = 1; |
Happyfield | 6:c97264a28123 | 168 | motor2MagnitudePin = fabs(PI2); |
Happyfield | 6:c97264a28123 | 169 | } |
Happyfield | 6:c97264a28123 | 170 | |
Happyfield | 6:c97264a28123 | 171 | else if (PI1>0 && PI2>0) |
Happyfield | 6:c97264a28123 | 172 | { |
Happyfield | 6:c97264a28123 | 173 | motor1DirectionPin = 1; |
Happyfield | 6:c97264a28123 | 174 | motor1MagnitudePin = fabs(PI1); |
Happyfield | 4:95bf97b44237 | 175 | motor2DirectionPin = 1; |
Happyfield | 5:21e846902e3e | 176 | motor2MagnitudePin = fabs(PI2); |
Happyfield | 4:95bf97b44237 | 177 | } |
Happyfield | 4:95bf97b44237 | 178 | } |
JornJan | 0:4c07bb5a9f9f | 179 | |
JornJan | 0:4c07bb5a9f9f | 180 | void aansturing() |
Happyfield | 6:c97264a28123 | 181 | { |
JornJan | 0:4c07bb5a9f9f | 182 | // encoders uitlezen |
JornJan | 0:4c07bb5a9f9f | 183 | q1_puls = q1_enc.getPulses(); |
JornJan | 0:4c07bb5a9f9f | 184 | q1_pos = q1_puls*pulses2rad; // berekent positie q1 in radialen |
JornJan | 0:4c07bb5a9f9f | 185 | q2_puls = q2_enc.getPulses(); |
JornJan | 0:4c07bb5a9f9f | 186 | q2_pos = q2_puls*pulses2rad; // berekent positie q2 in radialen |
JornJan | 2:821ae727bf8c | 187 | |
JornJan | 0:4c07bb5a9f9f | 188 | // kinematica bepaald gewenste q1 en q2 referenties afhankelijk van ingegeven vx en vy |
JornJan | 2:821ae727bf8c | 189 | kinematics(); |
JornJan | 2:821ae727bf8c | 190 | |
JornJan | 0:4c07bb5a9f9f | 191 | // PD controller gebruikt PD control en stuurt motor aan |
Happyfield | 6:c97264a28123 | 192 | controller(); |
Happyfield | 6:c97264a28123 | 193 | } |
JornJan | 0:4c07bb5a9f9f | 194 | |
JornJan | 0:4c07bb5a9f9f | 195 | |
JornJan | 0:4c07bb5a9f9f | 196 | int main() |
JornJan | 0:4c07bb5a9f9f | 197 | { |
JornJan | 0:4c07bb5a9f9f | 198 | pc.baud(115200); |
JornJan | 0:4c07bb5a9f9f | 199 | pulses2rad=(2*pi)/4200; |
Happyfield | 5:21e846902e3e | 200 | rad2pulses=4200/(2*pi); |
Happyfield | 6:c97264a28123 | 201 | tick_wasd.attach_us(&wasd, 1000); |
JornJan | 2:821ae727bf8c | 202 | tick_sample.attach_us(&aansturing, 1000); //sample frequency 1000 Hz; |
JornJan | 2:821ae727bf8c | 203 | led_g = 0; |
JornJan | 2:821ae727bf8c | 204 | |
JornJan | 0:4c07bb5a9f9f | 205 | while(true) |
JornJan | 0:4c07bb5a9f9f | 206 | { |
JornJan | 2:821ae727bf8c | 207 | |
JornJan | 0:4c07bb5a9f9f | 208 | } |
JornJan | 0:4c07bb5a9f9f | 209 | } |