Bug fixed: If you are logged in and result is sent in result dialog. If you want...
[speedfreak] / Client / accrealtimedialog.cpp
1 /*
2  * Acceleration info in real time dialog
3  *
4  * @author      Jukka Kurttila <jukka.kurttila@fudeco.com>
5  * @author      Toni Jussila    <toni.jussila@fudeco.com>
6  * @copyright   (c) 2010 Speed Freak team
7  * @license     http://opensource.org/licenses/gpl-license.php GNU Public License
8  */
9
10 #include "accrealtimedialog.h"
11 #include "ui_accrealtimedialog.h"
12 #include <math.h>
13
14 AccRealTimeDialog::AccRealTimeDialog(QWidget *parent) :
15     QDialog(parent),
16     ui(new Ui::AccRealTimeDialog)
17 {
18     ui->setupUi(this);
19
20     accelerometer = new Accelerometer();
21     movingAverageZ = new MovingAverage(10);
22     calculate = new Calculate();
23     accelerationStartThreshold = 0.1;
24
25     accelerometerTimer = new QTimer(this);
26     connect(accelerometerTimer, SIGNAL(timeout()), this, SLOT(readAccelerometerData()));
27     isNewRun = true;
28     updateScreenCounter = 0;
29     resetAccelerometerMeasurements();
30
31     resultDialog = NULL;
32 }
33
34 AccRealTimeDialog::~AccRealTimeDialog()
35 {
36     delete ui;
37     delete accelerometer;
38     delete accelerometerTimer;
39     delete calculate;
40     delete movingAverageZ;
41     if(resultDialog)
42         delete resultDialog;
43 }
44
45 void AccRealTimeDialog::changeEvent(QEvent *e)
46 {
47     QDialog::changeEvent(e);
48     switch (e->type()) {
49     case QEvent::LanguageChange:
50         ui->retranslateUi(this);
51         break;
52     default:
53         break;
54     }
55 }
56 /**
57   *This function is called to read (and process) data from the accelerometer
58   */
59 void AccRealTimeDialog::readAccelerometerData()
60 {
61     QString s;
62     double changeInAcceleration = 0;
63     qreal x, y, z;
64
65     accelerometer->getAcceleration(x, y, z);
66
67     //  keep the following line as close to the SetKinematicsProperties method as possible
68     currentTime = elapsedTime.elapsed();
69
70     //accelerometer->smoothData(x, y, z);
71
72     //Calculate average
73     movingAverageZ->Enqueue(z);
74     z = movingAverageZ->Average();
75
76     // Apply calibration
77     x -= accelerometer->getCalibrationX();
78     y -= accelerometer->getCalibrationY();
79     z -= accelerometer->getCalibrationZ();
80
81 //    QString str = QString("acc x: " + QString::number(x) + "\n" +
82 //                          "acc y: " + QString::number(y) + "\n" +
83 //                          "acc z: " + QString::number(z) + "\n");
84
85     currentAcceleration = z;//sqrt(x*x + y*y + z*z);
86     changeInAcceleration = currentAcceleration;
87
88     if (((fabs(changeInAcceleration) <= accelerationStartThreshold)
89                 && !vehicleStartedMoving))
90     {
91         return;
92     }
93     else if(!vehicleStartedMoving)
94     {
95         vehicleStartedMoving = true;
96         elapsedTime.start();
97         previousTime = 0;
98         currentTime = 0;
99     }
100
101     calculate->calculateParameters(changeInAcceleration, (currentTime - previousTime)/1000);
102     previousTime = currentTime;
103
104     //s.sprintf("%.2f", changeInAcceleration);
105     //currentAccelerationString = s;
106
107     speed = 0;
108     speed = calculate->getCurrentSpeed();
109     //Convert to km/h
110     speed = speed*3.6;
111     s.sprintf("%.1f", speed);
112     currentSpeed = s;
113
114     //s.sprintf("%.2f", calculate->getDistanceTraveled());
115     //distanceTraveled = s;
116
117     // TODO
118     //distanceTraveled;
119     //horsepower;
120
121     time = calculate->getTotalTime();
122
123     s.sprintf("%.2f", time);
124     totalTime = s;
125
126     //str.append("ca: " + currentAccelerationString + " G\n" );
127     //str.append("cspeed: " + currentSpeed + " km/h \n" );
128     //str.append("dist: " + distanceTraveled + " m \n" );
129     //str.append("time: " + totalTime + " s \n" );
130
131     if( updateScreenCounter == 5 )
132     {
133         ui->realSpeedLabel->setText( currentSpeed );
134         ui->timeLabel->setText( totalTime );
135         updateScreenCounter = 0;
136     }
137     updateScreenCounter++;
138
139     //Open result dialog if target speed reached
140     if( (stopMeasureSpeed > 0) && (speed > stopMeasureSpeed) )
141     {
142         this->accelerometerTimer->stop();
143         if(!resultDialog)
144         {
145             resultDialog = new ResultDialog(this);
146             connect(resultDialog, SIGNAL(rejected()), this, SLOT(killResultDialog()));
147             connect(resultDialog, SIGNAL(sendresult(double)), this, SLOT(sendResult(double)));
148         }
149         if(resultDialog)
150         {
151             resultDialog->setEnd(stopMeasureSpeed);
152             //Put all times from all speeds
153             QMap<int,double> tempMap = calculate->getValuesMap();
154
155             for( int i = 1 ; i <= tempMap.count() ; i++ )
156             {
157                 resultDialog->setValue(i*10,tempMap[i*10]);
158             }
159             resultDialog->show();
160             this->hide();
161         }
162     }
163 }
164
165 /**
166   * Resets Accelerometer measurement variables
167   */
168 void AccRealTimeDialog::resetAccelerometerMeasurements()
169 {
170     speed = 0;
171     currentAcceleration = 0;
172     currentSpeed = "";
173     currentTime = 0;
174     isNewRun = true;
175     previousTime = 0;
176     elapsedTime.start();
177     totalTime = "";
178     calculate->reset();
179     vehicleStartedMoving = false;
180     stopMeasureSpeed = 0;
181 }
182
183 void AccRealTimeDialog::Calibrate()
184 {
185     accelerometer->calibrate();
186 }
187
188 void AccRealTimeDialog::on_buttonAbort_clicked()
189 {
190     accelerometerTimer->stop();
191     resetAccelerometerMeasurements();
192     this->close();
193 }
194
195 void AccRealTimeDialog::startAccelerationMeasure()
196 {
197     double temp = stopMeasureSpeed;
198     resetAccelerometerMeasurements();
199     stopMeasureSpeed = temp;
200     accelerometerTimer->start(40);
201 }
202
203 void AccRealTimeDialog::SetStopMeasureSpeed(double speed)
204 {
205     stopMeasureSpeed = speed;
206 }
207
208 /**
209   *This slot function emit accelerationstart sendresult.
210   *
211   **/
212 void AccRealTimeDialog::sendResult(double result)
213 {
214     emit sendresult(result);
215 }
216
217 /**
218   *This slot function kills resultDialog.
219   *
220   **/
221 void AccRealTimeDialog::killResultDialog()
222 {
223     if(resultDialog)
224     {
225         delete resultDialog;
226         resultDialog = NULL;
227     }
228 }