2017-02-03 8 views
1

현재 GPS 모듈에서 판독 값을 가져와야하는 프로젝트에 참여한 후 판독 값과 고정 웨이 포인트 사이의 거리를 계산합니다. Gps가 작동하여 LAT - 54.9289 및 LON - -1.368의 값을 제공합니다.이 값은 약 3,200 미터 거리에 있어야합니다. 그러나 그것은 약 6105를 준다. 나는 또한 6105가 haha에 km 다라고하는 감각을 가지고있다. 임 궁금 그것의 부정적인 숫자를 올바르게 가지고 또는 내가 코드에서 몇 가지 변수 충돌이 있다면. 이것에 어떤 빛이라도 흘리는 것이 좋을 것입니다, 고마워요.Haversine 수식 오류 - 잘못된 거리 - Arduino

#include <TinyGPS.h> 
#include <SoftwareSerial.h> 
#include <rgb_lcd.h> 
#include <Wire.h> 

//Sets TX And RX Pins 
SoftwareSerial GPS(2,3); 
TinyGPS gps; 
void gpsdump(TinyGPS &gps); 
bool feedgps(); 
void CheckGPS(); 
void GetCoords(); 
long lat, lon; 
float LAT, LON; // Latitude is gained from GPS and stored in another variable to avoid errors - Should change with changing GPS value - Which would alter distance to waypoint. 
float LAT1,LON1; 
rgb_lcd lcd; 


void setup() 
{ 
// Sets Baud Rate 
    GPS.begin(9600); 
    Serial.begin(115200); 
} 
// Determines The Distance Between Current Location And Waypoint 

void GetDistance() 
{ 
    // Calculating Distance Between Waypoints 

    double Distance_Lat; // Distance between Lattitude values 
    double Distance_Lon; // Distance between Lonitude values 
    double Distance_Total = 0;// Total Distance 
    double val,val2; // Subsidary variable for holding numbers. - No actual value represented. 
    double fLAT1,fLAT2; 
    double LAT2 = 54.900000; // Waypoint Latitude 
    double LON2 = -1.368072; // Waypoint Longitude 

// Initialising Calculation 
    Distance_Lat = radians(LAT2-LAT1); // Must be done in radians 
    fLAT1 = radians(LAT1); 
    fLAT2 = radians(LAT2); 
    Distance_Lon = radians((LON2)-(LON1)); 

    // Calculating Distance - Using Haversines Formulae 
    Distance_Total = (sin(Distance_Lat/2.0)*sin(Distance_Lat/2.0)); 
    val = cos(fLAT1); 
    val = val*(cos(fLAT2)); 
    val = val*(sin(Distance_Lon/2.0)); 
    val = val*(sin(Distance_Lon/2.0)); 
    Distance_Total = Distance_Total + val; 

    Distance_Total = 2*atan2(sqrt(Distance_Total),sqrt(1.0-Distance_Total)); 
    Distance_Total = Distance_Total*6371.0000; // Converting to meters. 
Serial.println("Distance: "); 
Serial.println(Distance_Total); 

    //--------------------------------------------------------------------------------- 
} 

// Returns Latitude And Longitude As Decimal Degrees (DD). 
void GetCoords() 
{ 
long lat, lon; 
    CheckGPS(); 
    Serial.print("Latitude : "); 
    Serial.print(LAT/1000000,7); 
    Serial.print(" :: Longitude : "); 
    Serial.println(LON/1000000,7); 

} 

void CheckGPS() 

    { 
    bool newdata = false; 
    unsigned long start = millis(); 
// Every 1 seconds, Print an update 
    while (millis() - start < 1000) 
    { 
    if (feedgps()) 
    newdata = true; 
    if (newdata) 
    gpsdump(gps); 
    } 
    } 

// Checks If The GPS Has Any Data To Transmit 
    bool feedgps() 
    { 
    while (GPS.available()) 
    if (gps.encode(GPS.read())) 
     return true; 
    else 
     return false; 
    } 

// Transmits GPS Data And Gets Latitude And Longitude Positions. 
    void gpsdump(TinyGPS &gps) 

{ gps.get_position(&lat, &lon); 
    LAT = lat; 
    LON = lon; 
    //Keeps The GPS Fed To Avoid Checksum Errors. 
    feedgps(); 

} 

void loop() 
{ 
// Function That Returns The GPS Coordinates In DD. 
    GetCoords(); 
    GetDistance(); 
} 

답변

1

내가 지금에 위키 백과에보고하고있어 하버 사인 공식, https://en.wikipedia.org/wiki/Haversine_formula는 다 arcsin있다 (SQRT (DISTANCE_TOTAL)) 당신은 당신의 ATAN2 있습니다.

+0

고마워요, 제가 지금은 3052.94의 가치를 받고 있는데 문제가 있다고 생각합니다. – user7511905