author Kai Rasilainen Mon, 8 Mar 2010 11:46:57 +0000 (13:46 +0200) committer Kai Rasilainen Mon, 8 Mar 2010 11:46:57 +0000 (13:46 +0200)
 Client/calculate.cpp patch | blob | history Client/calculate.h patch | blob | history

index 85afca3..bbbdf9d 100644 (file)
@@ -1,12 +1,11 @@
/*
* Calculate class to process accelerometer data
*
- * @author      Kai Rasilainen
+ * @author      Kai Rasilainen
* @copyright   (c) 2010 Speed Freak team
*/

-
#include "calculate.h"
#include <math.h>

const double G_ACCELERATION = 9.80665;
const double SECONDS_IN_HOUR = 3600;
+const double AIR_DENSITY = 1.225;
+const double WATTS_PER_HORSEPOWER = 745.69987158227025;
+
+const double carFrontalArea = 1.5;
+const double dragCoefficient = 0.31;
+const int carWeight = 850;

Calculate::Calculate()
{
@@ -39,6 +44,113 @@ void Calculate::reset()
numOfIterations = 0;
totalTime = 0;
count = 0;
+
+    speedCheckPoints.append(10);
+    speedCheckPoints.append(20);
+    speedCheckPoints.append(30);
+    speedCheckPoints.append(40);
+
+}
+
+
+/**
+  * This is a main function for calculating various parameters. Accelerometer
+  * provides currentAcceleration and calling function measures time (seconds).
+  * This function should be called 20-30 times/second to minimize
+  * calculation error.
+
+  * To be added: params like horsepower.
+  */
+void Calculate::calculateParameters(double currentAcceleration, double seconds)
+{
+    double force, power1, power2;
+    numOfIterations++;
+    totalTime = (totalTime + seconds);
+
+    // v=v0 + a*t
+    // v(n) = v(n-1)+(a(n) + a(n-1))*(seconds)/2
+
+    // First integration of acceleration provides speed
+    currentSpeed = (lastSpeed + (((currentAcceleration + lastAcceleration) * seconds) / 2));
+
+    // Second integration: distance.
+    distanceTraveled = (lastDistance + (((currentSpeed + lastSpeed) * seconds) / 2));
+
+    // Average speed
+    averageSpeed = (distanceTraveled / totalTime);
+
+    // F=ma
+    force = (carWeight * currentAcceleration);
+
+    power1 = (force * currentSpeed);
+
+    power2 = ((AIR_DENSITY * (pow(currentSpeed, 3)
+             * (carFrontalArea * dragCoefficient))) / 2);
+
+    currentPower = ((power1 + power2) / WATTS_PER_HORSEPOWER);
+
+    // Save peak power
+    if ((currentPower > peakPower))
+    {
+        peakPower = currentPower;
+    }
+
+    if ((currentPower > 0))
+    {
+        averagePower = (averagePower + currentPower);
+    }
+    else
+    {
+        numOfIterations--;
+    }
+
+    // Checkpoints
+    if ((lastSpeed == 0))
+    {
+        lastCheckpoint = 0;
+    }
+
+    // List of checkpoints
+    if (!(speedCheckPoints.isEmpty()))
+    {
+        foreach (double speed, speedCheckPoints)
+        {
+            if ((lastCheckpoint != floor(speed)) && (floor(currentSpeed) == floor(speed)))
+            {
+                emit checkPointReached(totalTime, currentSpeed);
+                lastCheckpoint = floor(currentSpeed);
+            }
+        }
+    }
+
+    // Check for movement
+    accelStoppedCheck(currentAcceleration);
+
+    lastSpeed = currentSpeed;
+    lastAcceleration = currentAcceleration;
+    lastDistance = distanceTraveled;
+}
+
+/**
+  * This function checks if acceleration has stopped for
+  * a short period of time. Velocity is set to zero to avoid
+  * distance errors.
+  */
+void Calculate::accelStoppedCheck(double currentAcceleration)
+{
+
+    // counting number of acceleration samples that equals zero
+    if (currentAcceleration==0) {
+        count++;
+    } else {
+        count = 0;
+    }
+
+    // if count exceeds 25, we assume that velocity is zero
+    if (count >= 25)
+    {
+        currentSpeed=0;
+    }
}

// Getters and setters
@@ -122,60 +234,3 @@ void Calculate::setTotalTime(double value)
{
totalTime = value;
}
-
-/**
-  * This is a main function for calculating various parameters. Accelerometer
-  * provides currentAcceleration and calling function measures time (seconds).
-  * This function should be called 20-30 times/second to minimize
-  * calculation error.
-
-  * To be added: params like horsepower.
-  */
-void Calculate::calculateParameters(double currentAcceleration, double seconds)
-{
-    numOfIterations++;
-    totalTime = (totalTime + seconds);
-
-    // v=v0 + a*t
-    // v(n) = v(n-1)+(a(n) + a(n-1))*(seconds)/2
-
-    // First integration of acceleration provides speed
-    currentSpeed = (lastSpeed + (((currentAcceleration + lastAcceleration) * seconds) / 2));
-
-    // Second integration: distance.
-    distanceTraveled = (lastDistance + (((currentSpeed + lastSpeed) * seconds) / 2));
-
-    // Average speed
-    averageSpeed = (distanceTraveled / totalTime);
-
-    // Check for movement
-    accelStoppedCheck(currentAcceleration);
-
-    lastSpeed = currentSpeed;
-    lastAcceleration = currentAcceleration;
-    lastDistance = distanceTraveled;
-}
-
-/**
-  * This function checks if acceleration has stopped for
-  * a short period of time. Velocity is set to zero to avoid
-  * distance errors.
-  */
-void Calculate::accelStoppedCheck(double currentAcceleration)
-{
-
-    // counting number of acceleration samples that equals zero
-    if (currentAcceleration==0) {
-        count++;
-    } else {
-        count = 0;
-    }
-
-    // if count exceeds 25, we assume that velocity is zero
-    if (count >= 25)
-    {
-        currentSpeed=0;
-    }
-}
-
-
index ef000ba..ef37c23 100644 (file)
@@ -1,7 +1,7 @@
/*
* Calculate class to process accelerometer data
*
- * @author      Kai Rasilainen
+ * @author      Kai Rasilainen
* @copyright   (c) 2010 Speed Freak team
*/
@@ -11,6 +11,7 @@

#include <QObject>
#include <QTime>
+#include <QList>

class Calculate : public QObject
{
@@ -62,6 +63,13 @@ private:
long numOfIterations;
double totalTime;
int count;
+    double peakPower;
+    double currentPower;
+    double averagePower;
+    QList<int> speedCheckPoints;
+
+signals:
+    void checkPointReached(double totalTime, double currentSpeed);

};