Youtube video and text (draft).
[speedfreak] / Client / accelerometer.cpp
index ed904f6..c2f0bab 100644 (file)
@@ -2,27 +2,20 @@
  * Accelerometer class to access the device accelerometer
  *
  * @author      Rikhard Kuutti <rikhard.kuutti@fudeco.com>
- * @author      Kai Rasilainen 
+ * @author      Kai Rasilainen <kai.rasilainen@fudeco.com>
+ * @author      Jukka Kurttila <jukka.kurttila@fudeco.com>
  * @copyright   (c) 2010 Speed Freak team
  * @license     http://opensource.org/licenses/gpl-license.php GNU Public License
  */
 
 #include "accelerometer.h"
-#include "math.h"
 
-#include <QFile>
-#include <QString>
-#include <QRegExp>
-#include <QTimer>
 #include <QDBusConnection>
 #include <QDBusInterface>
 #include <QDBusPendingReply>
 
-#define kFilteringFactor    0.1
-#define kGravity            9.81
-#define kFileName           "/sys/class/i2c-adapter/i2c-3/3-001d/coord"
-
-static int sampleIndex=0;
+#define kFilteringFactor 0.2
+#define kIterations      1024
 
 /**
  * Default constructor for Accelerometer class
@@ -30,27 +23,8 @@ static int sampleIndex=0;
  */
 Accelerometer::Accelerometer()
 {
-    calculate = new Calculate();
-
-    timer = new QTimer(this);
-    connect(timer, SIGNAL(timeout()), this, SLOT(processData()));
-    sampleRate = 40;
-    initValues();
-}
-
-/**
- * Constructor for Accelerometer class that takes accelerometer sample rate as parameter
- *
- * @param p_SampleRate the desired sample rate of accelerometer in milliseconds
- */
-Accelerometer::Accelerometer(int p_SampleRate)
-{
-    calculate = new Calculate();
-
-    timer = new QTimer(this);
-    connect(timer, SIGNAL(timeout()), this, SLOT(processData()));
-    sampleRate = p_SampleRate;
     initValues();
+    calibrateDialog = NULL;
 }
 
 /**
@@ -59,18 +33,8 @@ Accelerometer::Accelerometer(int p_SampleRate)
  */
 Accelerometer::~Accelerometer()
 {
-}
-
-/**
- * Start measuring
- *
- */
-void Accelerometer::start()
-{
-    initValues();
-    calibrate();
-    timer->start(sampleRate);
-    now.restart();
+    if(calibrateDialog)
+        delete calibrateDialog;
 }
 
 /**
@@ -79,28 +43,9 @@ void Accelerometer::start()
  */
 void Accelerometer::initValues()
 {
-    calculate->reset();
-    accelerationX = 0;
-    accelerationY = 0;
-    accelerationZ = 0;
-    trueAccelerationX = 0;
-    trueAccelerationY = 0;
-    trueAccelerationZ = 0;
     previousAccelerationX = 0;
     previousAccelerationY = 0;
     previousAccelerationZ = 0;
-    previousSpeed = 0;
-    currentSpeed = 0;
-    currentAcceleration = 0;
-    previousAcceleration = 0;
-    totalAcceleration = 0;
-    intervalTime = 0;
-    totalTime = 0;
-    distanceTraveled = 0;
-    lastDistanceTraveled = 0;
-    averageSpeed = 0;
-    sampleRate = 0;
-    firstRun = true;
     calibrationX = 0;
     calibrationY = 0;
     calibrationZ = 0;
@@ -116,7 +61,13 @@ void Accelerometer::calibrate(void)
     unsigned int iteration = 0;
     qreal sampleX, sampleY, sampleZ;
 
+    calibrateDialog = new CalibrateDialog();
+    calibrateDialog->show();
+    calibrateDialog->resetProgressValue();
+    calibrateDialog->setMaxValue( kIterations + 1 );
+
     do {
+        calibrateDialog->setProgressValue(iteration);
 
         getAcceleration(sampleX, sampleY, sampleZ);
 
@@ -126,118 +77,42 @@ void Accelerometer::calibrate(void)
 
         iteration++;
 
-    } while(iteration != 1024);        // 1024 times
+    } while(iteration != kIterations);        // kIterations times
 
