Julesnaps / Mbed 2 deprecated Linefollowproject

Dependencies:   m3pi mbed

Committer:
mikkelbredholt
Date:
Tue Oct 11 13:33:53 2022 +0000
Revision:
35:be40a5d57023
Parent:
33:f035445a4604
Child:
36:17854202990b
test12

Who changed what in which revision?

UserRevisionLine numberNew contents of line
magnusmland 0:f562e4f9c29f 1 #include "mbed.h"
magnusmland 0:f562e4f9c29f 2 #include "m3pi.h"
magnusmland 0:f562e4f9c29f 3
magnusmland 0:f562e4f9c29f 4 m3pi m3pi;
uld 32:570b94fe2c19 5
mikkelbredholt 33:f035445a4604 6 //test11
mikkelbredholt 35:be40a5d57023 7 //test12
mikkelbredholt 33:f035445a4604 8
uld 32:570b94fe2c19 9 // DigitalOuts
vehus 15:8b76add42254 10 DigitalOut led1(LED1);
vehus 15:8b76add42254 11 DigitalOut led2(LED2);
vehus 15:8b76add42254 12 DigitalOut led3(LED3);
vehus 15:8b76add42254 13 DigitalOut led4(LED4);
magnusmland 0:f562e4f9c29f 14
magnusmland 0:f562e4f9c29f 15 // Minimum and maximum motor speeds
uld 30:ccf5fa970bd2 16 #define MAX 0.80
magnusmland 0:f562e4f9c29f 17 #define MIN 0
magnusmland 0:f562e4f9c29f 18
magnusmland 0:f562e4f9c29f 19 // PID terms
magnusmland 0:f562e4f9c29f 20 #define P_TERM 1
magnusmland 0:f562e4f9c29f 21 #define I_TERM 0
magnusmland 0:f562e4f9c29f 22 #define D_TERM 20
magnusmland 0:f562e4f9c29f 23
uld 31:434a78739a59 24 // Ccount before test
uld 31:434a78739a59 25 #define CYCLEBEFORETEST 1500
uld 20:76f94dec91d1 26
uld 20:76f94dec91d1 27 // Textfile paths
uld 23:7e9505da7f48 28 #define PITLOGPATH "/local/pitlog.txt"
uld 23:7e9505da7f48 29 #define VOLTAGELOGPATH "/local/voltage.txt"
uld 20:76f94dec91d1 30
uld 20:76f94dec91d1 31
uld 7:ac88c8e35048 32 // Prototypes
uld 8:5640c8c5088e 33 int PitTest(void); // Test if to robot needs to goto pit
vehus 15:8b76add42254 34 void InitialMessages(void); // Prints initial message to the LCD
uld 28:2c93dff934b1 35 void LED_Control(int ledNumber, int state); //Turn ledNumber to 1=on, 0 = off
uld 28:2c93dff934b1 36 void LED_Blink(int ledNumber); // Make ledNumber blinik
uld 28:2c93dff934b1 37 void LCDCountDown(int num); //LCD Coundown function
uld 26:e6d82a8ba556 38
mikkelbredholt 13:ddff4bb7c24f 39
uld 20:76f94dec91d1 40 void PS_PitStop(void); //
uld 20:76f94dec91d1 41 void PS_CreateLog(void); // create a log file or resets it (WIP
uld 20:76f94dec91d1 42 void PS_AddStopToLog(void); // Add one to the log
uld 20:76f94dec91d1 43 // void PS_DisplayNumberofPS(void); // Display the final number on screen WIP
uld 20:76f94dec91d1 44
uld 20:76f94dec91d1 45 void TE_CreateVoltageLog(void); //
uld 21:c3e256b18b96 46 void TE_LogVoltage(int count); // test funktion that write the woltage each time the battry is checked
uld 20:76f94dec91d1 47
magnusmland 0:f562e4f9c29f 48 int main() {
uld 22:5d3332fc4c5c 49 LocalFileSystem local("local");
uld 27:8561eeb0bd1d 50
uld 5:dbd32cb3650a 51
uld 6:6865930c1135 52 /*Base program Variable initiation*/
magnusmland 0:f562e4f9c29f 53 float right;
magnusmland 0:f562e4f9c29f 54 float left;
magnusmland 0:f562e4f9c29f 55 float current_pos_of_line = 0.0;
magnusmland 0:f562e4f9c29f 56 float previous_pos_of_line = 0.0;
magnusmland 0:f562e4f9c29f 57 float derivative,proportional,integral = 0;
magnusmland 0:f562e4f9c29f 58 float power;
magnusmland 0:f562e4f9c29f 59 float speed = MAX;
uld 6:6865930c1135 60
uld 6:6865930c1135 61 /*Team 7 Variabels*/
uld 7:ac88c8e35048 62 int gotoPit = 0; // wether or not the robot is heading to pit. Initialstate false.
vehus 15:8b76add42254 63 int ccount = 0; //used to count cycles
uld 6:6865930c1135 64
uld 23:7e9505da7f48 65 /*Printing secret cat mission*/
uld 23:7e9505da7f48 66 InitialMessages();
uld 27:8561eeb0bd1d 67 m3pi.sensor_auto_calibrate();
uld 27:8561eeb0bd1d 68
uld 23:7e9505da7f48 69
uld 20:76f94dec91d1 70 /*Create pitlog used to log the number of pitstop */
uld 20:76f94dec91d1 71 PS_CreateLog();
uld 22:5d3332fc4c5c 72 TE_CreateVoltageLog();
uld 7:ac88c8e35048 73
magnusmland 0:f562e4f9c29f 74 while (1) {
uld 8:5640c8c5088e 75 /* If cycle count divided by 100 does not have a rest. test if pit */
uld 31:434a78739a59 76 if (ccount % CYCLEBEFORETEST == 0 && gotoPit == 0)
vehus 15:8b76add42254 77 {
uld 21:c3e256b18b96 78 TE_LogVoltage(ccount);
vehus 15:8b76add42254 79 gotoPit = PitTest();
vehus 15:8b76add42254 80 }
vehus 15:8b76add42254 81 if (gotoPit == 1)
vehus 15:8b76add42254 82 {
uld 20:76f94dec91d1 83 /*Add one to the nummber allready in the pitlog*/
uld 20:76f94dec91d1 84 PS_AddStopToLog();
uld 20:76f94dec91d1 85 /*Run the pitstop function*/
uld 28:2c93dff934b1 86 PS_PitStop();
vehus 15:8b76add42254 87 }
vehus 15:8b76add42254 88
magnusmland 0:f562e4f9c29f 89 // Get the position of the line.
magnusmland 0:f562e4f9c29f 90 current_pos_of_line = m3pi.line_position();
magnusmland 0:f562e4f9c29f 91 proportional = current_pos_of_line;
magnusmland 0:f562e4f9c29f 92
magnusmland 0:f562e4f9c29f 93 // Compute the derivative
magnusmland 0:f562e4f9c29f 94 derivative = current_pos_of_line - previous_pos_of_line;
magnusmland 0:f562e4f9c29f 95
magnusmland 0:f562e4f9c29f 96 // Compute the integral
magnusmland 0:f562e4f9c29f 97 integral += proportional;
magnusmland 0:f562e4f9c29f 98
magnusmland 0:f562e4f9c29f 99 // Remember the last position.
magnusmland 0:f562e4f9c29f 100 previous_pos_of_line = current_pos_of_line;
magnusmland 0:f562e4f9c29f 101
magnusmland 0:f562e4f9c29f 102 // Compute the power
magnusmland 0:f562e4f9c29f 103 power = (proportional * (P_TERM) ) + (integral*(I_TERM)) + (derivative*(D_TERM)) ;
magnusmland 0:f562e4f9c29f 104
magnusmland 0:f562e4f9c29f 105 // Compute new speeds
magnusmland 0:f562e4f9c29f 106 right = speed+power;
magnusmland 0:f562e4f9c29f 107 left = speed-power;
magnusmland 0:f562e4f9c29f 108
magnusmland 0:f562e4f9c29f 109 // limit checks
magnusmland 0:f562e4f9c29f 110 if (right < MIN)
magnusmland 0:f562e4f9c29f 111 right = MIN;
magnusmland 0:f562e4f9c29f 112 else if (right > MAX)
magnusmland 0:f562e4f9c29f 113 right = MAX;
magnusmland 0:f562e4f9c29f 114
magnusmland 0:f562e4f9c29f 115 if (left < MIN)
magnusmland 0:f562e4f9c29f 116 left = MIN;
magnusmland 0:f562e4f9c29f 117 else if (left > MAX)
magnusmland 0:f562e4f9c29f 118 left = MAX;
magnusmland 0:f562e4f9c29f 119
magnusmland 0:f562e4f9c29f 120 // set speed
magnusmland 0:f562e4f9c29f 121 m3pi.left_motor(left);
magnusmland 0:f562e4f9c29f 122 m3pi.right_motor(right);
uld 6:6865930c1135 123
uld 8:5640c8c5088e 124 ccount++;
uld 7:ac88c8e35048 125 }
uld 20:76f94dec91d1 126 // PS_DisplayNumberofPS();
vehus 15:8b76add42254 127
uld 7:ac88c8e35048 128 }
uld 7:ac88c8e35048 129
vehus 15:8b76add42254 130 void InitialMessages(void){
uld 9:7b9094864268 131 /*Prints iniatl secret mission*/
uld 9:7b9094864268 132
uld 23:7e9505da7f48 133
uld 23:7e9505da7f48 134
uld 9:7b9094864268 135 m3pi.cls();
uld 9:7b9094864268 136 m3pi.locate(0,0);
uld 27:8561eeb0bd1d 137 m3pi.printf("DESTROY");
uld 9:7b9094864268 138 m3pi.locate(0,1);
uld 27:8561eeb0bd1d 139 m3pi.printf("**CATS**");
uld 20:76f94dec91d1 140 wait(5.0);
uld 9:7b9094864268 141
uld 9:7b9094864268 142 m3pi.cls();
uld 9:7b9094864268 143 m3pi.locate(0,0);
uld 20:76f94dec91d1 144 m3pi.printf("%4.4f ",m3pi.battery());
uld 9:7b9094864268 145 m3pi.locate(0,1);
uld 20:76f94dec91d1 146 m3pi.printf("%4.4f ",m3pi.pot_voltage());
uld 20:76f94dec91d1 147 wait(10.0);
uld 28:2c93dff934b1 148 m3pi.cls();
uld 27:8561eeb0bd1d 149 m3pi.locate(0,0);
uld 28:2c93dff934b1 150 m3pi.printf("ROBOT ON");
uld 27:8561eeb0bd1d 151 m3pi.locate(0,1);
uld 28:2c93dff934b1 152 m3pi.printf("TRACK!!");
uld 27:8561eeb0bd1d 153 wait(4.0);
uld 26:e6d82a8ba556 154 LCDCountDown(3);
uld 24:6427b144b17c 155 m3pi.cls();
uld 26:e6d82a8ba556 156 m3pi.locate(0,0);
uld 26:e6d82a8ba556 157 m3pi.printf("** GO **");
uld 26:e6d82a8ba556 158 }
uld 23:7e9505da7f48 159
uld 26:e6d82a8ba556 160 void LCDCountDown(int num){
uld 26:e6d82a8ba556 161 for (int i=0; i<num; i++)
uld 26:e6d82a8ba556 162 {
uld 26:e6d82a8ba556 163 m3pi.cls();
uld 26:e6d82a8ba556 164 m3pi.locate(0,0);
uld 26:e6d82a8ba556 165 m3pi.printf("** %d **", i );
uld 26:e6d82a8ba556 166 wait(1.0);
uld 26:e6d82a8ba556 167 }
uld 23:7e9505da7f48 168
uld 26:e6d82a8ba556 169 }
uld 26:e6d82a8ba556 170
uld 8:5640c8c5088e 171 int PitTest(void){
uld 7:ac88c8e35048 172 /* Test the batteri voltage if the robot is not headed for pit */
uld 7:ac88c8e35048 173
uld 30:ccf5fa970bd2 174 const float BATVOLTTRESHOLD = 0.05; // Treshold i volt
vehus 15:8b76add42254 175 int result = 0;
uld 7:ac88c8e35048 176
mikkelbredholt 13:ddff4bb7c24f 177 /*Test if the voltage is below the threshold if so turn on go to pit mode*/
vehus 15:8b76add42254 178 if (m3pi.battery() <= BATVOLTTRESHOLD ){
uld 9:7b9094864268 179 result = 1; // Set goto pit condition
vehus 15:8b76add42254 180 LED_Control(1, 1);
uld 7:ac88c8e35048 181 m3pi.cls();
uld 7:ac88c8e35048 182 m3pi.locate(0,0);
uld 7:ac88c8e35048 183 m3pi.printf("Going to");
uld 7:ac88c8e35048 184 m3pi.locate(0,1);
uld 27:8561eeb0bd1d 185 m3pi.printf("**PIT**");
magnusmland 0:f562e4f9c29f 186 }
uld 8:5640c8c5088e 187 return result;
mikkelbredholt 13:ddff4bb7c24f 188 }
mikkelbredholt 13:ddff4bb7c24f 189
mikkelbredholt 13:ddff4bb7c24f 190 void LED_Control(int ledNumber, int state){
mikkelbredholt 13:ddff4bb7c24f 191 //LED1 on if robot is looking for pit
uld 20:76f94dec91d1 192 if (ledNumber == 1) {
vehus 15:8b76add42254 193 led1 = state;
vehus 15:8b76add42254 194 }
uld 20:76f94dec91d1 195 if (ledNumber == 2){
vehus 15:8b76add42254 196 led2 = state;
mikkelbredholt 13:ddff4bb7c24f 197 }
uld 20:76f94dec91d1 198 if (ledNumber == 3){
vehus 15:8b76add42254 199 led3 = state;
vehus 15:8b76add42254 200 }
uld 20:76f94dec91d1 201 if (ledNumber == 4){
vehus 15:8b76add42254 202 led4 = state;
mikkelbredholt 13:ddff4bb7c24f 203 }
vehus 15:8b76add42254 204 }
vehus 15:8b76add42254 205
uld 28:2c93dff934b1 206 void LED_Blink(int ledNumber)
vehus 15:8b76add42254 207 {
vehus 15:8b76add42254 208 int a = 2;
vehus 15:8b76add42254 209 LED_Control (ledNumber , 0);
vehus 15:8b76add42254 210 wait(a);
vehus 15:8b76add42254 211 LED_Control (ledNumber , 1);
vehus 15:8b76add42254 212 wait(a);
mikkelbredholt 13:ddff4bb7c24f 213 }
vehus 15:8b76add42254 214
uld 20:76f94dec91d1 215 void PS_PitStop(void)
vehus 15:8b76add42254 216 {
vehus 15:8b76add42254 217 /* Testing alternative stop function
vehus 15:8b76add42254 218 m3pi.left_motor(0);
vehus 15:8b76add42254 219 m3pi.right_motor(0);
vehus 15:8b76add42254 220 */
vehus 15:8b76add42254 221 m3pi.stop(); // stop all engine
vehus 15:8b76add42254 222
vehus 15:8b76add42254 223 // increase counter with one
vehus 15:8b76add42254 224 while (1)
vehus 15:8b76add42254 225 {
uld 28:2c93dff934b1 226 LED_Blink (1); // signal in pit
vehus 15:8b76add42254 227
vehus 15:8b76add42254 228 /* missing input to stop blink. */
vehus 15:8b76add42254 229
mikkelbredholt 13:ddff4bb7c24f 230 }
uld 20:76f94dec91d1 231 }
uld 20:76f94dec91d1 232
uld 20:76f94dec91d1 233 void PS_CreateLog(void){
uld 20:76f94dec91d1 234 /* Create a pitlog file and test if it can open*/
uld 20:76f94dec91d1 235 FILE *fptr;
uld 20:76f94dec91d1 236 fptr = fopen(PITLOGPATH,"w");
uld 20:76f94dec91d1 237
uld 20:76f94dec91d1 238 if(fptr == NULL)
uld 20:76f94dec91d1 239 {
uld 20:76f94dec91d1 240 printf("Error creating log file ");
uld 20:76f94dec91d1 241 exit(1);
uld 20:76f94dec91d1 242 }
uld 20:76f94dec91d1 243 fprintf(fptr,"%d", 0);
uld 20:76f94dec91d1 244 fclose(fptr);
uld 20:76f94dec91d1 245 }
uld 20:76f94dec91d1 246
uld 20:76f94dec91d1 247 void PS_AddStopToLog(void){
uld 20:76f94dec91d1 248 /*Opens the pit log and read the number.
uld 20:76f94dec91d1 249 * Then adds one to that number at write it into the pitlog */
uld 20:76f94dec91d1 250
uld 20:76f94dec91d1 251 FILE *fptr;
uld 20:76f94dec91d1 252 int x, y;
uld 20:76f94dec91d1 253 if ((fptr = fopen(PITLOGPATH,"r")) == NULL){
uld 20:76f94dec91d1 254 printf("Error! opening file");
uld 20:76f94dec91d1 255 // Program exits if the file pointer returns NULL.
uld 20:76f94dec91d1 256 exit(1);
uld 20:76f94dec91d1 257 }
uld 20:76f94dec91d1 258
uld 20:76f94dec91d1 259 fscanf(fptr,"%d", &x);
uld 20:76f94dec91d1 260 fclose(fptr);
uld 20:76f94dec91d1 261
uld 20:76f94dec91d1 262 y = x+1;
uld 20:76f94dec91d1 263 fptr = fopen(PITLOGPATH,"w");
uld 20:76f94dec91d1 264
uld 20:76f94dec91d1 265 if(fptr == NULL)
uld 20:76f94dec91d1 266 {
uld 20:76f94dec91d1 267 printf("Error creating log file ");
uld 20:76f94dec91d1 268 exit(1);
uld 20:76f94dec91d1 269 }
uld 20:76f94dec91d1 270 fprintf(fptr,"%d", y);
uld 20:76f94dec91d1 271 fclose(fptr);
uld 20:76f94dec91d1 272 }
uld 20:76f94dec91d1 273
uld 20:76f94dec91d1 274 /*
uld 20:76f94dec91d1 275 void PS_DisplayNumberofPS(void){
uld 27:8561eeb0bd1d 276 // Display the number i the pitstop recorded in the logfile
uld 20:76f94dec91d1 277 FILE *fptr;
uld 20:76f94dec91d1 278 int x;
uld 20:76f94dec91d1 279 if ((fptr = fopen(PITLOGPATH,"r")) == NULL){
uld 20:76f94dec91d1 280 printf("Error! opening file");
uld 20:76f94dec91d1 281 // Program exits if the file pointer returns NULL.
uld 20:76f94dec91d1 282 exit(1);
uld 20:76f94dec91d1 283 }
uld 20:76f94dec91d1 284 fscanf(fptr,"%d", &x);
uld 20:76f94dec91d1 285 printf("Final number of pits stops %d", x);
uld 20:76f94dec91d1 286 fclose(fptr);
uld 20:76f94dec91d1 287 }
uld 20:76f94dec91d1 288 */
uld 20:76f94dec91d1 289
uld 20:76f94dec91d1 290 void TE_CreateVoltageLog(void){
uld 20:76f94dec91d1 291 /* Create a voltagelog file and test if it can open*/
uld 20:76f94dec91d1 292 FILE *fptr;
uld 20:76f94dec91d1 293 fptr = fopen(VOLTAGELOGPATH,"w");
uld 20:76f94dec91d1 294
uld 20:76f94dec91d1 295 if(fptr == NULL)
uld 20:76f94dec91d1 296 {
uld 20:76f94dec91d1 297 printf("Error creating log file ");
uld 20:76f94dec91d1 298 exit(1);
uld 20:76f94dec91d1 299 }
uld 20:76f94dec91d1 300
uld 20:76f94dec91d1 301 fclose(fptr);
uld 20:76f94dec91d1 302 }
uld 20:76f94dec91d1 303
uld 21:c3e256b18b96 304 void TE_LogVoltage(int count){
uld 20:76f94dec91d1 305 /* Create a pitlog file and test if it can open*/
uld 20:76f94dec91d1 306 FILE *fptr; /* voltagelog adres */
uld 20:76f94dec91d1 307 fptr = fopen(VOLTAGELOGPATH,"a");
uld 20:76f94dec91d1 308
uld 26:e6d82a8ba556 309 fprintf(fptr," %8d %4.4f %4.4f \n" ,count, m3pi.battery(),m3pi.pot_voltage() );
uld 20:76f94dec91d1 310 fclose(fptr);
uld 32:570b94fe2c19 311 }
uld 32:570b94fe2c19 312
uld 32:570b94fe2c19 313 // Julesnaps smager godt og Andreas skal drikke hvergang en anden drikker