2013-02-25 04:50:56 -04:00
|
|
|
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
|
|
|
// position_vector.pde related utility functions
|
|
|
|
|
2014-05-27 04:30:32 -03:00
|
|
|
// position vectors are Vector3f
|
2013-02-25 04:50:56 -04:00
|
|
|
// .x = latitude from home in cm
|
|
|
|
// .y = longitude from home in cm
|
2013-03-20 10:29:08 -03:00
|
|
|
// .z = altitude above home in cm
|
2013-02-25 04:50:56 -04:00
|
|
|
|
2014-11-04 22:39:28 -04:00
|
|
|
// pv_location_to_vector - convert lat/lon coordinates to a position vector
|
2014-05-27 04:30:32 -03:00
|
|
|
Vector3f pv_location_to_vector(const Location& loc)
|
2013-03-20 10:29:08 -03:00
|
|
|
{
|
2015-02-02 22:12:16 -04:00
|
|
|
const struct Location &origin = inertial_nav.get_origin();
|
2015-04-13 18:12:10 -03:00
|
|
|
float alt_above_origin = pv_alt_above_origin(loc.alt); // convert alt-relative-to-home to alt-relative-to-origin
|
|
|
|
return Vector3f((loc.lat-origin.lat) * LATLON_TO_CM, (loc.lng-origin.lng) * LATLON_TO_CM * scaleLongDown, alt_above_origin);
|
2013-02-25 04:50:56 -04:00
|
|
|
}
|
|
|
|
|
2014-11-04 22:39:28 -04:00
|
|
|
// pv_location_to_vector_with_default - convert lat/lon coordinates to a position vector,
|
2014-11-05 22:39:53 -04:00
|
|
|
// defaults to the default_posvec's lat/lon and alt if the provided lat/lon or alt are zero
|
|
|
|
Vector3f pv_location_to_vector_with_default(const Location& loc, const Vector3f& default_posvec)
|
2014-11-04 22:39:28 -04:00
|
|
|
{
|
|
|
|
Vector3f pos = pv_location_to_vector(loc);
|
|
|
|
|
|
|
|
// set target altitude to default if not provided
|
|
|
|
if (loc.alt == 0) {
|
2014-11-05 22:39:53 -04:00
|
|
|
pos.z = default_posvec.z;
|
2014-11-04 22:39:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// set target position to default if not provided
|
|
|
|
if (loc.lat == 0 && loc.lng == 0) {
|
2014-11-05 22:39:53 -04:00
|
|
|
pos.x = default_posvec.x;
|
|
|
|
pos.y = default_posvec.y;
|
2014-11-04 22:39:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
2015-02-10 08:26:53 -04:00
|
|
|
// pv_alt_above_origin - convert altitude above home to altitude above EKF origin
|
|
|
|
float pv_alt_above_origin(float alt_above_home_cm)
|
|
|
|
{
|
|
|
|
const struct Location &origin = inertial_nav.get_origin();
|
|
|
|
return alt_above_home_cm + (ahrs.get_home().alt - origin.alt);
|
|
|
|
}
|
|
|
|
|
|
|
|
// pv_alt_above_home - convert altitude above EKF origin to altitude above home
|
|
|
|
float pv_alt_above_home(float alt_above_origin_cm)
|
|
|
|
{
|
|
|
|
const struct Location &origin = inertial_nav.get_origin();
|
|
|
|
return alt_above_origin_cm + (origin.alt - ahrs.get_home().alt);
|
|
|
|
}
|
|
|
|
|
2013-03-20 10:29:08 -03:00
|
|
|
// pv_get_bearing_cd - return bearing in centi-degrees between two positions
|
2013-08-13 13:22:28 -03:00
|
|
|
float pv_get_bearing_cd(const Vector3f &origin, const Vector3f &destination)
|
2013-02-25 04:50:56 -04:00
|
|
|
{
|
2015-02-02 20:45:37 -04:00
|
|
|
float bearing = fast_atan2(destination.y-origin.y, destination.x-origin.x) * DEGX100;
|
2013-02-25 04:50:56 -04:00
|
|
|
if (bearing < 0) {
|
|
|
|
bearing += 36000;
|
|
|
|
}
|
|
|
|
return bearing;
|
2013-08-04 21:19:19 -03:00
|
|
|
}
|
2014-06-02 06:03:02 -03:00
|
|
|
|
|
|
|
// pv_get_horizontal_distance_cm - return distance between two positions in cm
|
|
|
|
float pv_get_horizontal_distance_cm(const Vector3f &origin, const Vector3f &destination)
|
|
|
|
{
|
|
|
|
return pythagorous2(destination.x-origin.x,destination.y-origin.y);
|
|
|
|
}
|