Julesnaps / Mbed 2 deprecated Linefollowproject

Dependencies:   m3pi mbed

Committer:
uld
Date:
Wed Oct 12 11:25:20 2022 +0000
Revision:
68:bb2ee5b4f9dd
Parent:
67:990fbcfee16f
Child:
69:d7125d5b5cc8
Fixed Log

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