-    calibrationX = calibrationX/1024;  // division by 1024
-    calibrationY = calibrationY/1024;
-    calibrationZ = calibrationZ/1024;
-}
+    calibrationX = calibrationX/kIterations;  // division by kIterations
+    calibrationY = calibrationY/kIterations;
+    calibrationZ = calibrationZ/kIterations;
 
-/**
- * Stop measuring
- *
- */
-void Accelerometer::stop()
-{
-    timer->stop();
+    calibrateDialog->close();
 }
 
 /**
- * Processes Accelerometer data
- *
- * Opens the accelerometer value file for reading and reads and parses accelerometer values.
- * Forwards data to Calculate class for processing
+ * Smooths Accelerometer data by applying a low pass filter to data
  *
+ * @param x accelerometer's x-axis input
+ * @param y accelerometer's y-axis input
+ * @param z accelerometer's z-axis input
  */
-void Accelerometer::processData()
+void Accelerometer::smoothData(qreal &x, qreal &y, qreal &z)
 {
-    getAcceleration(accelerationX, accelerationY, accelerationZ);
-    //smoothData(accelerationX, accelerationY, accelerationZ);
-
-    // Apply calibration
-    accelerationX = accelerationX - calibrationX;
-    accelerationY = accelerationY - calibrationY;
-    accelerationZ = accelerationZ - calibrationZ;
-
-    // If the function is run the first time, make sure that there
-    // are no differences with previous and current acceleration
-    if (firstRun) {
-        previousAccelerationX = accelerationX;
-        previousAccelerationY = accelerationY;
-        previousAccelerationZ = accelerationZ;
-        firstRun = false;
-    }
-
-    // Calculate the current acceleration for each axis
-    trueAccelerationX = (accelerationX - previousAccelerationX);
-    trueAccelerationY = (accelerationY - previousAccelerationY);
-    trueAccelerationZ = (accelerationZ - previousAccelerationZ);
+    x = (previousAccelerationX * (1 - kFilteringFactor)) + (x * kFilteringFactor);
+    y = (previousAccelerationY * (1 - kFilteringFactor)) + (y * kFilteringFactor);
+    z = (previousAccelerationZ * (1 - kFilteringFactor)) + (z * kFilteringFactor);
 
-    previousAccelerationX = accelerationX;
-    previousAccelerationY = accelerationY;
-    previousAccelerationZ = accelerationZ;
-
-    currentAcceleration = sqrt((trueAccelerationX * trueAccelerationX) +
-                               (trueAccelerationY * trueAccelerationY) +
-                               (trueAccelerationZ * trueAccelerationZ));
-
-    // Discrimination window for currentAcceleration
-    if ( fabs(currentAcceleration) < 0.10 ) { currentAcceleration = 0; }
-
-    // Measure time interval
-    intervalTime = now.restart();
-    intervalTime = intervalTime/1000; // millisecs to secs
-    totalTime = totalTime + intervalTime;
-
-    // Using calculate class to calculate velocity and distance etc.
-    calculate->calculateParameters(currentAcceleration,intervalTime );
-
-    currentSpeed = calculate->getCurrentSpeed();
-    distanceTraveled = calculate->getDistanceTraveled();
+    previousAccelerationX = x;
+    previousAccelerationY = y;
+    previousAccelerationZ = z;
 }
 
 /**
- * Smooths Accelerometer data
+ * Gets the raw acceleration data from accelerometer
  *
  * @param x accelerometer's x-axis input
  * @param y accelerometer's y-axis input
  * @param z accelerometer's z-axis input
  */
