Major changes to Accelerometer handling
authorRikhard Kuutti <rikhard.kuutti@fudeco.com>
Thu, 18 Mar 2010 09:03:08 +0000 (11:03 +0200)
committerRikhard Kuutti <rikhard.kuutti@fudeco.com>
Thu, 18 Mar 2010 09:03:08 +0000 (11:03 +0200)
Accelerometer now accessed from carmainwindow class
Changes to accelerometer data processing

Client/carmainwindow.cpp
Client/carmainwindow.h

index 3cdaac1..51db131 100644 (file)
@@ -5,6 +5,8 @@
  * @author     Janne Änäkkälä <janne.anakkala@fudeco.com>
  * @author     Tiina Kivilinna-Korhola <tiina.kivilinna-korhola@fudeco.com>
  * @author     Olavi Pulkkinen <olavi.pulkkinen@fudeco.com>
+ * @author     Rikhard Kuutti <rikhard.kuutti@fudeco.com>
+ * @author     Kai Rasilainen <kai.rasilainen@fudeco.com>
  * @copyright  (c) 2010 Speed Freak team
  * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  */
 #include "carmainwindow.h"
 #include "math.h"
 
+#define kAccelerometerSampleRate    50
+#define kFilteringFactor            0.2
+#define kSecondsInHour              3600
+
 /**
   *Constructor of this class.
   *@param QWidget pointer to parent object. By default the value is NULL.
@@ -40,9 +46,27 @@ CarMainWindow::CarMainWindow(QWidget *parent):QMainWindow(parent), ui(new Ui::Ca
     speed = 0;
     timer = new QTimer();
 
+    // Accelerometer
     accelerometer = new Accelerometer();
     accelerometer->setSampleRate(100);
 
+    reverseAccelerationFlag = false;
+    vehicleStartedMoving = false;
+    isNewRun = true;
+    isSetup = false;
+    stopTime = 0;
+    accelerationStartThreshold = 0.02;
+
+    QTimer *accelerometerTimer = new QTimer(this);
+    connect(accelerometerTimer, SIGNAL(timeout()), this, SLOT(readAccelerometerData()));
+    accelerometerTimer->start(kAccelerometerSampleRate);
+
+    // Calculate
+    calculate = new Calculate();
+    //connect(calculate, SIGNAL(checkPointReached()), this, SLOT(handleCheckPoint()));
+
+    resetAccelerometerMeasurements();
+
     measures = new Measures();
     this->initializeMeasures();
 
@@ -550,3 +574,153 @@ void CarMainWindow::userLogin()
 {
     myHttpClient->checkLogin();
 }
+
+/**
+  * Resets Accelerometer measurement variables
+  */
+void CarMainWindow::resetAccelerometerMeasurements()
+{
+    currentAcceleration = 0;
+    currentAccelerationString = "";
+    currentSpeed = "";
+    currentTime = 0;
+    distanceTraveled = "";
+    firstAcceleration = 0;
+    //horsepower = null;
+    isNewRun = true;
+    //lastScreenUpdateInSeconds = 0;
+    previousTime = 0;
+    reverseAccelerationFlag = false;
+    stopWatch.setHMS(0, 0, 0, 0);
+    //accelerometer->stop();
+    totalTime = "";
+    vehicleStartedMoving = false;
+    calculate->reset();
+}
+
+/**
+  * This function is called to handle checkpoints
+  *@param totalTime total time elapsed since starting measurements
+  *@param currentSpeed current speed of the device
+  */
+void CarMainWindow::handleCheckPoint(double totalTime, double currentSpeed)
+{
+    // TODO
+    //totalTime;
+    //currentSpeed;
+    return;
+}
+
+/**
+  *This function is called to read (and process) data from the accelerometer
+  */
+void CarMainWindow::readAccelerometerData()
+{
+    QString s;
+    double changeInAcceleration = 0;
+    qreal x, y, z;
+
+    accelerometer->getAcceleration(x, y, z);
+    accelerometer->smoothData(x, y, z);
+
+    // Apply calibration
+    x -= accelerometer->getCalibrationX();
+    y -= accelerometer->getCalibrationY();
+    z -= accelerometer->getCalibrationZ();
+
+    QString str = QString("acc x: " + QString::number(x) + "\n" +
+                          "acc y: " + QString::number(y) + "\n" +
+                          "acc z: " + QString::number(z) + "\n");
+
+    if (!vehicleStartedMoving)
+    {
+        if (isNewRun)
+        {
+            firstAcceleration = sqrt(x*x + y*y + z*z);
+            //firstAcceleration = y; // first read
+            isNewRun = false;
+        }
+    }
+
+    currentAcceleration = sqrt(x*x + y*y + z*z);
+    changeInAcceleration = (currentAcceleration - firstAcceleration); // firstAcceleration only gets set once
+
+    if (((abs(changeInAcceleration) <= accelerationStartThreshold)
+                && !vehicleStartedMoving))
+    {
+        return;
+    }
+
+    if (reverseAccelerationFlag)
+    {
+        // will be false until after 1st calculation
+        if ((changeInAcceleration <= 0))
+        {
+            // actually increasing here...
+            changeInAcceleration = abs(changeInAcceleration);
+        }
+        else
+        {
+            // actually decreasing here...
+            changeInAcceleration = (changeInAcceleration * -1);
+        }
+    }
+    if (!vehicleStartedMoving)
+    {
+        if ((changeInAcceleration < 0))
+        {
+            // we started to move backwards first time through
+            reverseAccelerationFlag = true;
+            changeInAcceleration = abs(changeInAcceleration);
+        }
+        vehicleStartedMoving = true;
+
+        stopWatch.setHMS(0, 0, 0, 0);
+        stopWatch.start();
+    }
+    //  keep the following line as close to the SetKinematicsProperties method as possible
+    currentTime = stopWatch.elapsed();
+    calculate->calculateParameters(changeInAcceleration, (currentTime - previousTime)/1000);
+    previousTime = currentTime;
+
+    s.sprintf("%.2f", changeInAcceleration);
+    currentAccelerationString = s;
+
+    double speed = 0.0;
+    speed = calculate->getCurrentSpeed();
+    speed = ((speed*1000)/kSecondsInHour);
+    s.sprintf("%.2f", speed);
+    currentSpeed = s;
+
+    s.sprintf("%.2f", calculate->getDistanceTraveled());
+    distanceTraveled = s;
+
+    // TODO
+    //distanceTraveled;
+    //horsepower;
+    //totalTime;
+
+    s.sprintf("%.2f", calculate->getTotalTime());
+    totalTime = s;
+
+    str.append("ca: " + currentAccelerationString + " G\n" );
+    str.append("cspeed: " + currentSpeed + " km/h \n" );
+    str.append("dist: " + distanceTraveled + " m \n" );
+    str.append("time: " + totalTime + " s \n" );
+
+    if ((stopTime > 0) && (previousTime >= stopTime))
+    {
+        // we want to end at a stopping point that the user chose
+        // output results
+        resetAccelerometerMeasurements();
+    }
+}
+
+/**
+  *This function is used to calibrate accelerometer
+  */
+void CarMainWindow::calibrateAccelerometer()
+{
+    resetAccelerometerMeasurements();
+    accelerometer->calibrate();
+}
index 90cdc61..c218a06 100644 (file)
@@ -5,6 +5,8 @@
  * @author     Janne Änäkkälä <janne.anakkala@fudeco.com>
  * @author     Tiina Kivilinna-Korhola <tiina.kivilinna-korhola@fudeco.com>
  * @author     Olavi Pulkkinen <olavi.pulkkinen@fudeco.com>
