X-Git-Url: http://git.maemo.org/git/?a=blobdiff_plain;f=Client%2Faccelerometer.cpp;h=5c16c9890b45d51012ee71f638c1dff14ed372c4;hb=f73389530ce2e0cd73cf6dda7b09a5ba86e60f13;hp=6f642f9801956ac2e63edbeb575a8e1ea98afcd8;hpb=0c54e49a827d3398115cd986293f17bd99db829a;p=speedfreak diff --git a/Client/accelerometer.cpp b/Client/accelerometer.cpp index 6f642f9..5c16c98 100644 --- a/Client/accelerometer.cpp +++ b/Client/accelerometer.cpp @@ -1,3 +1,12 @@ +/* + * Accelerometer class to access the device accelerometer + * + * @author Rikhard Kuutti + * @author Kai Rasilainen + * @copyright (c) 2010 Speed Freak team + * @license http://opensource.org/licenses/gpl-license.php GNU Public License + */ + #include "accelerometer.h" #include "math.h" @@ -26,6 +35,11 @@ Accelerometer::Accelerometer() initValues(); } +/** + * Constructor for Accelerometer class that takes accelerometer sample rate as parameter + * + * @param p_SampleRate the desired sample rate of accelerometer in milliseconds + */ Accelerometer::Accelerometer(int p_SampleRate) { calculate = new Calculate(); @@ -40,14 +54,16 @@ Accelerometer::Accelerometer(int p_SampleRate) * Default destructor for Accelerometer class * */ -Accelerometer::~Accelerometer() { +Accelerometer::~Accelerometer() +{ } /** * Start measuring * */ -void Accelerometer::start() { +void Accelerometer::start() +{ initValues(); calibrate(); timer->start(sampleRate); @@ -58,28 +74,33 @@ void Accelerometer::start() { * Init class members * */ -void Accelerometer::initValues() { - accelerationX=0; - accelerationY=0; - accelerationZ=0; - trueAccelerationX=0; - trueAccelerationY=0; - trueAccelerationZ=0; - previousAccelerationX=0; - previousAccelerationY=0; - previousAccelerationZ=0; - previousSpeed=0; - currentSpeed=0; - currentAcceleration=0; - previousAcceleration=0; - totalAcceleration=0; - intervalTime=0; - totalTime=0; - distanceTraveled=0; - lastDistanceTraveled=0; - averageSpeed=0; - sampleRate=0; - reverseAcceleration = false; +void Accelerometer::initValues() +{ + calculate->reset(); + accelerationX = 0; + accelerationY = 0; + accelerationZ = 0; + trueAccelerationX = 0; + trueAccelerationY = 0; + trueAccelerationZ = 0; + previousAccelerationX = 0; + previousAccelerationY = 0; + previousAccelerationZ = 0; + previousSpeed = 0; + currentSpeed = 0; + currentAcceleration = 0; + previousAcceleration = 0; + totalAcceleration = 0; + intervalTime = 0; + totalTime = 0; + distanceTraveled = 0; + lastDistanceTraveled = 0; + averageSpeed = 0; + sampleRate = 0; + firstRun = true; + calibrationX = 0; + calibrationY = 0; + calibrationZ = 0; } /** @@ -87,137 +108,204 @@ void Accelerometer::initValues() { * accelerometer when stationary. * */ -void Accelerometer::calibrate(void) { - QFile file(kFileName); - if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) return; +void Accelerometer::calibrate(void) +{ + unsigned int iteration = 0; - unsigned int iteration; - iteration = 0; - QByteArray line; - QRegExp rx("([0-9-]+) ([0-9-]+) ([0-9-]+)"); + do { + // Opening the file must be done inside the loop + // or else the values obtained from file.readLine(); + // will be empty for all but the first iteration + QFile file(kFileName); + if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) + { + return; + } + + QByteArray line; + QRegExp rx("([0-9-]+) ([0-9-]+) ([0-9-]+)"); - do{ // Read data, parse with regular expressions and process it line = file.readLine(); rx.indexIn(line); - int Sample_X = rx.cap(1).toInt(); - int Sample_Y = rx.cap(2).toInt(); - int Sample_Z = rx.cap(3).toInt(); + int sampleX = rx.cap(1).toInt(); + int sampleY = rx.cap(2).toInt(); + int sampleZ = rx.cap(3).toInt(); - sstatex += Sample_X; // Accumulate Samples - sstatey += Sample_Y; // for all axes. - sstatez += Sample_Z; + calibrationX = calibrationX + sampleX; // Accumulate Samples + calibrationY = calibrationY + sampleY; // for all axes. + calibrationZ = calibrationZ + sampleZ; iteration++; - }while(iteration!=1024); // 1024 times - sstatex = sstatex>>10; // division by 1024 - sstatey = sstatey>>10; - sstatez = sstatez>>10; + file.close(); + } while(iteration != 1024); // 1024 times - file.close(); + calibrationX = calibrationX>>10; // division by 1024 + calibrationY = calibrationY>>10; + calibrationZ = calibrationZ>>10; } /** * Stop measuring * */ -void Accelerometer::stop() { +void Accelerometer::stop() +{ timer->stop(); } /** * Set the sample rate of accelerometer * - * @param int pSampleRate desired sample rate + * @param pSampleRate desired sample rate */ -void Accelerometer::setSampleRate(int pSampleRate) { +void Accelerometer::setSampleRate(int pSampleRate) +{ sampleRate = pSampleRate; } -int Accelerometer::getSampleRate() { +/** + * Get the sample rate of accelerometer + * + * @return sampleRate the sample rate of the accelerometer in milliseconds + */ +int Accelerometer::getSampleRate() +{ return sampleRate; } -qreal Accelerometer::getCurrentAcceleration() { +qreal Accelerometer::getCurrentAcceleration() +{ return currentAcceleration; } -qreal Accelerometer::getPreviousTotalAcceleration() { +qreal Accelerometer::getPreviousTotalAcceleration() +{ return previousAcceleration; } -qreal Accelerometer::getTotalAcceleration() { +qreal Accelerometer::getTotalAcceleration() +{ return totalAcceleration; } -qreal Accelerometer::getDistanceTraveled() { +qreal Accelerometer::getDistanceTraveled() +{ return distanceTraveled; } -qreal Accelerometer::getLastDistanceTraveled() { +qreal Accelerometer::getLastDistanceTraveled() +{ return lastDistanceTraveled; } -qreal Accelerometer::getAverageSpeed() { +qreal Accelerometer::getAverageSpeed() +{ return averageSpeed; } -qreal Accelerometer::getTrueAccelerationX() { +qreal Accelerometer::getTrueAccelerationX() +{ return trueAccelerationX; } -qreal Accelerometer::getTrueAccelerationY() { +qreal Accelerometer::getTrueAccelerationY() +{ return trueAccelerationY; } -qreal Accelerometer::getTrueAccelerationZ() { +qreal Accelerometer::getTrueAccelerationZ() +{ return trueAccelerationZ; } -qreal Accelerometer::getPreviousSpeed() { +qreal Accelerometer::getPreviousSpeed() +{ return previousSpeed; } -qreal Accelerometer::getCurrentSpeed() { +qreal Accelerometer::getCurrentSpeed() +{ return currentSpeed; } -qreal Accelerometer::getIntervalTime() { +qreal Accelerometer::getIntervalTime() +{ return intervalTime; } +qreal Accelerometer::getTotalTime() +{ + return totalTime; +} + +int Accelerometer::getCalibrationX() +{ + return calibrationX; +} + +int Accelerometer::getCalibrationY() +{ + return calibrationY; +} + +int Accelerometer::getCalibrationZ() +{ + return calibrationZ; +} + /** * Processes Accelerometer data * + * Opens the accelerometer value file for reading and reads and parses accelerometer values. + * Forwards data to Calculate class for processing + * */ void Accelerometer::processData() { QFile file(kFileName); - if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) return; + if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) + { + return; + } // Read data, parse with regular expressions and process it QByteArray line = file.readLine(); - //QByteArray line; QRegExp rx("([0-9-]+) ([0-9-]+) ([0-9-]+)"); rx.indexIn(line); smoothData(rx.cap(1).toInt(), rx.cap(2).toInt(), rx.cap(3).toInt()); - // Add calibration - trueAccelerationX = accelerationX - sstatex; - trueAccelerationY = accelerationY - sstatey; - trueAccelerationZ = accelerationZ - sstatez; + // Apply calibration + accelerationX = accelerationX - calibrationX; + accelerationX = accelerationY - calibrationY; + accelerationX = accelerationZ - calibrationZ; + + // If the function is run the first time, make sure that there + // are no differences with previous and current acceleration + if (firstRun) { + previousAccelerationX = accelerationX; + previousAccelerationY = accelerationY; + previousAccelerationZ = accelerationZ; + firstRun = false; + } // Discrimination window for acceleration values - if (trueAccelerationX <= 30 && trueAccelerationX >= -30) { trueAccelerationX = 0; } - if (trueAccelerationY <= 30 && trueAccelerationY >= -30) { trueAccelerationY = 0; } - if (trueAccelerationZ <= 30 && trueAccelerationZ >= -30) { trueAccelerationZ = 0; } + /*if ( fabs(accelerationX) < 20 ) { accelerationX = 0; } + if ( fabs(accelerationY) < 20 ) { accelerationY = 0; } + if ( fabs(accelerationZ) < 20 ) { accelerationZ = 0; }*/ + + // Calculate the current acceleration for each axis + trueAccelerationX = (accelerationX - previousAccelerationX) /*/ 1000 * kGravity*/; + trueAccelerationY = (accelerationY - previousAccelerationY) /*/ 1000 * kGravity*/; + trueAccelerationZ = (accelerationZ - previousAccelerationZ) /*/ 1000 * kGravity*/; - trueAccelerationX = (accelerationX - previousAccelerationX) / 1000 * kGravity; - trueAccelerationY = (accelerationY - previousAccelerationY) / 1000 * kGravity; - trueAccelerationZ = (accelerationZ - previousAccelerationZ) / 1000 * kGravity; + // Discrimination window for acceleration values + if ( fabs(trueAccelerationX) < 20 ) { trueAccelerationX = 0; } + if ( fabs(trueAccelerationY) < 20 ) { trueAccelerationY = 0; } + if ( fabs(trueAccelerationZ) < 20 ) { trueAccelerationZ = 0; } previousAccelerationX = accelerationX; previousAccelerationY = accelerationY; @@ -227,42 +315,41 @@ void Accelerometer::processData() trueAccelerationY * trueAccelerationY + trueAccelerationZ * trueAccelerationZ ); - totalAcceleration = currentAcceleration - previousAcceleration; - previousAcceleration = currentAcceleration; + // Discrimination window for currentAcceleration + if ( fabs(currentAcceleration) < 20 ) { currentAcceleration = 0; } // Measure time interval - intervalTime = now.restart(); // millisecs to secs - intervalTime = intervalTime/1000; + intervalTime = now.restart(); + intervalTime = intervalTime/1000; // millisecs to secs totalTime = totalTime + intervalTime; - // Filter out acceleration caused by noise. - if (fabs(currentAcceleration) < 0.09) { - return; - } - // Using calculate class to calculate velocity and distance etc. - calculate->CalculateParameters(currentAcceleration,intervalTime ); + calculate->calculateParameters(currentAcceleration,intervalTime ); + + currentSpeed = calculate->getCurrentSpeed(); + distanceTraveled = calculate->getDistanceTraveled(); - currentSpeed = calculate->CurrentSpeed(); - distanceTraveled = calculate->DistanceTraveled(); file.close(); } /** * Smooths Accelerometer data * - * @param qreal x Accelerometers x-axis raw input - * @param qreal y Accelerometers y-axis raw input - * @param qreal z Accelerometers z-axis raw input + * @param x accelerometer's x-axis input + * @param y accelerometer's y-axis input + * @param z accelerometer's z-axis input */ -void Accelerometer::smoothData(qreal x, qreal y, qreal z) { +void Accelerometer::smoothData(qreal x, qreal y, qreal z) +{ accelerationX = x; accelerationY = y; accelerationZ = z; - if(sampleIndex>0) { + if (sampleIndex > 0) + { accelerationX = previousAccelerationX + (accelerationX-previousAccelerationX) * kFilteringFactor; accelerationY = previousAccelerationY + (accelerationY-previousAccelerationY) * kFilteringFactor; accelerationZ = previousAccelerationZ + (accelerationZ-previousAccelerationZ) * kFilteringFactor; } sampleIndex++; } +