*/
#include "accelerometer.h"
+
#include "math.h"
#include <QFile>
#include <QDBusInterface>
#include <QDBusPendingReply>
-#define kFilteringFactor 0.1
-#define kGravity 9.81
-#define kFileName "/sys/class/i2c-adapter/i2c-3/3-001d/coord"
+#define kFilteringFactor 0.2
-static int sampleIndex=0;
+static int sampleIndex = 0;
/**
* Default constructor for Accelerometer class
{
calculate = new Calculate();
+
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(processData()));
sampleRate = p_SampleRate;
void Accelerometer::start()
{
initValues();
- calibrate();
+ //calibrate();
timer->start(sampleRate);
now.restart();
}
calibrationX = calibrationX/1024; // division by 1024
calibrationY = calibrationY/1024;
calibrationZ = calibrationZ/1024;
+
}
/**
* Forwards data to Calculate class for processing
*
*/
+/*
void Accelerometer::processData()
{
getAcceleration(accelerationX, accelerationY, accelerationZ);
currentSpeed = calculate->getCurrentSpeed();
distanceTraveled = calculate->getDistanceTraveled();
}
-
+*/
/**
* Smooths Accelerometer data
*
* @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)
{
- accelerationX = previousAccelerationX + (accelerationX-previousAccelerationX) * kFilteringFactor;
- accelerationY = previousAccelerationY + (accelerationY-previousAccelerationY) * kFilteringFactor;
- accelerationZ = previousAccelerationZ + (accelerationZ-previousAccelerationZ) * kFilteringFactor;
+ accelerationX = ((previousAccelerationX) * (1-kFilteringFactor)) + (accelerationX * kFilteringFactor);
+ accelerationY = ((previousAccelerationY) * (1-kFilteringFactor)) + (accelerationY * kFilteringFactor);
+ accelerationZ = ((previousAccelerationZ) * (1-kFilteringFactor)) + (accelerationZ * kFilteringFactor);
}
sampleIndex++;
}
void Accelerometer::getAcceleration(qreal &x, qreal &y, qreal &z)
{
- /*
- QFile file(kFileName);
- if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
- {
- return;
- }
-
- // Read data, parse with regular expressions and process it
- QByteArray line = file.readLine();
- QRegExp rx("([0-9-]+) ([0-9-]+) ([0-9-]+)");
- rx.indexIn(line);
-
- x = rx.cap(1).toInt()/1000;
- y = rx.cap(2).toInt()/1000;
- z = rx.cap(3).toInt()/1000;
-
- file.close();
- */
-
QDBusConnection connection(QDBusConnection::systemBus());
if (connection.isConnected()) {
QDBusInterface interface("com.nokia.mce", "/com/nokia/icd", QString(), connection);
Accelerometer(int p_SampleRate);
~Accelerometer();
+ void getAcceleration(qreal &x, qreal &y, qreal &z);
+ void smoothData(qreal &x, qreal &y, qreal &z);
void calibrate();
void start();
void setSampleRate(int pSampleRate);
int getSampleRate();
- void getAcceleration(qreal &x, qreal &y, qreal &z);
qreal getTrueAccelerationX();
qreal getTrueAccelerationY();
qreal getTrueAccelerationZ();
+
qreal getPreviousSpeed();
qreal getCurrentSpeed();
qreal getIntervalTime();
qreal getTotalTime();
+ Calculate *calculate;
+
private slots:
- void processData();
- void smoothData(qreal x, qreal y, qreal z);
+ //void processData();
-private:
- Calculate *calculate;
+private:
qreal accelerationX, accelerationY, accelerationZ;
qreal trueAccelerationX,trueAccelerationY,trueAccelerationZ;
qreal previousAccelerationX,previousAccelerationY,previousAccelerationZ;
double sampleRate;
bool firstRun;
+
};
#endif // ACCELEROMETER_H
void Calculate::reset()
{
averageSpeed = 0;
+ averagePower = 0;
+ peakPower = 0;
+ currentPower = 0;
currentSpeed = 0;
distanceTraveled = 0;
lastAcceleration = 0;
lastSpeed = 0;
numOfIterations = 0;
totalTime = 0;
- count = 0;
+ //count = 0;
speedCheckPoints.append(10);
speedCheckPoints.append(20);
{
foreach (double speed, speedCheckPoints)
{
- if ((lastCheckpoint != floor(speed)) && (floor(currentSpeed) == floor(speed)))
+ if ((lastCheckpoint != floor(speed)) && (floor(getCurrentSpeed()) == floor(speed)))
{
- emit checkPointReached(totalTime, currentSpeed);
- lastCheckpoint = floor(currentSpeed);
+ emit checkPointReached(totalTime, getCurrentSpeed());
+ lastCheckpoint = floor(getCurrentSpeed());
}
}
}
// Check for movement
- accelStoppedCheck(currentAcceleration);
+ //accelStoppedCheck(currentAcceleration);
lastSpeed = currentSpeed;
lastAcceleration = currentAcceleration;
* a short period of time. Velocity is set to zero to avoid
* distance errors.
*/
+/*
void Calculate::accelStoppedCheck(double currentAcceleration)
{
currentSpeed=0;
}
}
-
+*/
// Getters and setters
double Calculate::getAverageSpeed()