2012-07-03 20:19:36 -03:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
/*
|
|
|
|
* location.cpp
|
|
|
|
* Copyright (C) Andrew Tridgell 2011
|
|
|
|
*
|
|
|
|
* This file is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2012-08-17 03:20:14 -03:00
|
|
|
* this module deals with calculations involving struct Location
|
2012-07-03 20:19:36 -03:00
|
|
|
*/
|
2013-10-02 03:08:13 -03:00
|
|
|
#include <AP_HAL.h>
|
2012-09-18 15:08:18 -03:00
|
|
|
#include <stdlib.h>
|
2012-07-03 20:19:36 -03:00
|
|
|
#include "AP_Math.h"
|
|
|
|
|
2012-08-10 22:56:54 -03:00
|
|
|
// radius of earth in meters
|
|
|
|
#define RADIUS_OF_EARTH 6378100
|
|
|
|
|
2013-08-12 00:14:23 -03:00
|
|
|
// scaling factor from 1e-7 degrees to meters at equater
|
|
|
|
// == 1.0e-7 * DEG_TO_RAD * RADIUS_OF_EARTH
|
|
|
|
#define LOCATION_SCALING_FACTOR 0.011131884502145034f
|
|
|
|
// inverse of LOCATION_SCALING_FACTOR
|
|
|
|
#define LOCATION_SCALING_FACTOR_INV 89.83204953368922f
|
|
|
|
|
2013-08-04 21:14:33 -03:00
|
|
|
float longitude_scale(const struct Location &loc)
|
2012-07-03 20:19:36 -03:00
|
|
|
{
|
2012-08-17 03:20:14 -03:00
|
|
|
static int32_t last_lat;
|
|
|
|
static float scale = 1.0;
|
2013-08-04 21:14:33 -03:00
|
|
|
if (labs(last_lat - loc.lat) < 100000) {
|
2012-08-17 03:20:14 -03:00
|
|
|
// we are within 0.01 degrees (about 1km) of the
|
|
|
|
// same latitude. We can avoid the cos() and return
|
|
|
|
// the same scale factor.
|
|
|
|
return scale;
|
|
|
|
}
|
2013-08-12 00:14:23 -03:00
|
|
|
scale = cosf(loc.lat * 1.0e-7f * DEG_TO_RAD);
|
2013-08-04 21:14:33 -03:00
|
|
|
last_lat = loc.lat;
|
2012-08-17 03:20:14 -03:00
|
|
|
return scale;
|
2012-07-03 20:19:36 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-04-11 09:15:49 -03:00
|
|
|
// return distance in meters between two locations
|
2013-08-04 21:14:33 -03:00
|
|
|
float get_distance(const struct Location &loc1, const struct Location &loc2)
|
2012-07-03 20:19:36 -03:00
|
|
|
{
|
2013-08-04 21:14:33 -03:00
|
|
|
float dlat = (float)(loc2.lat - loc1.lat);
|
|
|
|
float dlong = ((float)(loc2.lng - loc1.lng)) * longitude_scale(loc2);
|
2013-08-12 00:14:23 -03:00
|
|
|
return pythagorous2(dlat, dlong) * LOCATION_SCALING_FACTOR;
|
2012-07-03 20:19:36 -03:00
|
|
|
}
|
|
|
|
|
2013-01-26 04:04:12 -04:00
|
|
|
// return distance in centimeters to between two locations
|
2013-08-04 21:14:33 -03:00
|
|
|
uint32_t get_distance_cm(const struct Location &loc1, const struct Location &loc2)
|
2012-07-10 18:49:05 -03:00
|
|
|
{
|
|
|
|
return get_distance(loc1, loc2) * 100;
|
|
|
|
}
|
|
|
|
|
2012-07-03 20:19:36 -03:00
|
|
|
// return bearing in centi-degrees between two locations
|
2013-08-04 21:14:33 -03:00
|
|
|
int32_t get_bearing_cd(const struct Location &loc1, const struct Location &loc2)
|
2012-07-03 20:19:36 -03:00
|
|
|
{
|
2013-08-04 21:14:33 -03:00
|
|
|
int32_t off_x = loc2.lng - loc1.lng;
|
|
|
|
int32_t off_y = (loc2.lat - loc1.lat) / longitude_scale(loc2);
|
2013-01-10 14:42:24 -04:00
|
|
|
int32_t bearing = 9000 + atan2f(-off_y, off_x) * 5729.57795f;
|
2012-08-17 03:20:14 -03:00
|
|
|
if (bearing < 0) bearing += 36000;
|
|
|
|
return bearing;
|
2012-07-03 20:19:36 -03:00
|
|
|
}
|
|
|
|
|
2012-08-16 01:36:55 -03:00
|
|
|
// see if location is past a line perpendicular to
|
|
|
|
// the line between point1 and point2. If point1 is
|
2012-07-03 20:19:36 -03:00
|
|
|
// our previous waypoint and point2 is our target waypoint
|
|
|
|
// then this function returns true if we have flown past
|
|
|
|
// the target waypoint
|
2013-03-26 08:58:54 -03:00
|
|
|
bool location_passed_point(const struct Location &location,
|
|
|
|
const struct Location &point1,
|
|
|
|
const struct Location &point2)
|
2012-07-03 20:19:36 -03:00
|
|
|
{
|
2012-08-17 03:20:14 -03:00
|
|
|
// the 3 points form a triangle. If the angle between lines
|
|
|
|
// point1->point2 and location->point2 is greater than 90
|
|
|
|
// degrees then we have passed the waypoint
|
|
|
|
Vector2f loc1(location.lat, location.lng);
|
|
|
|
Vector2f pt1(point1.lat, point1.lng);
|
|
|
|
Vector2f pt2(point2.lat, point2.lng);
|
|
|
|
float angle = (loc1 - pt2).angle(pt1 - pt2);
|
2012-07-04 01:24:04 -03:00
|
|
|
if (isinf(angle)) {
|
2012-08-16 01:36:55 -03:00
|
|
|
// two of the points are co-located.
|
2012-07-04 01:24:04 -03:00
|
|
|
// If location is equal to point2 then say we have passed the
|
|
|
|
// waypoint, otherwise say we haven't
|
2013-08-04 21:14:33 -03:00
|
|
|
if (get_distance(location, point2) == 0) {
|
2012-07-04 01:24:04 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
} else if (angle == 0) {
|
2012-08-17 03:20:14 -03:00
|
|
|
// if we are exactly on the line between point1 and
|
|
|
|
// point2 then we are past the waypoint if the
|
|
|
|
// distance from location to point1 is greater then
|
|
|
|
// the distance from point2 to point1
|
2013-08-04 21:14:33 -03:00
|
|
|
return get_distance(location, point1) >
|
|
|
|
get_distance(point2, point1);
|
2012-08-16 01:36:55 -03:00
|
|
|
|
2012-08-17 03:20:14 -03:00
|
|
|
}
|
|
|
|
if (degrees(angle) > 90) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2012-07-03 20:19:36 -03:00
|
|
|
}
|
|
|
|
|
2012-08-10 22:56:54 -03:00
|
|
|
/*
|
2012-08-17 03:20:14 -03:00
|
|
|
* extrapolate latitude/longitude given bearing and distance
|
2013-08-12 00:14:23 -03:00
|
|
|
* Note that this function is accurate to about 1mm at a distance of
|
|
|
|
* 100m. This function has the advantage that it works in relative
|
|
|
|
* positions, so it keeps the accuracy even when dealing with small
|
|
|
|
* distances and floating point numbers
|
2012-08-17 03:20:14 -03:00
|
|
|
*/
|
2013-08-04 21:14:33 -03:00
|
|
|
void location_update(struct Location &loc, float bearing, float distance)
|
2012-08-10 22:56:54 -03:00
|
|
|
{
|
2013-08-12 00:14:23 -03:00
|
|
|
float ofs_north = cosf(radians(bearing))*distance;
|
|
|
|
float ofs_east = sinf(radians(bearing))*distance;
|
|
|
|
location_offset(loc, ofs_north, ofs_east);
|
2012-08-10 22:56:54 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-08-17 03:20:14 -03:00
|
|
|
* extrapolate latitude/longitude given distances north and east
|
|
|
|
* This function costs about 80 usec on an AVR2560
|
|
|
|
*/
|
2013-08-04 21:14:33 -03:00
|
|
|
void location_offset(struct Location &loc, float ofs_north, float ofs_east)
|
2012-08-10 22:56:54 -03:00
|
|
|
{
|
|
|
|
if (ofs_north != 0 || ofs_east != 0) {
|
2013-09-15 22:38:55 -03:00
|
|
|
int32_t dlat = ofs_north * LOCATION_SCALING_FACTOR_INV;
|
|
|
|
int32_t dlng = (ofs_east * LOCATION_SCALING_FACTOR_INV) / longitude_scale(loc);
|
2013-08-04 21:14:33 -03:00
|
|
|
loc.lat += dlat;
|
|
|
|
loc.lng += dlng;
|
2012-08-10 22:56:54 -03:00
|
|
|
}
|
|
|
|
}
|
2013-03-28 23:13:37 -03:00
|
|
|
|
2013-08-12 00:14:23 -03:00
|
|
|
/*
|
|
|
|
return the distance in meters in North/East plane as a N/E vector
|
|
|
|
from loc1 to loc2
|
|
|
|
*/
|
|
|
|
Vector2f location_diff(const struct Location &loc1, const struct Location &loc2)
|
|
|
|
{
|
|
|
|
return Vector2f((loc2.lat - loc1.lat) * LOCATION_SCALING_FACTOR,
|
|
|
|
(loc2.lng - loc1.lng) * LOCATION_SCALING_FACTOR * longitude_scale(loc1));
|
|
|
|
}
|
|
|
|
|
2013-03-28 23:13:37 -03:00
|
|
|
/*
|
|
|
|
wrap an angle in centi-degrees to 0..36000
|
|
|
|
*/
|
|
|
|
int32_t wrap_360_cd(int32_t error)
|
|
|
|
{
|
2013-10-02 03:08:13 -03:00
|
|
|
if (error > 360000 || error < -360000) {
|
|
|
|
// for very large numbers use modulus
|
|
|
|
error = error % 36000;
|
|
|
|
}
|
|
|
|
if (error > 36000) error -= 36000;
|
|
|
|
if (error < 0) error += 36000;
|
2013-03-28 23:13:37 -03:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
wrap an angle in centi-degrees to -18000..18000
|
|
|
|
*/
|
|
|
|
int32_t wrap_180_cd(int32_t error)
|
|
|
|
{
|
2013-10-02 03:08:13 -03:00
|
|
|
if (error > 360000 || error < -360000) {
|
|
|
|
// for very large numbers use modulus
|
|
|
|
error = error % 36000;
|
|
|
|
}
|
|
|
|
if (error > 18000) { error -= 36000; }
|
|
|
|
if (error < -18000) { error += 36000; }
|
2013-03-28 23:13:37 -03:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2013-06-01 05:18:50 -03:00
|
|
|
/*
|
|
|
|
wrap an angle defined in radians to -PI ~ PI (equivalent to +- 180 degrees)
|
|
|
|
*/
|
|
|
|
float wrap_PI(float angle_in_radians)
|
|
|
|
{
|
2013-10-02 03:08:13 -03:00
|
|
|
if (angle_in_radians > 10*PI || angle_in_radians < -10*PI) {
|
|
|
|
// for very large numbers use modulus
|
|
|
|
angle_in_radians = fmodf(angle_in_radians, 2*PI);
|
|
|
|
}
|
|
|
|
while (angle_in_radians > PI) angle_in_radians -= 2*PI;
|
|
|
|
while (angle_in_radians < -PI) angle_in_radians += 2*PI;
|
2013-06-01 05:18:50 -03:00
|
|
|
return angle_in_radians;
|
|
|
|
}
|
2013-04-19 20:49:58 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
print a int32_t lat/long in decimal degrees
|
|
|
|
*/
|
|
|
|
void print_latlon(AP_HAL::BetterStream *s, int32_t lat_or_lon)
|
|
|
|
{
|
|
|
|
int32_t dec_portion, frac_portion;
|
|
|
|
int32_t abs_lat_or_lon = labs(lat_or_lon);
|
|
|
|
|
|
|
|
// extract decimal portion (special handling of negative numbers to ensure we round towards zero)
|
|
|
|
dec_portion = abs_lat_or_lon / 10000000UL;
|
|
|
|
|
|
|
|
// extract fractional portion
|
|
|
|
frac_portion = abs_lat_or_lon - dec_portion*10000000UL;
|
|
|
|
|
|
|
|
// print output including the minus sign
|
|
|
|
if( lat_or_lon < 0 ) {
|
|
|
|
s->printf_P(PSTR("-"));
|
|
|
|
}
|
|
|
|
s->printf_P(PSTR("%ld.%07ld"),(long)dec_portion,(long)frac_portion);
|
|
|
|
}
|