1 /*
2  * Calculate class to process accelerometer data
3  *
4  * @author      Kai Rasilainen
5  * @copyright   (c) 2010 Speed Freak team
7  */
9 #include "calculate.h"
10 #include <math.h>
12 #include <QFile>
13 #include <QString>
14 #include <QTimer>
15 #include <QRegExp>
17 const double G_ACCELERATION = 9.80665;
18 const double SECONDS_IN_HOUR = 3600;
19 const double AIR_DENSITY = 1.225;
20 const double WATTS_PER_HORSEPOWER = 745.69987158227025;
22 const double carFrontalArea = 1.5;
23 const double dragCoefficient = 0.31;
24 const int carWeight = 850;
26 Calculate::Calculate()
27 {
28     this->reset();
29 }
31 Calculate::~Calculate()
32 {
34 }
36 void Calculate::reset()
37 {
38     averageSpeed = 0;
39     currentSpeed = 0;
40     distanceTraveled = 0;
41     lastAcceleration = 0;
42     lastDistance = 0;
43     lastSpeed = 0;
44     numOfIterations = 0;
45     totalTime = 0;
46     count = 0;
48     speedCheckPoints.append(10);
49     speedCheckPoints.append(20);
50     speedCheckPoints.append(30);
51     speedCheckPoints.append(40);
53 }
56 /**
57   * This is a main function for calculating various parameters. Accelerometer
58   * provides currentAcceleration and calling function measures time (seconds).
59   * This function should be called 20-30 times/second to minimize
60   * calculation error.
62   * To be added: params like horsepower.
63   */
64 void Calculate::calculateParameters(double currentAcceleration, double seconds)
65 {
66     double force, power1, power2;
67     numOfIterations++;
68     totalTime = (totalTime + seconds);
70     // v=v0 + a*t
71     // v(n) = v(n-1)+(a(n) + a(n-1))*(seconds)/2
73     // First integration of acceleration provides speed
74     currentSpeed = (lastSpeed + (((currentAcceleration + lastAcceleration) * seconds) / 2));
76     // Second integration: distance.
77     distanceTraveled = (lastDistance + (((currentSpeed + lastSpeed) * seconds) / 2));
79     // Average speed
80     averageSpeed = (distanceTraveled / totalTime);
82     // F=ma
83     force = (carWeight * currentAcceleration);
85     power1 = (force * currentSpeed);
87     power2 = ((AIR_DENSITY * (pow(currentSpeed, 3)
88              * (carFrontalArea * dragCoefficient))) / 2);
90     currentPower = ((power1 + power2) / WATTS_PER_HORSEPOWER);
92     // Save peak power
93     if ((currentPower > peakPower))
94     {
95         peakPower = currentPower;
96     }
98     if ((currentPower > 0))
99     {
100         averagePower = (averagePower + currentPower);
101     }
102     else
103     {
104         numOfIterations--;
105     }
107     // Checkpoints
108     if ((lastSpeed == 0))
109     {
110         lastCheckpoint = 0;
111     }
113     // List of checkpoints
114     if (!(speedCheckPoints.isEmpty()))
115     {
116         foreach (double speed, speedCheckPoints)
117         {
118             if ((lastCheckpoint != floor(speed)) && (floor(currentSpeed) == floor(speed)))
119             {
120                 emit checkPointReached(totalTime, currentSpeed);
121                 lastCheckpoint = floor(currentSpeed);
122             }
123         }
124     }
126     // Check for movement
127     accelStoppedCheck(currentAcceleration);
129     lastSpeed = currentSpeed;
130     lastAcceleration = currentAcceleration;
131     lastDistance = distanceTraveled;
132 }
134 /**
135   * This function checks if acceleration has stopped for
136   * a short period of time. Velocity is set to zero to avoid
137   * distance errors.
138   */
139 void Calculate::accelStoppedCheck(double currentAcceleration)
140 {
142     // counting number of acceleration samples that equals zero
143     if (currentAcceleration==0) {
144         count++;
145     } else {
146         count = 0;
147     }
149     // if count exceeds 25, we assume that velocity is zero
150     if (count >= 25)
151     {
152         currentSpeed=0;
153     }
154 }
156 // Getters and setters
158 double Calculate::getAverageSpeed()
159 {
160     return averageSpeed;
161 }
163 void Calculate::setAverageSpeed(double value)
164 {
165     averageSpeed = value;
166 }
168 double Calculate::getCurrentSpeed()
169 {
170     return currentSpeed;
171 }
173 void Calculate::setCurrentSpeed(double value)
174 {
175     currentSpeed = value;
176 }
178 double Calculate::getDistanceTraveled()
179 {
180     return distanceTraveled;
181 }
183 void Calculate::setDistanceTraveled(double value)
184 {
185     distanceTraveled = value;
186 }
188 double Calculate::getLastAcceleration()
189 {
190     return lastAcceleration;
191 }
193 void Calculate::setLastAcceleration(double value)
194 {
195     lastAcceleration = value;
196 }
198 double Calculate::getLastDistance()
199 {
200     return lastDistance;
201 }
203 void Calculate::setLastDistance(double value)
204 {
205     lastDistance = value;
206 }
208 double Calculate::getLastSpeed()
209 {
210     return lastSpeed;
211 }
213 void Calculate::setLastSpeed(double value)
214 {
215     lastSpeed = value;
216 }
218 long Calculate::getNumOfIterations()
219 {
220     return numOfIterations;
221 }
223 void Calculate::setNumOfIterations(long value)
224 {
225     numOfIterations = value;
226 }
228 double Calculate::getTotalTime()
229 {
230     return totalTime;
231 }
233 void Calculate::setTotalTime(double value)
234 {
235     totalTime = value;
236 }