Julesnaps / Mbed 2 deprecated Linefollowproject

Dependencies:   m3pi mbed

Committer:
uld
Date:
Tue Oct 25 09:28:05 2022 +0000
Revision:
81:a7a44a0944b5
Parent:
80:dd2134edd58b
Child:
82:f7443adac0c7
Fixes

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 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 7:ac88c8e35048 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 6:6865930c1135 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 6:6865930c1135 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 7:ac88c8e35048 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 26:e6d82a8ba556 193 }
uld 73:0646bad028c5 194
uld 7:ac88c8e35048 195
uld 79:9d46b0d532e1 196 void InitialMessage(void){
uld 79:9d46b0d532e1 197 /*Prints iniatl secret mission*/
uld 7:ac88c8e35048 198
uld 79:9d46b0d532e1 199 m3pi.cls();
uld 79:9d46b0d532e1 200 m3pi.locate(0,0);
uld 81:a7a44a0944b5 201 m3pi.printf("Destroy");
uld 79:9d46b0d532e1 202 m3pi.locate(0,1);
uld 81:a7a44a0944b5 203 m3pi.printf("cats");
uld 79:9d46b0d532e1 204 wait(200.0);
uld 79:9d46b0d532e1 205
uld 79:9d46b0d532e1 206 m3pi.cls();
uld 79:9d46b0d532e1 207 m3pi.locate(0,0);
uld 79:9d46b0d532e1 208 m3pi.printf("%f.3 ",m3pi.battery());
uld 79:9d46b0d532e1 209 m3pi.locate(0,1);
uld 79:9d46b0d532e1 210 m3pi.printf("%f.3 ",m3pi.pot_voltage());
uld 79:9d46b0d532e1 211 wait(200.0);
uld 79:9d46b0d532e1 212 m3pi.cls();
uld 79:9d46b0d532e1 213 }
magnusmland 0:f562e4f9c29f 214
uld 72:fad84371f431 215 /**
uld 72:fad84371f431 216 * PS_BatteryTest - Test the batteri voltage if the robot is not headed for pi
uld 72:fad84371f431 217 *
uld 72:fad84371f431 218 *return: 0 if the battery is above the threshold- Return 1 if the robot needs to goto pit
uld 72:fad84371f431 219 */
uld 53:cef682f8684b 220 int PS_BatteryTest(void){
uld 7:ac88c8e35048 221
uld 53:cef682f8684b 222 const float BATVOLTTRESHOLD = 0.5; // Treshold i volt
vehus 15:8b76add42254 223 int result = 0;
uld 77:e1ece5800d7a 224
mikkelbredholt 13:ddff4bb7c24f 225 /*Test if the voltage is below the threshold if so turn on go to pit mode*/
vehus 15:8b76add42254 226 if (m3pi.battery() <= BATVOLTTRESHOLD ){
uld 9:7b9094864268 227 result = 1; // Set goto pit condition
vehus 15:8b76add42254 228 LED_Control(1, 1);
uld 79:9d46b0d532e1 229
uld 7:ac88c8e35048 230 m3pi.cls();
uld 7:ac88c8e35048 231 m3pi.locate(0,0);
uld 7:ac88c8e35048 232 m3pi.printf("Going to");
uld 7:ac88c8e35048 233 m3pi.locate(0,1);
uld 69:d7125d5b5cc8 234 m3pi.printf("**PIT**");
magnusmland 0:f562e4f9c29f 235 }
uld 8:5640c8c5088e 236 return result;
mikkelbredholt 13:ddff4bb7c24f 237 }
mikkelbredholt 13:ddff4bb7c24f 238
mikkelbredholt 13:ddff4bb7c24f 239 void LED_Control(int ledNumber, int state){
mikkelbredholt 13:ddff4bb7c24f 240 //LED1 on if robot is looking for pit
uld 20:76f94dec91d1 241 if (ledNumber == 1) {
vehus 15:8b76add42254 242 led1 = state;
vehus 15:8b76add42254 243 }
uld 20:76f94dec91d1 244 if (ledNumber == 2){
vehus 15:8b76add42254 245 led2 = state;
mikkelbredholt 13:ddff4bb7c24f 246 }
uld 20:76f94dec91d1 247 if (ledNumber == 3){
vehus 15:8b76add42254 248 led3 = state;
vehus 15:8b76add42254 249 }
uld 20:76f94dec91d1 250 if (ledNumber == 4){
vehus 15:8b76add42254 251 led4 = state;
mikkelbredholt 13:ddff4bb7c24f 252 }
vehus 15:8b76add42254 253 }
uld 75:b3a43f70e44c 254
uld 72:fad84371f431 255 /**
uld 72:fad84371f431 256 * LED_Blink - Make a LED blink
uld 72:fad84371f431 257 *@ledNumber - The number of the targeted LED
uld 72:fad84371f431 258 */
uld 28:2c93dff934b1 259 void LED_Blink(int ledNumber)
vehus 15:8b76add42254 260 {
vehus 15:8b76add42254 261 int a = 2;
vehus 15:8b76add42254 262 LED_Control (ledNumber , 0);
vehus 15:8b76add42254 263 wait(a);
vehus 15:8b76add42254 264 LED_Control (ledNumber , 1);
vehus 15:8b76add42254 265 wait(a);
magnusmland 63:be66d3b84cef 266 LED_Control (ledNumber , 0);
magnusmland 63:be66d3b84cef 267 wait(a);
mikkelbredholt 13:ddff4bb7c24f 268 }
vehus 15:8b76add42254 269
uld 72:fad84371f431 270 /**
uld 72:fad84371f431 271 * PS_PitStop - Stops the robot and starts the signal
uld 72:fad84371f431 272 */
uld 20:76f94dec91d1 273 void PS_PitStop(void)
vehus 15:8b76add42254 274 {
vehus 15:8b76add42254 275 m3pi.stop(); // stop all engine
vehus 15:8b76add42254 276 // increase counter with one
vehus 15:8b76add42254 277 while (1)
vehus 15:8b76add42254 278 {
uld 28:2c93dff934b1 279 LED_Blink (1); // signal in pit
magnusmland 0:f562e4f9c29f 280 }
mikkelbredholt 10:9b04c532b57b 281 }
uld 7:ac88c8e35048 282
uld 72:fad84371f431 283 /**
uld 72:fad84371f431 284 * PS_CreateLog - Create a pitstop log i none excist
uld 72:fad84371f431 285 *
uld 72:fad84371f431 286 * Return: The number of ppitstops as noted in the log
uld 72:fad84371f431 287 */
uld 20:76f94dec91d1 288 void PS_CreateLog(void){
uld 20:76f94dec91d1 289 FILE *fptr;
uld 68:bb2ee5b4f9dd 290
uld 68:bb2ee5b4f9dd 291 if ((fptr = fopen(PITLOGPATH,"r")) == NULL){
uld 68:bb2ee5b4f9dd 292 fptr = fopen(PITLOGPATH,"w");
uld 68:bb2ee5b4f9dd 293 fprintf(fptr,"%d", 0);
uld 71:4747e63eb48c 294 fclose(fptr);
uld 68:bb2ee5b4f9dd 295 }
uld 68:bb2ee5b4f9dd 296
uld 20:76f94dec91d1 297 }
uld 20:76f94dec91d1 298
uld 75:b3a43f70e44c 299 /**
uld 75:b3a43f70e44c 300 * PS_AddStopToLog - Add one to the number in the pitstop log
uld 75:b3a43f70e44c 301 */
uld 20:76f94dec91d1 302 void PS_AddStopToLog(void){
uld 75:b3a43f70e44c 303
uld 20:76f94dec91d1 304 FILE *fptr;
uld 20:76f94dec91d1 305 int x, y;
uld 20:76f94dec91d1 306 if ((fptr = fopen(PITLOGPATH,"r")) == NULL){
uld 20:76f94dec91d1 307 printf("Error! opening file");
uld 20:76f94dec91d1 308 // Program exits if the file pointer returns NULL.
uld 20:76f94dec91d1 309 exit(1);
uld 20:76f94dec91d1 310 }
uld 20:76f94dec91d1 311
uld 20:76f94dec91d1 312 fscanf(fptr,"%d", &x);
uld 20:76f94dec91d1 313 fclose(fptr);
uld 20:76f94dec91d1 314
uld 20:76f94dec91d1 315 y = x+1;
uld 20:76f94dec91d1 316 fptr = fopen(PITLOGPATH,"w");
uld 20:76f94dec91d1 317
uld 20:76f94dec91d1 318 if(fptr == NULL)
uld 20:76f94dec91d1 319 {
uld 20:76f94dec91d1 320 printf("Error creating log file ");
uld 20:76f94dec91d1 321 exit(1);
uld 20:76f94dec91d1 322 }
uld 20:76f94dec91d1 323 fprintf(fptr,"%d", y);
uld 20:76f94dec91d1 324 fclose(fptr);
uld 20:76f94dec91d1 325 }
uld 20:76f94dec91d1 326
uld 75:b3a43f70e44c 327 /** PS_GetNumberofPS - Return the number i the pitstop recorded in the logfile
uld 75:b3a43f70e44c 328 *
uld 75:b3a43f70e44c 329 *return: An interger from the the pitlog
uld 75:b3a43f70e44c 330 */
uld 72:fad84371f431 331 int PS_GetNumberofPS(void){
uld 75:b3a43f70e44c 332 //
uld 20:76f94dec91d1 333 FILE *fptr;
uld 20:76f94dec91d1 334 int x;
uld 20:76f94dec91d1 335 if ((fptr = fopen(PITLOGPATH,"r")) == NULL){
uld 20:76f94dec91d1 336 printf("Error! opening file");
uld 20:76f94dec91d1 337 // Program exits if the file pointer returns NULL.
uld 20:76f94dec91d1 338 exit(1);
uld 20:76f94dec91d1 339 }
uld 20:76f94dec91d1 340 fscanf(fptr,"%d", &x);
uld 72:fad84371f431 341 //printf("Final number of pits stops %d", x);
uld 20:76f94dec91d1 342 fclose(fptr);
uld 72:fad84371f431 343 return x;
uld 20:76f94dec91d1 344 }
uld 72:fad84371f431 345
uld 75:b3a43f70e44c 346 /** TE_CreateVoltageLog - create a voltagelog
uld 75:b3a43f70e44c 347 */
uld 20:76f94dec91d1 348 void TE_CreateVoltageLog(void){
uld 20:76f94dec91d1 349 /* Create a voltagelog file and test if it can open*/
uld 20:76f94dec91d1 350 FILE *fptr;
uld 20:76f94dec91d1 351 fptr = fopen(VOLTAGELOGPATH,"w");
uld 20:76f94dec91d1 352
uld 20:76f94dec91d1 353 if(fptr == NULL)
uld 20:76f94dec91d1 354 {
uld 20:76f94dec91d1 355 printf("Error creating log file ");
uld 20:76f94dec91d1 356 exit(1);
uld 20:76f94dec91d1 357 }
uld 20:76f94dec91d1 358
uld 20:76f94dec91d1 359 fclose(fptr);
uld 20:76f94dec91d1 360 }
uld 76:4a6286ff0aee 361
uld 75:b3a43f70e44c 362 /** TE_LogVoltage - Add an entry to the voltagelog
uld 75:b3a43f70e44c 363 *@count: The number the counter has reached in the main loop
uld 75:b3a43f70e44c 364 */
uld 21:c3e256b18b96 365 void TE_LogVoltage(int count){
uld 75:b3a43f70e44c 366
uld 20:76f94dec91d1 367 FILE *fptr; /* voltagelog adres */
uld 20:76f94dec91d1 368 fptr = fopen(VOLTAGELOGPATH,"a");
uld 20:76f94dec91d1 369
uld 78:0d905b656132 370 fprintf(fptr,"%8d,%8d,%4.4f,%4.4f\n" , timer.read()-startTime, count, m3pi.battery(),m3pi.pot_voltage() );
uld 20:76f94dec91d1 371 fclose(fptr);
uld 58:852b91920a44 372
uld 79:9d46b0d532e1 373 }