+ * @author     Rikhard Kuutti <rikhard.kuutti@fudeco.com>
+ * @author     Kai Rasilainen <kai.rasilainen@fudeco.com>
  * @copyright  (c) 2010 Speed Freak team
  * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  */
@@ -39,6 +41,7 @@
 #include "categorylist.h"
 #include "httpclient.h"
 #include "routedialog.h"
+#include "calculate.h"
 
 namespace Ui {
     class CarMainWindow;
@@ -73,6 +76,8 @@ private:
     void initComboBoxStartTabUnits();                   //Start-tab view
     void initListViewStartTabAccelerationCategories();  //Start-tab view
     void initializeMeasures();
+    void resetAccelerometerMeasurements();
+    void calibrateAccelerometer();
 
 private:
     QStringList accelerationCategoriesStartTab;         //Start-tab view
@@ -88,6 +93,29 @@ private:
     QModelIndex choice;
     int choiceInt;
 
+    Calculate *calculate;
+
+    bool reverseAccelerationFlag;
+    bool vehicleStartedMoving;
+    bool isNewRun;
+    bool isSetup;
+
+    double stopTime;
+    double accelerationStartThreshold;
+    double currentAcceleration;
+    double currentTime;
+    double previousTime;
+    double firstAcceleration;
+
+    QTimer *accelerometerTimer;
+    QTime stopWatch;
+
+    QString currentAccelerationString;
+    QString currentSpeed;
+    QString distanceTraveled;
+    QString horsepower;
+    QString totalTime;
+
 signals:
     void speedAchieved();
     void userNameChanged();
@@ -112,6 +140,8 @@ private slots:
     void updateUserName();
     void regUserToServer();
     void userLogin();
+    void readAccelerometerData();
+    void handleCheckPoint(double totalTime, double currentSpeed);
 };
 
 #endif // CARMAINWINDOW_H