car using PID from centre line

Dependencies:   FRDM-TFC mbed CBuffer XBEE mbed_angular_speed motor2 MMA8451Q

Fork of KL25Z_Camera_Test by GDP 4

Revision:
18:0095a3a8f8e4
Parent:
17:6ae90788cc2b
Child:
19:65f0b6febc23
--- a/main.cpp	Fri Dec 02 14:36:37 2016 +0000
+++ b/main.cpp	Tue Dec 06 14:15:55 2016 +0000
@@ -31,6 +31,8 @@
 Timer t1;
 Timer t2;
 
+//testing timer
+Timer test;
 
 int main() {
     //Set up TFC driver stuff
@@ -78,7 +80,7 @@
             #endif
             
             // Check if car is at the stop line
-            //handleStartStop();
+            handleStartStop();
             
             
             //Reset image ready flag
@@ -167,7 +169,16 @@
     }
     
     if(turning) {
-        dutyCycleCorner(0.4,servo_pid.output);
+        
+        //default
+        //TFC_SetMotorPWM(0.4,0.4);
+        
+        dutyCycleCorner(speed,servo_pid.output);
+        
+        //may want to have just to set cornering speed at different if going to be slowing down for cornering.
+        //dutyCycleCorner(float cornerspeed, servo_pid.output);
+        //dutyCycleCorner(speed, servo_pid.output);
+        
         //sensorCorner(left_motor_pid.desired_value, right_motor_pid.desired_value , servo_pid.output, 50);  
     }
     
@@ -256,8 +267,10 @@
 inline void handleStartStop() {
     //Hacky way to detect the start/stop signal
     if(right - left < 60) {
-        sendString("START STOP!! &d",startstop); 
-        
+        sendString("START STOP!! &d",startstop);//do you mean %d?
+    
+        lapTime(); 
+        //testSpeed(speed) HOLY SHIT ITS DAT BOI!!!!!!!!
         if(seen) {
             seen = false;
         } else {
@@ -270,7 +283,8 @@
             TFC_HBRIDGE_DISABLE;
             startstop = 0;      
         }
-    }    
+    }   
+     
 }
 
 
@@ -404,17 +418,20 @@
         }
         
         if(xb.cBuffer->available() > 0 && curr_cmd == 0) {
+            //Start car
             char cmd = xb.cBuffer->read();
             if(cmd == 'D') {
                 ALIGN_SERVO;
                 TFC_HBRIDGE_ENABLE;
                 TFC_SetMotorPWM(RIGHT_MOTOR_COMPENSATION_RATIO*speed,speed);
                 servo_pid.integral = 0;
-                
+                test.start();
+                lapNo =0;
                 
             } else if (cmd == 'C') {
                 TFC_SetMotorPWM(0.0,0.0);
                 TFC_HBRIDGE_DISABLE;
+                endTest();
             } else if(cmd == 'A') {
                 curr_cmd = 'A';
             } else if(cmd == 'F') {
@@ -425,4 +442,60 @@
             
         }
 }
+
+float testSpeed(float speed)
+{
+    // search: Speed Increase
+    // every time the car sees the stop start the speed of the car will increase
+    // this can occur on stop start trigger.
+    // may need to send the speed back to the telemetry.
+    if (speed>0.4)
+    {
+        speed+=0.05;
+     }
+     
+     else
+     {
+       speed+=0.1;   
+         
+     }   
+     
+     
+     sendString("s = %f", speed);
+     return speed;
+        
+}
+
+
+
+int lapTime()
+{
+    // function which sends the lap time back to the telemetry.
+    float newTime= test.read();
+    lapNo += 1;
+    float lapTime= newTime-oldTime;
+    float avgTime= newTime/lapNo;
+    
+    sendString("For lap number: %d  Lap Time: %f  Avergae time: %f ", lapNo,lapTime,avgTime);
+    
+     // OH WHAT UP IT'S DAT BOI!!!!
+    return 0;
+}
+
+
+void endTest()
+{// This runs when the car has stopped, this should give the final elapsed time and othere things. this also stops the timer
+    
+    float time= test.read();
+    
+    sendString("Laps done: %d  Time elapsed: %f   Average time: %f",lapNo, time,float (time/lapNo));
+    test.stop();
+    
+    
+}
+
+
+
+
+
 #endif
\ No newline at end of file