Iterative Learning Control
Dependencies: btbee m3pi_ng mbed
main.cpp@1:b7028d9ab40d, 2015-06-10 (annotated)
- Committer:
- crmackey
- Date:
- Wed Jun 10 12:10:09 2015 +0000
- Revision:
- 1:b7028d9ab40d
- Parent:
- 0:21048d818ec9
- Child:
- 2:e8a983e2852b
Created U and E vectors
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
crmackey | 0:21048d818ec9 | 1 | #include "mbed.h" |
crmackey | 0:21048d818ec9 | 2 | #include "m3pi_ng.h" |
crmackey | 0:21048d818ec9 | 3 | #include "time.h" |
crmackey | 0:21048d818ec9 | 4 | #include "btbee.h" |
crmackey | 1:b7028d9ab40d | 5 | #include <vector> |
crmackey | 1:b7028d9ab40d | 6 | #include <string> |
crmackey | 0:21048d818ec9 | 7 | |
crmackey | 0:21048d818ec9 | 8 | m3pi m3pi; |
crmackey | 0:21048d818ec9 | 9 | btbee btbee; |
crmackey | 0:21048d818ec9 | 10 | DigitalIn m3pi_pb(p21); |
crmackey | 0:21048d818ec9 | 11 | DigitalIn m3pi_IN[] = {(p12)}; |
crmackey | 0:21048d818ec9 | 12 | DigitalOut mbed_led[] = {(LED1), (LED2),(LED3), (LED4)}; |
crmackey | 0:21048d818ec9 | 13 | DigitalOut m3pi_led[] = {(p13), (p14), (p15), (p16), (p17), (p18), (p19), (p20)}; |
crmackey | 0:21048d818ec9 | 14 | |
crmackey | 0:21048d818ec9 | 15 | // Minimum and maximum motor speeds |
crmackey | 0:21048d818ec9 | 16 | #define MAX 1.0 |
crmackey | 0:21048d818ec9 | 17 | #define MIN 0 |
crmackey | 0:21048d818ec9 | 18 | |
crmackey | 0:21048d818ec9 | 19 | // PID terms |
crmackey | 0:21048d818ec9 | 20 | #define P_TERM 1.5 |
crmackey | 0:21048d818ec9 | 21 | #define I_TERM 0 |
crmackey | 0:21048d818ec9 | 22 | #define D_TERM 0.1 |
crmackey | 0:21048d818ec9 | 23 | |
crmackey | 0:21048d818ec9 | 24 | int main() { |
crmackey | 0:21048d818ec9 | 25 | |
crmackey | 0:21048d818ec9 | 26 | |
crmackey | 0:21048d818ec9 | 27 | while(1) { |
crmackey | 0:21048d818ec9 | 28 | |
crmackey | 0:21048d818ec9 | 29 | btbee.reset(); |
crmackey | 0:21048d818ec9 | 30 | m3pi_pb.mode(PullUp); |
crmackey | 0:21048d818ec9 | 31 | |
crmackey | 0:21048d818ec9 | 32 | m3pi.printf("Wait 4"); |
crmackey | 0:21048d818ec9 | 33 | m3pi.locate(0,1); |
crmackey | 0:21048d818ec9 | 34 | m3pi.printf("PC"); |
crmackey | 0:21048d818ec9 | 35 | |
crmackey | 0:21048d818ec9 | 36 | while(m3pi_pb) { |
crmackey | 0:21048d818ec9 | 37 | m3pi_led[0]=!m3pi_led[0]; |
crmackey | 0:21048d818ec9 | 38 | wait(3); |
crmackey | 0:21048d818ec9 | 39 | btbee.printf("\n"); |
crmackey | 0:21048d818ec9 | 40 | btbee.printf("PC connected. Press the button \n"); |
crmackey | 0:21048d818ec9 | 41 | } |
crmackey | 0:21048d818ec9 | 42 | |
crmackey | 0:21048d818ec9 | 43 | m3pi.cls(); |
crmackey | 0:21048d818ec9 | 44 | |
crmackey | 0:21048d818ec9 | 45 | Timer LapTimer; |
crmackey | 0:21048d818ec9 | 46 | Timer LoopTimer; |
crmackey | 0:21048d818ec9 | 47 | |
crmackey | 0:21048d818ec9 | 48 | char Bat[] = {'V','o','l','t',' ','i','s'}; |
crmackey | 0:21048d818ec9 | 49 | m3pi.print(Bat,7); |
crmackey | 0:21048d818ec9 | 50 | wait(0.75); |
crmackey | 0:21048d818ec9 | 51 | m3pi.cls(); |
crmackey | 0:21048d818ec9 | 52 | float batteryvoltage = m3pi.battery(); |
crmackey | 0:21048d818ec9 | 53 | char* str = new char[30]; |
crmackey | 0:21048d818ec9 | 54 | sprintf(str, "%.4g", batteryvoltage); |
crmackey | 0:21048d818ec9 | 55 | m3pi.print(str,6); |
crmackey | 0:21048d818ec9 | 56 | btbee.printf("Battery voltage is %f \n", batteryvoltage); |
crmackey | 0:21048d818ec9 | 57 | wait(0.75); |
crmackey | 0:21048d818ec9 | 58 | m3pi.cls(); |
crmackey | 0:21048d818ec9 | 59 | |
crmackey | 0:21048d818ec9 | 60 | if (batteryvoltage < 4.2) |
crmackey | 0:21048d818ec9 | 61 | { |
crmackey | 0:21048d818ec9 | 62 | char low[] = {'L','o','w',' ','b','a','t'}; |
crmackey | 0:21048d818ec9 | 63 | m3pi.print(low,7); |
crmackey | 0:21048d818ec9 | 64 | char ExitSound[]={'V','1','5','O','6','E','4','O','5','E','4'}; |
crmackey | 0:21048d818ec9 | 65 | m3pi.playtune(ExitSound,11); |
crmackey | 0:21048d818ec9 | 66 | btbee.printf("Battery voltage is too low. Stopping program"); |
crmackey | 0:21048d818ec9 | 67 | exit(1); |
crmackey | 0:21048d818ec9 | 68 | } |
crmackey | 0:21048d818ec9 | 69 | |
crmackey | 0:21048d818ec9 | 70 | m3pi.locate(0,1); |
crmackey | 0:21048d818ec9 | 71 | m3pi.printf("Line PID"); |
crmackey | 0:21048d818ec9 | 72 | |
crmackey | 0:21048d818ec9 | 73 | wait(2.0); |
crmackey | 0:21048d818ec9 | 74 | |
crmackey | 0:21048d818ec9 | 75 | btbee.printf("Now calibrating \n"); |
crmackey | 0:21048d818ec9 | 76 | |
crmackey | 0:21048d818ec9 | 77 | m3pi.sensor_auto_calibrate(); |
crmackey | 0:21048d818ec9 | 78 | |
crmackey | 0:21048d818ec9 | 79 | btbee.printf("Finished calibrating \n"); |
crmackey | 0:21048d818ec9 | 80 | |
crmackey | 0:21048d818ec9 | 81 | |
crmackey | 0:21048d818ec9 | 82 | float right; |
crmackey | 0:21048d818ec9 | 83 | float left; |
crmackey | 0:21048d818ec9 | 84 | float current_pos_of_line = 0.0; |
crmackey | 0:21048d818ec9 | 85 | float previous_pos_of_line = 0.0; |
crmackey | 0:21048d818ec9 | 86 | float derivative,proportional,integral = 0; |
crmackey | 0:21048d818ec9 | 87 | float power; |
crmackey | 0:21048d818ec9 | 88 | float speed = MAX; |
crmackey | 0:21048d818ec9 | 89 | int LapTest[5]; |
crmackey | 0:21048d818ec9 | 90 | int s1, s2, s3, s4, s5; |
crmackey | 0:21048d818ec9 | 91 | int counter = -1; |
crmackey | 0:21048d818ec9 | 92 | int checkvar = 0; |
crmackey | 0:21048d818ec9 | 93 | double Time1, Time2, Time3, Time4, Time5, TimeAve; |
crmackey | 0:21048d818ec9 | 94 | btbee.printf("\n"); |
crmackey | 0:21048d818ec9 | 95 | btbee.printf("Now starting \n"); |
crmackey | 0:21048d818ec9 | 96 | int number = 0; |
crmackey | 0:21048d818ec9 | 97 | double dt; |
crmackey | 1:b7028d9ab40d | 98 | vector < double > uvalues; |
crmackey | 1:b7028d9ab40d | 99 | vector < double > evalues; |
crmackey | 1:b7028d9ab40d | 100 | double u; |
crmackey | 1:b7028d9ab40d | 101 | double e; |
crmackey | 0:21048d818ec9 | 102 | |
crmackey | 0:21048d818ec9 | 103 | |
crmackey | 0:21048d818ec9 | 104 | |
crmackey | 0:21048d818ec9 | 105 | while (1) { |
crmackey | 0:21048d818ec9 | 106 | |
crmackey | 0:21048d818ec9 | 107 | if(number == 0) { |
crmackey | 0:21048d818ec9 | 108 | |
crmackey | 0:21048d818ec9 | 109 | dt = 0.005; |
crmackey | 0:21048d818ec9 | 110 | number = 1; |
crmackey | 0:21048d818ec9 | 111 | |
crmackey | 0:21048d818ec9 | 112 | } |
crmackey | 0:21048d818ec9 | 113 | else { |
crmackey | 0:21048d818ec9 | 114 | |
crmackey | 0:21048d818ec9 | 115 | LoopTimer.stop(); |
crmackey | 0:21048d818ec9 | 116 | dt = LoopTimer.read(); |
crmackey | 0:21048d818ec9 | 117 | |
crmackey | 0:21048d818ec9 | 118 | } |
crmackey | 0:21048d818ec9 | 119 | |
crmackey | 0:21048d818ec9 | 120 | LoopTimer.reset(); |
crmackey | 0:21048d818ec9 | 121 | LoopTimer.start(); |
crmackey | 0:21048d818ec9 | 122 | |
crmackey | 0:21048d818ec9 | 123 | // Get the position of the line. |
crmackey | 0:21048d818ec9 | 124 | current_pos_of_line = m3pi.line_position(); |
crmackey | 0:21048d818ec9 | 125 | proportional = current_pos_of_line; |
crmackey | 1:b7028d9ab40d | 126 | e = current_pos_of_line; |
crmackey | 0:21048d818ec9 | 127 | |
crmackey | 0:21048d818ec9 | 128 | // Compute the derivative |
crmackey | 0:21048d818ec9 | 129 | derivative = (current_pos_of_line - previous_pos_of_line)/(dt); |
crmackey | 0:21048d818ec9 | 130 | |
crmackey | 0:21048d818ec9 | 131 | // Remember the last position. |
crmackey | 0:21048d818ec9 | 132 | previous_pos_of_line = current_pos_of_line; |
crmackey | 0:21048d818ec9 | 133 | |
crmackey | 0:21048d818ec9 | 134 | // Compute the power |
crmackey | 0:21048d818ec9 | 135 | power = (proportional * (P_TERM) ) + (derivative*(D_TERM)) ; |
crmackey | 1:b7028d9ab40d | 136 | u = power; |
crmackey | 0:21048d818ec9 | 137 | |
crmackey | 0:21048d818ec9 | 138 | // Compute new speeds |
crmackey | 0:21048d818ec9 | 139 | right = speed+power; |
crmackey | 0:21048d818ec9 | 140 | left = speed-power; |
crmackey | 0:21048d818ec9 | 141 | |
crmackey | 0:21048d818ec9 | 142 | // limit checks |
crmackey | 0:21048d818ec9 | 143 | if (right < MIN) |
crmackey | 0:21048d818ec9 | 144 | right = MIN; |
crmackey | 0:21048d818ec9 | 145 | else if (right > MAX) |
crmackey | 0:21048d818ec9 | 146 | right = MAX; |
crmackey | 0:21048d818ec9 | 147 | |
crmackey | 0:21048d818ec9 | 148 | if (left < MIN) |
crmackey | 0:21048d818ec9 | 149 | left = MIN; |
crmackey | 0:21048d818ec9 | 150 | else if (left > MAX) |
crmackey | 0:21048d818ec9 | 151 | left = MAX; |
crmackey | 0:21048d818ec9 | 152 | |
crmackey | 0:21048d818ec9 | 153 | // set speed |
crmackey | 0:21048d818ec9 | 154 | m3pi.left_motor(left); |
crmackey | 0:21048d818ec9 | 155 | m3pi.right_motor(right); |
crmackey | 0:21048d818ec9 | 156 | |
crmackey | 1:b7028d9ab40d | 157 | uvalues.push_back(u); |
crmackey | 1:b7028d9ab40d | 158 | evalues.push_back(e); |
crmackey | 1:b7028d9ab40d | 159 | |
crmackey | 0:21048d818ec9 | 160 | m3pi.calibrated_sensor(LapTest); |
crmackey | 0:21048d818ec9 | 161 | |
crmackey | 0:21048d818ec9 | 162 | s1 = LapTest[0]; |
crmackey | 0:21048d818ec9 | 163 | s2 = LapTest[1]; |
crmackey | 0:21048d818ec9 | 164 | s3 = LapTest[2]; |
crmackey | 0:21048d818ec9 | 165 | s4 = LapTest[3]; |
crmackey | 0:21048d818ec9 | 166 | s5 = LapTest[4]; |
crmackey | 0:21048d818ec9 | 167 | |
crmackey | 0:21048d818ec9 | 168 | if (s1 > 500 and s5 > 500) { |
crmackey | 0:21048d818ec9 | 169 | checkvar = 1; |
crmackey | 0:21048d818ec9 | 170 | } |
crmackey | 0:21048d818ec9 | 171 | |
crmackey | 0:21048d818ec9 | 172 | if (s1 < 500 and s5 < 500 and checkvar == 1 and counter == -1) { |
crmackey | 0:21048d818ec9 | 173 | LapTimer.start(); |
crmackey | 0:21048d818ec9 | 174 | } |
crmackey | 0:21048d818ec9 | 175 | |
crmackey | 0:21048d818ec9 | 176 | if (s1 < 500 and s5 < 500 and checkvar == 1) { |
crmackey | 0:21048d818ec9 | 177 | counter = counter + 1; |
crmackey | 0:21048d818ec9 | 178 | m3pi.cls(); |
crmackey | 0:21048d818ec9 | 179 | m3pi.printf("%d", counter); |
crmackey | 0:21048d818ec9 | 180 | checkvar = 0; |
crmackey | 0:21048d818ec9 | 181 | switch (counter) { |
crmackey | 0:21048d818ec9 | 182 | case 1: { |
crmackey | 0:21048d818ec9 | 183 | Time1 = LapTimer.read(); |
crmackey | 0:21048d818ec9 | 184 | LapTimer.reset(); |
crmackey | 0:21048d818ec9 | 185 | LapTimer.start(); |
crmackey | 0:21048d818ec9 | 186 | btbee.printf("Lap 1: %f \n", Time1); |
crmackey | 0:21048d818ec9 | 187 | break; |
crmackey | 0:21048d818ec9 | 188 | } |
crmackey | 0:21048d818ec9 | 189 | case 2: { |
crmackey | 0:21048d818ec9 | 190 | Time2 = LapTimer.read(); |
crmackey | 0:21048d818ec9 | 191 | LapTimer.reset(); |
crmackey | 0:21048d818ec9 | 192 | LapTimer.start(); |
crmackey | 0:21048d818ec9 | 193 | btbee.printf("Lap 2: %f \n", Time2); |
crmackey | 0:21048d818ec9 | 194 | break; |
crmackey | 0:21048d818ec9 | 195 | } |
crmackey | 0:21048d818ec9 | 196 | case 3: { |
crmackey | 0:21048d818ec9 | 197 | Time3 = LapTimer.read(); |
crmackey | 0:21048d818ec9 | 198 | LapTimer.reset(); |
crmackey | 0:21048d818ec9 | 199 | LapTimer.start(); |
crmackey | 0:21048d818ec9 | 200 | btbee.printf("Lap 3: %f \n", Time3); |
crmackey | 0:21048d818ec9 | 201 | break; |
crmackey | 0:21048d818ec9 | 202 | } |
crmackey | 0:21048d818ec9 | 203 | case 4: { |
crmackey | 0:21048d818ec9 | 204 | Time4 = LapTimer.read(); |
crmackey | 0:21048d818ec9 | 205 | LapTimer.reset(); |
crmackey | 0:21048d818ec9 | 206 | LapTimer.start(); |
crmackey | 0:21048d818ec9 | 207 | btbee.printf("Lap 4: %f \n", Time4); |
crmackey | 0:21048d818ec9 | 208 | break; |
crmackey | 0:21048d818ec9 | 209 | } |
crmackey | 0:21048d818ec9 | 210 | case 5: { |
crmackey | 0:21048d818ec9 | 211 | Time5 = LapTimer.read(); |
crmackey | 0:21048d818ec9 | 212 | LapTimer.reset(); |
crmackey | 0:21048d818ec9 | 213 | LapTimer.start(); |
crmackey | 0:21048d818ec9 | 214 | btbee.printf("Lap 5: %f \n", Time5); |
crmackey | 0:21048d818ec9 | 215 | break; |
crmackey | 0:21048d818ec9 | 216 | } |
crmackey | 0:21048d818ec9 | 217 | } |
crmackey | 0:21048d818ec9 | 218 | |
crmackey | 0:21048d818ec9 | 219 | |
crmackey | 0:21048d818ec9 | 220 | } |
crmackey | 0:21048d818ec9 | 221 | |
crmackey | 0:21048d818ec9 | 222 | if (counter == 5) { |
crmackey | 0:21048d818ec9 | 223 | m3pi.stop(); |
crmackey | 0:21048d818ec9 | 224 | m3pi.cls(); |
crmackey | 0:21048d818ec9 | 225 | m3pi.locate(0,0); |
crmackey | 0:21048d818ec9 | 226 | m3pi.printf("Ave time"); |
crmackey | 0:21048d818ec9 | 227 | TimeAve = (Time1 + Time2 + Time3 + Time4 + Time5)/5.0; |
crmackey | 0:21048d818ec9 | 228 | m3pi.locate(0,1); |
crmackey | 0:21048d818ec9 | 229 | m3pi.printf("%f", TimeAve); |
crmackey | 0:21048d818ec9 | 230 | btbee.printf("Average time per lap: %f ", TimeAve); |
crmackey | 0:21048d818ec9 | 231 | exit(1); |
crmackey | 0:21048d818ec9 | 232 | } |
crmackey | 0:21048d818ec9 | 233 | |
crmackey | 0:21048d818ec9 | 234 | |
crmackey | 0:21048d818ec9 | 235 | } |
crmackey | 0:21048d818ec9 | 236 | |
crmackey | 0:21048d818ec9 | 237 | } |
crmackey | 0:21048d818ec9 | 238 | |
crmackey | 0:21048d818ec9 | 239 | } |