Iterative Learning Control

Dependencies:   btbee m3pi_ng mbed

Committer:
crmackey
Date:
Wed Jun 10 11:58:48 2015 +0000
Revision:
0:21048d818ec9
Child:
1:b7028d9ab40d
Added true PD controller

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 0:21048d818ec9 5
crmackey 0:21048d818ec9 6 m3pi m3pi;
crmackey 0:21048d818ec9 7 btbee btbee;
crmackey 0:21048d818ec9 8 DigitalIn m3pi_pb(p21);
crmackey 0:21048d818ec9 9 DigitalIn m3pi_IN[] = {(p12)};
crmackey 0:21048d818ec9 10 DigitalOut mbed_led[] = {(LED1), (LED2),(LED3), (LED4)};
crmackey 0:21048d818ec9 11 DigitalOut m3pi_led[] = {(p13), (p14), (p15), (p16), (p17), (p18), (p19), (p20)};
crmackey 0:21048d818ec9 12
crmackey 0:21048d818ec9 13 // Minimum and maximum motor speeds
crmackey 0:21048d818ec9 14 #define MAX 1.0
crmackey 0:21048d818ec9 15 #define MIN 0
crmackey 0:21048d818ec9 16
crmackey 0:21048d818ec9 17 // PID terms
crmackey 0:21048d818ec9 18 #define P_TERM 1.5
crmackey 0:21048d818ec9 19 #define I_TERM 0
crmackey 0:21048d818ec9 20 #define D_TERM 0.1
crmackey 0:21048d818ec9 21
crmackey 0:21048d818ec9 22 int main() {
crmackey 0:21048d818ec9 23
crmackey 0:21048d818ec9 24
crmackey 0:21048d818ec9 25 while(1) {
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 0:21048d818ec9 96
crmackey 0:21048d818ec9 97
crmackey 0:21048d818ec9 98
crmackey 0:21048d818ec9 99
crmackey 0:21048d818ec9 100 while (1) {
crmackey 0:21048d818ec9 101
crmackey 0:21048d818ec9 102 if(number == 0) {
crmackey 0:21048d818ec9 103
crmackey 0:21048d818ec9 104 dt = 0.005;
crmackey 0:21048d818ec9 105 number = 1;
crmackey 0:21048d818ec9 106
crmackey 0:21048d818ec9 107 }
crmackey 0:21048d818ec9 108 else {
crmackey 0:21048d818ec9 109
crmackey 0:21048d818ec9 110 LoopTimer.stop();
crmackey 0:21048d818ec9 111 dt = LoopTimer.read();
crmackey 0:21048d818ec9 112
crmackey 0:21048d818ec9 113 }
crmackey 0:21048d818ec9 114
crmackey 0:21048d818ec9 115 LoopTimer.reset();
crmackey 0:21048d818ec9 116 LoopTimer.start();
crmackey 0:21048d818ec9 117
crmackey 0:21048d818ec9 118 // Get the position of the line.
crmackey 0:21048d818ec9 119 current_pos_of_line = m3pi.line_position();
crmackey 0:21048d818ec9 120 proportional = current_pos_of_line;
crmackey 0:21048d818ec9 121
crmackey 0:21048d818ec9 122 // Compute the derivative
crmackey 0:21048d818ec9 123 derivative = (current_pos_of_line - previous_pos_of_line)/(dt);
crmackey 0:21048d818ec9 124
crmackey 0:21048d818ec9 125 // Remember the last position.
crmackey 0:21048d818ec9 126 previous_pos_of_line = current_pos_of_line;
crmackey 0:21048d818ec9 127
crmackey 0:21048d818ec9 128 // Compute the power
crmackey 0:21048d818ec9 129 power = (proportional * (P_TERM) ) + (derivative*(D_TERM)) ;
crmackey 0:21048d818ec9 130
crmackey 0:21048d818ec9 131 // Compute new speeds
crmackey 0:21048d818ec9 132 right = speed+power;
crmackey 0:21048d818ec9 133 left = speed-power;
crmackey 0:21048d818ec9 134
crmackey 0:21048d818ec9 135 // limit checks
crmackey 0:21048d818ec9 136 if (right < MIN)
crmackey 0:21048d818ec9 137 right = MIN;
crmackey 0:21048d818ec9 138 else if (right > MAX)
crmackey 0:21048d818ec9 139 right = MAX;
crmackey 0:21048d818ec9 140
crmackey 0:21048d818ec9 141 if (left < MIN)
crmackey 0:21048d818ec9 142 left = MIN;
crmackey 0:21048d818ec9 143 else if (left > MAX)
crmackey 0:21048d818ec9 144 left = MAX;
crmackey 0:21048d818ec9 145
crmackey 0:21048d818ec9 146 // set speed
crmackey 0:21048d818ec9 147 m3pi.left_motor(left);
crmackey 0:21048d818ec9 148 m3pi.right_motor(right);
crmackey 0:21048d818ec9 149
crmackey 0:21048d818ec9 150 m3pi.calibrated_sensor(LapTest);
crmackey 0:21048d818ec9 151
crmackey 0:21048d818ec9 152 s1 = LapTest[0];
crmackey 0:21048d818ec9 153 s2 = LapTest[1];
crmackey 0:21048d818ec9 154 s3 = LapTest[2];
crmackey 0:21048d818ec9 155 s4 = LapTest[3];
crmackey 0:21048d818ec9 156 s5 = LapTest[4];
crmackey 0:21048d818ec9 157
crmackey 0:21048d818ec9 158 if (s1 > 500 and s5 > 500) {
crmackey 0:21048d818ec9 159 checkvar = 1;
crmackey 0:21048d818ec9 160 }
crmackey 0:21048d818ec9 161
crmackey 0:21048d818ec9 162 if (s1 < 500 and s5 < 500 and checkvar == 1 and counter == -1) {
crmackey 0:21048d818ec9 163 LapTimer.start();
crmackey 0:21048d818ec9 164 }
crmackey 0:21048d818ec9 165
crmackey 0:21048d818ec9 166 if (s1 < 500 and s5 < 500 and checkvar == 1) {
crmackey 0:21048d818ec9 167 counter = counter + 1;
crmackey 0:21048d818ec9 168 m3pi.cls();
crmackey 0:21048d818ec9 169 m3pi.printf("%d", counter);
crmackey 0:21048d818ec9 170 checkvar = 0;
crmackey 0:21048d818ec9 171 switch (counter) {
crmackey 0:21048d818ec9 172 case 1: {
crmackey 0:21048d818ec9 173 Time1 = LapTimer.read();
crmackey 0:21048d818ec9 174 LapTimer.reset();
crmackey 0:21048d818ec9 175 LapTimer.start();
crmackey 0:21048d818ec9 176 btbee.printf("Lap 1: %f \n", Time1);
crmackey 0:21048d818ec9 177 break;
crmackey 0:21048d818ec9 178 }
crmackey 0:21048d818ec9 179 case 2: {
crmackey 0:21048d818ec9 180 Time2 = LapTimer.read();
crmackey 0:21048d818ec9 181 LapTimer.reset();
crmackey 0:21048d818ec9 182 LapTimer.start();
crmackey 0:21048d818ec9 183 btbee.printf("Lap 2: %f \n", Time2);
crmackey 0:21048d818ec9 184 break;
crmackey 0:21048d818ec9 185 }
crmackey 0:21048d818ec9 186 case 3: {
crmackey 0:21048d818ec9 187 Time3 = LapTimer.read();
crmackey 0:21048d818ec9 188 LapTimer.reset();
crmackey 0:21048d818ec9 189 LapTimer.start();
crmackey 0:21048d818ec9 190 btbee.printf("Lap 3: %f \n", Time3);
crmackey 0:21048d818ec9 191 break;
crmackey 0:21048d818ec9 192 }
crmackey 0:21048d818ec9 193 case 4: {
crmackey 0:21048d818ec9 194 Time4 = LapTimer.read();
crmackey 0:21048d818ec9 195 LapTimer.reset();
crmackey 0:21048d818ec9 196 LapTimer.start();
crmackey 0:21048d818ec9 197 btbee.printf("Lap 4: %f \n", Time4);
crmackey 0:21048d818ec9 198 break;
crmackey 0:21048d818ec9 199 }
crmackey 0:21048d818ec9 200 case 5: {
crmackey 0:21048d818ec9 201 Time5 = LapTimer.read();
crmackey 0:21048d818ec9 202 LapTimer.reset();
crmackey 0:21048d818ec9 203 LapTimer.start();
crmackey 0:21048d818ec9 204 btbee.printf("Lap 5: %f \n", Time5);
crmackey 0:21048d818ec9 205 break;
crmackey 0:21048d818ec9 206 }
crmackey 0:21048d818ec9 207 }
crmackey 0:21048d818ec9 208
crmackey 0:21048d818ec9 209
crmackey 0:21048d818ec9 210 }
crmackey 0:21048d818ec9 211
crmackey 0:21048d818ec9 212 if (counter == 5) {
crmackey 0:21048d818ec9 213 m3pi.stop();
crmackey 0:21048d818ec9 214 m3pi.cls();
crmackey 0:21048d818ec9 215 m3pi.locate(0,0);
crmackey 0:21048d818ec9 216 m3pi.printf("Ave time");
crmackey 0:21048d818ec9 217 TimeAve = (Time1 + Time2 + Time3 + Time4 + Time5)/5.0;
crmackey 0:21048d818ec9 218 m3pi.locate(0,1);
crmackey 0:21048d818ec9 219 m3pi.printf("%f", TimeAve);
crmackey 0:21048d818ec9 220 btbee.printf("Average time per lap: %f ", TimeAve);
crmackey 0:21048d818ec9 221 exit(1);
crmackey 0:21048d818ec9 222 }
crmackey 0:21048d818ec9 223
crmackey 0:21048d818ec9 224
crmackey 0:21048d818ec9 225 }
crmackey 0:21048d818ec9 226
crmackey 0:21048d818ec9 227 }
crmackey 0:21048d818ec9 228
crmackey 0:21048d818ec9 229 }