Gps acceleration and route saving in txt-file.
authorToni Jussila <toni.jussila@fudeco.com>
Thu, 25 Mar 2010 09:52:17 +0000 (11:52 +0200)
committerToni Jussila <toni.jussila@fudeco.com>
Thu, 25 Mar 2010 09:52:17 +0000 (11:52 +0200)
Client/carmainwindow.cpp
Client/carmainwindow.h
Client/carmainwindow.ui
Client/gpsdata.cpp
Client/gpsdata.h

index 4272b3a..0deeddc 100644 (file)
@@ -49,6 +49,13 @@ CarMainWindow::CarMainWindow(QWidget *parent):QMainWindow(parent), ui(new Ui::Ca
     gpsData = new GPSData(location);
     connect(location,SIGNAL(agnss()),this,SLOT(gpsStatus()));
     gpsTime = new QDateTime();
+    gpsTimer = new QTimer();
+    connect(gpsTimer, SIGNAL(timeout()),this, SLOT(gpsTimerTimeout()));
+    gpsSpeedNow = 0.0;
+    gpsSpeedPrevious = 0.0;
+    gpsAcceleration = 0.0;
+    timeFromGps = 0.0; //Measure-tab view.
+    gpsSpeed = 0.0;
 
     this->time = 0;
     this->speed = 0;
@@ -78,16 +85,16 @@ CarMainWindow::CarMainWindow(QWidget *parent):QMainWindow(parent), ui(new Ui::Ca
     measures = new Measures();
     this->initializeMeasures();
 
-    this->timer->setInterval(300);
+    this->timer->setInterval(100);
 
     connect(this->timer, SIGNAL(timeout()), this, SLOT(after_timeout()));
     connect(myLogin, SIGNAL( userNameChanged()), this, SLOT(updateUserName()));
 
     ui->labelMeasureTabResult->hide();
     ui->pushButtonShowResultDialog->setEnabled(false);
+    ui->pushButtonShowResultDialog->setEnabled(false);
 
     this->setWindowTitle("Speed Freak");
-
 }
 
 /**
@@ -109,6 +116,14 @@ CarMainWindow::~CarMainWindow()
     gpsData = NULL;
     delete gpsTime;
     gpsTime = NULL;
+
+    //Route-tab view
+    gpsSpeedNow = 0.0;
+    gpsSpeedPrevious = 0.0;
+    gpsAcceleration = 0.0;
+    timeFromGps = 0.0;
+    gpsSpeed = 0.0;
+    gpsUpdateTime = 0;
 }
 
 /**
@@ -321,15 +336,10 @@ void CarMainWindow::on_manualStartButton_clicked()
   */
 void CarMainWindow::after_timeout()
 {
-    QString timeString, speedString;
-    //time++;
-    //speed = speed +10;
-    timeString.setNum(time);
-    speedString.setNum(speed);
-    ui->labelMeasureTabTime->setText(timeString);
-    ui->labelMeasureTabSpeed->setText(speedString);
-
-    //handleCheckPoint(time, speed);
+    if ( gpsSpeed > 1.0 )
+    {
+        timeFromGps += 0.1;
+    }
 }
 
 /**
@@ -375,6 +385,10 @@ void CarMainWindow::on_pushButtonMeasureTabAbort_clicked()
     this->speed = 0;
     ui->tabWidget->setCurrentWidget(this->ui->StartTab);
     //this->close();
+
+    //GPS
+    gpsSpeed = 0.0;
+    timeFromGps = 0.0;
 }
 
 /**
@@ -662,63 +676,135 @@ void CarMainWindow::calibrateAccelerometer()
 }
 
 /**
-  *This slot function is called when GPS on checkbox state changed.  Route-tab view.
+  *This slot function is called when GPS on checkbox state changed. Route-tab view.
   *@param int GPSState
   */
 void CarMainWindow::on_gpsOnCheckBox_stateChanged(int GPSState)
 {
+    //Stop polling GPS. Route-tab view.
     if (GPSState == 0)
     {
-        ui->labelRouteTabGPSStatus->setText("GPS off");//testing
+        ui->labelRouteTabGPSStatus->setText("GPS status: GPS off");
         location->stopPollingGPS();
+        gpsUpdateTime = 0;
+        gpsTimer->stop();
     }
+    //Start polling GPS. Route-tab view.
     else
     {
-        ui->labelRouteTabGPSStatus->setText("GPS on");//testing
+        ui->labelRouteTabGPSStatus->setText("GPS status: GPS on");
         location->startPollingGPS();
     }
 }
 
 /**
-  *This slot function is called when GPS status changed.  Route-tab view.
+  *This slot function is called when GPS status changed. Route- and measure-tab view.
   */
 void CarMainWindow::gpsStatus()
 {
+    //IF GPS checkbox is ON
     if (ui->gpsOnCheckBox->isChecked())
     {
+        //If GPS find 4 satellite.
         if (location->getSatellitesInUse() >= 4)
         {
-            //Set status
-            //ui->labelRouteTabGPSStatus->setText("GPS ready");
-            ui->labelRouteTabGPSStatus->setText(QString::number(gpsData->roundCounter));
+            //Set gps status. Route-tab view.
+            ui->labelRouteTabGPSStatus->setText("GPS ready");
 
-            //Set time
+            //Set time. Route-tab view.
             gpsTime->setTime_t(location->getTime());
-            ui->labelRouteTabGPSTime->setText(gpsTime->toString());
-
-            //Set latitude & longitude
-            ui->labelRouteTabLatitude->setText("Lat: " + QString::number(location->getLatitude()));
-            ui->labelRouteTabLongitude->setText("Lon: " + QString::number(location->getLongitude()));
+            QString gpsDateNow = gpsTime->toString("dd.MM.yyyy hh:mm:ss");
+            ui->labelRouteTabGPSTime->setText("GPS time: " + gpsDateNow);
+
+            //Set latitude & longitude. Route-tab view.
+            ui->labelRouteTabLatitude->setText("Latitude: " + QString::number(location->getLatitude()));
+            ui->labelRouteTabLongitude->setText("Longitude: " + QString::number(location->getLongitude()));
+
+            //Set rec status. Route-tab view.
+            if (ui->startRecPushButton->text() == "Stop recording")
+            {
+                ui->labelRouteTabRecStatus->setText("Recorded " + QString::number(gpsData->roundCounter) + " point");
+            }
+
+            //Get speed. Route- and Measure-tab view.
+            gpsSpeed = location->getSpeed();
+
+            //Set speed. Route-tab view.
+            ui->labelRouteTabSpeed->setText("Speed:" + QString::number(gpsSpeed) + "km/h +/-" + QString::number(location->getEps()) + "km/h");
+
+            //Measure-tab view.
+            if (gpsSpeed < 40.0)
+            {              
+                ui->labelMeasureTabSpeed->setText(QString::number(gpsSpeed)); //Set speed. //Measure-tab view.
+                ui->labelMeasureTabTime->setText(QString::number(timeFromGps)); //Set time. //Measure-tab view.
+            }
+            //Measure-tab view.
+            else
+            {
+                timer->stop(); //Measure timer
+                ui->labelMeasureTabResult->setText(QString::number(timeFromGps));
+                ui->labelMeasureTabResult->show();
+                ui->pushButtonShowResultDialog->setEnabled(true);
+                ui->pushButtonShowResultDialog->setEnabled(true);
+            }
         }
 
+        //If GPS find less than 4 satellite.
         else
         {
-            ui->labelRouteTabGPSStatus->setText("Waiting for GPS");
+            ui->labelRouteTabGPSStatus->setText("GPS status: Waiting for GPS");
+            gpsTimer->stop();
         }
+
+        gpsUpdateTime = 0;
+        gpsTimer->start(10);
     }
-    ui->labelRouteTabSpeed->setText(QString::number(location->getSpeed()));
 }
 
-void CarMainWindow::on_startRecPushButton_clicked()
+/**
+  *This slot function is called when gps timer timeout(10ms).
+  */
+void CarMainWindow::gpsTimerTimeout()
 {
-    ui->labelRouteTabRecStatus->setText("Recording started");
-    gpsData->startRouteRecording(ui->labelRouteTabGPSTime->text());
+    int time1000ms;
+    time1000ms += 10;
+
+    //IF time is 1 second
+    if (time1000ms == 1000)
+    {
+        //Calculate acceleration 1/s
+        gpsSpeedPrevious = gpsSpeedNow; //Previous speed
+        gpsSpeedNow = (location->getSpeed())/3.6; //Speed now (m/s)
+        gpsAcceleration = (gpsSpeedNow - gpsSpeedPrevious)/1; //Calculate acceleration: speed now - previous speed / 1s.
+        //Set acceleration. Route-tab view.
+        ui->labelRouteTabAcceleration->setText("Acceleration: " + QString::number( gpsAcceleration ) + " m/s2");
+    }
+
+    gpsUpdateTime++;
+    //Set GPS update time. Route-tab view.
+    ui->labelRouteTabGPSUpdateTime->setText("GPS update time: " + QString::number(gpsUpdateTime) + " ms");
 }
 
-void CarMainWindow::on_stopRecPushButton_clicked()
+/**
+  *This slot function is called when start rec push button clicked. Route-tab view.
+  */
+void CarMainWindow::on_startRecPushButton_clicked()
 {
-    ui->labelRouteTabRecStatus->setText("Recording stopped");
-    gpsData->stopRouteRecording(ui->labelRouteTabGPSTime->text());
+    //Start route recording.
+    if (ui->startRecPushButton->text() == "Start recording")
+    {
+        ui->startRecPushButton->setText("Stop recording");
+        ui->labelRouteTabRecStatus->setText("Recording started");
+        gpsData->startRouteRecording(ui->labelRouteTabGPSTime->text());
+    }
+
+    //Stop route recording.
+    else if (ui->startRecPushButton->text() == "Stop recording")
+    {
+        ui->startRecPushButton->setText("Start recording");
+        ui->labelRouteTabRecStatus->setText("Recording stopped");
+        gpsData->stopRouteRecording(ui->labelRouteTabGPSTime->text());
+    }
 }
 
 /**
index 25992c3..d35218a 100644 (file)
@@ -121,6 +121,13 @@ private:
     QString totalTime;
 
     QDateTime *gpsTime;
+    QTimer *gpsTimer;
+    double gpsSpeedNow;     //Route-tab view
+    double gpsSpeedPrevious;//Route-tab view
+    double gpsAcceleration; //Route-tab view
+    double timeFromGps;     //Route-tab view
+    double gpsSpeed;        //Route-tab view
+    int gpsUpdateTime;      //Route-tab view
 
     int counterForSaveResults;
 
@@ -129,8 +136,8 @@ signals:
     void userNameChanged();
 
 private slots:
-    void on_stopRecPushButton_clicked();
-    void on_startRecPushButton_clicked();
+    void gpsTimerTimeout();                             //Route-tab view
+    void on_startRecPushButton_clicked();               //Route-tab view
     void on_pushButtonShowResultDialog_clicked();
     void on_gpsOnCheckBox_stateChanged(int GPSState);   //Route-tab view
     void gpsStatus();                                   //Route-tab view
index 3780027..a7eca51 100644 (file)
      <widget class="QCheckBox" name="gpsOnCheckBox">
       <property name="geometry">
        <rect>
-        <x>10</x>
-        <y>20</y>
-        <width>201</width>
+        <x>20</x>
+        <y>40</y>
+        <width>191</width>
         <height>41</height>
        </rect>
       </property>
       <property name="geometry">
        <rect>
         <x>10</x>
-        <y>76</y>
+        <y>130</y>
         <width>201</width>
         <height>61</height>
        </rect>
        <string>Start recording</string>
       </property>
      </widget>
-     <widget class="QPushButton" name="stopRecPushButton">
-      <property name="geometry">
-       <rect>
-        <x>10</x>
-        <y>160</y>
-        <width>201</width>
-        <height>61</height>
-       </rect>
-      </property>
-      <property name="font">
-       <font>
-        <family>Bitstream Charter</family>
-        <pointsize>16</pointsize>
-        <weight>75</weight>
-        <bold>true</bold>
-       </font>
-      </property>
-      <property name="text">
-       <string>Stop recording</string>
-      </property>
-     </widget>
      <widget class="QPushButton" name="drawRoutePushButton">
       <property name="geometry">
        <rect>
        <rect>
         <x>310</x>
         <y>16</y>
-        <width>451</width>
-        <height>211</height>
+        <width>461</width>
+        <height>291</height>
        </rect>
       </property>
       <layout class="QVBoxLayout" name="verticalLayout">
        <item>
         <widget class="QLabel" name="labelRouteTabGPSStatus">
          <property name="text">
-          <string>Label GPS Status</string>
+          <string>GPS Status</string>
+         </property>
+        </widget>
+       </item>
+       <item>
+        <widget class="QLabel" name="labelRouteTabGPSUpdateTime">
+         <property name="text">
+          <string>GPS Update time</string>
          </property>
         </widget>
        </item>
        <item>
         <widget class="QLabel" name="labelRouteTabGPSTime">
          <property name="text">
-          <string>Label GPS Time</string>
+          <string>GPS Time</string>
          </property>
         </widget>
        </item>
        <item>
         <widget class="QLabel" name="labelRouteTabLatitude">
          <property name="text">
-          <string>Label Latitude</string>
+          <string>Latitude</string>
          </property>
         </widget>
        </item>
        <item>
         <widget class="QLabel" name="labelRouteTabLongitude">
          <property name="text">
-          <string>Label Longitude</string>
+          <string>Longitude</string>
          </property>
         </widget>
        </item>
        <item>
-        <widget class="QLabel" name="labelRouteTabRecStatus">
+        <widget class="QLabel" name="labelRouteTabSpeed">
          <property name="text">
-          <string>Label rec status</string>
+          <string>Speed</string>
          </property>
         </widget>
        </item>
        <item>
-        <widget class="QLabel" name="labelRouteTabSpeed">
+        <widget class="QLabel" name="labelRouteTabAcceleration">
+         <property name="text">
+          <string>Acceleration</string>
+         </property>
+        </widget>
+       </item>
+       <item>
+        <widget class="QLabel" name="labelRouteTabRecStatus">
          <property name="text">
-          <string>Label speed</string>
+          <string>Rec status</string>
          </property>
         </widget>
        </item>
index baf6448..fe7c0d3 100644 (file)
@@ -23,6 +23,12 @@ GPSData::GPSData(Maemo5Location *maemo5location)
     connect(location,SIGNAL(gps_error(int)),this,SLOT(gpsError()));
     connect(location,SIGNAL(gpsd_running()),this,SLOT(gpsdRunning()));
     connect(location,SIGNAL(gpsd_stopped()),this,SLOT(gpsdStopped()));
+
+    gpsTimer = new QTimer();
+    gpsTimeMS = 0;
+    connect(gpsTimer, SIGNAL(timeout()),this, SLOT(gpsTimerTimeout()));
+
+    resetAll();
 }
 
 /**
@@ -68,34 +74,43 @@ void GPSData::agnss()
     //gpsOnline = QString::number(location->getGpsOnline());              //Returns gsp online                     
     //ept = QString::number(location->getEpt());                          //Returns time accuracy in seconds.
     //eph = QString::number(location->getEph());                          //Returns horizontal position accuracy in cm.
-    //epv = QString::number(location->getEpv());                          //Returns altitude accuracy in meters.
     //track = QString::number(location->getTrack());                      //Returns direction of motion in degrees(0-359).
     //epd = QString::number(location->getEpd());                          //Returns track accuracy in degrees.
-    //eps = QString::number(location->getEps());                          //Returns speed accuracy in km/h.
     //climb = QString::number(location->getClimb());                      //Returns current rate of climb in m/s.
     //epc = QString::number(location->getEpc());                          //Returns climb accuracy in m/s.
     //location->distance_between_two_points(double latitude_s, double longitude_s, double latitude_f, double longitude_f);
     //time = location->getTime();//Returns timestamp of the update in seconds.
 
-    latitude    = location->getLatitude();  //Returns latitude.
-    longitude   = location->getLongitude(); //Returns longitude.
-    altitude    = location->getAltitude();  //Returns fix altitude in meters.
-    speed       = location->getSpeed();     //Returns current speed in km/h.
-
-    latitudeNow.sprintf("%.4f", latitude);  //Latitude now to string
-    longitudeNow.sprintf("%.4f", longitude);//Longitude now to string
-    latitudePrevious.sprintf("%.4f", gpsDataArray[roundCounter-1][0]);  //Previous latitude to string
-    longitudePrevious.sprintf("%.4f", gpsDataArray[roundCounter-1][1]); //Previous longitude to string
-
+    //If route recording true
     if ( recordingStatus == true )
     {
-        if ( roundCounter == 0 || latitudeNow != latitudePrevious || longitudeNow != longitudePrevious )
+        latitude    = location->getLatitude();  //Returns latitude.
+        longitude   = location->getLongitude(); //Returns longitude.
+        altitude    = location->getAltitude();  //Returns fix altitude in meters.
+        epv         = location->getEpv();       //Returns altitude accuracy in meters.
+        speed       = location->getSpeed();     //Returns current speed in km/h.
+        eps         = location->getEps();       //Returns speed accuracy in km/h.
+
+        gpsTimer->start(1);
+
+        //If first round
+        if (roundCounter == 0)
         {
-            gpsDataArray[roundCounter][0] = latitude;
-            gpsDataArray[roundCounter][1] = longitude;
-            gpsDataArray[roundCounter][2] = altitude;
-            gpsDataArray[roundCounter][3] = speed;
-            roundCounter ++;
+            saveRoute();
+        }
+
+        else
+        { 
+            latitudeNow.sprintf("%.4f", latitude);  //Latitude now to string
+            longitudeNow.sprintf("%.4f", longitude);//Longitude now to string
+            latitudePrevious.sprintf("%.4f", gpsDataArray[0]);  //Previous latitude to string
+            longitudePrevious.sprintf("%.4f", gpsDataArray[1]); //Previous longitude to string
+
+            //If latitude or longitude change
+            if ( latitudeNow != latitudePrevious || longitudeNow != longitudePrevious )
+            {
+                saveRoute();
+            }
         }
     }
 }
@@ -156,6 +171,10 @@ void GPSData::gpsdStopped()
 
 }
 
+/**
+  *This function start route recording.
+  *@param QString time recording start time.
+  */
 void GPSData::startRouteRecording(QString time)
 {
     if (recordingStatus == false)
@@ -166,35 +185,79 @@ void GPSData::startRouteRecording(QString time)
     }
 }
 
