2013-08-29 02:34:34 -03:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2012-03-10 15:38:45 -04:00
|
|
|
/*
|
|
|
|
* Adam M Rivera
|
|
|
|
* With direction from: Andrew Tridgell, Jason Short, Justin Beech
|
|
|
|
*
|
|
|
|
* Adapted from: http://www.societyofrobots.com/robotforum/index.php?topic=11855.0
|
|
|
|
* Scott Ferguson
|
|
|
|
* scottfromscott@gmail.com
|
|
|
|
*
|
2012-08-17 03:19:39 -03:00
|
|
|
*/
|
2015-12-23 09:25:34 -04:00
|
|
|
#include "AP_Declination.h"
|
|
|
|
|
2016-03-31 18:43:36 -03:00
|
|
|
#include <cmath>
|
2012-03-10 15:38:45 -04:00
|
|
|
|
2015-08-11 03:28:42 -03:00
|
|
|
#include <AP_Common/AP_Common.h>
|
|
|
|
#include <AP_Math/AP_Math.h>
|
2012-03-11 06:53:22 -03:00
|
|
|
|
2017-08-17 06:09:00 -03:00
|
|
|
/*
|
|
|
|
calculate magnetic field intensity and orientation
|
|
|
|
*/
|
|
|
|
bool AP_Declination::get_mag_field_ef(float latitude_deg, float longitude_deg, float &intensity_gauss, float &declination_deg, float &inclination_deg)
|
2012-03-10 15:38:45 -04:00
|
|
|
{
|
2017-08-17 06:09:00 -03:00
|
|
|
bool valid_input_data = true;
|
|
|
|
|
2022-08-22 08:03:21 -03:00
|
|
|
/* round down to nearest sampling resolution. On some platforms (e.g. clang on macOS),
|
|
|
|
the behaviour of implicit casts from int32 to float can be undefined thus making it explicit here. */
|
|
|
|
float min_lat = float(static_cast<int32_t>(static_cast<int32_t>(floorf(latitude_deg / SAMPLING_RES)) * SAMPLING_RES));
|
|
|
|
float min_lon = float(static_cast<int32_t>(static_cast<int32_t>(floorf(longitude_deg / SAMPLING_RES)) * SAMPLING_RES));
|
2017-08-17 06:09:00 -03:00
|
|
|
|
|
|
|
/* for the rare case of hitting the bounds exactly
|
|
|
|
* the rounding logic wouldn't fit, so enforce it.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* limit to table bounds - required for maxima even when table spans full globe range */
|
|
|
|
if (latitude_deg <= SAMPLING_MIN_LAT) {
|
2022-08-22 08:03:21 -03:00
|
|
|
min_lat = float(static_cast<int32_t>(SAMPLING_MIN_LAT));
|
2017-08-17 06:09:00 -03:00
|
|
|
valid_input_data = false;
|
|
|
|
}
|
2012-03-10 15:38:45 -04:00
|
|
|
|
2017-08-17 06:09:00 -03:00
|
|
|
if (latitude_deg >= SAMPLING_MAX_LAT) {
|
2022-08-22 08:03:21 -03:00
|
|
|
min_lat = float(static_cast<int32_t>(static_cast<int32_t>(latitude_deg / SAMPLING_RES) * SAMPLING_RES - SAMPLING_RES));
|
2017-08-17 06:09:00 -03:00
|
|
|
valid_input_data = false;
|
|
|
|
}
|
2012-03-10 15:38:45 -04:00
|
|
|
|
2017-08-17 06:09:00 -03:00
|
|
|
if (longitude_deg <= SAMPLING_MIN_LON) {
|
2022-08-22 08:03:21 -03:00
|
|
|
min_lon = float(static_cast<int32_t>(SAMPLING_MIN_LON));
|
2017-08-17 06:09:00 -03:00
|
|
|
valid_input_data = false;
|
|
|
|
}
|
2012-03-10 15:38:45 -04:00
|
|
|
|
2017-08-17 06:09:00 -03:00
|
|
|
if (longitude_deg >= SAMPLING_MAX_LON) {
|
2022-08-22 08:03:21 -03:00
|
|
|
min_lon = float(static_cast<int32_t>(static_cast<int32_t>(longitude_deg / SAMPLING_RES) * SAMPLING_RES - SAMPLING_RES));
|
2017-08-17 06:09:00 -03:00
|
|
|
valid_input_data = false;
|
|
|
|
}
|
2012-03-10 15:38:45 -04:00
|
|
|
|
2017-08-17 06:09:00 -03:00
|
|
|
/* find index of nearest low sampling point */
|
2022-03-08 09:42:50 -04:00
|
|
|
uint32_t min_lat_index = constrain_int32(static_cast<uint32_t>((-(SAMPLING_MIN_LAT) + min_lat) / SAMPLING_RES), 0, LAT_TABLE_SIZE - 2);
|
|
|
|
uint32_t min_lon_index = constrain_int32(static_cast<uint32_t>((-(SAMPLING_MIN_LON) + min_lon) / SAMPLING_RES), 0, LON_TABLE_SIZE -2);
|
2012-03-10 15:38:45 -04:00
|
|
|
|
2017-08-17 06:09:00 -03:00
|
|
|
/* calculate intensity */
|
2012-03-15 00:10:01 -03:00
|
|
|
|
2017-08-17 06:09:00 -03:00
|
|
|
float data_sw = intensity_table[min_lat_index][min_lon_index];
|
2022-02-05 08:18:18 -04:00
|
|
|
float data_se = intensity_table[min_lat_index][min_lon_index + 1];
|
2017-08-17 06:09:00 -03:00
|
|
|
float data_ne = intensity_table[min_lat_index + 1][min_lon_index + 1];
|
|
|
|
float data_nw = intensity_table[min_lat_index + 1][min_lon_index];
|
2012-08-17 03:19:39 -03:00
|
|
|
|
2017-08-17 06:09:00 -03:00
|
|
|
/* perform bilinear interpolation on the four grid corners */
|
2012-08-17 03:19:39 -03:00
|
|
|
|
2017-08-17 06:09:00 -03:00
|
|
|
float data_min = ((longitude_deg - min_lon) / SAMPLING_RES) * (data_se - data_sw) + data_sw;
|
|
|
|
float data_max = ((longitude_deg - min_lon) / SAMPLING_RES) * (data_ne - data_nw) + data_nw;
|
2012-08-17 03:19:39 -03:00
|
|
|
|
2017-08-17 06:09:00 -03:00
|
|
|
intensity_gauss = ((latitude_deg - min_lat) / SAMPLING_RES) * (data_max - data_min) + data_min;
|
2012-08-17 03:19:39 -03:00
|
|
|
|
2017-08-17 06:09:00 -03:00
|
|
|
/* calculate declination */
|
2012-08-17 03:19:39 -03:00
|
|
|
|
2017-08-17 06:09:00 -03:00
|
|
|
data_sw = declination_table[min_lat_index][min_lon_index];
|
2022-02-05 08:18:18 -04:00
|
|
|
data_se = declination_table[min_lat_index][min_lon_index + 1];
|
2017-08-17 06:09:00 -03:00
|
|
|
data_ne = declination_table[min_lat_index + 1][min_lon_index + 1];
|
|
|
|
data_nw = declination_table[min_lat_index + 1][min_lon_index];
|
2012-08-17 03:19:39 -03:00
|
|
|
|
2017-08-17 06:09:00 -03:00
|
|
|
/* perform bilinear interpolation on the four grid corners */
|
2012-08-17 03:19:39 -03:00
|
|
|
|
2017-08-17 06:09:00 -03:00
|
|
|
data_min = ((longitude_deg - min_lon) / SAMPLING_RES) * (data_se - data_sw) + data_sw;
|
|
|
|
data_max = ((longitude_deg - min_lon) / SAMPLING_RES) * (data_ne - data_nw) + data_nw;
|
2012-08-17 03:19:39 -03:00
|
|
|
|
2017-08-17 06:09:00 -03:00
|
|
|
declination_deg = ((latitude_deg - min_lat) / SAMPLING_RES) * (data_max - data_min) + data_min;
|
2012-08-17 03:19:39 -03:00
|
|
|
|
2017-08-17 06:09:00 -03:00
|
|
|
/* calculate inclination */
|
2012-08-17 03:19:39 -03:00
|
|
|
|
2017-08-17 06:09:00 -03:00
|
|
|
data_sw = inclination_table[min_lat_index][min_lon_index];
|
2022-02-05 08:18:18 -04:00
|
|
|
data_se = inclination_table[min_lat_index][min_lon_index + 1];
|
2017-08-17 06:09:00 -03:00
|
|
|
data_ne = inclination_table[min_lat_index + 1][min_lon_index + 1];
|
|
|
|
data_nw = inclination_table[min_lat_index + 1][min_lon_index];
|
2012-08-17 03:19:39 -03:00
|
|
|
|
2017-08-17 06:09:00 -03:00
|
|
|
/* perform bilinear interpolation on the four grid corners */
|
2012-08-17 03:19:39 -03:00
|
|
|
|
2017-08-17 06:09:00 -03:00
|
|
|
data_min = ((longitude_deg - min_lon) / SAMPLING_RES) * (data_se - data_sw) + data_sw;
|
|
|
|
data_max = ((longitude_deg - min_lon) / SAMPLING_RES) * (data_ne - data_nw) + data_nw;
|
2012-08-17 03:19:39 -03:00
|
|
|
|
2017-08-17 06:09:00 -03:00
|
|
|
inclination_deg = ((latitude_deg - min_lat) / SAMPLING_RES) * (data_max - data_min) + data_min;
|
2012-08-17 03:19:39 -03:00
|
|
|
|
2017-08-17 06:09:00 -03:00
|
|
|
return valid_input_data;
|
|
|
|
}
|
2012-08-17 03:19:39 -03:00
|
|
|
|
|
|
|
|
2017-08-17 06:09:00 -03:00
|
|
|
/*
|
|
|
|
calculate magnetic field intensity and orientation
|
|
|
|
*/
|
|
|
|
float AP_Declination::get_declination(float latitude_deg, float longitude_deg)
|
|
|
|
{
|
|
|
|
float declination_deg=0, inclination_deg=0, intensity_gauss=0;
|
|
|
|
|
|
|
|
get_mag_field_ef(latitude_deg, longitude_deg, intensity_gauss, declination_deg, inclination_deg);
|
|
|
|
|
|
|
|
return declination_deg;
|
2012-03-29 23:23:18 -03:00
|
|
|
}
|
2017-08-17 06:09:00 -03:00
|
|
|
|
2019-06-01 08:01:11 -03:00
|
|
|
/*
|
|
|
|
get earth field as a Vector3f in Gauss given a Location
|
|
|
|
*/
|
|
|
|
Vector3f AP_Declination::get_earth_field_ga(const Location &loc)
|
|
|
|
{
|
|
|
|
float declination_deg=0, inclination_deg=0, intensity_gauss=0;
|
|
|
|
get_mag_field_ef(loc.lat*1.0e-7f, loc.lng*1.0e-7f, intensity_gauss, declination_deg, inclination_deg);
|
|
|
|
|
|
|
|
// create earth field
|
|
|
|
Vector3f mag_ef = Vector3f(intensity_gauss, 0.0, 0.0);
|
|
|
|
Matrix3f R;
|
|
|
|
|
|
|
|
R.from_euler(0.0f, -ToRad(inclination_deg), ToRad(declination_deg));
|
|
|
|
mag_ef = R * mag_ef;
|
|
|
|
return mag_ef;
|
|
|
|
}
|