Updated with Statistics Library

Dependencies:   QEI2 chair_BNO055 PID Watchdog VL53L1X_Filter ros_lib_kinetic

Files at this revision

API Documentation at this revision

Comitter:
t1jain
Date:
Fri Jun 28 23:52:23 2019 +0000
Parent:
27:da718b990837
Commit message:
Updated with Statistics Library

Changed in this revision

Statistics/statistics.cpp Show annotated file Show diff for this revision Revisions of this file
Statistics/statistics.h Show annotated file Show diff for this revision Revisions of this file
wheelchair.cpp Show annotated file Show diff for this revision Revisions of this file
wheelchair.h Show annotated file Show diff for this revision Revisions of this file
--- a/Statistics/statistics.cpp	Fri Jun 28 21:16:26 2019 +0000
+++ b/Statistics/statistics.cpp	Fri Jun 28 23:52:23 2019 +0000
@@ -1,13 +1,14 @@
 #include "statistics.h"
 #include "mbed.h"
 
-    statistics::statistics(int* Input, int dataLengthIn, int firstDataPoint){
+    statistics::statistics(int* Input, int dataLengthIn, int firstDataPointIn){
         data = Input;
         dataLength = dataLengthIn;
+        firstDataPoint = firstDataPointIn;
         }
     double statistics::mean(){
         double sum;
-        for(int i = 0; i < dataLength; ++i)
+        for(int i = firstDataPoint; i < (firstDataPoint + dataLength); ++i)
         {
             sum += data[i];
         }
@@ -16,19 +17,24 @@
         return average;
     }
     double statistics::stdev(){
-        float sum = 0.0, mean, standardDeviation = 0.0;
+        double sum = 0.0, mean, standardDeviation = 0.0;
+        printf("The length of the array is %d\n", dataLength);
+        //double num = (pow(2.0,2.0));
+        //printf("2^2 is %f\n", num);
+        //int i;
 
-        int i;
-
-        for(int i = 0; i < dataLength; ++i)
+        for(int i = firstDataPoint; i < (firstDataPoint + dataLength); ++i)
         {
             sum += data[i];
+            printf("%d\n", data[i]);
         }
     
         mean = sum/dataLength;
     
-        for(int i = 0; i < dataLength; ++i)
-            standardDeviation += pow(data[i] - mean, 2);
+        for(int i = firstDataPoint; i < (firstDataPoint + dataLength); ++i) {
+            //standardDeviation += pow(data[i] - mean, 2);
+            standardDeviation += (data[i] - mean)*(data[i] - mean);
+            }
     
         return sqrt(standardDeviation / dataLength);
     }
--- a/Statistics/statistics.h	Fri Jun 28 21:16:26 2019 +0000
+++ b/Statistics/statistics.h	Fri Jun 28 23:52:23 2019 +0000
@@ -14,6 +14,7 @@
   private:
    int* data;
    int dataLength;
+   int firstDataPoint;
 
     
 };
--- a/wheelchair.cpp	Fri Jun 28 21:16:26 2019 +0000
+++ b/wheelchair.cpp	Fri Jun 28 23:52:23 2019 +0000
@@ -35,6 +35,24 @@
     curr_pos = wheel->getDistance(53.975);
 }
 
+void Wheelchair::emergencyButton_thread ()
+{
+    while(1) {
+        while(!e_button) {
+
+            //Stop wheelchair
+            Wheelchair::stop();
+            printf("E-button has been pressed\n\n\n");
+            off->write(high);                              //turn off PCB
+            on->write(0);                                  //make sure PCB not on
+            //Reset Board
+            NVIC_SystemReset();
+
+        }
+
+    }
+}
+
 void Wheelchair::assistSafe_thread()
 {
     int ToFV[12];
@@ -103,7 +121,7 @@
     wheel = qei;   
     ToF = ToFT;                                                                         // passes pointer with addresses of ToF sensors
     
-    for(int i = 0; i < 12; i++)                                               // initializes the ToF Sensors
+    for(int i = 0; i < 12; i++)                                                         // initializes the ToF Sensors
     {
         (*(ToF+i))->initReading(0x31+((0x02)*i), 50000);
     }
@@ -116,8 +134,9 @@
         ledgeArrayRF[i] = (*(ToF+1))->readFromOneSensor();
     }
     int* aaa = ledgeArrayLF; 
-    statistics LFTStats(aaa, 100, 0); 
-    out->printf("stadistics = %f, %f",  LFTStats.mean(), LFTStats.stdev());
+    
+    statistics LFTStats(aaa, 99, 1); 
+    out->printf("Statistics: Mean = %f, Standard Dev = %f\n",  LFTStats.mean(), LFTStats.stdev());
     myPID.SetMode(AUTOMATIC);                                                           // PID mode: Automatic
 }
 
@@ -180,7 +199,7 @@
  */
 void Wheelchair::pid_right(int deg)
 {
-    bool overturn = false;                                                              //Boolean if angle over 360˚
+    bool overturn = false;                                                              // Boolean if angle over 360˚
     
     out->printf("pid right\r\r\n");                                
     x->write(def);                                                                      // Update x sent to chair to be stationary
--- a/wheelchair.h	Fri Jun 28 21:16:26 2019 +0000
+++ b/wheelchair.h	Fri Jun 28 23:52:23 2019 +0000
@@ -75,7 +75,7 @@
     void velocity_thread();
     void rosCom_thread();
     void assistSafe_thread();
-
+    void emergencyButton_thread();
     
     /* Move x millimiters foward using PID*/
     void pid_forward(double mm);
@@ -128,6 +128,12 @@
     PwmOut* x;
     PwmOut* y;
     
+    //Pointers for PCB
+    PwmOut* on;
+    PwmOut* off;
+    
+    DigitalIn* e_button;                //Pointer to e_button
+    
     chair_BNO055* imu;                  // Pointer to IMU
     Serial* out;                        // Pointer to Serial Monitor
     Timer* ti;                          // Pointer to the timer