+/**
+  *This function stop route recording.
+  *@param QString time recording stop time.
+  */
 void GPSData::stopRouteRecording(QString time)
 {
-    routeStopTime = time;
-    saveRoute();
     if (recordingStatus == true)
     {
+        routeStopTime = time;
         recordingStatus = false;
         roundCounter = 0;
+        saveRoute();
     }
 }
 
+/**
+  *This slot function is called when gps timer timeout(10s).
+  */
+void GPSData::gpsTimerTimeout()
+{
+    gpsTimeMS++;
+}
+
+/**
+  *This function save route to .txt file.
+  */
 void GPSData::saveRoute()
 {
-    QFile file("testroute.txt");
-    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
-    {
-        return;
-    }
+    QFile file("route" + routeStartTime + ".txt");
     QTextStream route(&file);
 
-    route << "Start: " << routeStartTime << "\n";
-    for (int i = 0 ; i <= roundCounter ; i++)
+    if ( recordingStatus == true )
     {
-        route << " lat: " << gpsDataArray[i][0]
-              << " lon: " << gpsDataArray[i][1]
-              << " alt: " << gpsDataArray[i][2]
-              << " spe: " << gpsDataArray[i][3]
+        //First round.
+        if ( roundCounter == 0 )
+        {
+            if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
+                return;
+
+            route << "Start: " << routeStartTime << "\n";
+        }
+
+        else
+        {
+            if (!file.open(QIODevice::Append | QIODevice::Text))
+                return;
+        }
+
+        gpsDataArray[0] = latitude;
+        gpsDataArray[1] = longitude;
+        gpsDataArray[2] = altitude;
+        gpsDataArray[3] = speed;
+        roundCounter ++;
+
+        route << " la: " << latitude
+              << " \t lo: " << longitude
+              << " \t al: " << altitude
+              << " \t epv: " << epv
+              << " \t sp: " << speed
+              << " \t eps: " << eps
+              << " \t ms: " << gpsTimeMS
               << "\n";
+
+        gpsTimeMS = 0;
+        file.close();
+    }
+
+    //Final round.
+    else
+    {
+        if (!file.open(QIODevice::Append | QIODevice::Text))
+            return;
+        route << "Stop: " << routeStopTime << "\n";
+        file.close();
     }
-    route << "Stop: " << routeStopTime << "\n";
-    file.close();
 }
index 2c6f4b6..f24e6eb 100644 (file)
@@ -13,6 +13,7 @@
 #include <maemo5location.h>
 #include <QFile>
 #include <QTextStream>
+#include <QTimer>
 
 class GPSData : public QObject
 {
@@ -54,7 +55,9 @@ private:
 
     bool recordingStatus;
     //int roundCounter;
-    double gpsDataArray[100][4];
+    double gpsDataArray[4];
+    QTimer *gpsTimer;
+    int gpsTimeMS;
 
 private slots:
     void agnss();
@@ -65,6 +68,7 @@ private slots:
     void gpsError();
     void gpsdRunning();
     void gpsdStopped();
+    void gpsTimerTimeout();
 
 };