-void Accelerometer::smoothData(qreal x, qreal y, qreal z)
-{
-    accelerationX = x;
-    accelerationY = y;
-    accelerationZ = z;
-    if (sampleIndex > 0)
-    {
-        accelerationX = previousAccelerationX + (accelerationX-previousAccelerationX) * kFilteringFactor;
-        accelerationY = previousAccelerationY + (accelerationY-previousAccelerationY) * kFilteringFactor;
-        accelerationZ = previousAccelerationZ + (accelerationZ-previousAccelerationZ) * kFilteringFactor;
-    }
-    sampleIndex++;
-}
-
 void Accelerometer::getAcceleration(qreal &x, qreal &y, qreal &z)
 {
-    /*
-    QFile file(kFileName);
-    if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
-    {
-        return;
-    }
-
-    // Read data, parse with regular expressions and process it
-    QByteArray line = file.readLine();
-    QRegExp rx("([0-9-]+) ([0-9-]+) ([0-9-]+)");
-    rx.indexIn(line);
-
-    x = rx.cap(1).toInt()/1000;
-    y = rx.cap(2).toInt()/1000;
-    z = rx.cap(3).toInt()/1000;
-
-    file.close();
-    */
-
     QDBusConnection connection(QDBusConnection::systemBus());
     if (connection.isConnected()) {
         QDBusInterface interface("com.nokia.mce", "/com/nokia/icd", QString(), connection);
@@ -251,100 +126,30 @@ void Accelerometer::getAcceleration(qreal &x, qreal &y, qreal &z)
 }
 
 /**
- * Set the sample rate of accelerometer
+ * Get the x calibration component
  *
- * @param pSampleRate desired sample rate
+ * @return calibrationX x calibration component
  */
-void Accelerometer::setSampleRate(int pSampleRate)
+qreal Accelerometer::getCalibrationX()
 {
-    sampleRate = pSampleRate;
+    return calibrationX;
 }
 
 /**
- * Get the sample rate of accelerometer
+ * Get the y calibration component
  *
- * @return sampleRate the sample rate of the accelerometer in milliseconds
+ * @return calibrationY y calibration component
  */
-int Accelerometer::getSampleRate()
-{
-    return sampleRate;
-}
-
-qreal Accelerometer::getCurrentAcceleration()
-{
-    return currentAcceleration;
-}
-
-qreal Accelerometer::getPreviousTotalAcceleration()
-{
-    return previousAcceleration;
-}
-
-qreal Accelerometer::getTotalAcceleration()
-{
-    return totalAcceleration;
-}
-
-qreal Accelerometer::getDistanceTraveled()
-{
-    return distanceTraveled;
-}
-
-qreal Accelerometer::getLastDistanceTraveled()
-{
-    return lastDistanceTraveled;
-}
-
-qreal Accelerometer::getAverageSpeed()
-{
-    return averageSpeed;
-}
-
-qreal Accelerometer::getTrueAccelerationX()
-{
-    return trueAccelerationX;
-}
-
-qreal Accelerometer::getTrueAccelerationY()
-{
-    return trueAccelerationY;
-}
-
-qreal Accelerometer::getTrueAccelerationZ()
-{
-    return trueAccelerationZ;
-}
-
-qreal Accelerometer::getPreviousSpeed()
-{
-    return previousSpeed;
-}
-
-qreal Accelerometer::getCurrentSpeed()
-{
-    return currentSpeed;
-}
-
-qreal Accelerometer::getIntervalTime()
-{
-    return intervalTime;
-}
-
-qreal Accelerometer::getTotalTime()
-{
-    return totalTime;
-}
-
-qreal Accelerometer::getCalibrationX()
-{
-    return calibrationX;
-}
-
 qreal Accelerometer::getCalibrationY()
 {
     return calibrationY;
 }
 
+/**
+ * Get the z calibration component
+ *
+ * @return calibrationZ z calibration component
+ */
 qreal Accelerometer::getCalibrationZ()
 {
     return calibrationZ;