Hi Guys!
My first project is an GPS autopilot RC boat. I wrote a code, that is measure the distance between two GPS point. The two GPS point is(in the code):
- local = the present/actual GPS coordinate where i am
- cel = if i press the Joystick Switch then is will be the present/actual.
If i control the boat to destiantion, then i press the button, and the coordinate will save into cel variables. Then i control back the boat to me and i hope i can measure the distance from between this two point. I hope it will be very accurate...
My first problem is, when i stand still, press the button then the actual coordinate will be the destination coordinate too. So the distance need to be 0, or cloe to it, don't? But i see on serial monitor, that the distance is going up from 0 to 60~70 or worst 100+... Is this normal??? I stand still, i hoped that this distance remain 0....
Ohh, i almost forgot: i use Arduino UNO with NEO-6M GPS as you see in the picture....
I paste the code, is there any wrong??
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
float local[2] = {0,0}; // this is the actual place
float cel[2] = {0,0}; // this is the destination place
int SW = 9; // this is the switch
int SW_state = 0;
float radius_of_earth = 63781000; // radius_of_earth/m
static const int RXPin = 3, TXPin = 2;
static const uint32_t GPSBaud = 9600;
TinyGPSPlus gps;
SoftwareSerial ss(RXPin, TXPin);
void setup()
{
Serial.begin(9600);
ss.begin(GPSBaud);
pinMode(SW, INPUT_PULLUP);
pinMode(OUTPUT,8);
}
double distance_in_km(float start_lat, float start_long, float end_lat, float end_long) {
start_lat/= 180 / PI;
start_long/= 180 / PI;
end_lat/= 180 / PI;
end_long/= 180 / PI;
// haversine formula
float a = pow(sin((end_lat-start_lat)/2), 2) + cos(start_lat) * cos(end_lat) * pow(sin((end_long-start_long)/2), 2);
float answer = radius_of_earth * 2 * atan2(sqrt(a), sqrt(1-a));
return int(answer);
}
void loop()
{
while (ss.available() > 0)
if (gps.encode(ss.read()))
displayInfo();
}
void displayInfo()
{
Serial.print(" - Distance from the actual point to destenation point ");
Serial.print(distance_in_km(gps.location.lat(),gps.location.lng(), cel[0], cel[1]));
if (distance_in_km(gps.location.lat(),gps.location.lng(), cel[0], cel[1]) < 80.00)
{
// Serial.println("We arrived");
tone(8, 500);
delay(100);
noTone(8);
}
if (distance_in_km(gps.location.lat(),gps.location.lng(), cel[0], cel[1]) > 81.10)
{
// Serial.println("We are far from destination");
noTone(8);
}
SW_state = digitalRead(SW);
// Serial.println(SW_state);
if (SW_state == 0) // switch pressed
{
cel[0]=gps.location.lat(); // is this ok, or need parameter????
cel[1]=gps.location.lng(); // is this ok, or need parameter????
}
}
else
{
Serial.print(F("INVALID"));
}
Serial.println();
}



