Janus Bo Andersen / Mbed 2 deprecated T2PRO1_master

Dependencies:   mbed m3pi

Revision:
25:17e9e410795c
Parent:
24:7fa7383e1b07
Child:
26:857158816262
--- a/main.cpp	Wed Dec 05 11:46:47 2018 +0000
+++ b/main.cpp	Wed Dec 05 12:34:41 2018 +0000
@@ -1,6 +1,12 @@
 #include "mbed.h"
 #include "m3pi.h"
 
+/*TEST PARAMS SET CURRENTLY
+ * Battery voltage: 4.3
+ * Charging time: 60 seconds
+ * Shows the battery voltage at each loop of the infinite loop
+ */
+
 m3pi m3pi; //declare a new car object
 
 //Filename settings
@@ -8,15 +14,15 @@
 #define FILENAME "/robot/data.txt"  //filename to write backups to
 
 //Team calibrated parameters for car and battery
-#define LOW_BATTERY_VOLTAGE 4.75  //measured voltage where charging is required
-#define CHARGE_TIME 20 //in seconds. 1200 seconds = 20 minutes
+#define LOW_BATTERY_VOLTAGE 4.3  //measured voltage where charging is required
+#define CHARGE_TIME 60 //in seconds. 1200 seconds = 20 minutes
 #define CROSSROAD_REFL_VAL 350 //sensor reading on PC0 and PC4 at crossroad
 
 //Pit control
 #define ENTER_PIT 1  //direction constant to mark driven forward out of pit
 #define LEAVE_PIT -1  //direction constant to mark backing out of pit
 #define SPEED_INSIDE_PIT 0.2  //speed to drive inside the pit
-#define PIT_TRANSPORT_TIME 1  //seconds to drive pit stretch at interal pit spd
+#define PIT_TRANSPORT_TIME 1  //seconds to drive pit stretch at internal pit spd
 
 //Car sensors and LEDs
 #define PC0 0 //location of PC0 reflectance sensor
@@ -47,6 +53,10 @@
 } performance_data;  
 
 
+//Prototype
+void write_to_file(performance_data data);
+
+
 //Function to write car performance data to persistent file
 void write_to_file(performance_data data) {
 
@@ -199,8 +209,14 @@
     
     for (int i = 0; i < charge_time; i++) {
         
+        show_voltage();
+        
         // PERFORM SOME WEIRD CALCULATION HERE
-        wait(1.0);
+        
+        //For now just put in a dummy number
+        wh_this_session += 1;
+        
+        wait(1.0); //wait one second
     
     } //end for
     
@@ -208,6 +224,11 @@
     return wh_this_session;
 }
 
+//Show the battery voltage in the display
+void show_voltage() {
+    m3pi.printf("%07.2fV", m3pi.battery() );
+}
+
 
 //display values from the performance data struct in the display
 void show_car_data(performance_data data) {
@@ -229,11 +250,11 @@
     
     //print Wh in first line
     m3pi.locate(0,0);
-    printf("%06.2fWh", data.wh); //6 wide in total, 2 after comma, pad with zero
+    m3pi.printf("%06.2fWh", data.wh); //6 wide in total, 2 after comma, pad with zero
     
     //print Pitstops in second line
     m3pi.locate(0,1);
-    printf("%06dPs", data.pitstops); //6 wide in total, padded with zeros
+    m3pi.printf("%06dPs", data.pitstops); //6 wide in total, padded with zeros
     
 }
 
@@ -256,6 +277,9 @@
     bool pit_crossroad_flag = false; //goes true when car is at the pit crossrd
     bool button_pressed_flag = false; //goes true when the button has been prs
     
+    DigitalOut pit_light(LED1);  //Blue LED on mbed, lights up when in pit
+    DigitalOut low_batt_light(p20);  //Red LED on expansion board
+    
     
     //define the button: user push button on p21
     DigitalIn button(p21);
@@ -282,13 +306,17 @@
     //Begin main event loop    
     while (1) {
       
+      //TEST ONLY
+      show_voltage();
+      
       /* Check sensors and update sensor-based flags
        * - might not be every loop: wrap this in a timer of sorts */
       battery_low_flag = battery_low();
+      low_batt_light = battery_low_flag;
 
       pit_crossroad_flag = car_at_pit_crossroad(sensor_values); //anden frekvens?
       
-      //debounce the button
+      //Detect button press... Do we need to debounce the button?
       if (button == 0) {
           button_pressed_flag = true;
       }
@@ -312,7 +340,7 @@
           
           //if low_batt: decellerate car (slow down) and continue to pit
           if ( battery_low_flag ) {
-              speed = (speed > PIT_SPEED) ? speed-DECELLERATION : PIT_SPEED;
+              speed = (speed > PIT_SPEED) ? speed-DECELLERATION : PIT_SPEED;   
           }
           
           //if @pitcrossrd, not going to charge: increment internal lap counter
@@ -322,7 +350,10 @@
           
           //if low_batt && atPitIntersection: Stop && execute pit navigation program
           if ( battery_low_flag && pit_crossroad_flag ) {
-              //We are at the pit, and the battery is low
+              //We are at the pit because the battery is low
+              
+              //turn on the pitlight
+              pit_light = 1;
               
               //Enter the pit
               pit_navigate(ENTER_PIT);
@@ -343,6 +374,12 @@
               //Reset pit-related flags
               battery_low_flag = false;
               pit_crossroad_flag = false;
+              
+              show_car_data(car_data);
+              
+              pit_light = 0;
+              
+              wait(5);
           }
           
           /*