2 * Calculate class to process accelerometer data
4 * @author Kai Rasilainen
5 * @copyright (c) 2010 Speed Freak team
6 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
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()
31 Calculate::~Calculate()
36 void Calculate::reset()
52 speedCheckPoints.append(10);
53 speedCheckPoints.append(20);
54 speedCheckPoints.append(30);
55 speedCheckPoints.append(40);
61 * This is a main function for calculating various parameters. Accelerometer
62 * provides currentAcceleration and calling function measures time (seconds).
63 * This function should be called 20-30 times/second to minimize
68 void Calculate::calculateParameters(double currentAcceleration, double seconds)
70 double force, power1, power2;
72 currentAcceleration *= G_ACCELERATION;
74 totalTime = (totalTime + seconds);
77 // v(n) = v(n-1)+(a(n) + a(n-1))*(seconds)/2
79 // First integration of acceleration provides speed
80 currentSpeed = (lastSpeed + (((currentAcceleration + lastAcceleration) * seconds) / 2));
82 // Update maximum speed
83 if (currentSpeed > maxSpeed)
84 maxSpeed = currentSpeed;
86 // Second integration: distance.
87 distanceTraveled = (lastDistance + (((currentSpeed + lastSpeed) * seconds) / 2));
90 averageSpeed = (distanceTraveled / totalTime);
93 force = (carWeight * currentAcceleration);
95 power1 = (force * currentSpeed);
97 power2 = ((AIR_DENSITY * (pow(currentSpeed, 3)
98 * (carFrontalArea * dragCoefficient))) / 2);
100 currentPower = ((power1 + power2) / WATTS_PER_HORSEPOWER);
103 if ((currentPower > peakPower))
105 peakPower = currentPower;
108 if ((currentPower > 0))
110 averagePower = (averagePower + currentPower);
118 if ((lastSpeed == 0))
123 // List of checkpoints
124 if (!(speedCheckPoints.isEmpty()))
126 foreach (double speed, speedCheckPoints)
128 if ((lastCheckpoint != floor(speed)) && (floor(getCurrentSpeed()) == floor(speed)))
130 emit checkPointReached(totalTime, getCurrentSpeed());
131 lastCheckpoint = floor(getCurrentSpeed());
136 // Check for movement
137 accelStoppedCheck(currentAcceleration);
139 lastSpeed = currentSpeed;
140 lastAcceleration = currentAcceleration;
141 lastDistance = distanceTraveled;
145 * This function checks if acceleration has stopped for
146 * a short period of time. Velocity is set to zero to avoid
149 void Calculate::accelStoppedCheck(double currentAcceleration)
151 // counting number of acceleration samples that equals zero
152 if (currentAcceleration==0) {
158 // if count exceeds 25, we assume that velocity is zero
165 // Getters and setters
167 double Calculate::getAverageSpeed()
172 void Calculate::setAverageSpeed(double value)
174 averageSpeed = value;
177 double Calculate::getCurrentSpeed()
182 void Calculate::setCurrentSpeed(double value)
184 currentSpeed = value;
187 double Calculate::getDistanceTraveled()
189 return distanceTraveled;
192 void Calculate::setDistanceTraveled(double value)
194 distanceTraveled = value;
197 double Calculate::getLastAcceleration()
199 return lastAcceleration;
202 void Calculate::setLastAcceleration(double value)
204 lastAcceleration = value;
207 double Calculate::getLastDistance()
212 void Calculate::setLastDistance(double value)
214 lastDistance = value;
217 double Calculate::getLastSpeed()
222 void Calculate::setLastSpeed(double value)
227 long Calculate::getNumOfIterations()
229 return numOfIterations;
232 void Calculate::setNumOfIterations(long value)
234 numOfIterations = value;
237 double Calculate::getTotalTime()
242 void Calculate::setTotalTime(double value)
247 double Calculate::getCurrentPower()
252 void Calculate::setCurrentPower(double value)
254 currentPower = value;
257 double Calculate::getPeakPower()
262 void Calculate::setPeakPower(double value)
267 double Calculate::getAveragePower()
269 if (numOfIterations > 0)
271 return (averagePower/numOfIterations);
279 void Calculate::setAveragePower(double value)
281 averagePower = value;
284 double Calculate::getMaxSpeed()
289 void Calculate::setMaxSpeed(double value)