I have two codes that receives data from my phone app gps latitude and longitude, then i have a gps module code that shows in serial the latitude and longitude of the device.
BUT im having a hard time aligning them, the project im working on is a follow me robot and i need that both to be accurate to compare each other if the device needs to run the motors to align the gps data from my phone being sent but its not happening. The output shows GOING status even tho the devices are inches apart it should consider it to be close enough. HELP
I tried padding or calibrating but still doesnt work, is there a better approach on this project?? using UNO, NeoM8n GPS module, HC05 bluetooth and a compass.
This is the output but the devices are really close:
GPS (raw): 10.643825, 122.940353
GPS (calibrated): 10.643551, 122.940429
BT: 10.643960, 122.940444
Proximity value: 0.000409
GOING
#include <AltSoftSerial.h>
#include <SoftwareSerial.h>
#include <TinyGPS++.h>
// Bluetooth Module (HC-05)
#define BT_RX 2 // TX of Bluetooth
#define BT_TX 3 // RX of Bluetooth
SoftwareSerial BTSerial(BT_RX, BT_TX);
// GPS Module (Uses AltSoftSerial - Fixed to Pins 8 & 9)
AltSoftSerial gpsSerial;
TinyGPSPlus gps;
// Store GPS Coordinates
double gpsLat = 0.0, gpsLon = 0.0;
// Calibration based on your test data
// Adjusting for systematic differences between GPS and BT
const double LAT_CALIBRATION = -0.000274; // 10.643940 - 10.643666
const double LON_CALIBRATION = 0.000076; // 122.940429 - 122.940505
// Proximity threshold - approximately 1 meter in decimal degrees
// 0.00001 is roughly 1.1 meters at the equator
const double PROXIMITY_THRESHOLD = 0.00001;
void setup() {
Serial.begin(9600);
BTSerial.begin(38400); // Bluetooth
gpsSerial.begin(9600); // GPS
Serial.println("System Initialized: GPS & Bluetooth Ready");
}
void loop() {
// Continuously update GPS coordinates
updateGPSCoordinates();
// Check for Bluetooth data
if (BTSerial.available()) {
String btData = BTSerial.readString();
btData.trim(); // Remove any whitespace
// Process location data
processLocationData(btData);
}
}
void processLocationData(String btData) {
double btLat = 0.0, btLon = 0.0;
// Parse Bluetooth data (expected format: "lat,lon")
int commaIndex = btData.indexOf(',');
if (commaIndex > 0) {
btLat = btData.substring(0, commaIndex).toFloat();
btLon = btData.substring(commaIndex + 1).toFloat();
} else {
Serial.println("Invalid data format received");
return;
}
// Compare if GPS data is valid
if (gpsLat != 0.0 && gpsLon != 0.0) {
// Apply calibration to align GPS with BT
double calibratedGpsLat = gpsLat + LAT_CALIBRATION;
double calibratedGpsLon = gpsLon + LON_CALIBRATION;
// Calculate absolute differences between coordinates
double latDiff = abs(btLat - calibratedGpsLat);
double lonDiff = abs(btLon - calibratedGpsLon);
// Calculate combined difference (simple approximation of distance)
double combinedDiff = sqrt(latDiff*latDiff + lonDiff*lonDiff);
// Debug output
Serial.print("GPS (raw): ");
Serial.print(gpsLat, 6);
Serial.print(", ");
Serial.println(gpsLon, 6);
Serial.print("GPS (calibrated): ");
Serial.print(calibratedGpsLat, 6);
Serial.print(", ");
Serial.println(calibratedGpsLon, 6);
Serial.print("BT: ");
Serial.print(btLat, 6);
Serial.print(", ");
Serial.println(btLon, 6);
Serial.print("Proximity value: ");
Serial.println(combinedDiff, 6);
// Send proximity value back to the phone
BTSerial.print("PROX:");
BTSerial.println(combinedDiff, 6);
// Simple proximity check with much tighter threshold (~1m)
if (combinedDiff <= PROXIMITY_THRESHOLD) {
Serial.println("ARRIVED");
BTSerial.println("ARRIVED");
} else {
Serial.println("GOING");
BTSerial.println("GOING");
}
}
}
// Function to update GPS coordinates
void updateGPSCoordinates() {
while (gpsSerial.available()) {
gps.encode(gpsSerial.read());
if (gps.location.isUpdated()) {
gpsLat = gps.location.lat();
gpsLon = gps.location.lng();
}
}
}