Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: HIDScope MODSERIAL QEI biquadFilter mbed
main.cpp@17:457dd9a70c7c, 2016-10-26 (annotated)
- Committer:
- IngmarLoohuis
- Date:
- Wed Oct 26 13:08:37 2016 +0000
- Revision:
- 17:457dd9a70c7c
- Parent:
- 15:d38d5d4ae86a
- Child:
- 18:e1354199fd48
PIDF, 2 Motors works
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| IngmarLoohuis | 0:2f40eb89ffce | 1 | #include "mbed.h" |
| IngmarLoohuis | 2:665df4abd084 | 2 | #include "MODSERIAL.h" |
| IngmarLoohuis | 0:2f40eb89ffce | 3 | #include "QEI.h" |
| IngmarLoohuis | 0:2f40eb89ffce | 4 | #include "math.h" |
| IngmarLoohuis | 12:0cf4e70f6b8e | 5 | #include "BiQuad.h" |
| IngmarLoohuis | 11:eda4fbf91948 | 6 | //#include "HIDScope.h" |
| IngmarLoohuis | 6:6bc6ce1fe94e | 7 | |
| IngmarLoohuis | 6:6bc6ce1fe94e | 8 | //*****************Defining ports******************** |
| IngmarLoohuis | 17:457dd9a70c7c | 9 | |
| IngmarLoohuis | 1:f26a53da33ed | 10 | PwmOut motor1MagnitudePin(D5); |
| IngmarLoohuis | 17:457dd9a70c7c | 11 | PwmOut motor2MagnitudePin(D6); |
| IngmarLoohuis | 17:457dd9a70c7c | 12 | DigitalOut motor1DirectionPin (D4); |
| IngmarLoohuis | 17:457dd9a70c7c | 13 | DigitalOut motor2DirectionPin (D7); |
| IngmarLoohuis | 17:457dd9a70c7c | 14 | |
| IngmarLoohuis | 15:d38d5d4ae86a | 15 | QEI encoder_m1(D12,D13,NC,32); |
| IngmarLoohuis | 15:d38d5d4ae86a | 16 | QEI encoder_m2(D10,D11,NC,32); |
| IngmarLoohuis | 17:457dd9a70c7c | 17 | //HIDScope scope(1); |
| IngmarLoohuis | 2:665df4abd084 | 18 | DigitalIn button(D2); |
| IngmarLoohuis | 17:457dd9a70c7c | 19 | MODSERIAL pc(USBTX,USBRX); |
| IngmarLoohuis | 6:6bc6ce1fe94e | 20 | //*******************Setting tickers and printers******************* |
| IngmarLoohuis | 15:d38d5d4ae86a | 21 | Ticker angPos1; |
| IngmarLoohuis | 6:6bc6ce1fe94e | 22 | Ticker t1; |
| IngmarLoohuis | 7:742b1969f6c9 | 23 | Ticker t2; |
| IngmarLoohuis | 7:742b1969f6c9 | 24 | Ticker t3; |
| IngmarLoohuis | 15:d38d5d4ae86a | 25 | Ticker t4; |
| IngmarLoohuis | 15:d38d5d4ae86a | 26 | Ticker t5; |
| IngmarLoohuis | 15:d38d5d4ae86a | 27 | Ticker t6; |
| IngmarLoohuis | 6:6bc6ce1fe94e | 28 | //**************Go flags******************************************** |
| IngmarLoohuis | 6:6bc6ce1fe94e | 29 | volatile bool fn1_go = false; |
| IngmarLoohuis | 6:6bc6ce1fe94e | 30 | void fn1_activate(){ fn1_go = true; }; //Activates the go−flag |
| IngmarLoohuis | 7:742b1969f6c9 | 31 | volatile bool fn2_go = false; |
| IngmarLoohuis | 7:742b1969f6c9 | 32 | void fn2_activate(){ fn2_go = true; }; //Activates the go-flag |
| IngmarLoohuis | 7:742b1969f6c9 | 33 | volatile bool fn3_go = false; |
| IngmarLoohuis | 7:742b1969f6c9 | 34 | void fn3_activate(){ fn3_go = true; }; //Activates the go-flag |
| IngmarLoohuis | 15:d38d5d4ae86a | 35 | volatile bool fn4_go = false; |
| IngmarLoohuis | 15:d38d5d4ae86a | 36 | void fn4_activate(){ fn4_go = true; }; //Activates the go−flag |
| IngmarLoohuis | 15:d38d5d4ae86a | 37 | volatile bool fn5_go = false; |
| IngmarLoohuis | 15:d38d5d4ae86a | 38 | void fn5_activate(){ fn5_go = true; }; //Activates the go-flag |
| IngmarLoohuis | 15:d38d5d4ae86a | 39 | volatile bool fn6_go = false; |
| IngmarLoohuis | 15:d38d5d4ae86a | 40 | void fn6_activate(){ fn6_go = true; }; //Activates the go-flag |
| IngmarLoohuis | 6:6bc6ce1fe94e | 41 | //***************Global Variables*********************************** |
| IngmarLoohuis | 6:6bc6ce1fe94e | 42 | const double pi = 3.14159265359; |
| IngmarLoohuis | 15:d38d5d4ae86a | 43 | //const double transmissionShoulder =94.4/40.2; |
| IngmarLoohuis | 15:d38d5d4ae86a | 44 | //const double transmissionElbow = 1.0; |
| IngmarLoohuis | 12:0cf4e70f6b8e | 45 | // controller constants |
| IngmarLoohuis | 17:457dd9a70c7c | 46 | const double m1_Kp = 120.0, m1_Ki = 1.44876354368902, m1_Kd = -1.55261758822823, m1_N = 1.70578345077793; |
| IngmarLoohuis | 17:457dd9a70c7c | 47 | const double m2_Kp = 120.0, m2_Ki = 1.44876354368902, m2_Kd = -1.55261758822823, m2_N = 1.70578345077793; |
| IngmarLoohuis | 15:d38d5d4ae86a | 48 | const double m1_Ts = 0.001; // Controller sample time motor 1 |
| IngmarLoohuis | 15:d38d5d4ae86a | 49 | const double m2_Ts = 0.001; // Controller sample time motor 2 |
| IngmarLoohuis | 15:d38d5d4ae86a | 50 | double m1_v1 = 0; |
| IngmarLoohuis | 15:d38d5d4ae86a | 51 | double m1_v2 = 0; |
| IngmarLoohuis | 15:d38d5d4ae86a | 52 | double m2_v1 = 0; |
| IngmarLoohuis | 15:d38d5d4ae86a | 53 | double m2_v2 = 0; |
| IngmarLoohuis | 12:0cf4e70f6b8e | 54 | // position variable |
| IngmarLoohuis | 15:d38d5d4ae86a | 55 | volatile double radians_m1; |
| IngmarLoohuis | 15:d38d5d4ae86a | 56 | volatile double radians_m2; |
| IngmarLoohuis | 12:0cf4e70f6b8e | 57 | //plant variable |
| IngmarLoohuis | 9:6a065971d0ae | 58 | volatile double motor1; |
| IngmarLoohuis | 15:d38d5d4ae86a | 59 | volatile double motor2; |
| IngmarLoohuis | 15:d38d5d4ae86a | 60 | |
| IngmarLoohuis | 12:0cf4e70f6b8e | 61 | |
| IngmarLoohuis | 12:0cf4e70f6b8e | 62 | |
| IngmarLoohuis | 6:6bc6ce1fe94e | 63 | //*****************Angles Arms*********************** |
| IngmarLoohuis | 1:f26a53da33ed | 64 | |
| IngmarLoohuis | 6:6bc6ce1fe94e | 65 | double O1=1.7633; |
| IngmarLoohuis | 6:6bc6ce1fe94e | 66 | double O2=2.0915; |
| IngmarLoohuis | 6:6bc6ce1fe94e | 67 | double O3=1.8685; |
| IngmarLoohuis | 6:6bc6ce1fe94e | 68 | double O4=1.1363; |
| IngmarLoohuis | 6:6bc6ce1fe94e | 69 | double O5=2.3960; |
| IngmarLoohuis | 6:6bc6ce1fe94e | 70 | double O6=2.0827; |
| IngmarLoohuis | 6:6bc6ce1fe94e | 71 | double B1=1.3551; |
| IngmarLoohuis | 6:6bc6ce1fe94e | 72 | double B2=0.5964; |
| IngmarLoohuis | 6:6bc6ce1fe94e | 73 | double B3=0.06652; |
| IngmarLoohuis | 6:6bc6ce1fe94e | 74 | double B4=0.0669; |
| IngmarLoohuis | 6:6bc6ce1fe94e | 75 | double B5=1.7462; |
| IngmarLoohuis | 6:6bc6ce1fe94e | 76 | double B6=-0.8994; |
| IngmarLoohuis | 5:931594a366b7 | 77 | |
| IngmarLoohuis | 6:6bc6ce1fe94e | 78 | //**********functions****************************** |
| IngmarLoohuis | 17:457dd9a70c7c | 79 | double PID1( double err, const double Kp, const double Ki, const double Kd, |
| IngmarLoohuis | 12:0cf4e70f6b8e | 80 | const double Ts, const double N, double &v1, double &v2 ) { |
| IngmarLoohuis | 12:0cf4e70f6b8e | 81 | // These variables are only calculated once! |
| IngmarLoohuis | 12:0cf4e70f6b8e | 82 | const double a1 = (-4.0/(N*Ts+2)); |
| IngmarLoohuis | 12:0cf4e70f6b8e | 83 | const double a2 = -(N*Ts-2)/(N*Ts+2); |
| IngmarLoohuis | 12:0cf4e70f6b8e | 84 | const double b0 = (4.0*Kp + 4*Kd*N + 2*Ki*Ts + 2*Kp*N*Ts + Ki*N*pow(Ts,2))/(2*N*Ts + 4); |
| IngmarLoohuis | 12:0cf4e70f6b8e | 85 | const double b1 = (Ki*N*pow(Ts,2) - 4.0*Kp - 4.0*Kd*N)/(N*Ts + 2.0); |
| IngmarLoohuis | 12:0cf4e70f6b8e | 86 | const double b2 = (4*Kp + 4*Kd*N - 2*Ki*Ts - 2*Kp*N*Ts + Ki*N*pow(Ts,2))/(2*N*Ts + 4); |
| IngmarLoohuis | 12:0cf4e70f6b8e | 87 | |
| IngmarLoohuis | 12:0cf4e70f6b8e | 88 | double v = err - a1*v1 - a2*v2; |
| IngmarLoohuis | 14:6ecf2b986a4b | 89 | double u = b0*v + b1*v1 + b2*v2; |
| IngmarLoohuis | 12:0cf4e70f6b8e | 90 | v2 = v1; v1 = v; |
| IngmarLoohuis | 12:0cf4e70f6b8e | 91 | return u; |
| IngmarLoohuis | 12:0cf4e70f6b8e | 92 | } |
| IngmarLoohuis | 12:0cf4e70f6b8e | 93 | |
| IngmarLoohuis | 17:457dd9a70c7c | 94 | double PID2( double err, const double Kp, const double Ki, const double Kd, |
| IngmarLoohuis | 17:457dd9a70c7c | 95 | const double Ts, const double N, double &v1_2, double &v2_2 ) { |
| IngmarLoohuis | 17:457dd9a70c7c | 96 | // These variables are only calculated once! |
| IngmarLoohuis | 17:457dd9a70c7c | 97 | const double a1 = (-4.0/(N*Ts+2)); |
| IngmarLoohuis | 17:457dd9a70c7c | 98 | const double a2 = -(N*Ts-2)/(N*Ts+2); |
| IngmarLoohuis | 17:457dd9a70c7c | 99 | const double b0 = (4.0*Kp + 4*Kd*N + 2*Ki*Ts + 2*Kp*N*Ts + Ki*N*pow(Ts,2))/(2*N*Ts + 4); |
| IngmarLoohuis | 17:457dd9a70c7c | 100 | const double b1 = (Ki*N*pow(Ts,2) - 4.0*Kp - 4.0*Kd*N)/(N*Ts + 2.0); |
| IngmarLoohuis | 17:457dd9a70c7c | 101 | const double b2 = (4*Kp + 4*Kd*N - 2*Ki*Ts - 2*Kp*N*Ts + Ki*N*pow(Ts,2))/(2*N*Ts + 4); |
| IngmarLoohuis | 17:457dd9a70c7c | 102 | |
| IngmarLoohuis | 17:457dd9a70c7c | 103 | double v = err - a1*v1_2 - a2*v2_2; |
| IngmarLoohuis | 17:457dd9a70c7c | 104 | double u = b0*v + b1*v1_2 + b2*v2_2; |
| IngmarLoohuis | 17:457dd9a70c7c | 105 | v2_2 = v1_2; v1_2 = v; |
| IngmarLoohuis | 17:457dd9a70c7c | 106 | return u; |
| IngmarLoohuis | 17:457dd9a70c7c | 107 | } |
| IngmarLoohuis | 9:6a065971d0ae | 108 | |
| IngmarLoohuis | 15:d38d5d4ae86a | 109 | void getAngPosition_m1() //Get angular position motor 1 |
| IngmarLoohuis | 6:6bc6ce1fe94e | 110 | { |
| IngmarLoohuis | 15:d38d5d4ae86a | 111 | volatile int pulses_m1 = encoder_m1.getPulses(); |
| IngmarLoohuis | 17:457dd9a70c7c | 112 | radians_m1 = (pulses_m1*0.002991*0.5); //2 = encoding type, 3591.84 = counts per revoluton for the two big motors |
| IngmarLoohuis | 15:d38d5d4ae86a | 113 | } |
| IngmarLoohuis | 13:f92e918af729 | 114 | |
| IngmarLoohuis | 15:d38d5d4ae86a | 115 | void getAngPosition_m2() //Get angular position motor 2 |
| IngmarLoohuis | 15:d38d5d4ae86a | 116 | { |
| IngmarLoohuis | 15:d38d5d4ae86a | 117 | volatile int pulses_m2 = encoder_m2.getPulses(); |
| IngmarLoohuis | 17:457dd9a70c7c | 118 | radians_m2 = (pulses_m2*0.002991*0.5); //2 = encoding type, 3591.84 = counts per revoluton for the two big motors |
| IngmarLoohuis | 6:6bc6ce1fe94e | 119 | } |
| IngmarLoohuis | 6:6bc6ce1fe94e | 120 | |
| IngmarLoohuis | 7:742b1969f6c9 | 121 | // Next task, measure the error and apply the output to the plant |
| IngmarLoohuis | 15:d38d5d4ae86a | 122 | void motor1_Controller(double radians_m1) |
| IngmarLoohuis | 7:742b1969f6c9 | 123 | { |
| IngmarLoohuis | 17:457dd9a70c7c | 124 | double reference_m1 = -1.0*pi; |
| IngmarLoohuis | 15:d38d5d4ae86a | 125 | volatile double error_m1 = reference_m1 - radians_m1; |
| IngmarLoohuis | 17:457dd9a70c7c | 126 | motor1 = PID1( error_m1,m1_Kp,m1_Ki,m1_Kd,m1_Ts, m1_N, m1_v1, m1_v2 ); |
| IngmarLoohuis | 15:d38d5d4ae86a | 127 | } |
| IngmarLoohuis | 15:d38d5d4ae86a | 128 | |
| IngmarLoohuis | 15:d38d5d4ae86a | 129 | // Next task, measure the error and apply the output to the plant |
| IngmarLoohuis | 15:d38d5d4ae86a | 130 | void motor2_Controller(double radians_m2) |
| IngmarLoohuis | 15:d38d5d4ae86a | 131 | { |
| IngmarLoohuis | 17:457dd9a70c7c | 132 | double reference_m2 = -0.5*pi; |
| IngmarLoohuis | 15:d38d5d4ae86a | 133 | volatile double error_m2 = reference_m2 - radians_m2; |
| IngmarLoohuis | 17:457dd9a70c7c | 134 | motor2 = PID1( error_m2,m2_Kp,m2_Ki,m2_Kd,m2_Ts, m2_N, m2_v1, m2_v2 ); |
| IngmarLoohuis | 17:457dd9a70c7c | 135 | |
| IngmarLoohuis | 17:457dd9a70c7c | 136 | pc.printf("motor2 = %d",reference_m2); |
| IngmarLoohuis | 7:742b1969f6c9 | 137 | } |
| IngmarLoohuis | 7:742b1969f6c9 | 138 | |
| IngmarLoohuis | 7:742b1969f6c9 | 139 | |
| IngmarLoohuis | 15:d38d5d4ae86a | 140 | |
| IngmarLoohuis | 15:d38d5d4ae86a | 141 | void control_m1(double motor1) |
| IngmarLoohuis | 4:30d8610b63a6 | 142 | { |
| IngmarLoohuis | 17:457dd9a70c7c | 143 | if(abs(motor1)>1.0) |
| IngmarLoohuis | 2:665df4abd084 | 144 | { |
| IngmarLoohuis | 9:6a065971d0ae | 145 | motor1MagnitudePin=0.5;//MOET NOG TUSSENWAAREN KRIJGEN |
| IngmarLoohuis | 2:665df4abd084 | 146 | } |
| IngmarLoohuis | 9:6a065971d0ae | 147 | else |
| IngmarLoohuis | 2:665df4abd084 | 148 | { |
| IngmarLoohuis | 6:6bc6ce1fe94e | 149 | motor1MagnitudePin=0.0; |
| IngmarLoohuis | 2:665df4abd084 | 150 | } |
| IngmarLoohuis | 9:6a065971d0ae | 151 | if(motor1<=0) |
| IngmarLoohuis | 9:6a065971d0ae | 152 | { |
| IngmarLoohuis | 9:6a065971d0ae | 153 | motor1DirectionPin=0.0; |
| IngmarLoohuis | 9:6a065971d0ae | 154 | } |
| IngmarLoohuis | 9:6a065971d0ae | 155 | else { |
| IngmarLoohuis | 9:6a065971d0ae | 156 | motor1DirectionPin=1.0; |
| IngmarLoohuis | 9:6a065971d0ae | 157 | } |
| IngmarLoohuis | 0:2f40eb89ffce | 158 | } |
| IngmarLoohuis | 7:742b1969f6c9 | 159 | |
| IngmarLoohuis | 15:d38d5d4ae86a | 160 | void control_m2(double motor2) |
| IngmarLoohuis | 15:d38d5d4ae86a | 161 | { |
| IngmarLoohuis | 17:457dd9a70c7c | 162 | if(abs(motor2)>1) |
| IngmarLoohuis | 15:d38d5d4ae86a | 163 | { |
| IngmarLoohuis | 17:457dd9a70c7c | 164 | motor2MagnitudePin=0.2;///MOET NOG TUSSENWAAREN KRIJGEN |
| IngmarLoohuis | 15:d38d5d4ae86a | 165 | } |
| IngmarLoohuis | 15:d38d5d4ae86a | 166 | else |
| IngmarLoohuis | 15:d38d5d4ae86a | 167 | { |
| IngmarLoohuis | 15:d38d5d4ae86a | 168 | motor2MagnitudePin=0.0; |
| IngmarLoohuis | 15:d38d5d4ae86a | 169 | } |
| IngmarLoohuis | 17:457dd9a70c7c | 170 | if(motor2<=0) |
| IngmarLoohuis | 15:d38d5d4ae86a | 171 | { |
| IngmarLoohuis | 15:d38d5d4ae86a | 172 | motor2DirectionPin=1.0; |
| IngmarLoohuis | 15:d38d5d4ae86a | 173 | } |
| IngmarLoohuis | 15:d38d5d4ae86a | 174 | else { |
| IngmarLoohuis | 15:d38d5d4ae86a | 175 | motor2DirectionPin=0.0; |
| IngmarLoohuis | 15:d38d5d4ae86a | 176 | } |
| IngmarLoohuis | 15:d38d5d4ae86a | 177 | } |
| IngmarLoohuis | 15:d38d5d4ae86a | 178 | |
| IngmarLoohuis | 17:457dd9a70c7c | 179 | |
| IngmarLoohuis | 17:457dd9a70c7c | 180 | |
| IngmarLoohuis | 6:6bc6ce1fe94e | 181 | //****************MAIN FUNCTION********************************* |
| IngmarLoohuis | 0:2f40eb89ffce | 182 | int main() |
| IngmarLoohuis | 15:d38d5d4ae86a | 183 | { |
| IngmarLoohuis | 15:d38d5d4ae86a | 184 | motor1MagnitudePin.period(1.0/1000.0); |
| IngmarLoohuis | 15:d38d5d4ae86a | 185 | motor2MagnitudePin.period(1.0/1000.0); |
| IngmarLoohuis | 13:f92e918af729 | 186 | t1.attach(&fn1_activate, 0.0001f); |
| IngmarLoohuis | 7:742b1969f6c9 | 187 | t2.attach(&fn2_activate, 0.0001f); |
| IngmarLoohuis | 7:742b1969f6c9 | 188 | t3.attach(&fn3_activate, 0.0001f); |
| IngmarLoohuis | 15:d38d5d4ae86a | 189 | t4.attach(&fn4_activate, 0.0001f); |
| IngmarLoohuis | 15:d38d5d4ae86a | 190 | t5.attach(&fn5_activate, 0.0001f); |
| IngmarLoohuis | 15:d38d5d4ae86a | 191 | t6.attach(&fn6_activate, 0.0001f); |
| IngmarLoohuis | 11:eda4fbf91948 | 192 | pc.baud(115200); |
| IngmarLoohuis | 10:54b66bd1db20 | 193 | while(true) |
| IngmarLoohuis | 10:54b66bd1db20 | 194 | { |
| IngmarLoohuis | 6:6bc6ce1fe94e | 195 | if(fn1_go) |
| IngmarLoohuis | 6:6bc6ce1fe94e | 196 | { |
| IngmarLoohuis | 6:6bc6ce1fe94e | 197 | fn1_go = false; |
| IngmarLoohuis | 17:457dd9a70c7c | 198 | getAngPosition_m1(); |
| IngmarLoohuis | 6:6bc6ce1fe94e | 199 | } |
| IngmarLoohuis | 7:742b1969f6c9 | 200 | if(fn2_go) |
| IngmarLoohuis | 7:742b1969f6c9 | 201 | { |
| IngmarLoohuis | 7:742b1969f6c9 | 202 | fn2_go = false; |
| IngmarLoohuis | 15:d38d5d4ae86a | 203 | motor1_Controller(radians_m1); |
| IngmarLoohuis | 7:742b1969f6c9 | 204 | } |
| IngmarLoohuis | 7:742b1969f6c9 | 205 | if(fn3_go) |
| IngmarLoohuis | 7:742b1969f6c9 | 206 | { |
| IngmarLoohuis | 7:742b1969f6c9 | 207 | fn3_go = false; |
| IngmarLoohuis | 17:457dd9a70c7c | 208 | control_m1(motor1); |
| IngmarLoohuis | 15:d38d5d4ae86a | 209 | } |
| IngmarLoohuis | 15:d38d5d4ae86a | 210 | if(fn4_go) |
| IngmarLoohuis | 15:d38d5d4ae86a | 211 | { |
| IngmarLoohuis | 15:d38d5d4ae86a | 212 | fn4_go = false; |
| IngmarLoohuis | 17:457dd9a70c7c | 213 | getAngPosition_m2(); |
| IngmarLoohuis | 7:742b1969f6c9 | 214 | } |
| IngmarLoohuis | 15:d38d5d4ae86a | 215 | if(fn5_go) |
| IngmarLoohuis | 15:d38d5d4ae86a | 216 | { |
| IngmarLoohuis | 15:d38d5d4ae86a | 217 | fn5_go = false; |
| IngmarLoohuis | 15:d38d5d4ae86a | 218 | motor2_Controller(radians_m2); |
| IngmarLoohuis | 15:d38d5d4ae86a | 219 | } |
| IngmarLoohuis | 15:d38d5d4ae86a | 220 | if(fn6_go) |
| IngmarLoohuis | 15:d38d5d4ae86a | 221 | { |
| IngmarLoohuis | 15:d38d5d4ae86a | 222 | fn6_go = false; |
| IngmarLoohuis | 17:457dd9a70c7c | 223 | control_m2(motor2); |
| IngmarLoohuis | 15:d38d5d4ae86a | 224 | } |
| IngmarLoohuis | 15:d38d5d4ae86a | 225 | |
| IngmarLoohuis | 7:742b1969f6c9 | 226 | } |
| IngmarLoohuis | 2:665df4abd084 | 227 | } |