X-Git-Url: http://git.maemo.org/git/?p=speedfreak;a=blobdiff_plain;f=Client%2Fcalculate.cpp;h=6201f8f74a13cad43230987ba6c3a77834964b04;hp=bbbdf9d9e21f987e1920393c4178850b4aba292f;hb=d9a92767c3442a574f15f3d9050e109fea4b75ba;hpb=9f59e05380da5887e6141cb89bdfab1ce9122f15 diff --git a/Client/calculate.cpp b/Client/calculate.cpp index bbbdf9d..6201f8f 100644 --- a/Client/calculate.cpp +++ b/Client/calculate.cpp @@ -1,14 +1,15 @@ /* * Calculate class to process accelerometer data * - * @author Kai Rasilainen + * @author Kai Rasilainen + * @author Jukka Kurttila + * @author Toni Jussila * @copyright (c) 2010 Speed Freak team * @license http://opensource.org/licenses/gpl-license.php GNU Public License */ #include "calculate.h" #include - #include #include #include @@ -18,25 +19,38 @@ 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; +/** + * Default constructor for Calculate class. + */ Calculate::Calculate() { this->reset(); } +/** + * Default destructor for Calculate class. + * Deletes all dynamic objects and sets them to NULL. + */ Calculate::~Calculate() { } +/** + * This function reset all variables. + */ void Calculate::reset() { averageSpeed = 0; + averagePower = 0; + peakPower = 0; + currentPower = 0; currentSpeed = 0; + maxSpeed = 0; distanceTraveled = 0; lastAcceleration = 0; lastDistance = 0; @@ -45,25 +59,38 @@ void Calculate::reset() totalTime = 0; count = 0; - speedCheckPoints.append(10); - speedCheckPoints.append(20); - speedCheckPoints.append(30); - speedCheckPoints.append(40); - + if(speedCheckPoints.count() == 0) + { + speedCheckPoints.append(10); + speedCheckPoints.append(20); + speedCheckPoints.append(30); + speedCheckPoints.append(40); + speedCheckPoints.append(50); + speedCheckPoints.append(60); + speedCheckPoints.append(70); + speedCheckPoints.append(80); + speedCheckPoints.append(90); + speedCheckPoints.append(100); + } + checkPointCounter = 0; + checkPoint = speedCheckPoints[checkPointCounter]; + valuesMap.clear(); } - /** * 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. + * + * @param double current acceleration + * @param double seconds */ void Calculate::calculateParameters(double currentAcceleration, double seconds) { double force, power1, power2; + + currentAcceleration *= G_ACCELERATION; numOfIterations++; totalTime = (totalTime + seconds); @@ -73,6 +100,10 @@ void Calculate::calculateParameters(double currentAcceleration, double seconds) // First integration of acceleration provides speed currentSpeed = (lastSpeed + (((currentAcceleration + lastAcceleration) * seconds) / 2)); + // Update maximum speed + if (currentSpeed > maxSpeed) + maxSpeed = currentSpeed; + // Second integration: distance. distanceTraveled = (lastDistance + (((currentSpeed + lastSpeed) * seconds) / 2)); @@ -104,27 +135,27 @@ void Calculate::calculateParameters(double currentAcceleration, double seconds) numOfIterations--; } - // Checkpoints - if ((lastSpeed == 0)) + if( (checkPoint > 0) && (currentSpeed*3.6 > checkPoint) ) { - lastCheckpoint = 0; - } - - // List of checkpoints - if (!(speedCheckPoints.isEmpty())) - { - foreach (double speed, speedCheckPoints) + //Update checkPoint + if( checkPointCounter <= speedCheckPoints.count() ) { - if ((lastCheckpoint != floor(speed)) && (floor(currentSpeed) == floor(speed))) + //Save time + valuesMap.insert( checkPoint, totalTime ); + if( checkPointCounter < speedCheckPoints.count() ) + { + checkPoint = speedCheckPoints[checkPointCounter]; + } + else { - emit checkPointReached(totalTime, currentSpeed); - lastCheckpoint = floor(currentSpeed); + checkPoint = 0; } + checkPointCounter++; } } // Check for movement - accelStoppedCheck(currentAcceleration); + //accelStoppedCheck(currentAcceleration); lastSpeed = currentSpeed; lastAcceleration = currentAcceleration; @@ -135,10 +166,11 @@ void Calculate::calculateParameters(double currentAcceleration, double seconds) * This function checks if acceleration has stopped for * a short period of time. Velocity is set to zero to avoid * distance errors. + * + * @param double current acceleration */ void Calculate::accelStoppedCheck(double currentAcceleration) { - // counting number of acceleration samples that equals zero if (currentAcceleration==0) { count++; @@ -153,84 +185,259 @@ void Calculate::accelStoppedCheck(double currentAcceleration) } } -// Getters and setters - +/** + * Get average speed. + * + * @return double average speed + */ double Calculate::getAverageSpeed() { return averageSpeed; } +/** + * Set average speed. + * + * @param double average speed + */ void Calculate::setAverageSpeed(double value) { averageSpeed = value; } +/** + * Get current speed. + * + * @return double current speed + */ double Calculate::getCurrentSpeed() { return currentSpeed; } +/** + * Set current speed. + * + * @param double current speed + */ void Calculate::setCurrentSpeed(double value) { currentSpeed = value; } +/** + * Get travelled distance. + * + * @return double travelled distance + */ double Calculate::getDistanceTraveled() { return distanceTraveled; } +/** + * Set travelled distance. + * + * @param double travelled distance + */ void Calculate::setDistanceTraveled(double value) { distanceTraveled = value; } +/** + * Get last acceleration. + * + * @return double last acceleration + */ double Calculate::getLastAcceleration() { return lastAcceleration; } +/** + * Set last acceleration. + * + * @param double last acceleration + */ void Calculate::setLastAcceleration(double value) { lastAcceleration = value; } +/** + * Get last distance. + * + * @return double last distance + */ double Calculate::getLastDistance() { return lastDistance; } +/** + * Set last distance. + * + * @param double last distance + */ void Calculate::setLastDistance(double value) { lastDistance = value; } +/** + * Get last speed. + * + * @return double last speed + */ double Calculate::getLastSpeed() { return lastSpeed; } +/** + * Set last speed. + * + * @param double last speed + */ void Calculate::setLastSpeed(double value) { lastSpeed = value; } +/** + * Get number of iterations. + * + * @return long number of iterations + */ long Calculate::getNumOfIterations() { return numOfIterations; } +/** + * Set number of iterations. + * + * @param long number of iterations + */ void Calculate::setNumOfIterations(long value) { numOfIterations = value; } +/** + * Get total time. + * + * @return double total time + */ double Calculate::getTotalTime() { return totalTime; } +/** + * Set total time. + * + * @param double total time + */ void Calculate::setTotalTime(double value) { totalTime = value; } + +/** + * Get current power. + * + * @return double current power + */ +double Calculate::getCurrentPower() +{ + return currentPower; +} + +/** + * Set current power. + * + * @param double current power + */ +void Calculate::setCurrentPower(double value) +{ + currentPower = value; +} + +/** + * Get peek power. + * + * @return double peek power + */ +double Calculate::getPeakPower() +{ + return peakPower; +} + +/** + * Set peek power. + * + * @param double peek power + */ +void Calculate::setPeakPower(double value) +{ + peakPower = value; +} + +/** + * Get average power. + * + * @return double average power + */ +double Calculate::getAveragePower() +{ + if (numOfIterations > 0) + { + return (averagePower/numOfIterations); + } + else + { + return 0; + } +} + +/** + * Set average power. + * + * @param double average power + */ +void Calculate::setAveragePower(double value) +{ + averagePower = value; +} + +/** + * Get max speed. + * + * @return double max speed + */ +double Calculate::getMaxSpeed() +{ + return maxSpeed; +} + +/** + * Set max speed. + * + * @param double max speed + */ +void Calculate::setMaxSpeed(double value) +{ + maxSpeed = value; +} + +/** + * Get values map. + * + * @return QMap values map. + */ +QMap Calculate::getValuesMap() +{ + return valuesMap; +}