Janus Bo Andersen / Mbed 2 deprecated T2PRO1_master

Dependencies:   mbed m3pi

Revision:
19:c0d59019de53
Parent:
18:8b3efa4d4a36
Child:
20:32e5cf8fe6dd
diff -r 8b3efa4d4a36 -r c0d59019de53 main.cpp
--- a/main.cpp	Mon Dec 03 15:51:51 2018 +0000
+++ b/main.cpp	Tue Dec 04 15:01:11 2018 +0000
@@ -8,58 +8,84 @@
 #define FILENAME "/robot/data.txt"
 
 // Minimum and maximum motor speeds
-#define MAX 0.3
+#define MAX 1.0
 #define MIN 0
+#define OPTIMAL_SPEED 0.8
+#define PIT_SPEED 0.3
 
 // PID terms
 #define P_TERM 1
+#define D_TERM 8
 #define I_TERM 0
-#define D_TERM 8
+
+//Forward declare our type for storing car performance date
+//It can now be used as a type by the name performance_data
+typedef struct pd {
+    float wh;
+    int pitstops;
+} performance_data;  
 
 
-//typedef -> definer som type i C++
-//uden typedef - det er ikke en type, så du skal skrive struct performance_data
-typedef struct performance_data { //<- navn på structen
-    float wh;
-    int pitstops;
-} performance_data;  //<- navn på typen
-
-
+//Function to write car performance data to persistent file
 void write_to_file(performance_data data) {
 
     //Define file system
     LocalFileSystem local(FILESYS);
     
-    //Make a pointer to the file
-    FILE *fp = fopen(FILENAME, "w");  //"w" "r" "a"
+    //Open a file (or create a new) with write access
+    //"w": write "r":read "a":append (add @end)
+    //"w": will overwrite any existing data, so we just store one line.
+    FILE *fp = fopen(FILENAME, "w");
     
-    fprintf(fp, "%0.2f %d", data.wh, data.pitstops);  //write to the file
+    //write performance data to the file
+    fprintf(fp, "%f %d", data.wh, data.pitstops);
     
-    fclose(fp);  //close when done.
+    //close the file and release the memory when done.
+    fclose(fp);  
 
 }
 
+//Function to read car performance data from persistent file
 performance_data read_from_file() {
 
+    //Declare the variable which will ultimately be returned
     performance_data data;
     
-    
-    //Define file system
+    //Define file system on mbed LPC1768
     LocalFileSystem local(FILESYS);
     
-    //Make a pointer to the file
-    FILE *fp = fopen(FILENAME, "r");  //"w" "r" "a"
+    //Try to open the file as read (it might not exist)
+    FILE *fp = fopen(FILENAME, "r");
+    
+    //Test whether the file exists
+    if (fp != NULL) {
+        
+        //file exists, so read from it
+        
+        //Read stored data from the file and put in struct
+        // &(struct.member) and &struct.member yield same address
+        fscanf(fp, "%f %d", &(data.wh), &(data.pitstops) );
     
-    //Make some error prevention later
-    fscanf(fp, "%f %d", &(data.wh), &(data.pitstops) );  //write to the file
+        //close file and release mem.
+        fclose(fp);  
+        
+    } else {
+        
+        //file doesn't exist, so just return zeros (no performance yet)
+        data.wh = 0.0;
+        data.pitstops = 0;
+    }   
     
-    fclose(fp);  //close when done.
-    
+    //return the data object to the caller
     return data;
-   
+}
+
+bool battery_low() {
+    return 0;   
 }
 
 
+//TO DO: CLEAN UP THIS FUNCTION
 void sensor_all(int arr[]) //lav en funktion? ved ikke hvad jeg skal her
      {
         m3pi.putc(0x87); // send noget sensor data
@@ -97,44 +123,38 @@
 
 int main() {
     
-    //test the function to write to a local file
-    performance_data robot_stats;
-    robot_stats.wh = 120.5;
-    robot_stats.pitstops = 200;
-    
-    write_to_file(robot_stats);
-    
-    performance_data new_data;
-    new_data = read_from_file();
-    //might get an error here due to timing
-    
-    
-    new_data.wh = new_data.wh * 2;
-    new_data.pitstops = new_data.pitstops * 2;
-    
-    write_to_file(new_data);
-    //should get 247... and 246
+    /* Define all needed variables in this section */
+    performance_data car_data; //Saves the car's Wh and pitstops data
+
+    float right; //right motor speed
+    float left; //left motor speed
+    float current_pos_of_line = 0.0; //for PID
+    float previous_pos_of_line = 0.0; //for PID
+    float derivative,proportional,integral = 0; //for PID
+    float power; //differential speed
+    float speed = OPTIMAL_SPEED; //our optimal speed setting
+    int sensor_val[5]; //array for reflectance sensor values
+
 
+    /* When the car starts, read previous progress
+     * if only connected to USB, do not perform this restore process 
+     */
+     
+    // If robot is on this returns a value, otherwise it's just connected USB
+    if (m3pi.battery() > 0) {  
+        
+        car_data = read_from_file(); //restore data from file
+ 
+    }
+
+    //Info to user
     m3pi.locate(0,1);
     m3pi.printf("Line PID");
 
-    wait(1.0);
-
+    //Calibrate before starting <- might be moved somewhere else
     m3pi.sensor_auto_calibrate();
 
-    float right;
-    float left;
-    float current_pos_of_line = 0.0;
-    float previous_pos_of_line = 0.0;
-    float derivative,proportional,integral = 0;
-    float power;
-    float speed = MAX;
-    int sensor_val[5];
-    
-    
-    /* if restartedFromOff: Load values from file, recalibrate and start.
-    */
-    
+    //Begin main event loop    
     while (1) {
       
       /*
@@ -142,7 +162,10 @@
           if nothing else: line following
           if low_batt: slow down and continue
           if atPitIntersection: increment internal lap counter
+          
           if low_batt && atPitIntersection: Stop && execute pit navigation program
+          
+          
           if all_sensors == 1000 (lifted from track): 
                 show Wh and pitstops on display
                 stop motors, reset PID errors, wait 20 secs, recalibrate sensors