Julesnaps / Mbed 2 deprecated Linefollowproject

Dependencies:   m3pi mbed

Committer:
uld
Date:
Tue Oct 11 11:42:16 2022 +0000
Revision:
23:7e9505da7f48
Parent:
22:5d3332fc4c5c
Child:
24:6427b144b17c
Fixedstorage

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;
vehus 15:8b76add42254 5 DigitalOut led1(LED1);
vehus 15:8b76add42254 6 DigitalOut led2(LED2);
vehus 15:8b76add42254 7 DigitalOut led3(LED3);
vehus 15:8b76add42254 8 DigitalOut led4(LED4);
magnusmland 0:f562e4f9c29f 9
magnusmland 0:f562e4f9c29f 10 // Minimum and maximum motor speeds
magnusmland 0:f562e4f9c29f 11 #define MAX 1.0
magnusmland 0:f562e4f9c29f 12 #define MIN 0
magnusmland 0:f562e4f9c29f 13
magnusmland 0:f562e4f9c29f 14 // PID terms
magnusmland 0:f562e4f9c29f 15 #define P_TERM 1
magnusmland 0:f562e4f9c29f 16 #define I_TERM 0
magnusmland 0:f562e4f9c29f 17 #define D_TERM 20
magnusmland 0:f562e4f9c29f 18
uld 20:76f94dec91d1 19
uld 20:76f94dec91d1 20 // Textfile paths
uld 22:5d3332fc4c5c 21
uld 22:5d3332fc4c5c 22
uld 22:5d3332fc4c5c 23
uld 23:7e9505da7f48 24 #define PITLOGPATH "/local/pitlog.txt"
uld 23:7e9505da7f48 25 #define VOLTAGELOGPATH "/local/voltage.txt"
uld 20:76f94dec91d1 26
uld 20:76f94dec91d1 27
uld 7:ac88c8e35048 28 // Prototypes
uld 8:5640c8c5088e 29 int PitTest(void); // Test if to robot needs to goto pit
vehus 15:8b76add42254 30 void InitialMessages(void); // Prints initial message to the LCD
vehus 15:8b76add42254 31 void LED_Control(int ledNumber, int state); //turn ledNumber to 1=on, 0 = off
vehus 15:8b76add42254 32 void Blink(int ledNumber); // make ledNumber blinik
mikkelbredholt 13:ddff4bb7c24f 33
uld 20:76f94dec91d1 34 void PS_PitStop(void); //
uld 20:76f94dec91d1 35 void PS_CreateLog(void); // create a log file or resets it (WIP
uld 20:76f94dec91d1 36 void PS_AddStopToLog(void); // Add one to the log
uld 20:76f94dec91d1 37 // void PS_DisplayNumberofPS(void); // Display the final number on screen WIP
uld 20:76f94dec91d1 38
uld 20:76f94dec91d1 39 void TE_CreateVoltageLog(void); //
uld 21:c3e256b18b96 40 void TE_LogVoltage(int count); // test funktion that write the woltage each time the battry is checked
uld 20:76f94dec91d1 41
magnusmland 0:f562e4f9c29f 42 int main() {
uld 22:5d3332fc4c5c 43 LocalFileSystem local("local");
magnusmland 0:f562e4f9c29f 44 m3pi.sensor_auto_calibrate();
uld 5:dbd32cb3650a 45
uld 6:6865930c1135 46 /*Base program Variable initiation*/
magnusmland 0:f562e4f9c29f 47 float right;
magnusmland 0:f562e4f9c29f 48 float left;
magnusmland 0:f562e4f9c29f 49 float current_pos_of_line = 0.0;
magnusmland 0:f562e4f9c29f 50 float previous_pos_of_line = 0.0;
magnusmland 0:f562e4f9c29f 51 float derivative,proportional,integral = 0;
magnusmland 0:f562e4f9c29f 52 float power;
magnusmland 0:f562e4f9c29f 53 float speed = MAX;
uld 6:6865930c1135 54
uld 6:6865930c1135 55 /*Team 7 Variabels*/
uld 7:ac88c8e35048 56 int gotoPit = 0; // wether or not the robot is heading to pit. Initialstate false.
vehus 15:8b76add42254 57 int ccount = 0; //used to count cycles
uld 6:6865930c1135 58
uld 23:7e9505da7f48 59 /*Printing secret cat mission*/
uld 23:7e9505da7f48 60 InitialMessages();
uld 23:7e9505da7f48 61
uld 20:76f94dec91d1 62 /*Create pitlog used to log the number of pitstop */
uld 20:76f94dec91d1 63 PS_CreateLog();
uld 22:5d3332fc4c5c 64 TE_CreateVoltageLog();
uld 23:7e9505da7f48 65
uld 20:76f94dec91d1 66
uld 7:ac88c8e35048 67
uld 6:6865930c1135 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 */
vehus 15:8b76add42254 71 if (ccount % 100 == 0 && gotoPit == 0)
vehus 15:8b76add42254 72 {
uld 21:c3e256b18b96 73 TE_LogVoltage(ccount);
vehus 15:8b76add42254 74 gotoPit = PitTest();
uld 20:76f94dec91d1 75
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 20:76f94dec91d1 81 /*Run the pitstop function*/
uld 20:76f94dec91d1 82 PS_PitStop ();
vehus 15:8b76add42254 83
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 8:5640c8c5088e 121 ccount++;
uld 7:ac88c8e35048 122 }
uld 20:76f94dec91d1 123 // PS_DisplayNumberofPS();
vehus 15:8b76add42254 124
uld 7:ac88c8e35048 125 }
uld 7:ac88c8e35048 126
vehus 15:8b76add42254 127 void InitialMessages(void){
uld 9:7b9094864268 128 /*Prints iniatl secret mission*/
uld 9:7b9094864268 129
uld 23:7e9505da7f48 130
uld 23:7e9505da7f48 131
uld 9:7b9094864268 132 m3pi.cls();
uld 9:7b9094864268 133 m3pi.locate(0,0);
uld 9:7b9094864268 134 m3pi.printf("eliminate");
uld 9:7b9094864268 135 m3pi.locate(0,1);
uld 9:7b9094864268 136 m3pi.printf("all cats");
uld 20:76f94dec91d1 137 wait(5.0);
uld 9:7b9094864268 138
uld 9:7b9094864268 139 m3pi.cls();
uld 9:7b9094864268 140 m3pi.locate(0,0);
uld 20:76f94dec91d1 141 m3pi.printf("%4.4f ",m3pi.battery());
uld 9:7b9094864268 142 m3pi.locate(0,1);
uld 20:76f94dec91d1 143 m3pi.printf("%4.4f ",m3pi.pot_voltage());
uld 20:76f94dec91d1 144 wait(10.0);
uld 9:7b9094864268 145 m3pi.cls();
uld 23:7e9505da7f48 146 m3pi.locate(0,0);
uld 23:7e9505da7f48 147 m3pi.printf("** 3 **");
uld 23:7e9505da7f48 148 wait(1.0);
uld 23:7e9505da7f48 149 m3pi.cls();
uld 23:7e9505da7f48 150 m3pi.printf("** 2 **");
uld 23:7e9505da7f48 151
uld 23:7e9505da7f48 152
uld 23:7e9505da7f48 153
uld 9:7b9094864268 154 }
uld 8:5640c8c5088e 155 int PitTest(void){
uld 7:ac88c8e35048 156 /* Test the batteri voltage if the robot is not headed for pit */
uld 7:ac88c8e35048 157
uld 20:76f94dec91d1 158 const float BATVOLTTRESHOLD = 4.64; // Treshold i volt
vehus 15:8b76add42254 159 int result = 0;
uld 7:ac88c8e35048 160
mikkelbredholt 13:ddff4bb7c24f 161 /*Test if the voltage is below the threshold if so turn on go to pit mode*/
vehus 15:8b76add42254 162 if (m3pi.battery() <= BATVOLTTRESHOLD ){
uld 9:7b9094864268 163 result = 1; // Set goto pit condition
vehus 15:8b76add42254 164 LED_Control(1, 1);
uld 7:ac88c8e35048 165 m3pi.cls();
uld 7:ac88c8e35048 166 m3pi.locate(0,0);
uld 7:ac88c8e35048 167 m3pi.printf("Going to");
uld 7:ac88c8e35048 168 m3pi.locate(0,1);
uld 7:ac88c8e35048 169 m3pi.printf("PIT Party");
magnusmland 0:f562e4f9c29f 170 }
uld 8:5640c8c5088e 171 return result;
mikkelbredholt 13:ddff4bb7c24f 172 }
mikkelbredholt 13:ddff4bb7c24f 173
mikkelbredholt 13:ddff4bb7c24f 174 void LED_Control(int ledNumber, int state){
mikkelbredholt 13:ddff4bb7c24f 175 //LED1 on if robot is looking for pit
uld 20:76f94dec91d1 176 if (ledNumber == 1) {
vehus 15:8b76add42254 177 led1 = state;
vehus 15:8b76add42254 178 }
uld 20:76f94dec91d1 179 if (ledNumber == 2){
vehus 15:8b76add42254 180 led2 = state;
mikkelbredholt 13:ddff4bb7c24f 181 }
uld 20:76f94dec91d1 182 if (ledNumber == 3){
vehus 15:8b76add42254 183 led3 = state;
vehus 15:8b76add42254 184 }
uld 20:76f94dec91d1 185 if (ledNumber == 4){
vehus 15:8b76add42254 186 led4 = state;
mikkelbredholt 13:ddff4bb7c24f 187 }
vehus 15:8b76add42254 188 }
vehus 15:8b76add42254 189
vehus 15:8b76add42254 190 void Blink(int ledNumber)
vehus 15:8b76add42254 191 {
vehus 15:8b76add42254 192 int a = 2;
vehus 15:8b76add42254 193 LED_Control (ledNumber , 0);
vehus 15:8b76add42254 194 wait(a);
vehus 15:8b76add42254 195 LED_Control (ledNumber , 1);
vehus 15:8b76add42254 196 wait(a);
mikkelbredholt 13:ddff4bb7c24f 197 }
vehus 15:8b76add42254 198
uld 20:76f94dec91d1 199 void PS_PitStop(void)
vehus 15:8b76add42254 200 {
vehus 15:8b76add42254 201 /* Testing alternative stop function
vehus 15:8b76add42254 202 m3pi.left_motor(0);
vehus 15:8b76add42254 203 m3pi.right_motor(0);
vehus 15:8b76add42254 204 */
vehus 15:8b76add42254 205 m3pi.stop(); // stop all engine
vehus 15:8b76add42254 206
vehus 15:8b76add42254 207 // increase counter with one
vehus 15:8b76add42254 208 while (1)
vehus 15:8b76add42254 209 {
vehus 15:8b76add42254 210 Blink (1); // signal in pit
vehus 15:8b76add42254 211
vehus 15:8b76add42254 212 /* missing input to stop blink. */
vehus 15:8b76add42254 213
mikkelbredholt 13:ddff4bb7c24f 214 }
uld 20:76f94dec91d1 215 }
uld 20:76f94dec91d1 216
uld 20:76f94dec91d1 217 void PS_CreateLog(void){
uld 20:76f94dec91d1 218 /* Create a pitlog file and test if it can open*/
uld 20:76f94dec91d1 219 FILE *fptr;
uld 20:76f94dec91d1 220 fptr = fopen(PITLOGPATH,"w");
uld 20:76f94dec91d1 221
uld 20:76f94dec91d1 222 if(fptr == NULL)
uld 20:76f94dec91d1 223 {
uld 20:76f94dec91d1 224 printf("Error creating log file ");
uld 20:76f94dec91d1 225 exit(1);
uld 20:76f94dec91d1 226 }
uld 20:76f94dec91d1 227 fprintf(fptr,"%d", 0);
uld 20:76f94dec91d1 228 fclose(fptr);
uld 20:76f94dec91d1 229 }
uld 20:76f94dec91d1 230
uld 20:76f94dec91d1 231 void PS_AddStopToLog(void){
uld 20:76f94dec91d1 232 /*Opens the pit log and read the number.
uld 20:76f94dec91d1 233 * Then adds one to that number at write it into the pitlog */
uld 20:76f94dec91d1 234
uld 20:76f94dec91d1 235 FILE *fptr;
uld 20:76f94dec91d1 236 int x, y;
uld 20:76f94dec91d1 237 if ((fptr = fopen(PITLOGPATH,"r")) == NULL){
uld 20:76f94dec91d1 238 printf("Error! opening file");
uld 20:76f94dec91d1 239 // Program exits if the file pointer returns NULL.
uld 20:76f94dec91d1 240 exit(1);
uld 20:76f94dec91d1 241 }
uld 20:76f94dec91d1 242
uld 20:76f94dec91d1 243 fscanf(fptr,"%d", &x);
uld 20:76f94dec91d1 244 fclose(fptr);
uld 20:76f94dec91d1 245
uld 20:76f94dec91d1 246 y = x+1;
uld 20:76f94dec91d1 247 fptr = fopen(PITLOGPATH,"w");
uld 20:76f94dec91d1 248
uld 20:76f94dec91d1 249 if(fptr == NULL)
uld 20:76f94dec91d1 250 {
uld 20:76f94dec91d1 251 printf("Error creating log file ");
uld 20:76f94dec91d1 252 exit(1);
uld 20:76f94dec91d1 253 }
uld 20:76f94dec91d1 254 fprintf(fptr,"%d", y);
uld 20:76f94dec91d1 255 fclose(fptr);
uld 20:76f94dec91d1 256 }
uld 20:76f94dec91d1 257
uld 20:76f94dec91d1 258 /*
uld 20:76f94dec91d1 259 void PS_DisplayNumberofPS(void){
uld 20:76f94dec91d1 260 //Display the number i the pitstop recorded in the logfile
uld 20:76f94dec91d1 261 /*
uld 20:76f94dec91d1 262 FILE *fptr;
uld 20:76f94dec91d1 263 int x;
uld 20:76f94dec91d1 264 if ((fptr = fopen(PITLOGPATH,"r")) == NULL){
uld 20:76f94dec91d1 265 printf("Error! opening file");
uld 20:76f94dec91d1 266 // Program exits if the file pointer returns NULL.
uld 20:76f94dec91d1 267 exit(1);
uld 20:76f94dec91d1 268 }
uld 20:76f94dec91d1 269 fscanf(fptr,"%d", &x);
uld 20:76f94dec91d1 270 printf("Final number of pits stops %d", x);
uld 20:76f94dec91d1 271 fclose(fptr);
uld 20:76f94dec91d1 272 }
uld 20:76f94dec91d1 273 */
uld 20:76f94dec91d1 274
uld 20:76f94dec91d1 275 void TE_CreateVoltageLog(void){
uld 20:76f94dec91d1 276 /* Create a voltagelog file and test if it can open*/
uld 20:76f94dec91d1 277 FILE *fptr;
uld 20:76f94dec91d1 278 fptr = fopen(VOLTAGELOGPATH,"w");
uld 20:76f94dec91d1 279
uld 20:76f94dec91d1 280 if(fptr == NULL)
uld 20:76f94dec91d1 281 {
uld 20:76f94dec91d1 282 printf("Error creating log file ");
uld 20:76f94dec91d1 283 exit(1);
uld 20:76f94dec91d1 284 }
uld 20:76f94dec91d1 285
uld 20:76f94dec91d1 286 fclose(fptr);
uld 20:76f94dec91d1 287 }
uld 20:76f94dec91d1 288
uld 20:76f94dec91d1 289
uld 21:c3e256b18b96 290 void TE_LogVoltage(int count){
uld 20:76f94dec91d1 291 /* Create a pitlog file and test if it can open*/
uld 20:76f94dec91d1 292 FILE *fptr; /* voltagelog adres */
uld 20:76f94dec91d1 293 fptr = fopen(VOLTAGELOGPATH,"a");
uld 20:76f94dec91d1 294
uld 20:76f94dec91d1 295 fprintf(fptr," %8d %4.4f %4.4f " ,count, m3pi.battery(),m3pi.pot_voltage() );
uld 20:76f94dec91d1 296 fclose(fptr);
magnusmland 0:f562e4f9c29f 297 }