geo_mag_declination.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2014 MAV GEO Library (MAVGEO). 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 MAVGEO 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 
46 #include <cstdint>
47 #include <cmath>
49 
51 #define SAMPLING_RES 10.0f
52 #define SAMPLING_MIN_LAT -60.0f
53 #define SAMPLING_MAX_LAT 60.0f
54 #define SAMPLING_MIN_LON -180.0f
55 #define SAMPLING_MAX_LON 180.0f
56 
57 static const int8_t declination_table[13][37] = \
58 {
59  { 46, 45, 44, 42, 41, 40, 38, 36, 33, 28, 23, 16, 10, 4, -1, -5, -9, -14, -19, -26, -33, -40, -48, -55, -61, -66, -71, -74, -75, -72, -61, -25, 22, 40, 45, 47, 46 },
60  { 30, 30, 30, 30, 29, 29, 29, 29, 27, 24, 18, 11, 3, -3, -9, -12, -15, -17, -21, -26, -32, -39, -45, -51, -55, -57, -56, -53, -44, -31, -14, 0, 13, 21, 26, 29, 30 },
61  { 21, 22, 22, 22, 22, 22, 22, 22, 21, 18, 13, 5, -3, -11, -17, -20, -21, -22, -23, -25, -29, -35, -40, -44, -45, -44, -40, -32, -22, -12, -3, 3, 9, 14, 18, 20, 21 },
62  { 16, 17, 17, 17, 17, 17, 16, 16, 16, 13, 8, 0, -9, -16, -21, -24, -25, -25, -23, -20, -21, -24, -28, -31, -31, -29, -24, -17, -9, -3, 0, 4, 7, 10, 13, 15, 16 },
63  { 12, 13, 13, 13, 13, 13, 12, 12, 11, 9, 3, -4, -12, -19, -23, -24, -24, -22, -17, -12, -9, -10, -13, -17, -18, -16, -13, -8, -3, 0, 1, 3, 6, 8, 10, 12, 12 },
64  { 10, 10, 10, 10, 10, 10, 10, 9, 9, 6, 0, -6, -14, -20, -22, -22, -19, -15, -10, -6, -2, -2, -4, -7, -8, -8, -7, -4, 0, 1, 1, 2, 4, 6, 8, 10, 10 },
65  { 9, 9, 9, 9, 9, 9, 8, 8, 7, 4, -1, -8, -15, -19, -20, -18, -14, -9, -5, -2, 0, 1, 0, -2, -3, -4, -3, -2, 0, 0, 0, 1, 3, 5, 7, 8, 9 },
66  { 8, 8, 8, 9, 9, 9, 8, 8, 6, 2, -3, -9, -15, -18, -17, -14, -10, -6, -2, 0, 1, 2, 2, 0, -1, -1, -2, -1, 0, 0, 0, 0, 1, 3, 5, 7, 8 },
67  { 8, 9, 9, 10, 10, 10, 10, 8, 5, 0, -5, -11, -15, -16, -15, -12, -8, -4, -1, 0, 2, 3, 2, 1, 0, 0, 0, 0, 0, -1, -2, -2, -1, 0, 3, 6, 8 },
68  { 6, 9, 10, 11, 12, 12, 11, 9, 5, 0, -7, -12, -15, -15, -13, -10, -7, -3, 0, 1, 2, 3, 3, 3, 2, 1, 0, 0, -1, -3, -4, -5, -5, -2, 0, 3, 6 },
69  { 5, 8, 11, 13, 15, 15, 14, 11, 5, -1, -9, -14, -17, -16, -14, -11, -7, -3, 0, 1, 3, 4, 5, 5, 5, 4, 3, 1, -1, -4, -7, -8, -8, -6, -2, 1, 5 },
70  { 4, 8, 12, 15, 17, 18, 16, 12, 5, -3, -12, -18, -20, -19, -16, -13, -8, -4, -1, 1, 4, 6, 8, 9, 9, 9, 7, 3, -1, -6, -10, -12, -11, -9, -5, 0, 4 },
71  { 3, 9, 14, 17, 20, 21, 19, 14, 4, -8, -19, -25, -26, -25, -21, -17, -12, -7, -2, 1, 5, 9, 13, 15, 16, 16, 13, 7, 0, -7, -12, -15, -14, -11, -6, -1, 3 },
72 };
73 
74 static float get_lookup_table_val(unsigned lat, unsigned lon);
75 
76 float get_mag_declination(float lat_rad, float lon_rad)
77 {
78  float lat = lat_rad / M_PI * 180.0f;
79  float lon = lon_rad / M_PI * 180.0f;
80 
81  /*
82  * If the values exceed valid ranges, return zero as default
83  * as we have no way of knowing what the closest real value
84  * would be.
85  */
86  if (lat < -90.0f || lat > 90.0f ||
87  lon < -180.0f || lon > 180.0f) {
88  return 0.0f;
89  }
90 
91  /* round down to nearest sampling resolution */
92  int min_lat = (int)(lat / SAMPLING_RES) * SAMPLING_RES;
93  int min_lon = (int)(lon / SAMPLING_RES) * SAMPLING_RES;
94 
95  /* for the rare case of hitting the bounds exactly
96  * the rounding logic wouldn't fit, so enforce it.
97  */
98 
99  /* limit to table bounds - required for maxima even when table spans full globe range */
100  if (lat <= SAMPLING_MIN_LAT) {
101  min_lat = SAMPLING_MIN_LAT;
102  }
103 
104  if (lat >= SAMPLING_MAX_LAT) {
105  min_lat = (int)(lat / SAMPLING_RES) * SAMPLING_RES - SAMPLING_RES;
106  }
107 
108  if (lon <= SAMPLING_MIN_LON) {
109  min_lon = SAMPLING_MIN_LON;
110  }
111 
112  if (lon >= SAMPLING_MAX_LON) {
113  min_lon = (int)(lon / SAMPLING_RES) * SAMPLING_RES - SAMPLING_RES;
114  }
115 
116  /* find index of nearest low sampling point */
117  unsigned min_lat_index = (-(SAMPLING_MIN_LAT) + min_lat) / SAMPLING_RES;
118  unsigned min_lon_index = (-(SAMPLING_MIN_LON) + min_lon) / SAMPLING_RES;
119 
120  float declination_sw = get_lookup_table_val(min_lat_index, min_lon_index);
121  float declination_se = get_lookup_table_val(min_lat_index, min_lon_index + 1);
122  float declination_ne = get_lookup_table_val(min_lat_index + 1, min_lon_index + 1);
123  float declination_nw = get_lookup_table_val(min_lat_index + 1, min_lon_index);
124 
125  /* perform bilinear interpolation on the four grid corners */
126 
127  float declination_min = ((lon - min_lon) / SAMPLING_RES) * (declination_se - declination_sw) + declination_sw;
128  float declination_max = ((lon - min_lon) / SAMPLING_RES) * (declination_ne - declination_nw) + declination_nw;
129 
130  float declination_ret = ((lat - min_lat) / SAMPLING_RES) * (declination_max - declination_min) + declination_min;
131 
132  return declination_ret / 180.0f * M_PI;
133 }
134 
135 float get_lookup_table_val(unsigned lat_index, unsigned lon_index)
136 {
137  return declination_table[lat_index][lon_index];
138 }
#define SAMPLING_MIN_LAT
float get_mag_declination(float lat_rad, float lon_rad)
#define M_PI
#define SAMPLING_MAX_LON
#define SAMPLING_MIN_LON
#define SAMPLING_MAX_LAT
#define SAMPLING_RES
static const int8_t declination_table[13][37]
static float get_lookup_table_val(unsigned lat, unsigned lon)


rotors_gazebo_plugins
Author(s): Fadri Furrer, Michael Burri, Mina Kamel, Janosch Nikolic, Markus Achtelik
autogenerated on Mon Feb 28 2022 23:39:04