2016-01-14 15:30:56 -04:00
|
|
|
#include "Sub.h"
|
2015-12-30 18:57:56 -04:00
|
|
|
|
|
|
|
// position_vector.pde related utility functions
|
|
|
|
|
|
|
|
// position vectors are Vector3f
|
|
|
|
// .x = latitude from home in cm
|
|
|
|
// .y = longitude from home in cm
|
|
|
|
// .z = altitude above home in cm
|
|
|
|
|
|
|
|
// pv_location_to_vector - convert lat/lon coordinates to a position vector
|
2016-01-14 15:30:56 -04:00
|
|
|
Vector3f Sub::pv_location_to_vector(const Location& loc)
|
2015-12-30 18:57:56 -04:00
|
|
|
{
|
|
|
|
const struct Location &origin = inertial_nav.get_origin();
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// pv_alt_above_origin - convert altitude above home to altitude above EKF origin
|
2016-01-14 15:30:56 -04:00
|
|
|
float Sub::pv_alt_above_origin(float alt_above_home_cm)
|
2015-12-30 18:57:56 -04:00
|
|
|
{
|
|
|
|
const struct Location &origin = inertial_nav.get_origin();
|
|
|
|
return alt_above_home_cm + (ahrs.get_home().alt - origin.alt);
|
|
|
|
}
|
|
|
|
|
|
|
|
// pv_get_bearing_cd - return bearing in centi-degrees between two positions
|
2016-01-14 15:30:56 -04:00
|
|
|
float Sub::pv_get_bearing_cd(const Vector3f &origin, const Vector3f &destination)
|
2015-12-30 18:57:56 -04:00
|
|
|
{
|
|
|
|
float bearing = atan2f(destination.y-origin.y, destination.x-origin.x) * DEGX100;
|
|
|
|
if (bearing < 0) {
|
|
|
|
bearing += 36000;
|
|
|
|
}
|
|
|
|
return bearing;
|
|
|
|
}
|
|
|
|
|
|
|
|
// pv_get_horizontal_distance_cm - return distance between two positions in cm
|
2016-01-14 15:30:56 -04:00
|
|
|
float Sub::pv_get_horizontal_distance_cm(const Vector3f &origin, const Vector3f &destination)
|
2015-12-30 18:57:56 -04:00
|
|
|
{
|
2017-02-03 00:18:27 -04:00
|
|
|
return norm(destination.x-origin.x,destination.y-origin.y);
|
2015-12-30 18:57:56 -04:00
|
|
|
}
|