Julesnaps / Mbed 2 deprecated Linefollowproject

Dependencies:   m3pi mbed

Committer:
magnusmland
Date:
Tue Oct 25 09:48:58 2022 +0000
Revision:
86:ca4ecb8828ed
Parent:
84:9727f2186ee2
Parent:
85:204026c91369
Child:
87:88aebb4c7226
test

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"
uld 71:4747e63eb48c 3 #include <cstdio>
magnusmland 0:f562e4f9c29f 4
magnusmland 0:f562e4f9c29f 5 m3pi m3pi;
uld 77:e1ece5800d7a 6 Timer timer;
uld 32:570b94fe2c19 7
uld 77:e1ece5800d7a 8 // DigitalOuts & Global Variabels
vehus 15:8b76add42254 9 DigitalOut led1(LED1);
vehus 15:8b76add42254 10 DigitalOut led2(LED2);
vehus 15:8b76add42254 11 DigitalOut led3(LED3);
vehus 15:8b76add42254 12 DigitalOut led4(LED4);
uld 77:e1ece5800d7a 13 int startTime;
magnusmland 0:f562e4f9c29f 14
magnusmland 0:f562e4f9c29f 15 // Minimum and maximum motor speeds
magnusmland 74:23064681b4f3 16 #define MAX 0.65
magnusmland 0:f562e4f9c29f 17 #define MIN 0
magnusmland 0:f562e4f9c29f 18
magnusmland 86:ca4ecb8828ed 19 // PID terms Test
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 67:990fbcfee16f 24 // Ccount before test
uld 67:990fbcfee16f 25 #define CYCLEBEFORETEST 4500
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 7:ac88c8e35048 31 // Prototypes
uld 28:2c93dff934b1 32 void LED_Control(int ledNumber, int state); //Turn ledNumber to 1=on, 0 = off
uld 28:2c93dff934b1 33 void LED_Blink(int ledNumber); // Make ledNumber blinik
uld 75:b3a43f70e44c 34
uld 53:cef682f8684b 35 void LCD_CountDown(int num); //LCD Coundown function
uld 53:cef682f8684b 36 void LCD_InitialMessages(void); // Prints initial message to the LCD
uld 26:e6d82a8ba556 37
uld 53:cef682f8684b 38 int PS_BatteryTest(void); // Test if to robot needs to goto pit
uld 20:76f94dec91d1 39 void PS_PitStop(void); //
uld 20:76f94dec91d1 40 void PS_CreateLog(void); // create a log file or resets it (WIP
uld 72:fad84371f431 41 void PS_AddStopToLog(void); // Add one to the log
uld 81:a7a44a0944b5 42 int PS_GetNumberofPS(void); // Get number form file
uld 81:a7a44a0944b5 43 void PS_DisplayPS(void);
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
uld 7:ac88c8e35048 48
magnusmland 0:f562e4f9c29f 49 int main() {
uld 22:5d3332fc4c5c 50 LocalFileSystem local("local");
uld 77:e1ece5800d7a 51 timer.start();
uld 77:e1ece5800d7a 52 startTime = timer.read();
uld 77:e1ece5800d7a 53
uld 77:e1ece5800d7a 54
uld 6:6865930c1135 55 /*Base program Variable initiation*/
magnusmland 0:f562e4f9c29f 56 float right;
magnusmland 0:f562e4f9c29f 57 float left;
magnusmland 0:f562e4f9c29f 58 float current_pos_of_line = 0.0;
magnusmland 0:f562e4f9c29f 59 float previous_pos_of_line = 0.0;
magnusmland 0:f562e4f9c29f 60 float derivative,proportional,integral = 0;
magnusmland 0:f562e4f9c29f 61 float power;
magnusmland 0:f562e4f9c29f 62 float speed = MAX;
uld 6:6865930c1135 63
uld 6:6865930c1135 64 /*Team 7 Variabels*/
uld 7:ac88c8e35048 65 int gotoPit = 0; // wether or not the robot is heading to pit. Initialstate false.
uld 67:990fbcfee16f 66 int ccount = 0; //used to count cycles
uld 6:6865930c1135 67
uld 23:7e9505da7f48 68 /*Printing secret cat mission*/
uld 81:a7a44a0944b5 69 PS_DisplayPS();
uld 53:cef682f8684b 70 LCD_InitialMessages();
uld 27:8561eeb0bd1d 71 m3pi.sensor_auto_calibrate();
uld 27:8561eeb0bd1d 72
uld 72:fad84371f431 73
uld 72:fad84371f431 74 /*Create logs used to log the number of pitstop */
uld 68:bb2ee5b4f9dd 75 PS_CreateLog(); //
uld 22:5d3332fc4c5c 76 TE_CreateVoltageLog();
uld 7:ac88c8e35048 77
magnusmland 0:f562e4f9c29f 78 while (1) {
uld 5:dbd32cb3650a 79
uld 78:0d905b656132 80 /* If cycle count divided by a constant does not have a rest. test if pit */
uld 67:990fbcfee16f 81 if (ccount % CYCLEBEFORETEST == 0 && gotoPit == 0)
vehus 15:8b76add42254 82 {
uld 67:990fbcfee16f 83 TE_LogVoltage(ccount);
uld 53:cef682f8684b 84 gotoPit = PS_BatteryTest();
vehus 15:8b76add42254 85 }
vehus 15:8b76add42254 86 if (gotoPit == 1)
vehus 15:8b76add42254 87 {
uld 20:76f94dec91d1 88 /*Add one to the nummber allready in the pitlog*/
uld 20:76f94dec91d1 89 PS_AddStopToLog();
uld 69:d7125d5b5cc8 90
uld 20:76f94dec91d1 91 /*Run the pitstop function*/
uld 28:2c93dff934b1 92 PS_PitStop();
vehus 15:8b76add42254 93 }
vehus 15:8b76add42254 94
magnusmland 0:f562e4f9c29f 95 // Get the position of the line.
magnusmland 0:f562e4f9c29f 96 current_pos_of_line = m3pi.line_position();
magnusmland 0:f562e4f9c29f 97 proportional = current_pos_of_line;
magnusmland 0:f562e4f9c29f 98
magnusmland 0:f562e4f9c29f 99 // Compute the derivative
magnusmland 0:f562e4f9c29f 100 derivative = current_pos_of_line - previous_pos_of_line;
magnusmland 0:f562e4f9c29f 101
magnusmland 0:f562e4f9c29f 102 // Compute the integral
magnusmland 0:f562e4f9c29f 103 integral += proportional;
magnusmland 0:f562e4f9c29f 104
magnusmland 0:f562e4f9c29f 105 // Remember the last position.
magnusmland 0:f562e4f9c29f 106 previous_pos_of_line = current_pos_of_line;
magnusmland 0:f562e4f9c29f 107
magnusmland 0:f562e4f9c29f 108 // Compute the power
magnusmland 0:f562e4f9c29f 109 power = (proportional * (P_TERM) ) + (integral*(I_TERM)) + (derivative*(D_TERM)) ;
magnusmland 0:f562e4f9c29f 110
magnusmland 0:f562e4f9c29f 111 // Compute new speeds
magnusmland 0:f562e4f9c29f 112 right = speed+power;
magnusmland 0:f562e4f9c29f 113 left = speed-power;
magnusmland 0:f562e4f9c29f 114
magnusmland 0:f562e4f9c29f 115 // limit checks
magnusmland 0:f562e4f9c29f 116 if (right < MIN)
magnusmland 0:f562e4f9c29f 117 right = MIN;
magnusmland 0:f562e4f9c29f 118 else if (right > MAX)
magnusmland 0:f562e4f9c29f 119 right = MAX;
magnusmland 0:f562e4f9c29f 120
magnusmland 0:f562e4f9c29f 121 if (left < MIN)
magnusmland 0:f562e4f9c29f 122 left = MIN;
magnusmland 0:f562e4f9c29f 123 else if (left > MAX)
magnusmland 0:f562e4f9c29f 124 left = MAX;
magnusmland 0:f562e4f9c29f 125
magnusmland 0:f562e4f9c29f 126 // set speed
magnusmland 0:f562e4f9c29f 127 m3pi.left_motor(left);
magnusmland 0:f562e4f9c29f 128 m3pi.right_motor(right);
uld 6:6865930c1135 129
uld 67:990fbcfee16f 130 ccount++;
uld 7:ac88c8e35048 131 }
vehus 15:8b76add42254 132
uld 7:ac88c8e35048 133 }
uld 7:ac88c8e35048 134
uld 72:fad84371f431 135 /**
uld 72:fad84371f431 136 * LCD_InitialMessages -Prints iniatial secret mission
uld 72:fad84371f431 137 */
uld 53:cef682f8684b 138 void LCD_InitialMessages(void){
uld 73:0646bad028c5 139
uld 9:7b9094864268 140 m3pi.cls();
uld 9:7b9094864268 141 m3pi.locate(0,0);
uld 69:d7125d5b5cc8 142 m3pi.printf("DESTROY");
uld 9:7b9094864268 143 m3pi.locate(0,1);
uld 69:d7125d5b5cc8 144 m3pi.printf("**CATS**");
uld 20:76f94dec91d1 145 wait(5.0);
uld 9:7b9094864268 146 m3pi.cls();
uld 9:7b9094864268 147 m3pi.locate(0,0);
uld 69:d7125d5b5cc8 148 m3pi.printf("%4.4f ",m3pi.battery());
uld 9:7b9094864268 149 m3pi.locate(0,1);
uld 69:d7125d5b5cc8 150 m3pi.printf("%4.4f ",m3pi.pot_voltage());
uld 20:76f94dec91d1 151 wait(10.0);
uld 28:2c93dff934b1 152 m3pi.cls();
uld 27:8561eeb0bd1d 153 m3pi.locate(0,0);
uld 70:e6cc44d1487d 154 m3pi.printf("ROBOT ON");
uld 27:8561eeb0bd1d 155 m3pi.locate(0,1);
uld 69:d7125d5b5cc8 156 m3pi.printf("TRACK!!");
uld 27:8561eeb0bd1d 157 wait(4.0);
uld 53:cef682f8684b 158 LCD_CountDown(3);
uld 24:6427b144b17c 159 m3pi.cls();
uld 26:e6d82a8ba556 160 m3pi.locate(0,0);
uld 69:d7125d5b5cc8 161 m3pi.printf("** GO **");
uld 69:d7125d5b5cc8 162
uld 72:fad84371f431 163
uld 53:cef682f8684b 164 wait (1.0);
uld 26:e6d82a8ba556 165 }
uld 72:fad84371f431 166
uld 72:fad84371f431 167 /**
uld 72:fad84371f431 168 * PS_DisplayPS - Display the number of pidstops from the log
uld 72:fad84371f431 169 */
uld 72:fad84371f431 170
uld 72:fad84371f431 171 void PS_DisplayPS(void){
uld 72:fad84371f431 172 m3pi.cls();
uld 72:fad84371f431 173 m3pi.locate(0,0);
uld 72:fad84371f431 174 m3pi.printf("PITSTOP:");
uld 72:fad84371f431 175 m3pi.locate(0,1);
uld 73:0646bad028c5 176 m3pi.printf("%d", PS_GetNumberofPS() );
uld 72:fad84371f431 177
uld 72:fad84371f431 178 }
uld 73:0646bad028c5 179
uld 72:fad84371f431 180 /**
uld 72:fad84371f431 181 * LCD_CountDown - display a countdown
uld 72:fad84371f431 182 * @num The number to count down from-
uld 72:fad84371f431 183 */
uld 53:cef682f8684b 184 void LCD_CountDown(int num){
uld 56:ea9a5cfbcc6b 185
uld 72:fad84371f431 186 for (int i=num; i>0; i--)
uld 26:e6d82a8ba556 187 {
uld 26:e6d82a8ba556 188 m3pi.cls();
uld 26:e6d82a8ba556 189 m3pi.locate(0,0);
uld 70:e6cc44d1487d 190 m3pi.printf("** %d **", i);
uld 26:e6d82a8ba556 191 wait(1.0);
uld 26:e6d82a8ba556 192 }
uld 82:f7443adac0c7 193 }
uld 73:0646bad028c5 194
uld 72:fad84371f431 195 /**
uld 72:fad84371f431 196 * PS_BatteryTest - Test the batteri voltage if the robot is not headed for pi
uld 72:fad84371f431 197 *
uld 72:fad84371f431 198 *return: 0 if the battery is above the threshold- Return 1 if the robot needs to goto pit
uld 72:fad84371f431 199 */
uld 53:cef682f8684b 200 int PS_BatteryTest(void){
uld 7:ac88c8e35048 201
uld 53:cef682f8684b 202 const float BATVOLTTRESHOLD = 0.5; // Treshold i volt
vehus 15:8b76add42254 203 int result = 0;
uld 77:e1ece5800d7a 204
mikkelbredholt 13:ddff4bb7c24f 205 /*Test if the voltage is below the threshold if so turn on go to pit mode*/
vehus 15:8b76add42254 206 if (m3pi.battery() <= BATVOLTTRESHOLD ){
uld 9:7b9094864268 207 result = 1; // Set goto pit condition
vehus 15:8b76add42254 208 LED_Control(1, 1);
uld 79:9d46b0d532e1 209
uld 7:ac88c8e35048 210 m3pi.cls();
uld 7:ac88c8e35048 211 m3pi.locate(0,0);
uld 69:d7125d5b5cc8 212 m3pi.printf("Going to");
uld 7:ac88c8e35048 213 m3pi.locate(0,1);
uld 69:d7125d5b5cc8 214 m3pi.printf("**PIT**");
magnusmland 0:f562e4f9c29f 215 }
uld 8:5640c8c5088e 216 return result;
mikkelbredholt 13:ddff4bb7c24f 217 }
mikkelbredholt 13:ddff4bb7c24f 218
uld 72:fad84371f431 219 /**
uld 72:fad84371f431 220 * LED_Blink - Make a LED blink
uld 72:fad84371f431 221 *@ledNumber - The number of the targeted LED
uld 72:fad84371f431 222 */
uld 28:2c93dff934b1 223 void LED_Blink(int ledNumber)
vehus 15:8b76add42254 224 {
vehus 15:8b76add42254 225 int a = 2;
vehus 15:8b76add42254 226 LED_Control (ledNumber , 0);
vehus 15:8b76add42254 227 wait(a);
vehus 15:8b76add42254 228 LED_Control (ledNumber , 1);
vehus 15:8b76add42254 229 wait(a);
magnusmland 63:be66d3b84cef 230 LED_Control (ledNumber , 0);
magnusmland 63:be66d3b84cef 231 wait(a);
mikkelbredholt 13:ddff4bb7c24f 232 }
vehus 15:8b76add42254 233
uld 72:fad84371f431 234 /**
uld 72:fad84371f431 235 * PS_PitStop - Stops the robot and starts the signal
uld 72:fad84371f431 236 */
uld 20:76f94dec91d1 237 void PS_PitStop(void)
vehus 15:8b76add42254 238 {
vehus 15:8b76add42254 239 m3pi.stop(); // stop all engine
vehus 15:8b76add42254 240 // increase counter with one
vehus 15:8b76add42254 241 while (1)
vehus 15:8b76add42254 242 {
uld 28:2c93dff934b1 243 LED_Blink (1); // signal in pit
mikkelbredholt 13:ddff4bb7c24f 244 }
uld 20:76f94dec91d1 245 }
uld 20:76f94dec91d1 246
uld 72:fad84371f431 247 /**
uld 72:fad84371f431 248 * PS_CreateLog - Create a pitstop log i none excist
uld 72:fad84371f431 249 *
uld 72:fad84371f431 250 * Return: The number of ppitstops as noted in the log
uld 72:fad84371f431 251 */
uld 20:76f94dec91d1 252 void PS_CreateLog(void){
uld 20:76f94dec91d1 253 FILE *fptr;
uld 68:bb2ee5b4f9dd 254
uld 68:bb2ee5b4f9dd 255 if ((fptr = fopen(PITLOGPATH,"r")) == NULL){
uld 68:bb2ee5b4f9dd 256 fptr = fopen(PITLOGPATH,"w");
uld 68:bb2ee5b4f9dd 257 fprintf(fptr,"%d", 0);
uld 71:4747e63eb48c 258 fclose(fptr);
uld 68:bb2ee5b4f9dd 259 }
uld 68:bb2ee5b4f9dd 260
uld 20:76f94dec91d1 261 }
uld 20:76f94dec91d1 262
uld 75:b3a43f70e44c 263 /**
uld 75:b3a43f70e44c 264 * PS_AddStopToLog - Add one to the number in the pitstop log
uld 75:b3a43f70e44c 265 */
uld 20:76f94dec91d1 266 void PS_AddStopToLog(void){
uld 75:b3a43f70e44c 267
uld 20:76f94dec91d1 268 FILE *fptr;
uld 20:76f94dec91d1 269 int x, y;
uld 20:76f94dec91d1 270 if ((fptr = fopen(PITLOGPATH,"r")) == NULL){
uld 20:76f94dec91d1 271 printf("Error! opening file");
uld 20:76f94dec91d1 272 // Program exits if the file pointer returns NULL.
uld 20:76f94dec91d1 273 exit(1);
uld 20:76f94dec91d1 274 }
uld 20:76f94dec91d1 275
uld 20:76f94dec91d1 276 fscanf(fptr,"%d", &x);
uld 20:76f94dec91d1 277 fclose(fptr);
uld 20:76f94dec91d1 278
uld 20:76f94dec91d1 279 y = x+1;
uld 20:76f94dec91d1 280 fptr = fopen(PITLOGPATH,"w");
uld 20:76f94dec91d1 281
uld 20:76f94dec91d1 282 if(fptr == NULL)
uld 20:76f94dec91d1 283 {
uld 20:76f94dec91d1 284 printf("Error creating log file ");
uld 20:76f94dec91d1 285 exit(1);
uld 20:76f94dec91d1 286 }
uld 20:76f94dec91d1 287 fprintf(fptr,"%d", y);
uld 20:76f94dec91d1 288 fclose(fptr);
uld 20:76f94dec91d1 289 }
uld 20:76f94dec91d1 290
uld 75:b3a43f70e44c 291 /** PS_GetNumberofPS - Return the number i the pitstop recorded in the logfile
uld 75:b3a43f70e44c 292 *
uld 75:b3a43f70e44c 293 *return: An interger from the the pitlog
uld 75:b3a43f70e44c 294 */
uld 72:fad84371f431 295 int PS_GetNumberofPS(void){
uld 75:b3a43f70e44c 296 //
uld 20:76f94dec91d1 297 FILE *fptr;
uld 20:76f94dec91d1 298 int x;
uld 20:76f94dec91d1 299 if ((fptr = fopen(PITLOGPATH,"r")) == NULL){
uld 20:76f94dec91d1 300 printf("Error! opening file");
uld 20:76f94dec91d1 301 // Program exits if the file pointer returns NULL.
uld 20:76f94dec91d1 302 exit(1);
uld 20:76f94dec91d1 303 }
uld 20:76f94dec91d1 304 fscanf(fptr,"%d", &x);
uld 72:fad84371f431 305 //printf("Final number of pits stops %d", x);
uld 20:76f94dec91d1 306 fclose(fptr);
uld 72:fad84371f431 307 return x;
uld 20:76f94dec91d1 308 }
uld 72:fad84371f431 309
uld 75:b3a43f70e44c 310 /** TE_CreateVoltageLog - create a voltagelog
uld 75:b3a43f70e44c 311 */
uld 20:76f94dec91d1 312 void TE_CreateVoltageLog(void){
uld 20:76f94dec91d1 313 /* Create a voltagelog file and test if it can open*/
uld 20:76f94dec91d1 314 FILE *fptr;
uld 20:76f94dec91d1 315 fptr = fopen(VOLTAGELOGPATH,"w");
uld 20:76f94dec91d1 316
uld 20:76f94dec91d1 317 if(fptr == NULL)
uld 20:76f94dec91d1 318 {
uld 20:76f94dec91d1 319 printf("Error creating log file ");
uld 20:76f94dec91d1 320 exit(1);
uld 20:76f94dec91d1 321 }
uld 20:76f94dec91d1 322
uld 20:76f94dec91d1 323 fclose(fptr);
uld 20:76f94dec91d1 324 }
uld 20:76f94dec91d1 325
uld 75:b3a43f70e44c 326 /** TE_LogVoltage - Add an entry to the voltagelog
uld 75:b3a43f70e44c 327 *@count: The number the counter has reached in the main loop
uld 75:b3a43f70e44c 328 */
uld 21:c3e256b18b96 329 void TE_LogVoltage(int count){
uld 75:b3a43f70e44c 330
uld 20:76f94dec91d1 331 FILE *fptr; /* voltagelog adres */
uld 20:76f94dec91d1 332 fptr = fopen(VOLTAGELOGPATH,"a");
uld 20:76f94dec91d1 333
uld 78:0d905b656132 334 fprintf(fptr,"%8d,%8d,%4.4f,%4.4f\n" , timer.read()-startTime, count, m3pi.battery(),m3pi.pot_voltage() );
uld 20:76f94dec91d1 335 fclose(fptr);
uld 58:852b91920a44 336
uld 79:9d46b0d532e1 337 }