Iterative Learning Control

Dependencies:   btbee m3pi_ng mbed

Committer:
crmackey
Date:
Wed Jun 10 12:27:49 2015 +0000
Revision:
3:b0dcacecad2b
Parent:
2:e8a983e2852b
Child:
4:8918484e8125
Added LoopCounter

Who changed what in which revision?

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