Julesnaps / Mbed 2 deprecated Linefollowproject

Dependencies:   m3pi mbed

Committer:
uld
Date:
Tue Oct 25 10:16:14 2022 +0000
Revision:
88:55cd0d863d4f
Parent:
87:88aebb4c7226
Child:
89:5ea90b86fefb
Logfixes

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