Fixed bugs 5711 and 5832. Now graph's line wont draw out of the diagram and messagebo...
[speedfreak] / Client / accrealtimedialog.cpp
index 9b5bc3f..4406a08 100644 (file)
@@ -2,6 +2,7 @@
  * Acceleration info in real time dialog
  *
  * @author      Jukka Kurttila <jukka.kurttila@fudeco.com>
+ * @author      Toni Jussila   <toni.jussila@fudeco.com>
  * @copyright   (c) 2010 Speed Freak team
  * @license     http://opensource.org/licenses/gpl-license.php GNU Public License
  */
@@ -18,6 +19,7 @@ AccRealTimeDialog::AccRealTimeDialog(QWidget *parent) :
 
     accelerometer = new Accelerometer();
     movingAverageZ = new MovingAverage(10);
+    movingAverageY = new MovingAverage(10);
     calculate = new Calculate();
     accelerationStartThreshold = 0.1;
 
@@ -37,6 +39,7 @@ AccRealTimeDialog::~AccRealTimeDialog()
     delete accelerometerTimer;
     delete calculate;
     delete movingAverageZ;
+    delete movingAverageY;
     if(resultDialog)
         delete resultDialog;
 }
@@ -68,20 +71,47 @@ void AccRealTimeDialog::readAccelerometerData()
 
     //accelerometer->smoothData(x, y, z);
 
-    //Calculate average
+    //Calculate average for Z
     movingAverageZ->Enqueue(z);
     z = movingAverageZ->Average();
+    //Calculate average for Y
+    movingAverageY->Enqueue(y);
+    y = movingAverageY->Average();
 
     // Apply calibration
     x -= accelerometer->getCalibrationX();
     y -= accelerometer->getCalibrationY();
     z -= accelerometer->getCalibrationZ();
 
+    qreal calY = accelerometer->getCalibrationY();
+    qreal calZ = accelerometer->getCalibrationZ();
+
+    if(calY < 0)
+        calY = -calY;
+    if(calZ < 0)
+        calZ = -calZ;
+
+    //Take acceleration from more affecting axel
+    if(calZ < calY)
+    {
+        z = z*(1+calZ/2);
+        currentAcceleration = z;
+    }
+    else
+    {
+        y = y*(1+calY/2);
+        currentAcceleration = -y;
+    }
+
+    //screen up: y = 0, z = -1
+    //screen front: y = -1, z = 0
+
 //    QString str = QString("acc x: " + QString::number(x) + "\n" +
 //                          "acc y: " + QString::number(y) + "\n" +
 //                          "acc z: " + QString::number(z) + "\n");
 
-    currentAcceleration = z;//sqrt(x*x + y*y + z*z);
+    //currentAcceleration = z;//sqrt(x*x + y*y + z*z);
+    //qDebug("y: %f, calibZ: %f, calibY: %f\n",y,accelerometer->getCalibrationZ(),accelerometer->getCalibrationY());
     changeInAcceleration = currentAcceleration;
 
     if (((fabs(changeInAcceleration) <= accelerationStartThreshold)
@@ -142,18 +172,23 @@ void AccRealTimeDialog::readAccelerometerData()
         if(!resultDialog)
         {
             resultDialog = new ResultDialog(this);
+            connect(resultDialog, SIGNAL(rejected()), this, SLOT(killResultDialog()));
+            connect(resultDialog, SIGNAL(sendresult(double)), this, SLOT(sendResult(double)));
         }
-        resultDialog->setEnd(stopMeasureSpeed);
-
-        //Put all times from all speeds
-        QMap<int,double> tempMap = calculate->getValuesMap();
-
-        for( int i = 1 ; i <= tempMap.count() ; i++ )
+        if(resultDialog)
         {
-            resultDialog->setValue(i*10,tempMap[i*10]);
+            resultDialog->setEnd(stopMeasureSpeed);
+            //Put all times from all speeds
+            QMap<int,double> tempMap = calculate->getValuesMap();
+
+            for( int i = 1 ; i <= tempMap.count() ; i++ )
+            {
+                resultDialog->setValue(i*10,tempMap[i*10]);
+            }
+            calculate->reset();
+            resultDialog->show();
+            this->hide();
         }
-        resultDialog->show();
-        this->hide();
     }
 }
 
@@ -174,6 +209,7 @@ void AccRealTimeDialog::resetAccelerometerMeasurements()
     vehicleStartedMoving = false;
     stopMeasureSpeed = 0;
 }
+
 void AccRealTimeDialog::Calibrate()
 {
     accelerometer->calibrate();
@@ -185,6 +221,7 @@ void AccRealTimeDialog::on_buttonAbort_clicked()
     resetAccelerometerMeasurements();
     this->close();
 }
+
 void AccRealTimeDialog::startAccelerationMeasure()
 {
     double temp = stopMeasureSpeed;
@@ -192,7 +229,30 @@ void AccRealTimeDialog::startAccelerationMeasure()
     stopMeasureSpeed = temp;
     accelerometerTimer->start(40);
 }
+
 void AccRealTimeDialog::SetStopMeasureSpeed(double speed)
 {
     stopMeasureSpeed = speed;
 }
+
+/**
+  *This slot function emit accelerationstart sendresult.
+  *
+  **/
+void AccRealTimeDialog::sendResult(double result)
+{
+    emit sendresult(result);
+}
+
+/**
+  *This slot function kills resultDialog.
+  *
+  **/
+void AccRealTimeDialog::killResultDialog()
+{
+    if(resultDialog)
+    {
+        delete resultDialog;
+        resultDialog = NULL;
+    }
+}