Julesnaps / Mbed 2 deprecated Linefollowproject

Dependencies:   m3pi mbed

Committer:
uld
Date:
Tue Oct 25 08:58:07 2022 +0000
Revision:
77:e1ece5800d7a
Parent:
76:4a6286ff0aee
Child:
78:0d905b656132
disable blink

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