LP-311 Remove basic/advanced stabilization tab auto-switch (autotune/txpid lock issues)
[librepilot.git] / ground / gcs / src / libs / utils / worldmagmodel.h
blob447dc001d3936010871d2ff043a2a8c29de2c9a4
1 /**
2 ******************************************************************************
4 * @file worldmagmodel.h
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * Parts by Nokia Corporation (qt-info@nokia.com) Copyright (C) 2009.
7 * @brief
8 * @see The GNU Public License (GPL) Version 3
9 * @defgroup
10 * @{
12 *****************************************************************************/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #ifndef WORLDMAGMODEL_H
30 #define WORLDMAGMODEL_H
32 #include "utils_global.h"
34 // ******************************
35 // internal structure definitions
37 #define WMM_MAX_MODEL_DEGREES 12
38 #define WMM_MAX_SECULAR_VARIATION_MODEL_DEGREES 12
39 #define WMM_NUMTERMS 91 // ((WMM_MAX_MODEL_DEGREES + 1) * (WMM_MAX_MODEL_DEGREES + 2) / 2);
40 #define WMM_NUMPCUP 92 // NUMTERMS + 1
41 #define WMM_NUMPCUPS 13 // WMM_MAX_MODEL_DEGREES + 1
43 typedef struct {
44 double EditionDate;
45 double epoch; // Base time of Geomagnetic model epoch (yrs)
46 char ModelName[20];
47 // double Main_Field_Coeff_G[WMM_NUMTERMS]; // C - Gauss coefficients of main geomagnetic model (nT)
48 // double Main_Field_Coeff_H[WMM_NUMTERMS]; // C - Gauss coefficients of main geomagnetic model (nT)
49 // double Secular_Var_Coeff_G[WMM_NUMTERMS]; // CD - Gauss coefficients of secular geomagnetic model (nT/yr)
50 // double Secular_Var_Coeff_H[WMM_NUMTERMS]; // CD - Gauss coefficients of secular geomagnetic model (nT/yr)
51 int nMax; // Maximum degree of spherical harmonic model
52 int nMaxSecVar; // Maxumum degree of spherical harmonic secular model
53 int SecularVariationUsed; // Whether or not the magnetic secular variation vector will be needed by program
54 } WMMtype_MagneticModel;
56 typedef struct {
57 double a; // semi-major axis of the ellipsoid
58 double b; // semi-minor axis of the ellipsoid
59 double fla; // flattening
60 double epssq; // first eccentricity squared
61 double eps; // first eccentricity
62 double re; // mean radius of ellipsoid
63 } WMMtype_Ellipsoid;
65 typedef struct {
66 double lambda; // longitude
67 double phi; // geodetic latitude
68 double HeightAboveEllipsoid; // height above the ellipsoid (HaE)
69 } WMMtype_CoordGeodetic;
71 typedef struct {
72 double lambda; // longitude
73 double phig; // geocentric latitude
74 double r; // distance from the center of the ellipsoid
75 } WMMtype_CoordSpherical;
77 typedef struct {
78 int Year;
79 int Month;
80 int Day;
81 double DecimalYear;
82 } WMMtype_Date;
84 typedef struct {
85 double Pcup[WMM_NUMPCUP]; // Legendre Function
86 double dPcup[WMM_NUMPCUP]; // Derivative of Lagendre fn
87 } WMMtype_LegendreFunction;
89 typedef struct {
90 double Bx; // North
91 double By; // East
92 double Bz; // Down
93 } WMMtype_MagneticResults;
95 typedef struct {
96 double RelativeRadiusPower[WMM_MAX_MODEL_DEGREES + 1]; // [earth_reference_radius_km / sph. radius ]^n
97 double cos_mlambda[WMM_MAX_MODEL_DEGREES + 1]; // cp(m) - cosine of (m*spherical coord. longitude
98 double sin_mlambda[WMM_MAX_MODEL_DEGREES + 1]; // sp(m) - sine of (m*spherical coord. longitude)
99 } WMMtype_SphericalHarmonicVariables;
101 typedef struct {
102 double Decl; /*1. Angle between the magnetic field vector and true north, positive east */
103 double Incl; /*2. Angle between the magnetic field vector and the horizontal plane, positive down */
104 double F; /*3. Magnetic Field Strength */
105 double H; /*4. Horizontal Magnetic Field Strength */
106 double X; /*5. Northern component of the magnetic field vector */
107 double Y; /*6. Eastern component of the magnetic field vector */
108 double Z; /*7. Downward component of the magnetic field vector */
109 double GV; /*8. The Grid Variation */
110 double Decldot; /*9. Yearly Rate of change in declination */
111 double Incldot; /*10. Yearly Rate of change in inclination */
112 double Fdot; /*11. Yearly rate of change in Magnetic field strength */
113 double Hdot; /*12. Yearly rate of change in horizontal field strength */
114 double Xdot; /*13. Yearly rate of change in the northern component */
115 double Ydot; /*14. Yearly rate of change in the eastern component */
116 double Zdot; /*15. Yearly rate of change in the downward component */
117 double GVdot; /*16. Yearly rate of chnage in grid variation */
118 } WMMtype_GeoMagneticElements;
120 // ******************************
122 namespace Utils {
123 class QTCREATOR_UTILS_EXPORT WorldMagModel {
124 public:
125 WorldMagModel();
127 int GetMagVector(double LLA[3], int Month, int Day, int Year, double Be[3]);
129 private:
130 WMMtype_Ellipsoid Ellip;
131 WMMtype_MagneticModel MagneticModel;
133 double decimal_date;
135 void Initialize();
136 int Geomag(WMMtype_CoordSpherical *CoordSpherical, WMMtype_CoordGeodetic *CoordGeodetic, WMMtype_GeoMagneticElements *GeoMagneticElements);
137 void ComputeSphericalHarmonicVariables(WMMtype_CoordSpherical *CoordSpherical, int nMax, WMMtype_SphericalHarmonicVariables *SphVariables);
138 int AssociatedLegendreFunction(WMMtype_CoordSpherical *CoordSpherical, int nMax, WMMtype_LegendreFunction *LegendreFunction);
139 void Summation(WMMtype_LegendreFunction *LegendreFunction,
140 WMMtype_SphericalHarmonicVariables *SphVariables,
141 WMMtype_CoordSpherical *CoordSpherical,
142 WMMtype_MagneticResults *MagneticResults);
143 void SecVarSummation(WMMtype_LegendreFunction *LegendreFunction,
144 WMMtype_SphericalHarmonicVariables *SphVariables,
145 WMMtype_CoordSpherical *CoordSpherical,
146 WMMtype_MagneticResults *MagneticResults);
147 void RotateMagneticVector(WMMtype_CoordSpherical *CoordSpherical,
148 WMMtype_CoordGeodetic *CoordGeodetic,
149 WMMtype_MagneticResults *MagneticResultsSph,
150 WMMtype_MagneticResults *MagneticResultsGeo);
151 void CalculateGeoMagneticElements(WMMtype_MagneticResults *MagneticResultsGeo, WMMtype_GeoMagneticElements *GeoMagneticElements);
152 void CalculateSecularVariation(WMMtype_MagneticResults *MagneticVariation, WMMtype_GeoMagneticElements *MagneticElements);
153 int PcupHigh(double *Pcup, double *dPcup, double x, int nMax);
154 void PcupLow(double *Pcup, double *dPcup, double x, int nMax);
155 void SummationSpecial(WMMtype_SphericalHarmonicVariables *SphVariables, WMMtype_CoordSpherical *CoordSpherical, WMMtype_MagneticResults *MagneticResults);
156 void SecVarSummationSpecial(WMMtype_SphericalHarmonicVariables *SphVariables, WMMtype_CoordSpherical *CoordSpherical, WMMtype_MagneticResults *MagneticResults);
157 double get_main_field_coeff_g(int index);
158 double get_main_field_coeff_h(int index);
159 double get_secular_var_coeff_g(int index);
160 double get_secular_var_coeff_h(int index);
161 int DateToYear(int month, int day, int year);
162 void GeodeticToSpherical(WMMtype_CoordGeodetic *CoordGeodetic, WMMtype_CoordSpherical *CoordSpherical);
166 // ******************************
168 #endif // ifndef WORLDMAGMODEL_H