Julesnaps / Mbed 2 deprecated Linefollowproject

Dependencies:   m3pi mbed

Committer:
uld
Date:
Wed Oct 12 11:57:45 2022 +0000
Revision:
70:e6cc44d1487d
Parent:
69:d7125d5b5cc8
Child:
71:4747e63eb48c
Fixed

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