* @author Janne Änäkkälä <janne.anakkala@fudeco.com>
* @author Tiina Kivilinna-Korhola <tiina.kivilinna-korhola@fudeco.com>
* @author Olavi Pulkkinen <olavi.pulkkinen@fudeco.com>
+ * @author Rikhard Kuutti <rikhard.kuutti@fudeco.com>
+ * @author Kai Rasilainen <kai.rasilainen@fudeco.com>
* @copyright (c) 2010 Speed Freak team
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/
#include "carmainwindow.h"
#include "math.h"
+#define kAccelerometerSampleRate 50
+#define kFilteringFactor 0.2
+#define kSecondsInHour 3600
+
/**
*Constructor of this class.
*@param QWidget pointer to parent object. By default the value is NULL.
{
ui->setupUi(this);
ui->tabWidget->setCurrentWidget(this->ui->StartTab);
- //result = new ResultDialog();
+ result = new ResultDialog();
//measure = new MeasureDialog();
- xmlreader = new XmlReader();
+ welcomeDialog = new WelcomeDialog();
+ welcomeDialog->show();
initComboBoxStartTabUnits();
initListViewStartTabAccelerationCategories();
myLogin = new LoginWindow(this);
+ categorylist = new CategoryList();
+ myHttpClient = new HttpClient(this);
myRegistration = new Registration(this);
- xmlwriter = new XmlWriter();
- manager = new QNetworkAccessManager(this);
- connect(myRegistration,SIGNAL(sendregistration()),this,SLOT(registrate()));
+ connect(myRegistration,SIGNAL(sendregistration()),this,SLOT(regUserToServer()));
+ connect(myLogin,SIGNAL(userNameChanged()),this,SLOT(userLogin()));
+ myRoute = new RouteDialog( this);
+
+ //GPS
+ location = new Maemo5Location(this);
+ gpsData = new GPSData(location);
+ connect(location,SIGNAL(agnss()),this,SLOT(gpsStatus()));
time = 0;
speed = 0;
timer = new QTimer();
+ // Accelerometer
accelerometer = new Accelerometer();
- accelerometer->setSampleRate(100);
+
+ reverseAccelerationFlag = false;
+ vehicleStartedMoving = false;
+ isNewRun = true;
+ isSetup = false;
+ stopTime = 0;
+ accelerationStartThreshold = 0.02;
+
+ QTimer *accelerometerTimer = new QTimer(this);
+ connect(accelerometerTimer, SIGNAL(timeout()), this, SLOT(readAccelerometerData()));
+ accelerometerTimer->start(kAccelerometerSampleRate);
+
+ // Calculate
+ calculate = new Calculate();
+ //connect(calculate, SIGNAL(checkPointReached()), this, SLOT(handleCheckPoint()));
+
+ resetAccelerometerMeasurements();
measures = new Measures();
this->initializeMeasures();
}
/**
- *Destructor of this class. Should be used to release all allocated resources.
+ *Destructor of this class. Deletes all dynamic objects and sets them to NULL.
*/
CarMainWindow::~CarMainWindow()
{
delete ui;
+ ui = NULL;
//delete result;
//delete measure;
- delete xmlreader;
- delete xmlwriter;
- delete manager;
+ delete categorylist;
+ categorylist = NULL;
+ delete welcomeDialog;
+ welcomeDialog = NULL;
+ delete myRoute;
+ myRoute = NULL;
+ delete gpsData;
+ gpsData = NULL;
}
/**
*/
void CarMainWindow::on_autoStartButton_clicked()
{
+ choice = ui->listViewStartTabAccelerationCategories->currentIndex();
+ choiceInt = choice.row();
+ qDebug() << choiceInt;
+ if (choiceInt == 0)
+ {
+ ui->labelMeasureTabHeader->setText("Accelerate to 40 km/h");
+ result->setDiagramGapStem(75);
+ }
+
+ else if (choiceInt == 1)
+ {
+ ui->labelMeasureTabHeader->setText("Accelerate to 100 km/h");
+ result->setDiagramGapStem(30);
+ }
+ else
+ {
+ ui->labelMeasureTabHeader->setText("Accelerate to 80 km/h");
+ result->setDiagramGapStem(37.5);
+ }
+ ui->labelMeasureTabResult->setText("");
//delete measure;
//measure = NULL;
//measure = new MeasureDialog();
// connect(measure, SIGNAL(speedAchieved()), this, SLOT(openResultView()));
- accelerometer->start();
timer->start();
// Show measure dialog.
//measure->show();
+
+ // TODO: Move next if else to the function which is called when target speed
+ // has reached.
+ if (choiceInt == 0)
+ {
+ if (floor(this->measures->getTime40kmh()) <= 5)
+ {
+ result->setDiagramGapHorizontal(80);
+ }
+
+ else if (floor(this->measures->getTime40kmh()) <= 10)
+ {
+ result->setDiagramGapHorizontal(40);
+ }
+
+ else
+ {
+ result->setDiagramGapHorizontal(20);
+ }
+ }
+
+ else
+ {
+ if (floor(this->measures->getTime40kmh()) <= 5)
+ {
+ result->setDiagramGapHorizontal(80);
+ }
+
+ else if (floor(this->measures->getTime40kmh()) <= 10)
+ {
+ result->setDiagramGapHorizontal(40);
+ }
+
+ else
+ {
+ result->setDiagramGapHorizontal(20);
+ }
+ }
+
ui->tabWidget->setCurrentWidget(this->ui->tabMeasureResult);
}
*/
void CarMainWindow::setCategoryCompoBox()
{
- ui->comboBoxTopCategory->addItems(xmlreader->getCategoryList());
+ ui->comboBoxTopCategory->addItems(categorylist->getCategoryList());
}
/**
void CarMainWindow::setListViewTopList(QString category, int size)
{
QString topList;
- topList.append( xmlreader->getTopList(category, size));
+ topList.append( categorylist->getTopList(category, size));
ui->labelTopList->setText(topList);
}
//result->show();
ui->pushButtonSendResult->setEnabled(true);
QString timeInteger;
+ if (choiceInt == 0)
+ {
+ if (floor(this->measures->getTime40kmh()) <= 5)
+ {
+ result->setDiagramGapHorizontal(80);
+ }
+
+ else if (floor(this->measures->getTime40kmh()) <= 10)
+ {
+ result->setDiagramGapHorizontal(40);
+ }
+
+ else
+ {
+ result->setDiagramGapHorizontal(20);
+ }
+ }
+
+ else
+ {
+ if (floor(this->measures->getTime40kmh()) <= 5)
+ {
+ result->setDiagramGapHorizontal(80);
+ }
+
+ else if (floor(this->measures->getTime40kmh()) <= 10)
+ {
+ result->setDiagramGapHorizontal(40);
+ }
+
+ else
+ {
+ result->setDiagramGapHorizontal(20);
+ }
+ }
timeInteger.setNum(this->measures->getTime40kmh());
//time = "0 - 40 km/h: ";
//time.append(timeInteger);
*/
void CarMainWindow::on_buttonTopRefresh_clicked()
{
- requestCategories();
+ myHttpClient->requestCategories();
setCategoryCompoBox();
}
int limitNr = 5; //replace with real value?
QString limit = QString::number(limitNr);
category = "acceleration-0-100"; //replace with real value from category list/top window
- requestTopList(category, limit);
+ myHttpClient->requestTopList(category, limit);
setListViewTopList(category,10);
}
}
/**
- *@brief Sends registration information to the server in xml format.
- *Reads user name, password and emaol address from resuldialogs internal variables.
- *@todo Replace msg box with better reaction to server`s responce.
- */
-void CarMainWindow::registrate()
-{
- qDebug() << "_registrate" ;
- qDebug() << this->myRegistration->getUserName() << "+" << this->myRegistration->getPassword() << "+" << this->myRegistration->getEmail();
-
- QBuffer *regbuffer = new QBuffer();
- QUrl qurl("http://api.speedfreak-app.com/api/register");
- QNetworkRequest request(qurl);
- qDebug() << qurl.toString();
- QNetworkReply *currentDownload;
-
- regbuffer->open(QBuffer::ReadWrite);
- xmlwriter->writeRegistering(regbuffer,
- this->myRegistration->getUserName(),
- this->myRegistration->getPassword(),
- this->myRegistration->getEmail());
- //Tmp msgbox - later server responce
- QMessageBox::about(this,"Registrate",this->myRegistration->getUserName() + this->myRegistration->getPassword() + this->myRegistration->getEmail());
-
- currentDownload = manager->post(request, ("xml=" + regbuffer->data()));
- qDebug() << "carmainwindow: regbuffer->data(): " << regbuffer->data();
-
- connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfRegistration()));
- connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(errorFromServer(QNetworkReply::NetworkError)));
-
- regbuffer->close();
-}
-
-
-/**
- *@brief Sends result(s) to the server in xml format.
- *Send authentication information in the header.
- *@todo Read category elsewhere.
- */
-void CarMainWindow::sendResultXml()
-{
- qDebug() << "_sendResultXml";
-
- QBuffer *xmlbuffer = new QBuffer();
- QString category_name = "acceleration-0-100"; //replace with real value from category list
-
- QUrl qurl("http://api.speedfreak-app.com/api/update/" + category_name);
- qDebug() << qurl.toString();
- QNetworkRequest request(qurl);
- QNetworkReply *currentDownload;
-
- xmlbuffer->open(QBuffer::ReadWrite);
- xmlwriter->writeResult(xmlbuffer);
- qDebug() << "carmainwindow: xmlbuffer->data(): " << xmlbuffer->data();
-
- QString credentials = this->myRegistration->getUserName() + ":" + this->myRegistration->getPassword();
- credentials = "Basic " + credentials.toAscii().toBase64();
- request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
-
- currentDownload = manager->post(request, ("xml=" + xmlbuffer->data()));
- connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfResult()));
- connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(errorFromServer(QNetworkReply::NetworkError)));
-
- xmlbuffer->close();
-}
-
-/**
- *@brief Request the Top10List of certain category from the server.
- *Send authentication information in the header.
- *@param Category of results.
- *@param Limit, the number of results.
- */
-void CarMainWindow::requestTopList(QString category, QString limit)
-{
- qDebug() << "_requestTopList" ;
-
- QString urlBase = "http://api.speedfreak-app.com/api/results/";
- QUrl qurl(urlBase + category + "/" + limit);
- qDebug() << qurl.toString();
- QNetworkRequest request(qurl);
- QNetworkReply *currentDownload;
-
- QString credentials = this->myRegistration->getUserName() + ":" + this->myRegistration->getPassword();
- credentials = "Basic " + credentials.toAscii().toBase64();
- request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
-
- currentDownload = manager->post(request, ("data=" ));
- connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfToplist()));
- connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(errorFromServer(QNetworkReply::NetworkError)));
-}
-
-
-/**
- *@brief Request categories list from the server.
- *Send authentication information in the header.
- */
-void CarMainWindow::requestCategories()
-{
- qDebug() << "_requestCategories" ;
-
- QUrl qurl("http://api.speedfreak-app.com/api/categories/");
- qDebug() << qurl.toString();
- QNetworkRequest request(qurl);
- QNetworkReply *currentDownload;
-
- QString credentials = this->myRegistration->getUserName() + ":" + this->myRegistration->getPassword();
- credentials = "Basic " + credentials.toAscii().toBase64();
- request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
-
- currentDownload = manager->post(request, ("data=" ));
- connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfCategories()));
- connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(errorFromServer(QNetworkReply::NetworkError)));
-}
-
-
-/**
- *@brief React to servers responce after result has been sent.
- *@todo Implement consequencies of reply.
- */
-void CarMainWindow::ackOfResult()
-{
- qDebug() << "_ackOfResult";
-
- QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
- qDebug() << reply->readAll();
- QNetworkReply::NetworkError errorcode;
- errorcode = reply->error();
- if(errorcode != 0) {
- qDebug() << errorcode << reply->errorString();
- }
- else {
- qDebug() << "errorcode=0";
- }
-}
-
-
-/**
- *@brief React to servers responce after registration has been sent.
- *@todo Implement consequencies of reply.
- */
-void CarMainWindow::ackOfRegistration()
-{
- qDebug() << "_ackOfRegistration";
-
- QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
- qDebug() << reply->readAll();
- QNetworkReply::NetworkError errorcode;
- errorcode = reply->error();
- if(errorcode != 0) {
- qDebug() << "errorcode:" << errorcode << reply->errorString();
- }
- else {
- qDebug() << "errorcode=0";
- }
-}
-
-
-/**
- *@brief React to servers responce after request for categories has been sent.
- *@todo Implement reply`s feeding to categories list.
- */
-void CarMainWindow::ackOfCategories()
-{
- qDebug() << "_ackOfCategories";
-
- QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
- qDebug() << reply->readAll();
- QNetworkReply::NetworkError errorcode;
- errorcode = reply->error();
- if(errorcode != 0) {
- qDebug() << "errorcode:" << errorcode << reply->errorString();
- }
- else {
- qDebug() << "errorcode=0";
- }
-}
-
-/**
- *@brief Reports errors, when server has sent error signal.
- */
-void CarMainWindow::errorFromServer(QNetworkReply::NetworkError errorcode)
-{
- qDebug() << "_errorFromServer";
-
- if(errorcode != 0) {
- qDebug() << errorcode;
- }
- else {
- qDebug() << errorcode;
- }
-}
-
-
-/**
- *@brief React to servers responce after request of TopList in certain category has been sent.
- *@todo Implement routing reply`s contents to UI.
- */
-void CarMainWindow::ackOfToplist()
-{
- qDebug() << "_ackOfToplist";
-
- QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
- xmlreader->xmlRead(reply);
- qDebug() << reply->readAll();
- QNetworkReply::NetworkError errorcode;
- errorcode = reply->error();
- if(errorcode != 0) {
- qDebug() << errorcode << reply->errorString();
- }
- else {
- qDebug() << "errorcode=0";
- }
-}
-
-
-/**
*@brief Just for development, for the real button is not shown until
*measurin started and there are results.
*@todo Implement with real code and yet leave sendXml in the bottom in use.
void CarMainWindow::after_timeout()
{
QString timeString, speedString;
- //time++;
- time = accelerometer->getTotalTime();
- speed = accelerometer->getCurrentSpeed();
- //speed = speed +10;
+ time++;
+ speed = speed +10;
if (floor(speed) == 10)
{
if (speed >= 40.0)
{
timer->stop();
- accelerometer->stop();
time = 0;
speed = 0;
//emit this->speedAchieved();
measures->setTime90kmh(0);
measures->setTime100kmh(0);
timer->stop();
- accelerometer->stop();
time = 0;
speed = 0;
ui->tabWidget->setCurrentWidget(this->ui->StartTab);
void CarMainWindow::on_pushButtonSendResult_clicked()
{
- sendResultXml();
+ myHttpClient->sendResultXml();
ui->pushButtonSendResult->setEnabled(false);
}
this->setWindowTitle("Speed freak");
}
}
+
+void CarMainWindow::regUserToServer()
+{
+ myHttpClient->requestRegistration();
+}
+
+
+void CarMainWindow::on_drawRoutePushButton_clicked()
+{
+ myRoute->show();
+}
+
+/**
+ * Opens result dialog when show result button is clicked.
+ * Sends measures as parameter to the resultdialogs saveMeasuresToArray-function.
+ */
+void CarMainWindow::on_pushButtonShowResultDialog_clicked()
+{
+ Measures meas;
+ meas.setTime10kmh(1.3);
+ meas.setTime20kmh(2.5);
+ meas.setTime30kmh(3.6);
+ meas.setTime40kmh(6.7);
+ meas.setTime50kmh(7.3);
+ meas.setTime60kmh(7.5);
+ meas.setTime70kmh(8.6);
+ meas.setTime80kmh(8.7);
+ meas.setTime90kmh(9.6);
+ meas.setTime100kmh(9.9);
+ result->setDiagramGapHorizontal(40);
+ result->saveMeasuresToArray(&meas);
+ this->result->show();
+}
+
+void CarMainWindow::userLogin()
+{
+ myHttpClient->checkLogin();
+}
+
+/**
+ * Resets Accelerometer measurement variables
+ */
+void CarMainWindow::resetAccelerometerMeasurements()
+{
+ currentAcceleration = 0;
+ currentAccelerationString = "";
+ currentSpeed = "";
+ currentTime = 0;
+ distanceTraveled = "";
+ firstAcceleration = 0;
+ //horsepower = null;
+ isNewRun = true;
+ //lastScreenUpdateInSeconds = 0;
+ previousTime = 0;
+ reverseAccelerationFlag = false;
+ stopWatch.setHMS(0, 0, 0, 0);
+ //accelerometer->stop();
+ totalTime = "";
+ vehicleStartedMoving = false;
+ calculate->reset();
+}
+
+/**
+ * This function is called to handle checkpoints
+ *@param totalTime total time elapsed since starting measurements
+ *@param currentSpeed current speed of the device
+ */
+void CarMainWindow::handleCheckPoint(double totalTime, double currentSpeed)
+{
+ // TODO
+ //totalTime;
+ //currentSpeed;
+ return;
+}
+
+/**
+ *This function is called to read (and process) data from the accelerometer
+ */
+void CarMainWindow::readAccelerometerData()
+{
+ QString s;
+ double changeInAcceleration = 0;
+ qreal x, y, z;
+
+ accelerometer->getAcceleration(x, y, z);
+ accelerometer->smoothData(x, y, z);
+
+ // Apply calibration
+ x -= accelerometer->getCalibrationX();
+ y -= accelerometer->getCalibrationY();
+ z -= accelerometer->getCalibrationZ();
+
+ QString str = QString("acc x: " + QString::number(x) + "\n" +
+ "acc y: " + QString::number(y) + "\n" +
+ "acc z: " + QString::number(z) + "\n");
+
+ if (!vehicleStartedMoving)
+ {
+ if (isNewRun)
+ {
+ firstAcceleration = sqrt(x*x + y*y + z*z);
+ //firstAcceleration = y; // first read
+ isNewRun = false;
+ }
+ }
+
+ currentAcceleration = sqrt(x*x + y*y + z*z);
+ changeInAcceleration = (currentAcceleration - firstAcceleration); // firstAcceleration only gets set once
+
+ if (((abs(changeInAcceleration) <= accelerationStartThreshold)
+ && !vehicleStartedMoving))
+ {
+ return;
+ }
+
+ if (reverseAccelerationFlag)
+ {
+ // will be false until after 1st calculation
+ if ((changeInAcceleration <= 0))
+ {
+ // actually increasing here...
+ changeInAcceleration = abs(changeInAcceleration);
+ }
+ else
+ {
+ // actually decreasing here...
+ changeInAcceleration = (changeInAcceleration * -1);
+ }
+ }
+ if (!vehicleStartedMoving)
+ {
+ if ((changeInAcceleration < 0))
+ {
+ // we started to move backwards first time through
+ reverseAccelerationFlag = true;
+ changeInAcceleration = abs(changeInAcceleration);
+ }
+ vehicleStartedMoving = true;
+
+ stopWatch.setHMS(0, 0, 0, 0);
+ stopWatch.start();
+ }
+ // keep the following line as close to the SetKinematicsProperties method as possible
+ currentTime = stopWatch.elapsed();
+ calculate->calculateParameters(changeInAcceleration, (currentTime - previousTime)/1000);
+ previousTime = currentTime;
+
+ s.sprintf("%.2f", changeInAcceleration);
+ currentAccelerationString = s;
+
+ double speed = 0.0;
+ speed = calculate->getCurrentSpeed();
+ speed = ((speed*1000)/kSecondsInHour);
+ s.sprintf("%.2f", speed);
+ currentSpeed = s;
+
+ s.sprintf("%.2f", calculate->getDistanceTraveled());
+ distanceTraveled = s;
+
+ // TODO
+ //distanceTraveled;
+ //horsepower;
+ //totalTime;
+
+ s.sprintf("%.2f", calculate->getTotalTime());
+ totalTime = s;
+
+ str.append("ca: " + currentAccelerationString + " G\n" );
+ str.append("cspeed: " + currentSpeed + " km/h \n" );
+ str.append("dist: " + distanceTraveled + " m \n" );
+ str.append("time: " + totalTime + " s \n" );
+
+ if ((stopTime > 0) && (previousTime >= stopTime))
+ {
+ // we want to end at a stopping point that the user chose
+ // output results
+ resetAccelerometerMeasurements();
+ }
+}
+
+/**
+ *This function is used to calibrate accelerometer
+ */
+void CarMainWindow::calibrateAccelerometer()
+{
+ resetAccelerometerMeasurements();
+ accelerometer->calibrate();
+}
+
+/**
+ *This slot function is called when GPS on checkbox state changed. Route-tab view.
+ *@param int GPSState
+ */
+void CarMainWindow::on_gpsOnCheckBox_stateChanged(int GPSState)
+{
+ if (GPSState == 0)
+ {
+ ui->labelRouteTabGPSStatus->setText("GPS off");//testing
+ location->stopPollingGPS();
+ }
+ else
+ {
+ ui->labelRouteTabGPSStatus->setText("GPS on");//testing
+ location->startPollingGPS();
+ }
+}
+
+/**
+ *This slot function is called when GPS status changed. Route-tab view.
+ */
+void CarMainWindow::gpsStatus()
+{
+ if (ui->gpsOnCheckBox->isChecked())
+ {
+ if (location->getSatellitesInUse() >= 4)
+ {
+ ui->labelRouteTabGPSStatus->setText("GPS ready");
+ }
+
+ else
+ {
+ ui->labelRouteTabGPSStatus->setText("Waiting for GPS");
+ }
+ }
+}