General newbie disclaimer, bad formatting, inefficient, and anything done poorly.
Project, I'm trying to average out GPS location for precise 8 decimal precision.
Hardware, ESP32-cam, GY-NEO6MV2 NEO-6M GPS, using an SD card to store the data.
GPS is using the rx/tx pins for serial communication, and the built in SD reader is using IO4,2,12,13,14,15 (just in case someone wants to replicate)
My code does run error free, just having issues with the Float Arrays I'm using to collect a large averaging pool, truncating the decimal to two places.
Trying to keep array precision to 8 decimals.
any feedback is welcome as well
(i would post the actual created table to show the loss of precision, but does contain raw location data)
#include "SD_MMC.h"
#include "TinyGPS++.h"
#include <HardwareSerial.h>
TinyGPSPlus gps; // the TinyGPS++ object
/////////////////////////////////////
// variables
bool flag = false;
double time1 = 0;
double row = 0;
byte row1 = 0;
int M_yr = 0;
int M_mon = 0;
int M_day = 0;
int M_hr = 0;
int M_min = 0;
int M_sec = 0;
float check = 0; // using for troubleshooting
byte sat = 0;
unsigned long myTime;
unsigned long myTimeUpdate;
unsigned long myTimeUpdate2;
double count = 0;
float arrayLng[2][100]; //
float arrayLat[2][100]; //arrays for location data
float arrayAlt[2][100]; //
float rawLocLng = 0;
float offsetLng = 0;
float adjLng = 0;
float locationLng = 0;
float rawLocLat = 0;
float offsetLat = 0;
float adjLat = 0;
float locationLat = 0;
float rawLocAlt = 0;
float offsetAlt = 0;
float adjAlt = 0;
float locationAlt = 0;
/////////////////////////////////////
//starting serial and SD reader
void setup() {
Serial.begin(9600);
SD_MMC.begin();
}
/////////////////////////////////////
//collecting information from gps
void GpsRead(){
if (Serial.available()>0) {
gps.encode(Serial.read());
} else {
return;
}
// updates time and date
if (gps.date.isValid() && gps.time.isValid()) {
M_yr = gps.date.year();
M_mon = gps.date.month();
M_day = gps.date.day();
M_hr = gps.time.hour();
M_min = gps.time.minute();
M_sec = gps.time.second();
} else {
M_yr = 0;
M_mon = 0;
M_day = 0;
M_hr = 0;
M_min = 0;
M_sec = 0;
}
// updates location
if(gps.location.isValid()){
if (gps.location.isUpdated()){
rawLocAlt = gps.altitude.feet();
rawLocLat = gps.location.lat();
rawLocLng = gps.location.lng();
flag = true;
}
}
//satellite lock
sat = gps.satellites.value();
}
////////////////////////////////////////////
// math to avrage out gps location
void calcLocation(){
float t1Lat = 0;
float t1Lng = 0;
float t1Alt = 0;
float t2Lat = 0;
float t2Lng = 0;
float t2Alt = 0;
byte t1 = 0;
arrayLng[0][row1] = rawLocLng;
arrayLat[0][row1] = rawLocLat;
arrayAlt[0][row1] = rawLocAlt;
while(t1 < 101){
t1Lng += arrayLng[0][t1];
t1Lat += arrayLat[0][t1];
t1Alt += arrayAlt[0][t1];
t1++;
}
check = arrayLng[0][row1]; //
t1Lat /= 100;
t1Lng /= 100;
t1Alt /= 100;
arrayLng[1][row1] = rawLocLng + (t1Lng - rawLocLng);
arrayLat[1][row1] = rawLocLat + (t1Lat - rawLocLat);
arrayAlt[1][row1] = rawLocAlt + (t1Alt - rawLocAlt);
t1 = 0;
while(t1 < 101){
t2Lng += arrayLng[1][t1];
t2Lat += arrayLat[1][t1];
t2Alt += arrayAlt[1][t1];
t1++;
}
t2Lat /= 100;
t2Lng /= 100;
t2Alt /= 100;
offsetLat = t2Lat;
offsetLng = t2Lng;
offsetAlt = t2Alt;
locationLat = t1Lat + t2Lat;
locationLng = t1Lng + t2Lng;
locationAlt = t1Alt + t2Alt;
if(row1 == 100){
row1=0;
}
}
/////////////////////////////////////////
// setting up the .csv file for excel, writing and formatting the data
void SDsaveline(int tm){
char FileName[] = "/GPS.csv";
File GPSfile;
char Dash[] = "----------,";
//creates GPS.csv file if one doesnt exsist
if(!SD_MMC.exists(FileName)) {
fs::FS &fs = SD_MMC;
File GPSfile = fs.open(FileName, FILE_WRITE, true);
// collom headers
GPSfile.print("Row,");
GPSfile.print("Time +5hr,");
GPSfile.print("Date,");
GPSfile.print("Sat Lock,");
GPSfile.print("Raw Longitude,");
GPSfile.print("Longitude Offset,");
GPSfile.print("True Longitude,");
GPSfile.print("Raw Latitude,");
GPSfile.print("Latitude Offset,");
GPSfile.print("True Latitude,");
GPSfile.print("Raw Altitude,");
GPSfile.print("Altitude Offset,");
GPSfile.print("True Altatude,");
GPSfile.print("check,");
GPSfile.print("GPS not valid checks");
GPSfile.println("Update time");
GPSfile.close();
}
//appends GPS file with seperation apon boot
else if(row == 0){
fs::FS &fs = SD_MMC;
File GPSfile = fs.open(FileName, "a", false);
GPSfile.println((String)Dash + Dash + Dash + Dash + Dash + Dash + Dash + Dash + Dash + Dash + Dash + Dash + Dash + Dash + Dash);
GPSfile.close();
row ++;
}
//writes data to GPS file
else {
fs::FS &fs = SD_MMC;
File GPSfile = fs.open(FileName, "a", false);
GPSfile.print((String)row + ",");
GPSfile.print((String)M_hr + ":" + M_min + ":" + M_sec + ",");
GPSfile.print((String)M_mon + "-" + M_day + "-" + M_yr + "," + sat + ",");
GPSfile.print(rawLocLng,8);
GPSfile.print(",");
GPSfile.print(offsetLng,8);
GPSfile.print(",");
GPSfile.print(locationLng,8);
GPSfile.print(",");
GPSfile.print(rawLocLat,8);
GPSfile.print(",");
GPSfile.print(offsetLat,8);
GPSfile.print(",");
GPSfile.print(locationLat,8);
GPSfile.print(",");
GPSfile.print(rawLocAlt,8);
GPSfile.print(",");
GPSfile.print(offsetAlt,8);
GPSfile.print(",");
GPSfile.print(locationAlt,8);
GPSfile.print(",");
GPSfile.println((String)check + "," + count + "," + tm);
GPSfile.close();
row ++;
}
count = 0;
}
void loop() {
myTime = millis();
check = 0;
GpsRead();
if(flag){
if((myTime-myTimeUpdate) >= 100){
//avrages and normalizes all location data
calcLocation();
//sends data to file void
SDsaveline((myTime - myTimeUpdate));
myTimeUpdate = millis();
flag = false;
count = 0;
}
}
else {
count++;
}
}