working 1

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
fjwats
Date:
Sat Mar 11 15:18:15 2017 +0000
Parent:
8:1af0c46d21d2
Commit message:
changed how telemUpdate works, all data is now written at once from one routine that takes no args. Uses global vars "curWheelAngle" and "curVelocity". getTemp routine now returns temperature in float so no temp is actually stored in the program.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Sat Mar 11 14:12:04 2017 +0000
+++ b/main.cpp	Sat Mar 11 15:18:15 2017 +0000
@@ -1,9 +1,11 @@
 #include "mbed.h"
+#include <fstream>
 
 #define MAX 0x0A
 #define HI 0x01
 #define LO 0x00
 
+//pin declarations
 DigitalOut led_1(LED1);         //program running.
 DigitalOut led_2(LED2);         //sensors operating.
 DigitalOut led_3(LED3);         //is moving.
@@ -20,7 +22,7 @@
 PwmOut steering(p21);           // steering modification
 PwmOut velocity(p22);           // velocity modification
 
-Timer US;                                                                       // <-- Iain, comment pls
+Timer US;                       // measures time between ping and pong in getPing() routine
 
 //global variable dec
 int rawUS_data[5]={0,0,0,0,0};  //raw data{chan1,chan2,chan3,chan4,chan5}
@@ -30,7 +32,11 @@
 int US4_mean[MAX]={0,0,0,0,0,0,0,0,0,0};
 int US5_mean[MAX]={0,0,0,0,0,0,0,0,0,0};
 int turn_rate[2]={0,0};
-float vo = 0;
+
+float curVelocity = 0.0;                // current speed the car is travelling
+float curWheelAngle = 0.0;              // current angle the wheels are set to
+
+std::ofstream stream ("DriveData.txt");        // file object for holding info about telem data gathered since last power on
 
 //function constructs
 void setActiveUS(int chan);             //select sensor
@@ -38,14 +44,14 @@
 void turn(float s);                     //turn car -1 left 0 centre 1 right
 void drive(float v);                    //drive -1 reverse 1 forward
 void stop(void);                        //stop the car
-void getTemp(void);
+float getTemp(void);                    //read a temp from the sensor
 int measurement_mean(int chan);
 
 int main() {
     int iCount = 0;
     int measured = 0;       
     int iMean = 0;
-    double mean_measured1[2] = {0,0};
+    double mean_measured1[2] = {0,0};                                           // <-- Iain could you comment the local variables pls
     double mean_measured2[2] = {0,0};
     double mean_measured3[2] = {0,0};
     double mean_measured4[2] = {0,0};
@@ -59,7 +65,7 @@
             setActiveUS(iCount);            //set mux address
             rawUS_data[iCount] = getPing();  //get raw measurement
             iCount +=1;
-            wait(0.4);                                                          //will need calibrating
+            wait(0.4);                                                          // <-- will need calibrating
         }
         
         US1_mean[0]=rawUS_data[0];          //assign new value
@@ -94,11 +100,12 @@
         //steer right
         int value_to_steer_UCL = 1; 
         int value_to_steer_LCL = -1; 
-        int val1=0;
-        int val2=0;
-        int val3=0;
-        int val4=0;
-        int val5=0;
+        int val1=0;                         // left side
+        int val2=0;                         // left quarter
+        int val3=0;                         // forward
+        int val4=0;                         // right quarter
+        int val5=0;                         // right side
+        
         val1 = mean_measured1[1] - mean_measured1[0];
         val2 = mean_measured2[1] - mean_measured2[0];
         val3 = mean_measured3[1] - mean_measured3[0];
@@ -107,17 +114,22 @@
         
         if((val2 < value_to_steer_LCL) && (val4 > value_to_steer_UCL)){
             //turn right
+            //curWheelAngle += 10;
+            //drive(curWheelAngle);
             }
         else if((val2 > value_to_steer_UCL) && (val4 < value_to_steer_LCL)){
             //turn left
+            //curWheelAngle += 10;
+            //drive(curWheelAngle);
             }
         else {
             // drive
+            // <-- do we need an else here? the car will continue travelling anyway
             }
         
         //drive at 1 speed
         iMean +=1;
-        }   
+    }   
 }
 
 void setActiveUS(int chan){
@@ -175,35 +187,31 @@
 }
 
 void turn(float t){
-    if (t++>=0&&s<=2) {steering.pulsewidth(t/2000+0.001);}                      // calculate steering angle to pulsewidth conversion
-    telemUpdate();                                                              // save telem file after angle change
-    return;    
+    if (t++>=0 && t<=2) {steering.pulsewidth(t/2000+0.001);}                    // convert steering angle into a pulsewidth
+    curWheelAngle = t;
+    return;
 }
     
 void drive(float v){
-    if (v++>=0&&s<=2) {velocity.pulsewidth(v/2000+0.001);}                      // calculate velocity to pulsewidth conversion
-    telemUpdate(0.0);                                                           // save telem file after speed change
+    if (v++>=0 && v<=2) {velocity.pulsewidth(v/2000+0.001);}                    // convert velocity into a pulsewidth
+    curVelocity = v;                                                            // set current velocity
     return;
 }
 
 void stop(void){
-    drive(0.0);                                                                 // Reduce speed to 0
+    drive(0.0);                                                                 // Reduce speed to 0, regardless of direction car is travelling
+    curVelocity = 0.0;
     return;
 }
 
-void getTemp(void) {                                                            // Reads ambient temperature from Variable Resistor
-    telemUpdate(temperature);                                                   // calls telemUpdate to write ambient temp to file
-    return;                                                                                                                             // <-- Unfinished, needs algorithm for voltage to temp conversion
+float getTemp(void) {                                                            // Reads ambient temperature from Variable Resistor
+    return temperature;                                                                                                             // <-- Unfinished, needs algorithm for voltage to temp conversion
 }
 
-void telemUpdate(float steer, float speed) {                                    // Opens set file for logging, appends some data, then returns.
-    stream << "steer:" << steer << " speed:" << speed << "\n";                  // This method is only called for speed and heading changes.
-    return;
-}
-
-void telemUpdate(float temp) {                                                  // Opens set file for logging, appends some data, then returns.
-    stream << "temp:" << temp << "\n";                                          // This method is only called for ambient temperature readings.
-    return;
+void telemUpdate() {                                                            // Opens set file for logging, appends some data, then returns.
+    stream << "Steering angle:" << curWheelAngle << " Velocity:" << curVelocity;// Writes values for speed and heading changes.
+    stream << " Ambient Temp:" << getTemp() << "\n";                            // Writes value for temp sensor
+    return;                                                                                                                         // <-- If possible we should timestamp each telemUpdate in the file
 }
 
 int measurement_mean(int chan){