Various changes made to comply with altered implementation of accelerometer class
[speedfreak] / Client / carmainwindow.cpp
1 /*
2  * CarMainWindow main class
3  *
4  * @author     Toni Jussila <toni.jussila@fudeco.com>
5  * @author     Janne Änäkkälä <janne.anakkala@fudeco.com>
6  * @author     Tiina Kivilinna-Korhola <tiina.kivilinna-korhola@fudeco.com>
7  * @author     Olavi Pulkkinen <olavi.pulkkinen@fudeco.com>
8  * @author     Rikhard Kuutti <rikhard.kuutti@fudeco.com>
9  * @author     Kai Rasilainen <kai.rasilainen@fudeco.com>
10  * @copyright  (c) 2010 Speed Freak team
11  * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
12  */
13
14 #include "carmainwindow.h"
15 #include "math.h"
16
17 #define kAccelerometerSampleRate    50
18 #define kFilteringFactor            0.2
19 #define kSecondsInHour              3600
20
21 /**
22   *Constructor of this class.
23   *@param QWidget pointer to parent object. By default the value is NULL.
24   */
25 CarMainWindow::CarMainWindow(QWidget *parent):QMainWindow(parent), ui(new Ui::CarMainWindow)
26 {
27     ui->setupUi(this);
28     ui->tabWidget->setCurrentWidget(this->ui->StartTab);
29     result = new ResultDialog();
30     //measure = new MeasureDialog();
31     welcomeDialog = new WelcomeDialog();
32     welcomeDialog->show();
33
34     initComboBoxStartTabUnits();
35     initListViewStartTabAccelerationCategories();
36
37     myLogin = new LoginWindow(this);
38     categorylist = new CategoryList();
39     myHttpClient = new HttpClient(this);
40     myRegistration = new Registration(this);
41     connect(myRegistration,SIGNAL(sendregistration()),this,SLOT(regUserToServer()));
42     connect(myLogin,SIGNAL(userNameChanged()),this,SLOT(userLogin()));
43     myRoute = new RouteDialog( this);
44
45     //GPS
46     location = new Maemo5Location(this);
47     gpsData = new GPSData(location);
48     connect(location,SIGNAL(agnss()),this,SLOT(gpsStatus()));
49
50     time = 0;
51     speed = 0;
52     timer = new QTimer();
53
54     // Accelerometer
55     accelerometer = new Accelerometer();
56
57     reverseAccelerationFlag = false;
58     vehicleStartedMoving = false;
59     isNewRun = true;
60     isSetup = false;
61     stopTime = 0;
62     accelerationStartThreshold = 0.02;
63
64     QTimer *accelerometerTimer = new QTimer(this);
65     connect(accelerometerTimer, SIGNAL(timeout()), this, SLOT(readAccelerometerData()));
66     accelerometerTimer->start(kAccelerometerSampleRate);
67
68     // Calculate
69     calculate = new Calculate();
70     //connect(calculate, SIGNAL(checkPointReached()), this, SLOT(handleCheckPoint()));
71
72     resetAccelerometerMeasurements();
73
74     measures = new Measures();
75     this->initializeMeasures();
76
77     timer->setInterval(300);
78
79     connect(this->timer, SIGNAL(timeout()), this, SLOT(after_timeout()));
80     connect(myLogin, SIGNAL( userNameChanged()), this, SLOT(updateUserName()));
81
82     ui->labelMeasureTabResult->hide();
83
84     this->setWindowTitle("Speed Freak");
85
86 }
87
88 /**
89   *Destructor of this class. Deletes all dynamic objects and sets them to NULL.
90   */
91 CarMainWindow::~CarMainWindow()
92 {
93     delete ui;
94     ui = NULL;
95     //delete result;
96     //delete measure;
97     delete categorylist;
98     categorylist = NULL;
99     delete welcomeDialog;
100     welcomeDialog = NULL;
101     delete myRoute;
102     myRoute = NULL;
103     delete gpsData;
104     gpsData = NULL;
105 }
106
107 /**
108   *This function is used to .
109   *@param
110   */
111 void CarMainWindow::changeEvent(QEvent *e)
112 {
113     QMainWindow::changeEvent(e);
114     switch (e->type()) {
115     case QEvent::LanguageChange:
116         ui->retranslateUi(this);
117         break;
118     default:
119         break;
120     }
121 }
122
123 /**
124   *This slot function is called when ever list view is update. Start-tab view.
125   */
126 void CarMainWindow::on_listViewStartTabAccelerationCategories_clicked(QModelIndex index)
127 {
128     QString str = index.data().toString();
129     QStringList list = str.split("-");
130     QStringList list2 = list[1].split(" ");
131
132     ui->lineEditStartTabMin->setText(list[0]);
133     ui->lineEditStartTabMax->setText(list2[0]);
134     updateComboBoxStartTabUnits(list2[1]);
135 }
136
137 /**
138   *This slot function is called when ever auto start button clicked. Start-tab view.
139   */
140 void CarMainWindow::on_autoStartButton_clicked()
141 {
142     choice = ui->listViewStartTabAccelerationCategories->currentIndex();
143     choiceInt = choice.row();
144     qDebug() << choiceInt;
145     if (choiceInt == 0)
146     {
147         ui->labelMeasureTabHeader->setText("Accelerate to 40 km/h");
148         result->setDiagramGapStem(75);
149     }
150
151     else if (choiceInt == 1)
152     {
153         ui->labelMeasureTabHeader->setText("Accelerate to 100 km/h");
154         result->setDiagramGapStem(30);
155     }
156
157     else
158     {
159         ui->labelMeasureTabHeader->setText("Accelerate to 80 km/h");
160         result->setDiagramGapStem(37.5);
161     }
162     ui->labelMeasureTabResult->setText("");
163     //delete measure;
164     //measure = NULL;
165     //measure = new MeasureDialog();
166    // connect(measure, SIGNAL(speedAchieved()), this, SLOT(openResultView()));
167     timer->start();
168     // Show measure dialog.
169     //measure->show();
170
171     // TODO: Move next if else to the function which is called when target speed
172     // has reached.
173     if (choiceInt == 0)
174     {
175         if (floor(this->measures->getTime40kmh()) <= 5)
176         {
177             result->setDiagramGapHorizontal(80);
178         }
179
180         else if (floor(this->measures->getTime40kmh()) <= 10)
181         {
182             result->setDiagramGapHorizontal(40);
183         }
184
185         else
186         {
187             result->setDiagramGapHorizontal(20);
188         }
189     }
190
191     else
192     {
193         if (floor(this->measures->getTime40kmh()) <= 5)
194         {
195             result->setDiagramGapHorizontal(80);
196         }
197
198         else if (floor(this->measures->getTime40kmh()) <= 10)
199         {
200             result->setDiagramGapHorizontal(40);
201         }
202
203         else
204         {
205             result->setDiagramGapHorizontal(20);
206         }
207     }
208
209     ui->tabWidget->setCurrentWidget(this->ui->tabMeasureResult);
210 }
211
212 /**
213   *This slot function is called when ever list view is update. Start-tab view.
214   *@param QString unit.
215   */
216 void CarMainWindow::updateComboBoxStartTabUnits(QString unit)
217 {
218     ui->comboBoxStartTabUnits->setCurrentIndex(ui->comboBoxStartTabUnits->findText(unit, Qt::MatchExactly));
219 }
220
221 /**
222   *This function is used to init unit combobox. Start-tab view.
223   */
224 void CarMainWindow::initComboBoxStartTabUnits()
225 {
226     units << "km/h" << "km" << "h" << "m" << "min" << "Mile" << "Mph" << "in" << "ft" << "yrd";
227     ui->comboBoxStartTabUnits->addItems(units);
228 }
229
230 /**
231   *This function is used to set items to unit combobox. Start-tab view.
232   *@param QStringlist units
233   */
234 void CarMainWindow::setComboBoxStartTabUnits(QStringList units)
235 {
236     ui->comboBoxStartTabUnits->addItems(units);
237 }
238
239 /**
240   *This function is used to init listViewStartTabAccelerationCategories. Start-tab view.
241   */
242 void CarMainWindow::initListViewStartTabAccelerationCategories()
243 {
244     accelerationCategoriesStartTab << "0-40 km/h" << "0-100 km/h"; //<< "0-1/4 Mile" << "0-1/8 Mile" << "0-50 km" << "50-100 Mile" << "0-60 Mph" << "0-100 m" << "0-50 ft" << "0-50 yrd" << "0-500 in";
245     QAbstractItemModel *model = new StringListModel(accelerationCategoriesStartTab);
246     ui->listViewStartTabAccelerationCategories->setModel(model);
247 }
248
249 /**
250   *This function is used to set items to listViewStartTabAccelerationCategories. Start-tab view.
251   *@param QStringlist accelerationCategoriesStartTab
252   */
253 void CarMainWindow::setListViewStartTabAccelerationCategories(QStringList accelerationCategoriesStartTab)
254 {
255     QAbstractItemModel *model = new StringListModel(accelerationCategoriesStartTab);
256     ui->listViewStartTabAccelerationCategories->setModel(model);
257 }
258
259 /**
260   *This function is used to set items to category combobox. Top-tab view.
261   *@param
262   */
263 void CarMainWindow::setCategoryCompoBox()
264 {
265     ui->comboBoxTopCategory->addItems(categorylist->getCategoryList());
266 }
267
268 /**
269   *This function is used to set items to labelTopList. Top-tab view.
270   *@param QString category
271   */
272 void CarMainWindow::setListViewTopList(QString category, int size)
273 {
274     QString topList;
275     topList.append( categorylist->getTopList(category, size));
276     ui->labelTopList->setText(topList);
277 }
278
279 /**
280   *This slot function is called when speed is achieved in measure dialog. Opens result dialog.
281   */
282 void CarMainWindow::openResultView()
283 {
284     //result->saveMeasuresToArray(measure->measures);
285     // Show result dialog.
286     //result->show();
287     ui->pushButtonSendResult->setEnabled(true);
288     QString timeInteger;
289     if (choiceInt == 0)
290     {
291         if (floor(this->measures->getTime40kmh()) <= 5)
292         {
293             result->setDiagramGapHorizontal(80);
294         }
295
296         else if (floor(this->measures->getTime40kmh()) <= 10)
297         {
298             result->setDiagramGapHorizontal(40);
299         }
300
301         else
302         {
303             result->setDiagramGapHorizontal(20);
304         }
305     }
306
307     else
308     {
309         if (floor(this->measures->getTime40kmh()) <= 5)
310         {
311             result->setDiagramGapHorizontal(80);
312         }
313
314         else if (floor(this->measures->getTime40kmh()) <= 10)
315         {
316             result->setDiagramGapHorizontal(40);
317         }
318
319         else
320         {
321             result->setDiagramGapHorizontal(20);
322         }
323     }
324     timeInteger.setNum(this->measures->getTime40kmh());
325     //time = "0 - 40 km/h: ";
326     //time.append(timeInteger);
327     //ui->labelResult40kmh->setText(time);
328     ui->labelMeasureTabResult->show();
329     ui->labelMeasureTabResult->setText(timeInteger);
330     //ui->tabWidget->setCurrentWidget(this->ui->tabMeasureResult);
331 }
332
333 /**
334   *This slot function is called when registrate button is clicked.
335   */
336 void CarMainWindow::on_registratePushButton_clicked()
337 {
338     myRegistration->show();
339 }
340
341 /**
342   *This slot function is called when ever refresh button clicked. Top-tab view.
343   */
344 void CarMainWindow::on_buttonTopRefresh_clicked()
345 {
346     myHttpClient->requestCategories();
347     setCategoryCompoBox();
348 }
349
350 /**
351   *This slot function is called when ever category combobox current index changed. Top-tab view.
352   *@param QString category
353   *@todo Check where limitNr is taken.
354   */
355 void CarMainWindow::on_comboBoxTopCategory_currentIndexChanged(QString category)
356 {
357     int limitNr = 5;                    //replace with real value?
358     QString limit = QString::number(limitNr);
359     category = "acceleration-0-100";    //replace with real value from category list/top window
360     myHttpClient->requestTopList(category, limit);
361     setListViewTopList(category,10);
362 }
363
364 /**
365   *This slot function is called when ever category combobox activated. Top-tab view.
366   *@param QString category
367   */
368 void CarMainWindow::on_comboBoxTopCategory_activated(QString category)
369 {
370     setListViewTopList(category,10);
371 }
372
373 /**
374   *This slot function is called when set/change user button is clicked.
375   */
376 void CarMainWindow::on_setUserPushButton_clicked()
377 {
378     myLogin->show();
379 }
380
381 /**
382   *@brief Just for development, for the real button is not shown until
383   *measurin started and there are results.
384   *@todo Implement with real code and yet leave sendXml in the bottom in use.
385   */
386 void CarMainWindow::on_manualStartButton_clicked()
387 {
388
389 }
390
391 /**
392   * This slot function is called when timer gives timeout signal. Checks current speed
393   * and stores times in measure class.
394   */
395 void CarMainWindow::after_timeout()
396 {
397     QString timeString, speedString;
398     time++;
399     speed = speed +10;
400
401     if (floor(speed) == 10)
402     {
403         measures->setTime10kmh(time);
404     }
405
406     else if (floor(speed) == 20)
407     {
408         measures->setTime20kmh(time);
409     }
410
411     else if (floor(speed) == 30)
412     {
413         measures->setTime30kmh(time);
414     }
415
416     else if (floor(speed) == 40)
417     {
418         measures->setTime40kmh(time);
419     }
420
421     else if (floor(speed) == 50)
422     {
423         measures->setTime50kmh(time);
424     }
425
426     else if (floor(speed) == 60)
427     {
428         measures->setTime60kmh(time);
429     }
430
431     else if (floor(speed) == 70)
432     {
433         measures->setTime70kmh(time);
434     }
435
436     else if (floor(speed) == 80)
437     {
438         measures->setTime80kmh(time);
439     }
440
441     else if (floor(speed) == 90)
442     {
443         measures->setTime90kmh(time);
444     }
445
446     else if (floor(speed) == 100)
447     {
448         measures->setTime100kmh(time);
449     }
450
451     else
452     {
453
454     }
455
456     // If speed is over 40 km/h emits speedAchieved() signal and close this dialog.
457     if (speed >= 40.0)
458     {
459         timer->stop();
460         time = 0;
461         speed = 0;
462         //emit this->speedAchieved();
463         this->openResultView();
464         //this->close();
465
466     }
467
468     // Updates speed and time.
469     else
470     {
471         timeString.setNum(time);
472         speedString.setNum(speed);
473         ui->labelMeasureTabTime->setText(timeString);
474         ui->labelMeasureTabSpeed->setText(speedString);
475
476         timer->start();
477     }
478
479 }
480
481 /**
482   * Initializes measures class's member variables.
483   */
484 void CarMainWindow::initializeMeasures()
485 {
486     measures->setTime10kmh(0);
487     measures->setTime20kmh(0);
488     measures->setTime30kmh(0);
489     measures->setTime40kmh(0);
490     measures->setTime50kmh(0);
491     measures->setTime60kmh(0);
492     measures->setTime70kmh(0);
493     measures->setTime80kmh(0);
494     measures->setTime90kmh(0);
495     measures->setTime100kmh(0);
496 }
497
498 /**
499   * This slot function is called when Abort button is clicked.
500   */
501 void CarMainWindow::on_pushButtonMeasureTabAbort_clicked()
502 {
503     measures->setTime10kmh(0);
504     measures->setTime20kmh(0);
505     measures->setTime30kmh(0);
506     measures->setTime40kmh(0);
507     measures->setTime50kmh(0);
508     measures->setTime60kmh(0);
509     measures->setTime70kmh(0);
510     measures->setTime80kmh(0);
511     measures->setTime90kmh(0);
512     measures->setTime100kmh(0);
513     timer->stop();
514     time = 0;
515     speed = 0;
516     ui->tabWidget->setCurrentWidget(this->ui->StartTab);
517     //this->close();
518 }
519
520 void CarMainWindow::on_pushButtonSendResult_clicked()
521 {
522     myHttpClient->sendResultXml();
523     ui->pushButtonSendResult->setEnabled(false);
524 }
525
526 void CarMainWindow::updateUserName()
527 {
528     QString newUserName;
529
530     newUserName = myLogin->getUserName();
531     ui->userNameLabel->setText( "User: " + newUserName);
532
533     if (newUserName.length())
534     {
535        ui->setUserPushButton->setText( "Change User");
536        this->setWindowTitle("Speed freak - " + newUserName);
537     }
538     else
539     {
540         ui->setUserPushButton->setText( "Set User");
541         this->setWindowTitle("Speed freak");
542     }
543 }
544
545 void CarMainWindow::regUserToServer()
546 {
547     myHttpClient->requestRegistration();
548 }
549
550
551 void CarMainWindow::on_drawRoutePushButton_clicked()
552 {
553     myRoute->show();
554 }
555
556 /**
557   * Opens result dialog when show result button is clicked.
558   * Sends measures as parameter to the resultdialogs saveMeasuresToArray-function.
559   */
560 void CarMainWindow::on_pushButtonShowResultDialog_clicked()
561 {
562     Measures meas;
563     meas.setTime10kmh(1.3);
564     meas.setTime20kmh(2.5);
565     meas.setTime30kmh(3.6);
566     meas.setTime40kmh(6.7);
567     meas.setTime50kmh(7.3);
568     meas.setTime60kmh(7.5);
569     meas.setTime70kmh(8.6);
570     meas.setTime80kmh(8.7);
571     meas.setTime90kmh(9.6);
572     meas.setTime100kmh(9.9);
573     result->setDiagramGapHorizontal(40);
574     result->saveMeasuresToArray(&meas);
575     this->result->show();
576 }
577
578 void CarMainWindow::userLogin()
579 {
580     myHttpClient->checkLogin();
581 }
582
583 /**
584   * Resets Accelerometer measurement variables
585   */
586 void CarMainWindow::resetAccelerometerMeasurements()
587 {
588     currentAcceleration = 0;
589     currentAccelerationString = "";
590     currentSpeed = "";
591     currentTime = 0;
592     distanceTraveled = "";
593     firstAcceleration = 0;
594     //horsepower = null;
595     isNewRun = true;
596     //lastScreenUpdateInSeconds = 0;
597     previousTime = 0;
598     reverseAccelerationFlag = false;
599     stopWatch.setHMS(0, 0, 0, 0);
600     //accelerometer->stop();
601     totalTime = "";
602     vehicleStartedMoving = false;
603     calculate->reset();
604 }
605
606 /**
607   * This function is called to handle checkpoints
608   *@param totalTime total time elapsed since starting measurements
609   *@param currentSpeed current speed of the device
610   */
611 void CarMainWindow::handleCheckPoint(double totalTime, double currentSpeed)
612 {
613     // TODO
614     //totalTime;
615     //currentSpeed;
616     return;
617 }
618
619 /**
620   *This function is called to read (and process) data from the accelerometer
621   */
622 void CarMainWindow::readAccelerometerData()
623 {
624     QString s;
625     double changeInAcceleration = 0;
626     qreal x, y, z;
627
628     accelerometer->getAcceleration(x, y, z);
629     accelerometer->smoothData(x, y, z);
630
631     // Apply calibration
632     x -= accelerometer->getCalibrationX();
633     y -= accelerometer->getCalibrationY();
634     z -= accelerometer->getCalibrationZ();
635
636     QString str = QString("acc x: " + QString::number(x) + "\n" +
637                           "acc y: " + QString::number(y) + "\n" +
638                           "acc z: " + QString::number(z) + "\n");
639
640     if (!vehicleStartedMoving)
641     {
642         if (isNewRun)
643         {
644             firstAcceleration = sqrt(x*x + y*y + z*z);
645             //firstAcceleration = y; // first read
646             isNewRun = false;
647         }
648     }
649
650     currentAcceleration = sqrt(x*x + y*y + z*z);
651     changeInAcceleration = (currentAcceleration - firstAcceleration); // firstAcceleration only gets set once
652
653     if (((abs(changeInAcceleration) <= accelerationStartThreshold)
654                 && !vehicleStartedMoving))
655     {
656         return;
657     }
658
659     if (reverseAccelerationFlag)
660     {
661         // will be false until after 1st calculation
662         if ((changeInAcceleration <= 0))
663         {
664             // actually increasing here...
665             changeInAcceleration = abs(changeInAcceleration);
666         }
667         else
668         {
669             // actually decreasing here...
670             changeInAcceleration = (changeInAcceleration * -1);
671         }
672     }
673     if (!vehicleStartedMoving)
674     {
675         if ((changeInAcceleration < 0))
676         {
677             // we started to move backwards first time through
678             reverseAccelerationFlag = true;
679             changeInAcceleration = abs(changeInAcceleration);
680         }
681         vehicleStartedMoving = true;
682
683         stopWatch.setHMS(0, 0, 0, 0);
684         stopWatch.start();
685     }
686     //  keep the following line as close to the SetKinematicsProperties method as possible
687     currentTime = stopWatch.elapsed();
688     calculate->calculateParameters(changeInAcceleration, (currentTime - previousTime)/1000);
689     previousTime = currentTime;
690
691     s.sprintf("%.2f", changeInAcceleration);
692     currentAccelerationString = s;
693
694     double speed = 0.0;
695     speed = calculate->getCurrentSpeed();
696     speed = ((speed*1000)/kSecondsInHour);
697     s.sprintf("%.2f", speed);
698     currentSpeed = s;
699
700     s.sprintf("%.2f", calculate->getDistanceTraveled());
701     distanceTraveled = s;
702
703     // TODO
704     //distanceTraveled;
705     //horsepower;
706     //totalTime;
707
708     s.sprintf("%.2f", calculate->getTotalTime());
709     totalTime = s;
710
711     str.append("ca: " + currentAccelerationString + " G\n" );
712     str.append("cspeed: " + currentSpeed + " km/h \n" );
713     str.append("dist: " + distanceTraveled + " m \n" );
714     str.append("time: " + totalTime + " s \n" );
715
716     if ((stopTime > 0) && (previousTime >= stopTime))
717     {
718         // we want to end at a stopping point that the user chose
719         // output results
720         resetAccelerometerMeasurements();
721     }
722 }
723
724 /**
725   *This function is used to calibrate accelerometer
726   */
727 void CarMainWindow::calibrateAccelerometer()
728 {
729     resetAccelerometerMeasurements();
730     accelerometer->calibrate();
731 }
732
733 /**
734   *This slot function is called when GPS on checkbox state changed.  Route-tab view.
735   *@param int GPSState
736   */
737 void CarMainWindow::on_gpsOnCheckBox_stateChanged(int GPSState)
738 {
739     if (GPSState == 0)
740     {
741         ui->labelRouteTabGPSStatus->setText("GPS off");//testing
742         location->stopPollingGPS();
743     }
744     else
745     {
746         ui->labelRouteTabGPSStatus->setText("GPS on");//testing
747         location->startPollingGPS();
748     }
749 }
750
751 /**
752   *This slot function is called when GPS status changed.  Route-tab view.
753   */
754 void CarMainWindow::gpsStatus()
755 {
756     if (ui->gpsOnCheckBox->isChecked())
757     {
758         if (location->getSatellitesInUse() >= 4)
759         {
760             ui->labelRouteTabGPSStatus->setText("GPS ready");
761         }
762
763         else
764         {
765             ui->labelRouteTabGPSStatus->setText("Waiting for GPS");
766         }
767     }
768 }