Correction to set / change user
[speedfreak] / Client / calculate.cpp
1 /*
2  * Calculate class to process accelerometer data
3  *
4  * @author      Kai Rasilainen
5  * @copyright   (c) 2010 Speed Freak team
6  * @license     http://opensource.org/licenses/gpl-license.php GNU Public License
7  */
8
9 #include "calculate.h"
10 #include <math.h>
11
12 #include <QFile>
13 #include <QString>
14 #include <QTimer>
15 #include <QRegExp>
16
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;
21
22 const double carFrontalArea = 1.5;
23 const double dragCoefficient = 0.31;
24 const int carWeight = 850;
25
26 Calculate::Calculate()
27 {
28     this->reset();
29 }
30
31 Calculate::~Calculate()
32 {
33
34 }
35
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;
47
48     speedCheckPoints.append(10);
49     speedCheckPoints.append(20);
50     speedCheckPoints.append(30);
51     speedCheckPoints.append(40);
52
53 }
54
55
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.
61
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);
69
70     // v=v0 + a*t
71     // v(n) = v(n-1)+(a(n) + a(n-1))*(seconds)/2
72
73     // First integration of acceleration provides speed
74     currentSpeed = (lastSpeed + (((currentAcceleration + lastAcceleration) * seconds) / 2));
75
76     // Second integration: distance.
77     distanceTraveled = (lastDistance + (((currentSpeed + lastSpeed) * seconds) / 2));
78
79     // Average speed
80     averageSpeed = (distanceTraveled / totalTime);
81
82     // F=ma
83     force = (carWeight * currentAcceleration);
84
85     power1 = (force * currentSpeed);
86
87     power2 = ((AIR_DENSITY * (pow(currentSpeed, 3)
88              * (carFrontalArea * dragCoefficient))) / 2);
89
90     currentPower = ((power1 + power2) / WATTS_PER_HORSEPOWER);
91
92     // Save peak power
93     if ((currentPower > peakPower))
94     {
95         peakPower = currentPower;
96     }
97
98     if ((currentPower > 0))
99     {
100         averagePower = (averagePower + currentPower);
101     }
102     else
103     {
104         numOfIterations--;
105     }
106
107     // Checkpoints
108     if ((lastSpeed == 0))
109     {
110         lastCheckpoint = 0;
111     }
112
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     }
125
126     // Check for movement
127     accelStoppedCheck(currentAcceleration);
128
129     lastSpeed = currentSpeed;
130     lastAcceleration = currentAcceleration;
131     lastDistance = distanceTraveled;
132 }
133
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 {
141
142     // counting number of acceleration samples that equals zero
143     if (currentAcceleration==0) {
144         count++;
145     } else {
146         count = 0;
147     }
148
149     // if count exceeds 25, we assume that velocity is zero
150     if (count >= 25)
151     {
152         currentSpeed=0;
153     }
154 }
155
156 // Getters and setters
157
158 double Calculate::getAverageSpeed()
159 {
160     return averageSpeed;
161 }
162
163 void Calculate::setAverageSpeed(double value)
164 {
165     averageSpeed = value;
166 }
167
168 double Calculate::getCurrentSpeed()
169 {
170     return currentSpeed;
171 }
172
173 void Calculate::setCurrentSpeed(double value)
174 {
175     currentSpeed = value;
176 }
177
178 double Calculate::getDistanceTraveled()
179 {
180     return distanceTraveled;
181 }
182
183 void Calculate::setDistanceTraveled(double value)
184 {
185     distanceTraveled = value;
186 }
187
188 double Calculate::getLastAcceleration()
189 {
190     return lastAcceleration;
191 }
192
193 void Calculate::setLastAcceleration(double value)
194 {
195     lastAcceleration = value;
196 }
197
198 double Calculate::getLastDistance()
199 {
200     return lastDistance;
201 }
202
203 void Calculate::setLastDistance(double value)
204 {
205     lastDistance = value;
206 }
207
208 double Calculate::getLastSpeed()
209 {
210     return lastSpeed;
211 }
212
213 void Calculate::setLastSpeed(double value)
214 {
215     lastSpeed = value;
216 }
217
218 long Calculate::getNumOfIterations()
219 {
220     return numOfIterations;
221 }
222
223 void Calculate::setNumOfIterations(long value)
224 {
225     numOfIterations = value;
226 }
227
228 double Calculate::getTotalTime()
229 {
230     return totalTime;
231 }
232
233 void Calculate::setTotalTime(double value)
234 {
235     totalTime = value;
236 }