![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
kom op
main.cpp@5:949c2861a79b, 2017-11-02 (annotated)
- Committer:
- JornJan
- Date:
- Thu Nov 02 18:40:41 2017 +0000
- Revision:
- 5:949c2861a79b
- Parent:
- 4:e98ad06f227c
pittige avondsessie;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JornJan | 0:921402a95675 | 1 | #include "mbed.h" |
JornJan | 0:921402a95675 | 2 | #include "Serial.h" |
JornJan | 0:921402a95675 | 3 | #include "math.h" |
JornJan | 0:921402a95675 | 4 | #include "QEI.h" |
JornJan | 0:921402a95675 | 5 | |
JornJan | 0:921402a95675 | 6 | Serial pc(USBTX, USBRX); //Serial PC connectie |
JornJan | 0:921402a95675 | 7 | DigitalOut motor1DirectionPin(D4); //Motorrichting op D4 (connected op het bord) |
JornJan | 0:921402a95675 | 8 | PwmOut motor1MagnitudePin(D5); //Motorkracht op D5 (connected op het bord) |
JornJan | 0:921402a95675 | 9 | DigitalOut motor2DirectionPin(D7); //Motorrichting op D4 (connected op het bord) |
JornJan | 0:921402a95675 | 10 | PwmOut motor2MagnitudePin(D6); //Motorkracht op D5 (connected op het bord) |
JornJan | 0:921402a95675 | 11 | QEI q1_enc(D13, D12, NC, 32); //encoder motor 1 instellen |
JornJan | 0:921402a95675 | 12 | QEI q2_enc(D11, D10, NC, 32); // encoder motor 2 instellen |
JornJan | 0:921402a95675 | 13 | const double pi = 3.1415926535897; // waarde voor pi aanmaken |
JornJan | 0:921402a95675 | 14 | |
JornJan | 0:921402a95675 | 15 | Timer t; |
JornJan | 0:921402a95675 | 16 | Ticker aansturing; |
JornJan | 0:921402a95675 | 17 | Ticker stepres; |
JornJan | 0:921402a95675 | 18 | |
JornJan | 0:921402a95675 | 19 | double ref1 = 0; |
JornJan | 0:921402a95675 | 20 | double refrad1; |
JornJan | 0:921402a95675 | 21 | double refrad2; |
JornJan | 0:921402a95675 | 22 | double ref2 = 0; |
JornJan | 0:921402a95675 | 23 | double Kp1 = 0.002; //kp motor 2 = 0.0001 |
JornJan | 0:921402a95675 | 24 | double Ki1 = 0; |
JornJan | 4:e98ad06f227c | 25 | double Kp2 = 0.0005; |
JornJan | 4:e98ad06f227c | 26 | double Ki2 = 0.0005; |
JornJan | 0:921402a95675 | 27 | int q1_puls; |
JornJan | 0:921402a95675 | 28 | int q2_puls; |
JornJan | 3:3b5b85a32c9a | 29 | double rad2pulses=(2100/pi); |
JornJan | 4:e98ad06f227c | 30 | double pulses2rad = (pi)/2100; |
JornJan | 0:921402a95675 | 31 | double ts = 0.001; |
JornJan | 0:921402a95675 | 32 | double PI1; |
JornJan | 0:921402a95675 | 33 | int n; |
JornJan | 0:921402a95675 | 34 | |
JornJan | 5:949c2861a79b | 35 | double stap = 0.01; |
JornJan | 5:949c2861a79b | 36 | |
JornJan | 0:921402a95675 | 37 | double error1_1; |
JornJan | 0:921402a95675 | 38 | double error2_1 = 0; |
JornJan | 0:921402a95675 | 39 | double error_I_1; |
JornJan | 0:921402a95675 | 40 | double error_I2_1 = 0; |
JornJan | 0:921402a95675 | 41 | double error1_2; |
JornJan | 0:921402a95675 | 42 | double error2_2 = 0; |
JornJan | 0:921402a95675 | 43 | double error_I_2; |
JornJan | 0:921402a95675 | 44 | double error_I2_2 = 0; |
JornJan | 0:921402a95675 | 45 | double PI2; |
JornJan | 0:921402a95675 | 46 | |
JornJan | 2:0686b2132556 | 47 | // kinematica gegevens |
JornJan | 2:0686b2132556 | 48 | // lengte armen |
JornJan | 2:0686b2132556 | 49 | double L1 = 0.250; |
JornJan | 2:0686b2132556 | 50 | double L2 = 0.355; |
JornJan | 2:0686b2132556 | 51 | double L3 = 0.150; |
JornJan | 2:0686b2132556 | 52 | |
JornJan | 2:0686b2132556 | 53 | // reference position |
JornJan | 2:0686b2132556 | 54 | double q1=0; // positie q1 in radialen |
JornJan | 2:0686b2132556 | 55 | double q2=0; // positie q2 in radialen |
JornJan | 2:0686b2132556 | 56 | double q1_pos; |
JornJan | 2:0686b2132556 | 57 | double q2_pos; |
JornJan | 2:0686b2132556 | 58 | |
JornJan | 2:0686b2132556 | 59 | // EMG Input_k |
JornJan | 5:949c2861a79b | 60 | double vx = 0; |
JornJan | 5:949c2861a79b | 61 | double vy = 0; |
JornJan | 2:0686b2132556 | 62 | |
JornJan | 2:0686b2132556 | 63 | |
JornJan | 2:0686b2132556 | 64 | void kinematica() |
JornJan | 2:0686b2132556 | 65 | { |
JornJan | 2:0686b2132556 | 66 | // encoders uitlezen |
JornJan | 4:e98ad06f227c | 67 | q1_puls = q1_enc.getPulses(); |
JornJan | 3:3b5b85a32c9a | 68 | q1_pos = q1_puls*pulses2rad; // berekent positie q1 in radialen |
JornJan | 2:0686b2132556 | 69 | q2_puls = q2_enc.getPulses(); |
JornJan | 3:3b5b85a32c9a | 70 | q2_pos = q2_puls*pulses2rad; // berekent positie q2 in radialen |
JornJan | 2:0686b2132556 | 71 | |
JornJan | 5:949c2861a79b | 72 | |
JornJan | 5:949c2861a79b | 73 | px = oldpx + stap; |
JornJan | 5:949c2861a79b | 74 | py = oldpy + stap; |
JornJan | 5:949c2861a79b | 75 | |
JornJan | 5:949c2861a79b | 76 | if (px >= pxmax || px <= pxmin) |
JornJan | 5:949c2861a79b | 77 | { |
JornJan | 5:949c2861a79b | 78 | vx = oldvx; |
JornJan | 5:949c2861a79b | 79 | } |
JornJan | 5:949c2861a79b | 80 | else if (py >= pymax || py <= pymin) |
JornJan | 5:949c2861a79b | 81 | { |
JornJan | 5:949c2861a79b | 82 | vy = oldvy; |
JornJan | 5:949c2861a79b | 83 | } |
JornJan | 2:0686b2132556 | 84 | |
JornJan | 4:e98ad06f227c | 85 | q1 = |
JornJan | 4:e98ad06f227c | 86 | q2 = |
JornJan | 5:949c2861a79b | 87 | |
JornJan | 5:949c2861a79b | 88 | ref1 = q1 * rad2pulses; |
JornJan | 5:949c2861a79b | 89 | ref2 = q2 * rad2pulses; |
JornJan | 3:3b5b85a32c9a | 90 | |
JornJan | 5:949c2861a79b | 91 | px = oldpx; |
JornJan | 5:949c2861a79b | 92 | py = oldpy; |
JornJan | 4:e98ad06f227c | 93 | |
JornJan | 2:0686b2132556 | 94 | } |
JornJan | 2:0686b2132556 | 95 | |
JornJan | 2:0686b2132556 | 96 | |
JornJan | 0:921402a95675 | 97 | void controller() |
JornJan | 3:3b5b85a32c9a | 98 | { |
JornJan | 4:e98ad06f227c | 99 | kinematica(); |
JornJan | 0:921402a95675 | 100 | //PID 1 |
JornJan | 0:921402a95675 | 101 | error1_1 = ref1 - q1_puls; |
JornJan | 0:921402a95675 | 102 | error_I_1 = error_I2_1 + ts*((error1_1 + error2_1)/2); |
JornJan | 0:921402a95675 | 103 | |
JornJan | 0:921402a95675 | 104 | PI1 = Kp1*error1_1 + Ki1*error_I_1; |
JornJan | 0:921402a95675 | 105 | |
JornJan | 0:921402a95675 | 106 | error2_1 = error1_1; // opslaan variabelen voor integraal onderdeel |
JornJan | 0:921402a95675 | 107 | error_I2_1 = error_I_1; |
JornJan | 0:921402a95675 | 108 | |
JornJan | 0:921402a95675 | 109 | //PID 2 |
JornJan | 0:921402a95675 | 110 | error1_2 = ref2 - q2_puls; |
JornJan | 0:921402a95675 | 111 | error_I_2 = error_I2_2 + ts*((error1_2 + error2_2)/2); |
JornJan | 4:e98ad06f227c | 112 | error_I2_2 = error_I_2; |
JornJan | 4:e98ad06f227c | 113 | error2_2 = error1_2; |
JornJan | 0:921402a95675 | 114 | |
JornJan | 0:921402a95675 | 115 | PI2 = Kp2*error1_2 + Ki2*(error_I_2); |
JornJan | 0:921402a95675 | 116 | |
JornJan | 0:921402a95675 | 117 | |
JornJan | 0:921402a95675 | 118 | if (PI1<=0) |
JornJan | 0:921402a95675 | 119 | { |
JornJan | 0:921402a95675 | 120 | motor1DirectionPin = 1; |
JornJan | 5:949c2861a79b | 121 | motor1MagnitudePin = fabs(PI1); |
JornJan | 0:921402a95675 | 122 | } |
JornJan | 0:921402a95675 | 123 | else if (PI1>0) |
JornJan | 0:921402a95675 | 124 | { |
JornJan | 0:921402a95675 | 125 | motor1DirectionPin = 0; |
JornJan | 5:949c2861a79b | 126 | motor1MagnitudePin = fabs(PI1); |
JornJan | 0:921402a95675 | 127 | } |
JornJan | 0:921402a95675 | 128 | |
JornJan | 0:921402a95675 | 129 | if (PI2>=0) |
JornJan | 0:921402a95675 | 130 | { |
JornJan | 0:921402a95675 | 131 | motor2DirectionPin = 1; |
JornJan | 3:3b5b85a32c9a | 132 | motor2MagnitudePin = fabs(PI2); |
JornJan | 0:921402a95675 | 133 | } |
JornJan | 0:921402a95675 | 134 | else if(PI2<0) |
JornJan | 0:921402a95675 | 135 | { |
JornJan | 0:921402a95675 | 136 | motor2DirectionPin = 0; |
JornJan | 3:3b5b85a32c9a | 137 | motor2MagnitudePin = fabs(PI2); |
JornJan | 0:921402a95675 | 138 | } |
JornJan | 0:921402a95675 | 139 | |
JornJan | 0:921402a95675 | 140 | if(n==500){ |
JornJan | 4:e98ad06f227c | 141 | printf("q1_puls = %d q2_puls = %d ref1 = %f ref2 = %f\n\r", q1_puls, q2_puls, ref1, ref2); |
JornJan | 0:921402a95675 | 142 | n=0; |
JornJan | 0:921402a95675 | 143 | } |
JornJan | 0:921402a95675 | 144 | else{ |
JornJan | 0:921402a95675 | 145 | n=n+1; |
JornJan | 0:921402a95675 | 146 | } |
JornJan | 0:921402a95675 | 147 | |
JornJan | 0:921402a95675 | 148 | } |
JornJan | 0:921402a95675 | 149 | |
JornJan | 0:921402a95675 | 150 | |
JornJan | 0:921402a95675 | 151 | int main() |
JornJan | 0:921402a95675 | 152 | { |
JornJan | 0:921402a95675 | 153 | pc.baud(115200); |
JornJan | 0:921402a95675 | 154 | t.start(); |
JornJan | 0:921402a95675 | 155 | aansturing.attach_us(&controller, 1000); |
JornJan | 4:e98ad06f227c | 156 | |
JornJan | 0:921402a95675 | 157 | |
JornJan | 0:921402a95675 | 158 | while(true){ |
JornJan | 0:921402a95675 | 159 | |
JornJan | 0:921402a95675 | 160 | } |
JornJan | 0:921402a95675 | 161 | |
JornJan | 0:921402a95675 | 162 | |
JornJan | 0:921402a95675 | 163 | } |