2 * Acceleration info in real time dialog
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
10 #include "accrealtimedialog.h"
11 #include "ui_accrealtimedialog.h"
14 AccRealTimeDialog::AccRealTimeDialog(QWidget *parent) :
16 ui(new Ui::AccRealTimeDialog)
20 accelerometer = new Accelerometer();
21 movingAverageZ = new MovingAverage(10);
22 movingAverageY = new MovingAverage(10);
23 calculate = new Calculate();
24 accelerationStartThreshold = 0.1;
26 accelerometerTimer = new QTimer(this);
27 connect(accelerometerTimer, SIGNAL(timeout()), this, SLOT(readAccelerometerData()));
29 updateScreenCounter = 0;
30 resetAccelerometerMeasurements();
35 AccRealTimeDialog::~AccRealTimeDialog()
39 delete accelerometerTimer;
41 delete movingAverageZ;
42 delete movingAverageY;
47 void AccRealTimeDialog::changeEvent(QEvent *e)
49 QDialog::changeEvent(e);
51 case QEvent::LanguageChange:
52 ui->retranslateUi(this);
59 *This function is called to read (and process) data from the accelerometer
61 void AccRealTimeDialog::readAccelerometerData()
64 double changeInAcceleration = 0;
67 accelerometer->getAcceleration(x, y, z);
69 // keep the following line as close to the SetKinematicsProperties method as possible
70 currentTime = elapsedTime.elapsed();
72 //accelerometer->smoothData(x, y, z);
74 //Calculate average for Z
75 movingAverageZ->Enqueue(z);
76 z = movingAverageZ->Average();
77 //Calculate average for Y
78 movingAverageY->Enqueue(y);
79 y = movingAverageY->Average();
82 x -= accelerometer->getCalibrationX();
83 y -= accelerometer->getCalibrationY();
84 z -= accelerometer->getCalibrationZ();
86 qreal calY = accelerometer->getCalibrationY();
87 qreal calZ = accelerometer->getCalibrationZ();
94 //Take acceleration from more affecting axel
98 currentAcceleration = z;
103 currentAcceleration = -y;
106 //screen up: y = 0, z = -1
107 //screen front: y = -1, z = 0
109 // QString str = QString("acc x: " + QString::number(x) + "\n" +
110 // "acc y: " + QString::number(y) + "\n" +
111 // "acc z: " + QString::number(z) + "\n");
113 //currentAcceleration = z;//sqrt(x*x + y*y + z*z);
114 //qDebug("y: %f, calibZ: %f, calibY: %f\n",y,accelerometer->getCalibrationZ(),accelerometer->getCalibrationY());
115 changeInAcceleration = currentAcceleration;
117 if (((fabs(changeInAcceleration) <= accelerationStartThreshold)
118 && !vehicleStartedMoving))
122 else if(!vehicleStartedMoving)
124 vehicleStartedMoving = true;
130 calculate->calculateParameters(changeInAcceleration, (currentTime - previousTime)/1000);
131 previousTime = currentTime;
133 //s.sprintf("%.2f", changeInAcceleration);
134 //currentAccelerationString = s;
137 speed = calculate->getCurrentSpeed();
140 s.sprintf("%.1f", speed);
143 //s.sprintf("%.2f", calculate->getDistanceTraveled());
144 //distanceTraveled = s;
150 time = calculate->getTotalTime();
152 s.sprintf("%.2f", time);
155 //str.append("ca: " + currentAccelerationString + " G\n" );
156 //str.append("cspeed: " + currentSpeed + " km/h \n" );
157 //str.append("dist: " + distanceTraveled + " m \n" );
158 //str.append("time: " + totalTime + " s \n" );
160 if( updateScreenCounter == 5 )
162 ui->realSpeedLabel->setText( currentSpeed );
163 ui->timeLabel->setText( totalTime );
164 updateScreenCounter = 0;
166 updateScreenCounter++;
168 //Open result dialog if target speed reached
169 if( (stopMeasureSpeed > 0) && (speed > stopMeasureSpeed) )
171 this->accelerometerTimer->stop();
174 resultDialog = new ResultDialog(this);
175 connect(resultDialog, SIGNAL(rejected()), this, SLOT(killResultDialog()));
176 connect(resultDialog, SIGNAL(sendresult(double)), this, SLOT(sendResult(double)));
180 resultDialog->setEnd(stopMeasureSpeed);
181 //Put all times from all speeds
182 QMap<int,double> tempMap = calculate->getValuesMap();
184 for( int i = 1 ; i <= tempMap.count() ; i++ )
186 resultDialog->setValue(i*10,tempMap[i*10]);
188 resultDialog->show();
195 * Resets Accelerometer measurement variables
197 void AccRealTimeDialog::resetAccelerometerMeasurements()
200 currentAcceleration = 0;
208 vehicleStartedMoving = false;
209 stopMeasureSpeed = 0;
212 void AccRealTimeDialog::Calibrate()
214 accelerometer->calibrate();
217 void AccRealTimeDialog::on_buttonAbort_clicked()
219 accelerometerTimer->stop();
220 resetAccelerometerMeasurements();
224 void AccRealTimeDialog::startAccelerationMeasure()
226 double temp = stopMeasureSpeed;
227 resetAccelerometerMeasurements();
228 stopMeasureSpeed = temp;
229 accelerometerTimer->start(40);
232 void AccRealTimeDialog::SetStopMeasureSpeed(double speed)
234 stopMeasureSpeed = speed;
238 *This slot function emit accelerationstart sendresult.
241 void AccRealTimeDialog::sendResult(double result)
243 emit sendresult(result);
247 *This slot function kills resultDialog.
250 void AccRealTimeDialog::killResultDialog()