48 #include <mathlib/mathlib.h> 141 const double sin_lat =
sin(lat_rad);
142 const double cos_lat =
cos(lat_rad);
144 const double cos_d_lon =
cos(lon_rad - ref->
lon_rad);
147 const double c =
acos(arg);
174 const double c =
sqrt(x_rad * x_rad + y_rad * y_rad);
177 const double sin_c =
sin(c);
178 const double cos_c =
cos(c);
180 const double lat_rad =
asin(cos_c * ref->
sin_lat + (x_rad * sin_c * ref->
cos_lat) / c);
200 if (lat_0 !=
nullptr) {
204 if (lon_0 !=
nullptr) {
248 *alt = gl_ref.
alt - z;
263 if (alt_0 !=
nullptr) {
275 const double d_lat = lat_next_rad - lat_now_rad;
278 const double a =
sin(d_lat / 2.0) *
sin(d_lat / 2.0) +
sin(d_lon / 2.0) *
sin(d_lon / 2.0) *
cos(lat_now_rad) *
cos(lat_next_rad);
286 double *lat_target,
double *lon_target)
304 double *lat_target,
double *lon_target)
312 *lat_target =
asin(
sin(lat_start_rad) *
cos(radius_ratio) +
cos(lat_start_rad) *
sin(radius_ratio) *
cos((
double)bearing));
313 *lon_target = lon_start_rad +
atan2(
sin((
double)bearing) *
sin(radius_ratio) *
cos(lat_start_rad),
314 cos(radius_ratio) -
sin(lat_start_rad) *
sin(*lat_target));
325 const double cos_lat_next =
cos(lat_next_rad);
330 const float y =
static_cast<float>(
sin(d_lon) * cos_lat_next);
331 const float x =
static_cast<float>(
cos(lat_now_rad) *
sin(lat_next_rad) -
sin(lat_now_rad) * cos_lat_next *
cos(d_lon));
356 double d_lat = lat_next_rad - lat_now_rad;
357 double d_lon = lon_next_rad - lon_now_rad;
376 double lat_start,
double lon_start,
double lat_end,
double lon_end)
382 int return_value = -1;
385 crosstrack_error->
bearing = 0.0f;
390 if (dist_to_end < 0.1
f) {
396 float bearing_diff =
wrap_pi(bearing_track - bearing_end);
399 if (bearing_diff > M_PI_2_F || bearing_diff < -M_PI_2_F) {
405 crosstrack_error->
distance = (dist_to_end) * sinf(bearing_diff);
407 if (sinf(bearing_diff) >= 0) {
420 double lat_center,
double lon_center,
421 float radius,
float arc_start_bearing,
float arc_sweep)
429 float bearing_sector_start = 0.0f;
430 float bearing_sector_end = 0.0f;
433 int return_value = -1;
436 crosstrack_error->
bearing = 0.0f;
443 if (arc_sweep >= 0.0
f) {
444 bearing_sector_start = arc_start_bearing;
445 bearing_sector_end = arc_start_bearing + arc_sweep;
447 if (bearing_sector_end > 2.0
f *
M_PI_F) { bearing_sector_end -= (2 *
M_PI_F); }
450 bearing_sector_end = arc_start_bearing;
451 bearing_sector_start = arc_start_bearing - arc_sweep;
453 if (bearing_sector_start < 0.0
f) { bearing_sector_start += (2 *
M_PI_F); }
456 bool in_sector =
false;
459 if (bearing_sector_end >= bearing_sector_start && bearing_now >= bearing_sector_start
460 && bearing_now <= bearing_sector_end) {
466 if (bearing_sector_end < bearing_sector_start && (bearing_now > bearing_sector_start
467 || bearing_now < bearing_sector_end)) {
477 if (dist_to_center <= radius) {
478 crosstrack_error->
distance = radius - dist_to_center;
482 crosstrack_error->
distance = dist_to_center - radius;
483 crosstrack_error->
bearing = bearing_now;
495 double start_disp_x = (double)radius *
sin((
double)arc_start_bearing);
496 double start_disp_y = (double)radius *
cos((
double)arc_start_bearing);
497 double end_disp_x = (double)radius *
sin((
double)
wrap_pi(arc_start_bearing + arc_sweep));
498 double end_disp_y = (double)radius *
cos((
double)
wrap_pi(arc_start_bearing + arc_sweep));
499 double lon_start = lon_now + start_disp_x / 111111.0;
500 double lat_start = lat_now + start_disp_y *
cos(lat_now) / 111111.0;
501 double lon_end = lon_now + end_disp_x / 111111.0;
502 double lat_end = lat_now + end_disp_y *
cos(lat_now) / 111111.0;
506 if (dist_to_start < dist_to_end) {
507 crosstrack_error->
distance = dist_to_start;
512 crosstrack_error->
distance = dist_to_end;
524 double lat_next,
double lon_next,
float alt_next,
525 float *dist_xy,
float *dist_z)
527 double current_x_rad = lat_next / 180.0 *
M_PI;
528 double current_y_rad = lon_next / 180.0 *
M_PI;
529 double x_rad = lat_now / 180.0 *
M_PI;
530 double y_rad = lon_now / 180.0 *
M_PI;
532 double d_lat = x_rad - current_x_rad;
533 double d_lon = y_rad - current_y_rad;
535 double a =
sin(d_lat / 2.0) *
sin(d_lat / 2.0) +
sin(d_lon / 2.0) *
sin(d_lon / 2.0) *
cos(current_x_rad) *
cos(x_rad);
539 const float dz =
static_cast<float>(alt_now - alt_next);
541 *dist_xy = fabsf(dxy);
544 return sqrtf(dxy * dxy + dz * dz);
548 float x_next,
float y_next,
float z_next,
549 float *dist_xy,
float *dist_z)
551 float dx = x_now - x_next;
552 float dy = y_now - y_next;
553 float dz = z_now - z_next;
555 *dist_xy = sqrtf(dx * dx + dy * dy);
558 return sqrtf(dx * dx + dy * dy + dz * dz);
constexpr _Tp constrain(_Tp val, _Tp min_val, _Tp max_val)
Dual< Scalar, N > acos(const Dual< Scalar, N > &a)
float get_distance_to_point_global_wgs84(double lat_now, double lon_now, float alt_now, double lat_next, double lon_next, float alt_next, float *dist_xy, float *dist_z)
int globallocalconverter_init(double lat_0, double lon_0, float alt_0, uint64_t timestamp)
Initialize the global mapping between global position (spherical) and local position (NED)...
float mavlink_wpm_distance_to_point_local(float x_now, float y_now, float z_now, float x_next, float y_next, float z_next, float *dist_xy, float *dist_z)
Adapter / shim layer for system calls needed by ECL.
int map_projection_global_reference(double *ref_lat_rad, double *ref_lon_rad)
Writes the reference values of the global projection to ref_lat and ref_lon.
static constexpr double CONSTANTS_RADIUS_OF_EARTH
Definition of geo / math functions to perform geodesic calculations.
bool map_projection_initialized(const struct map_projection_reference_s *ref)
Checks if projection given as argument was initialized.
int globallocalconverter_getref(double *lat_0, double *lon_0, float *alt_0)
Get reference position of the global to local converter.
void get_vector_to_next_waypoint(double lat_now, double lon_now, double lat_next, double lon_next, float *v_n, float *v_e)
float get_distance_to_next_waypoint(double lat_now, double lon_now, double lat_next, double lon_next)
Returns the distance to the next waypoint in meters.
int get_distance_to_arc(struct crosstrack_error_s *crosstrack_error, double lat_now, double lon_now, double lat_center, double lon_center, float radius, float arc_start_bearing, float arc_sweep)
bool map_projection_global_initialized()
Checks if global projection was initialized.
Dual< Scalar, N > asin(const Dual< Scalar, N > &a)
constexpr T degrees(T radians)
static struct map_projection_reference_s mp_ref
int map_projection_init_timestamped(struct map_projection_reference_s *ref, double lat_0, double lon_0, uint64_t timestamp)
Initializes the map transformation given by the argument.
uint64_t map_projection_timestamp(const struct map_projection_reference_s *ref)
Get the timestamp of the map projection given by the argument.
Type wrap_2pi(Type x)
Wrap value in range [0, 2π)
#define ecl_absolute_time()
Dual< Scalar, N > cos(const Dual< Scalar, N > &a)
int map_projection_reproject(const struct map_projection_reference_s *ref, float x, float y, double *lat, double *lon)
Transforms a point in the local azimuthal equidistant plane to the geographic coordinate system using...
void waypoint_from_heading_and_distance(double lat_start, double lon_start, float bearing, float dist, double *lat_target, double *lon_target)
Creates a waypoint from given waypoint, distance and bearing see http://www.movable-type.co.uk/scripts/latlong.html.
int map_projection_global_getref(double *lat_0, double *lon_0)
Get reference position of the global map projection.
constexpr T radians(T degrees)
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
void add_vector_to_global_position(double lat_now, double lon_now, float v_n, float v_e, double *lat_res, double *lon_res)
float get_bearing_to_next_waypoint(double lat_now, double lon_now, double lat_next, double lon_next)
Returns the bearing to the next waypoint in radians.
Type wrap_pi(Type x)
Wrap value in range [-π, π)
uint64_t map_projection_global_timestamp()
Get the timestamp of the global map projection.
int map_projection_global_reproject(float x, float y, double *lat, double *lon)
Transforms a point in the local azimuthal equidistant plane to the geographic coordinate system using...
void create_waypoint_from_line_and_dist(double lat_A, double lon_A, double lat_B, double lon_B, float dist, double *lat_target, double *lon_target)
Creates a new waypoint C on the line of two given waypoints (A, B) at certain distance from waypoint ...
int globallocalconverter_toglobal(float x, float y, float z, double *lat, double *lon, float *alt)
Convert from local position coordinates to global position coordinates using the global reference...
static struct globallocal_converter_reference_s gl_ref
int map_projection_reference(const struct map_projection_reference_s *ref, double *ref_lat_rad, double *ref_lon_rad)
Writes the reference values of the projection given by the argument to ref_lat and ref_lon...
int globallocalconverter_tolocal(double lat, double lon, float alt, float *x, float *y, float *z)
Convert from global position coordinates to local position coordinates using the global reference...
int map_projection_project(const struct map_projection_reference_s *ref, double lat, double lon, float *x, float *y)
int map_projection_global_init(double lat_0, double lon_0, uint64_t timestamp)
Initializes the global map transformation.
bool globallocalconverter_initialized()
Checks if globallocalconverter was initialized.
void get_vector_to_next_waypoint_fast(double lat_now, double lon_now, double lat_next, double lon_next, float *v_n, float *v_e)
int get_distance_to_line(struct crosstrack_error_s *crosstrack_error, double lat_now, double lon_now, double lat_start, double lon_start, double lat_end, double lon_end)
int map_projection_global_project(double lat, double lon, float *x, float *y)
Transforms a point in the geographic coordinate system to the local azimuthal equidistant plane using...
int map_projection_init(struct map_projection_reference_s *ref, double lat_0, double lon_0)
Initializes the map transformation given by the argument and sets the timestamp to now...
Dual< Scalar, N > sin(const Dual< Scalar, N > &a)
Dual< Scalar, N > sqrt(const Dual< Scalar, N > &a)
Dual< Scalar, N > atan2(const Dual< Scalar, N > &a, const Dual< Scalar, N > &b)