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"
15 * Default constructor for AccRealTimeDialog class.
18 AccRealTimeDialog::AccRealTimeDialog(QWidget *parent) :
20 ui(new Ui::AccRealTimeDialog)
24 accelerometer = new Accelerometer();
25 movingAverageZ = new MovingAverage(10);
26 movingAverageY = new MovingAverage(10);
27 calculate = new Calculate();
28 accelerationStartThreshold = 0.1;
30 accelerometerTimer = new QTimer(this);
31 connect(accelerometerTimer, SIGNAL(timeout()), this, SLOT(readAccelerometerData()));
33 updateScreenCounter = 0;
34 resetAccelerometerMeasurements();
40 * Default destructor for AccRealTimeDialog class.
41 * Deletes all dynamic objects and sets them to NULL.
43 AccRealTimeDialog::~AccRealTimeDialog()
47 delete accelerometerTimer;
49 delete movingAverageZ;
50 delete movingAverageY;
58 void AccRealTimeDialog::changeEvent(QEvent *e)
60 QDialog::changeEvent(e);
62 case QEvent::LanguageChange:
63 ui->retranslateUi(this);
71 * This function is called to read (and process) data from the accelerometer
73 void AccRealTimeDialog::readAccelerometerData()
76 double changeInAcceleration = 0;
79 accelerometer->getAcceleration(x, y, z);
81 // keep the following line as close to the SetKinematicsProperties method as possible
82 currentTime = elapsedTime.elapsed();
84 // Calculate average for Z
85 movingAverageZ->Enqueue(z);
86 z = movingAverageZ->Average();
87 // Calculate average for Y
88 movingAverageY->Enqueue(y);
89 y = movingAverageY->Average();
92 x -= accelerometer->getCalibrationX();
93 y -= accelerometer->getCalibrationY();
94 z -= accelerometer->getCalibrationZ();
96 qreal calY = accelerometer->getCalibrationY();
97 qreal calZ = accelerometer->getCalibrationZ();
104 // Take acceleration from more affecting axel
108 currentAcceleration = z;
113 currentAcceleration = -y;
116 //screen up: y = 0, z = -1
117 //screen front: y = -1, z = 0
119 //QString str = QString("acc x: " + QString::number(x) + "\n" +
120 // "acc y: " + QString::number(y) + "\n" +
121 // "acc z: " + QString::number(z) + "\n");
123 //currentAcceleration = z;//sqrt(x*x + y*y + z*z);
124 //qDebug("y: %f, calibZ: %f, calibY: %f\n",y,accelerometer->getCalibrationZ(),accelerometer->getCalibrationY());
125 changeInAcceleration = currentAcceleration;
127 if (((fabs(changeInAcceleration) <= accelerationStartThreshold)
128 && !vehicleStartedMoving))
132 else if(!vehicleStartedMoving)
134 vehicleStartedMoving = true;
140 calculate->calculateParameters(changeInAcceleration, (currentTime - previousTime)/1000);
141 previousTime = currentTime;
143 //s.sprintf("%.2f", changeInAcceleration);
144 //currentAccelerationString = s;
147 speed = calculate->getCurrentSpeed();
150 s.sprintf("%.1f", speed);
153 //s.sprintf("%.2f", calculate->getDistanceTraveled());
154 //distanceTraveled = s;
160 time = calculate->getTotalTime();
162 s.sprintf("%.2f", time);
165 //str.append("ca: " + currentAccelerationString + " G\n" );
166 //str.append("cspeed: " + currentSpeed + " km/h \n" );
167 //str.append("dist: " + distanceTraveled + " m \n" );
168 //str.append("time: " + totalTime + " s \n" );
170 if( updateScreenCounter == 5 )
172 ui->realSpeedLabel->setText( currentSpeed );
173 ui->timeLabel->setText( totalTime );
174 updateScreenCounter = 0;
176 updateScreenCounter++;
178 //Open result dialog if target speed reached
179 if( (stopMeasureSpeed > 0) && (speed > stopMeasureSpeed) )
181 this->accelerometerTimer->stop();
184 resultDialog = new ResultDialog(this);
185 connect(resultDialog, SIGNAL(rejected()), this, SLOT(killResultDialog()));
186 connect(resultDialog, SIGNAL(sendresult(double)), this, SLOT(sendResult(double)));
190 resultDialog->setEnd(stopMeasureSpeed);
191 //Put all times from all speeds
192 QMap<int,double> tempMap = calculate->getValuesMap();
194 for( int i = 1 ; i <= tempMap.count() ; i++ )
196 resultDialog->setValue(i*10,tempMap[i*10]);
199 resultDialog->show();
206 * Resets Accelerometer measurement variables
208 void AccRealTimeDialog::resetAccelerometerMeasurements()
211 currentAcceleration = 0;
219 vehicleStartedMoving = false;
220 stopMeasureSpeed = 0;
226 void AccRealTimeDialog::Calibrate()
228 accelerometer->calibrate();
232 * This slot function called when ever abort button clicked.
234 void AccRealTimeDialog::on_buttonAbort_clicked()
236 accelerometerTimer->stop();
237 resetAccelerometerMeasurements();
244 void AccRealTimeDialog::startAccelerationMeasure()
246 double temp = stopMeasureSpeed;
247 resetAccelerometerMeasurements();
248 stopMeasureSpeed = temp;
249 accelerometerTimer->start(40);
254 * @param double speed
256 void AccRealTimeDialog::SetStopMeasureSpeed(double speed)
258 stopMeasureSpeed = speed;
262 *This slot function emit accelerationstart sendresult.
264 * @param double result
266 void AccRealTimeDialog::sendResult(double result)
268 emit sendresult(result);
272 *This slot function kills resultDialog.
275 void AccRealTimeDialog::killResultDialog()