Changed URLs. Added open/close to QBuffer buffers.
[speedfreak] / Client / accelerometer.cpp
index 04a05ce..5c16c98 100644 (file)
@@ -1,3 +1,12 @@
+/*
+ * Accelerometer class to access the device accelerometer
+ *
+ * @author      Rikhard Kuutti <rikhard.kuutti@fudeco.com>
+ * @author      Kai Rasilainen 
+ * @copyright   (c) 2010 Speed Freak team
+ * @license     http://opensource.org/licenses/gpl-license.php GNU Public License
+ */
+
 #include "accelerometer.h"
 #include "math.h"
 
@@ -67,27 +76,31 @@ void Accelerometer::start()
  */
 void Accelerometer::initValues()
 {
-    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;
-    reverseAcceleration = false;
+    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;
 }
 
 /**
@@ -97,15 +110,21 @@ void Accelerometer::initValues()
   */
 void Accelerometer::calibrate(void)
 {
-    QFile file(kFileName);
-    if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) return;
-
     unsigned int iteration = 0;
 
-    QByteArray line;
-    QRegExp rx("([0-9-]+) ([0-9-]+) ([0-9-]+)");
-
     do {
+        // Opening the file must be done inside the loop
+        // or else the values obtained from file.readLine();
+        // will be empty for all but the first iteration
+        QFile file(kFileName);
+        if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
+        {
+            return;
+        }
+
+        QByteArray line;
+        QRegExp rx("([0-9-]+) ([0-9-]+) ([0-9-]+)");
+
         // Read data, parse with regular expressions and process it
         line = file.readLine();
         rx.indexIn(line);
@@ -114,18 +133,18 @@ void Accelerometer::calibrate(void)
         int sampleY = rx.cap(2).toInt();
         int sampleZ = rx.cap(3).toInt();
 
-        calibrationX += sampleX; // Accumulate Samples
-        calibrationY += sampleY; // for all axes.
-        calibrationZ += sampleZ;
+        calibrationX = calibrationX + sampleX; // Accumulate Samples
+        calibrationY = calibrationY + sampleY; // for all axes.
+        calibrationZ = calibrationZ + sampleZ;
 
         iteration++;
-    } while(iteration!=1024);       // 1024 times
 
-    calibrationX = calibrationX>>10;     // division by 1024
+        file.close();
+    } while(iteration != 1024);       // 1024 times
+
+    calibrationX = calibrationX>>10;  // division by 1024
     calibrationY = calibrationY>>10;
     calibrationZ = calibrationZ>>10;
-
-    file.close();
 }
 
 /**
@@ -222,6 +241,21 @@ qreal Accelerometer::getTotalTime()
     return totalTime;
 }
 
+int Accelerometer::getCalibrationX()
+{
+    return calibrationX;
+}
+
+int Accelerometer::getCalibrationY()
+{
+    return calibrationY;
+}
+
+int Accelerometer::getCalibrationZ()
+{
+    return calibrationZ;
+}
+
 /**
  * Processes Accelerometer data
  *
@@ -232,7 +266,10 @@ qreal Accelerometer::getTotalTime()
 void Accelerometer::processData()
 {
     QFile file(kFileName);
-    if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) return;
+    if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
+    {
+        return;
+    }
 
     // Read data, parse with regular expressions and process it
     QByteArray line = file.readLine();
@@ -242,19 +279,34 @@ void Accelerometer::processData()
     smoothData(rx.cap(1).toInt(), rx.cap(2).toInt(), rx.cap(3).toInt());
 
     // Apply calibration
-    trueAccelerationX = accelerationX - calibrationX;
-    trueAccelerationY = accelerationY - calibrationY;
-    trueAccelerationZ = accelerationZ - calibrationZ;
+    accelerationX = accelerationX - calibrationX;
+    accelerationX = accelerationY - calibrationY;
+    accelerationX = 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;
+    }
 
     // Discrimination window for acceleration values
-    if (trueAccelerationX <= 30 && trueAccelerationX >= -30) { trueAccelerationX = 0; }
-    if (trueAccelerationY <= 30 && trueAccelerationY >= -30) { trueAccelerationY = 0; }
-    if (trueAccelerationZ <= 30 && trueAccelerationZ >= -30) { trueAccelerationZ = 0; }
+    /*if ( fabs(accelerationX) < 20 ) { accelerationX = 0; }
+      if ( fabs(accelerationY) < 20 ) { accelerationY = 0; }
+      if ( fabs(accelerationZ) < 20 ) { accelerationZ = 0; }*/
 
+    // Calculate the current acceleration for each axis
     trueAccelerationX = (accelerationX - previousAccelerationX) /*/ 1000 * kGravity*/;
     trueAccelerationY = (accelerationY - previousAccelerationY) /*/ 1000 * kGravity*/;
     trueAccelerationZ = (accelerationZ - previousAccelerationZ) /*/ 1000 * kGravity*/;
 
+    // Discrimination window for acceleration values
+    if ( fabs(trueAccelerationX) < 20 ) { trueAccelerationX = 0; }
+    if ( fabs(trueAccelerationY) < 20 ) { trueAccelerationY = 0; }
+    if ( fabs(trueAccelerationZ) < 20 ) { trueAccelerationZ = 0; }
+
     previousAccelerationX = accelerationX;
     previousAccelerationY = accelerationY;
     previousAccelerationZ = accelerationZ;
@@ -263,19 +315,14 @@ void Accelerometer::processData()
                                trueAccelerationY * trueAccelerationY +
                                trueAccelerationZ * trueAccelerationZ );
 
-    totalAcceleration = currentAcceleration - previousAcceleration;
-    previousAcceleration = currentAcceleration;
+    // Discrimination window for currentAcceleration
+    if ( fabs(currentAcceleration) < 20 ) { currentAcceleration = 0; }
 
     // Measure time interval
-    intervalTime = now.restart(); // millisecs to secs
-    intervalTime = intervalTime/1000;
+    intervalTime = now.restart();
+    intervalTime = intervalTime/1000; // millisecs to secs
     totalTime = totalTime + intervalTime;
 
-    // Filter out acceleration caused by noise.
-    if (fabs(currentAcceleration) < 0.09) {
-        return;
-    }
-
     // Using calculate class to calculate velocity and distance etc.
     calculate->calculateParameters(currentAcceleration,intervalTime );
 
@@ -297,10 +344,12 @@ void Accelerometer::smoothData(qreal x, qreal y, qreal z)
     accelerationX = x;
     accelerationY = y;
     accelerationZ = z;
-    if(sampleIndex>0) {
+    if (sampleIndex > 0)
+    {
         accelerationX = previousAccelerationX + (accelerationX-previousAccelerationX) * kFilteringFactor;
         accelerationY = previousAccelerationY + (accelerationY-previousAccelerationY) * kFilteringFactor;
         accelerationZ = previousAccelerationZ + (accelerationZ-previousAccelerationZ) * kFilteringFactor;
     }
     sampleIndex++;
 }
+