PX4 Firmware
PX4 Autopilot Software http://px4.io
gps_helper.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2012-2018 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 #include "gps_helper.h"
35 #include <math.h>
36 
37 #ifndef M_PI
38 #define M_PI 3.141592653589793238462643383280
39 #endif
40 
41 
42 /**
43  * @file gps_helper.cpp
44  *
45  * @author Thomas Gubler <thomasgubler@student.ethz.ch>
46  * @author Julian Oes <julian@oes.ch>
47  */
48 
49 GPSHelper::GPSHelper(GPSCallbackPtr callback, void *callback_user)
50  : _callback(callback), _callback_user(callback_user)
51 {
52 }
53 
54 void
56 {
57  _rate_count_vel = 0;
60 }
61 
62 void
64 {
65  _rate_vel = _rate_count_vel / (((float)(gps_absolute_time() - _interval_rate_start)) / 1000000.0f);
67 }
68 
69 void GPSHelper::ECEF2lla(double ecef_x, double ecef_y, double ecef_z, double &latitude, double &longitude,
70  float &altitude)
71 {
72  // WGS84 ellipsoid constants
73  constexpr double a = 6378137.; // radius
74  constexpr double e = 8.1819190842622e-2; // eccentricity
75 
76  constexpr double asq = a * a;
77  constexpr double esq = e * e;
78 
79  double x = ecef_x;
80  double y = ecef_y;
81  double z = ecef_z;
82 
83  double b = sqrt(asq * (1. - esq));
84  double bsq = b * b;
85  double ep = sqrt((asq - bsq) / bsq);
86  double p = sqrt(x * x + y * y);
87  double th = atan2(a * z, b * p);
88 
89  longitude = atan2(y, x);
90  double sin_th = sin(th);
91  double cos_th = cos(th);
92  latitude = atan2(z + ep * ep * b * sin_th * sin_th * sin_th, p - esq * a * cos_th * cos_th * cos_th);
93  double sin_lat = sin(latitude);
94  double N = a / sqrt(1. - esq * sin_lat * sin_lat);
95  altitude = (float)(p / cos(latitude) - N);
96 
97  // rad to deg
98  longitude *= 180. / M_PI;
99  latitude *= 180. / M_PI;
100 
101  // correction for altitude near poles left out.
102 }
void resetUpdateRates()
Definition: gps_helper.cpp:55
Dual< Scalar, N > cos(const Dual< Scalar, N > &a)
Definition: Dual.hpp:286
uint8_t _rate_count_vel
Definition: gps_helper.h:265
void storeUpdateRates()
Definition: gps_helper.cpp:63
int(* GPSCallbackPtr)(GPSCallbackType type, void *data1, int data2, void *user)
Callback function for platform-specific stuff.
Definition: gps_helper.h:132
uint64_t _interval_rate_start
Definition: gps_helper.h:270
float _rate_lat_lon
Definition: gps_helper.h:267
uint8_t _rate_count_lat_lon
Definition: gps_helper.h:264
#define M_PI
Definition: gps_helper.cpp:38
GPSHelper(GPSCallbackPtr callback, void *callback_user)
Definition: gps_helper.cpp:49
float _rate_vel
Definition: gps_helper.h:268
#define gps_absolute_time
Get the current time in us.
Definition: definitions.h:57
Dual< Scalar, N > sin(const Dual< Scalar, N > &a)
Definition: Dual.hpp:279
static void ECEF2lla(double ecef_x, double ecef_y, double ecef_z, double &latitude, double &longitude, float &altitude)
Convert an ECEF (Earth Centered Earth Fixed) coordinate to LLA WGS84 (Lat, Lon, Alt).
Definition: gps_helper.cpp:69
Dual< Scalar, N > sqrt(const Dual< Scalar, N > &a)
Definition: Dual.hpp:188
Dual< Scalar, N > atan2(const Dual< Scalar, N > &a, const Dual< Scalar, N > &b)
Definition: Dual.hpp:325