PIDs and fun

Dependencies:   btbee m3pi_ng mbed FatFileSystemCpp

Committer:
crmackey
Date:
Fri May 22 13:34:01 2015 +0000
Revision:
0:4d1ae7eb0070
Child:
1:936e329e840a
The whole thing

Who changed what in which revision?

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