SlashDevin / NeoGPS

NMEA and ublox GPS parser for Arduino, configurable to use as few as 10 bytes of RAM
GNU General Public License v3.0
709 stars 195 forks source link

Distance skipping heavily, even when stationary #151

Open sumagnadas opened 3 years ago

sumagnadas commented 3 years ago

Components:

Output Clock : OK Lon : OK Lat : OK Speed : OK

Problem :

HDOP : not showing Sat count : not showing Trip Distance is skipping heavily. For example 3.99km (walk) of distance is showing 7.xx Trip Distance is ticking when stationary.

CODE :

#include <NMEAGPS.h>
#include <GPSport.h>
#include "extraFuncs.h"

/* variables of default type */
float max_speed = 0, dist = 0;

/* variables of custom classes */
static NMEAGPS gps;
NeoGPS::Location_t prev_loc(91L, 181L);
const gps_fix fix;

/* Reads the data whenever it is available */
static void readData(uint8_t c)
{
   gps.handle(c);
}

/* setup() function */
void setup()
{
  Serial1.begin(9600);

  gpsPort.attachInterrupt(readData);
  gpsPort.begin(9600);
}

/* loop() function which will be run by the board constantly */
void loop()
{ 
  if (gps.available()){
    fix = gps.read();
  }
  //if (fix.valid.heading){sendData("dir", fix.heading());}
  if (fix.valid.hdop){sendData("hdop", fix.hdop);}
  if (fix.valid.satellites){sendData("sat", fix.satellites);}

  sendData("speed", fix.speed_kph());
  sendData("max", maxspeed());

  if (fix.valid.location){
    sendData("long", fix.longitude());
    sendData("lat", fix.latitude());
    if (prev_loc.lat() != 91L){
      dist += fix.location.DistanceKm(prev_loc);
    }
    prev_loc.lat(fix.latitudeL());
    prev_loc.lon(fix.longitudeL());
    sendData("dist", dist);
  }

  PrintTime();//Prints the time
  stopwatch();
  getData();
}

extraFuncs.h contains:

extern const gps_fix fix;
extern float max_speed, dist;

template <typename T> void sendData(char *var, T n);

void ender() {
  for (int i=1; i<=3; i++)
    Serial1.write(0xff);
}

void getData(){
  if (Serial1.available()){
    String a = Serial1.readString();
    if (a.indexOf("dis++") != -1){
      dist += 0.1;
    }
    if (a.indexOf("dis--") != -1 and dist >= 0.1){
      dist -= 0.1;
    }
    sendData("dist", dist);
  }
}

/* Function which sends the passed data, irrespective of the data type,
   to the Nextion display after converting it to String */
template <typename T>
void sendData(char *var, T n) {
  Serial1.print(String(var) + String(".") + String("txt") + String("=\"") + String(n) + String("\""));
  ender();
}

/* function for stopwatch */
void stopwatch() {
  int ms = millis() % 1000;
  int secs = millis() / 1000;
  int mins = secs / 60;
  secs %= 60;
  int hours = mins / 60;
  mins %= 60;
  char elapsed[12];
  sprintf(elapsed, "%02d:%02d:%02d.%02d", hours, mins, secs, ms);
  sendData("stopwatch", elapsed);
}

/* Get the max speed recorded by the GPS module */
static float maxspeed() {
  if (fix.speed_kph() > max_speed) {
    max_speed = fix.speed_kph();
  }
  return max_speed;
}

/* Print the time after adding a few hours for IST */
static void PrintTime() {
    int ind_minute = fix.dateTime.minutes + 30, ind_hour = fix.dateTime.hours + 5, ind_second = fix.dateTime.seconds;
    if (ind_minute >= 60) {
      ind_hour += 1;
      ind_minute -= 60;
    }
    if (ind_hour > 23) {
      ind_hour %= 24;
    }
    char ind_time[8];
    sprintf(ind_time, "%02d:%02d:%02d", ind_hour, ind_minute, ind_second);
    sendData("time", ind_time);
}