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@15:d38d5d4ae86a, 2016-10-25 (annotated)
- Committer:
- IngmarLoohuis
- Date:
- Tue Oct 25 13:15:14 2016 +0000
- Revision:
- 15:d38d5d4ae86a
- Parent:
- 14:6ecf2b986a4b
- Child:
- 16:2083f634c91c
- Child:
- 17:457dd9a70c7c
PIDT Controller, 2 motoren werkt niet perfect
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 | 2:665df4abd084 | 9 | DigitalOut motor1DirectionPin (D4); |
| IngmarLoohuis | 1:f26a53da33ed | 10 | PwmOut motor1MagnitudePin(D5); |
| IngmarLoohuis | 15:d38d5d4ae86a | 11 | DigitalOut motor2DirectionPin (D6); |
| IngmarLoohuis | 15:d38d5d4ae86a | 12 | PwmOut motor2MagnitudePin(D7); |
| IngmarLoohuis | 15:d38d5d4ae86a | 13 | QEI encoder_m1(D12,D13,NC,32); |
| IngmarLoohuis | 15:d38d5d4ae86a | 14 | QEI encoder_m2(D10,D11,NC,32); |
| IngmarLoohuis | 15:d38d5d4ae86a | 15 | //HIDScope scope(2); |
| IngmarLoohuis | 2:665df4abd084 | 16 | DigitalIn button(D2); |
| IngmarLoohuis | 1:f26a53da33ed | 17 | Serial pc(USBTX,USBRX); |
| IngmarLoohuis | 6:6bc6ce1fe94e | 18 | //*******************Setting tickers and printers******************* |
| IngmarLoohuis | 15:d38d5d4ae86a | 19 | Ticker angPos1; |
| IngmarLoohuis | 6:6bc6ce1fe94e | 20 | Ticker t1; |
| IngmarLoohuis | 7:742b1969f6c9 | 21 | Ticker t2; |
| IngmarLoohuis | 7:742b1969f6c9 | 22 | Ticker t3; |
| IngmarLoohuis | 15:d38d5d4ae86a | 23 | Ticker t4; |
| IngmarLoohuis | 15:d38d5d4ae86a | 24 | Ticker t5; |
| IngmarLoohuis | 15:d38d5d4ae86a | 25 | Ticker t6; |
| IngmarLoohuis | 15:d38d5d4ae86a | 26 | |
| IngmarLoohuis | 15:d38d5d4ae86a | 27 | |
| 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 | 13:f92e918af729 | 46 | const double m1_Kp = 0.000048765659063912, m1_Ki = 0.0000228295351674407, m1_Kd = 0.0000255784613247063, m1_N = 54.5397025421619; |
| IngmarLoohuis | 15:d38d5d4ae86a | 47 | const double m2_Kp = 0.000048765659063912, m2_Ki = 0.0000228295351674407, m2_Kd = 0.0000255784613247063, m2_N = 54.5397025421619; |
| 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 | 12:0cf4e70f6b8e | 79 | double PID( 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 | 9:6a065971d0ae | 94 | |
| IngmarLoohuis | 15:d38d5d4ae86a | 95 | void getAngPosition_m1() //Get angular position motor 1 |
| IngmarLoohuis | 6:6bc6ce1fe94e | 96 | { |
| IngmarLoohuis | 15:d38d5d4ae86a | 97 | volatile int pulses_m1 = encoder_m1.getPulses(); |
| IngmarLoohuis | 15:d38d5d4ae86a | 98 | radians_m1 = (pulses_m1 / (1 * 3591.84)) * 2*pi; //2 = encoding type, 3591.84 = counts per revoluton for the two big motors |
| IngmarLoohuis | 15:d38d5d4ae86a | 99 | } |
| IngmarLoohuis | 13:f92e918af729 | 100 | |
| IngmarLoohuis | 15:d38d5d4ae86a | 101 | void getAngPosition_m2() //Get angular position motor 2 |
| IngmarLoohuis | 15:d38d5d4ae86a | 102 | { |
| IngmarLoohuis | 15:d38d5d4ae86a | 103 | volatile int pulses_m2 = encoder_m2.getPulses(); |
| IngmarLoohuis | 15:d38d5d4ae86a | 104 | radians_m2 = (pulses_m2 / (1 * 3591.84)) * 2*pi; //2 = encoding type, 3591.84 = counts per revoluton for the two big motors |
| IngmarLoohuis | 6:6bc6ce1fe94e | 105 | } |
| IngmarLoohuis | 6:6bc6ce1fe94e | 106 | |
| IngmarLoohuis | 7:742b1969f6c9 | 107 | // Next task, measure the error and apply the output to the plant |
| IngmarLoohuis | 15:d38d5d4ae86a | 108 | void motor1_Controller(double radians_m1) |
| IngmarLoohuis | 7:742b1969f6c9 | 109 | { |
| IngmarLoohuis | 15:d38d5d4ae86a | 110 | double reference_m1 = 1.0*pi; |
| IngmarLoohuis | 15:d38d5d4ae86a | 111 | volatile double error_m1 = reference_m1 - radians_m1; |
| IngmarLoohuis | 15:d38d5d4ae86a | 112 | motor1 = PID( error_m1,m1_Kp,m1_Ki,m1_Kd,m1_Ts, m1_N, m1_v1, m1_v2 ); |
| IngmarLoohuis | 15:d38d5d4ae86a | 113 | } |
| IngmarLoohuis | 15:d38d5d4ae86a | 114 | |
| IngmarLoohuis | 15:d38d5d4ae86a | 115 | // Next task, measure the error and apply the output to the plant |
| IngmarLoohuis | 15:d38d5d4ae86a | 116 | void motor2_Controller(double radians_m2) |
| IngmarLoohuis | 15:d38d5d4ae86a | 117 | { |
| IngmarLoohuis | 15:d38d5d4ae86a | 118 | double reference_m2 = -1/2*pi; |
| IngmarLoohuis | 15:d38d5d4ae86a | 119 | volatile double error_m2 = reference_m2 - radians_m2; |
| IngmarLoohuis | 15:d38d5d4ae86a | 120 | motor2 = PID( error_m2,m2_Kp,m2_Ki,m2_Kd,m2_Ts, m2_N, m2_v1, m2_v2 ); |
| IngmarLoohuis | 15:d38d5d4ae86a | 121 | |
| IngmarLoohuis | 7:742b1969f6c9 | 122 | } |
| IngmarLoohuis | 7:742b1969f6c9 | 123 | |
| IngmarLoohuis | 7:742b1969f6c9 | 124 | |
| IngmarLoohuis | 15:d38d5d4ae86a | 125 | |
| IngmarLoohuis | 15:d38d5d4ae86a | 126 | void control_m1(double motor1) |
| IngmarLoohuis | 4:30d8610b63a6 | 127 | { |
| IngmarLoohuis | 14:6ecf2b986a4b | 128 | if(abs(motor1)>0.000005) |
| IngmarLoohuis | 2:665df4abd084 | 129 | { |
| IngmarLoohuis | 9:6a065971d0ae | 130 | motor1MagnitudePin=0.5;//MOET NOG TUSSENWAAREN KRIJGEN |
| IngmarLoohuis | 2:665df4abd084 | 131 | } |
| IngmarLoohuis | 9:6a065971d0ae | 132 | else |
| IngmarLoohuis | 2:665df4abd084 | 133 | { |
| IngmarLoohuis | 6:6bc6ce1fe94e | 134 | motor1MagnitudePin=0.0; |
| IngmarLoohuis | 2:665df4abd084 | 135 | } |
| IngmarLoohuis | 9:6a065971d0ae | 136 | if(motor1<=0) |
| IngmarLoohuis | 9:6a065971d0ae | 137 | { |
| IngmarLoohuis | 9:6a065971d0ae | 138 | motor1DirectionPin=0.0; |
| IngmarLoohuis | 9:6a065971d0ae | 139 | } |
| IngmarLoohuis | 9:6a065971d0ae | 140 | else { |
| IngmarLoohuis | 9:6a065971d0ae | 141 | motor1DirectionPin=1.0; |
| IngmarLoohuis | 9:6a065971d0ae | 142 | } |
| IngmarLoohuis | 0:2f40eb89ffce | 143 | } |
| IngmarLoohuis | 7:742b1969f6c9 | 144 | |
| IngmarLoohuis | 15:d38d5d4ae86a | 145 | void control_m2(double motor2) |
| IngmarLoohuis | 15:d38d5d4ae86a | 146 | { |
| IngmarLoohuis | 15:d38d5d4ae86a | 147 | if(abs(motor2)>0.000005) |
| IngmarLoohuis | 15:d38d5d4ae86a | 148 | { |
| IngmarLoohuis | 15:d38d5d4ae86a | 149 | motor2MagnitudePin=0.5;//MOET NOG TUSSENWAAREN KRIJGEN |
| IngmarLoohuis | 15:d38d5d4ae86a | 150 | } |
| IngmarLoohuis | 15:d38d5d4ae86a | 151 | else |
| IngmarLoohuis | 15:d38d5d4ae86a | 152 | { |
| IngmarLoohuis | 15:d38d5d4ae86a | 153 | motor2MagnitudePin=0.0; |
| IngmarLoohuis | 15:d38d5d4ae86a | 154 | } |
| IngmarLoohuis | 15:d38d5d4ae86a | 155 | if(motor2<=0) |
| IngmarLoohuis | 15:d38d5d4ae86a | 156 | { |
| IngmarLoohuis | 15:d38d5d4ae86a | 157 | motor2DirectionPin=1.0; |
| IngmarLoohuis | 15:d38d5d4ae86a | 158 | } |
| IngmarLoohuis | 15:d38d5d4ae86a | 159 | else { |
| IngmarLoohuis | 15:d38d5d4ae86a | 160 | motor2DirectionPin=0.0; |
| IngmarLoohuis | 15:d38d5d4ae86a | 161 | } |
| IngmarLoohuis | 15:d38d5d4ae86a | 162 | } |
| IngmarLoohuis | 15:d38d5d4ae86a | 163 | |
| IngmarLoohuis | 6:6bc6ce1fe94e | 164 | //****************MAIN FUNCTION********************************* |
| IngmarLoohuis | 0:2f40eb89ffce | 165 | int main() |
| IngmarLoohuis | 15:d38d5d4ae86a | 166 | { |
| IngmarLoohuis | 15:d38d5d4ae86a | 167 | motor1MagnitudePin.period(1.0/1000.0); |
| IngmarLoohuis | 15:d38d5d4ae86a | 168 | motor2MagnitudePin.period(1.0/1000.0); |
| IngmarLoohuis | 13:f92e918af729 | 169 | t1.attach(&fn1_activate, 0.0001f); |
| IngmarLoohuis | 7:742b1969f6c9 | 170 | t2.attach(&fn2_activate, 0.0001f); |
| IngmarLoohuis | 7:742b1969f6c9 | 171 | t3.attach(&fn3_activate, 0.0001f); |
| IngmarLoohuis | 15:d38d5d4ae86a | 172 | t4.attach(&fn4_activate, 0.0001f); |
| IngmarLoohuis | 15:d38d5d4ae86a | 173 | t5.attach(&fn5_activate, 0.0001f); |
| IngmarLoohuis | 15:d38d5d4ae86a | 174 | t6.attach(&fn6_activate, 0.0001f); |
| IngmarLoohuis | 11:eda4fbf91948 | 175 | pc.baud(115200); |
| IngmarLoohuis | 10:54b66bd1db20 | 176 | while(true) |
| IngmarLoohuis | 10:54b66bd1db20 | 177 | { |
| IngmarLoohuis | 6:6bc6ce1fe94e | 178 | if(fn1_go) |
| IngmarLoohuis | 6:6bc6ce1fe94e | 179 | { |
| IngmarLoohuis | 6:6bc6ce1fe94e | 180 | fn1_go = false; |
| IngmarLoohuis | 15:d38d5d4ae86a | 181 | control_m1(motor1); |
| IngmarLoohuis | 6:6bc6ce1fe94e | 182 | } |
| IngmarLoohuis | 7:742b1969f6c9 | 183 | if(fn2_go) |
| IngmarLoohuis | 7:742b1969f6c9 | 184 | { |
| IngmarLoohuis | 7:742b1969f6c9 | 185 | fn2_go = false; |
| IngmarLoohuis | 15:d38d5d4ae86a | 186 | motor1_Controller(radians_m1); |
| IngmarLoohuis | 7:742b1969f6c9 | 187 | } |
| IngmarLoohuis | 7:742b1969f6c9 | 188 | if(fn3_go) |
| IngmarLoohuis | 7:742b1969f6c9 | 189 | { |
| IngmarLoohuis | 7:742b1969f6c9 | 190 | fn3_go = false; |
| IngmarLoohuis | 15:d38d5d4ae86a | 191 | getAngPosition_m1(); |
| IngmarLoohuis | 15:d38d5d4ae86a | 192 | } |
| IngmarLoohuis | 15:d38d5d4ae86a | 193 | if(fn4_go) |
| IngmarLoohuis | 15:d38d5d4ae86a | 194 | { |
| IngmarLoohuis | 15:d38d5d4ae86a | 195 | fn4_go = false; |
| IngmarLoohuis | 15:d38d5d4ae86a | 196 | control_m2(motor2); |
| IngmarLoohuis | 7:742b1969f6c9 | 197 | } |
| IngmarLoohuis | 15:d38d5d4ae86a | 198 | if(fn5_go) |
| IngmarLoohuis | 15:d38d5d4ae86a | 199 | { |
| IngmarLoohuis | 15:d38d5d4ae86a | 200 | fn5_go = false; |
| IngmarLoohuis | 15:d38d5d4ae86a | 201 | motor2_Controller(radians_m2); |
| IngmarLoohuis | 15:d38d5d4ae86a | 202 | } |
| IngmarLoohuis | 15:d38d5d4ae86a | 203 | if(fn6_go) |
| IngmarLoohuis | 15:d38d5d4ae86a | 204 | { |
| IngmarLoohuis | 15:d38d5d4ae86a | 205 | fn6_go = false; |
| IngmarLoohuis | 15:d38d5d4ae86a | 206 | getAngPosition_m2(); |
| IngmarLoohuis | 15:d38d5d4ae86a | 207 | } |
| IngmarLoohuis | 15:d38d5d4ae86a | 208 | |
| IngmarLoohuis | 7:742b1969f6c9 | 209 | } |
| IngmarLoohuis | 2:665df4abd084 | 210 | } |