JSBSim Flight Dynamics Model 1.3.0 (09 Apr 2026)
An Open Source Flight Dynamics and Control Software Library in C++
Loading...
Searching...
No Matches
FGMagnetometer.cpp
1/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3 Module: FGMagnetometer.cpp
4 Author: Matthew Chave
5 Date started: August 2009
6
7 ------------- Copyright (C) 2009 -------------
8
9 This program is free software; you can redistribute it and/or modify it under
10 the terms of the GNU Lesser General Public License as published by the Free
11 Software Foundation; either version 2 of the License, or (at your option) any
12 later version.
13
14 This program is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
17 details.
18
19 You should have received a copy of the GNU Lesser General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc., 59
21 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22
23 Further information about the GNU Lesser General Public License can also be
24 found on the world wide web at http://www.gnu.org.
25
26FUNCTIONAL DESCRIPTION
27--------------------------------------------------------------------------------
28
29HISTORY
30--------------------------------------------------------------------------------
31
32%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
33COMMENTS, REFERENCES, and NOTES
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35
36%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37INCLUDES
38%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
39
40#include "FGMagnetometer.h"
41#include "simgear/magvar/coremag.hxx"
42#include "models/FGFCS.h"
43#include "models/FGMassBalance.h"
44
45using namespace std;
46
47namespace JSBSim {
48
49/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
50CLASS IMPLEMENTATION
51%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
52
53
54FGMagnetometer::FGMagnetometer(FGFCS* fcs, Element* element)
55 : FGSensor(fcs, element),
56 FGSensorOrientation(element),
57 counter(0), INERTIAL_UPDATE_RATE(1000)
58{
59 Propagate = fcs->GetExec()->GetPropagate();
60 MassBalance = fcs->GetExec()->GetMassBalance();
61 Inertial = fcs->GetExec()->GetInertial();
62
63 Element* location_element = element->FindElement("location");
64 if (location_element)
65 vLocation = location_element->FindElementTripletConvertTo("IN");
66 else {
67 XMLLogException err(element);
68 err << "No location given for magnetometer.\n";
69 throw err;
70 }
71
72 vRadius = MassBalance->StructuralToBody(vLocation);
73
74 //assuming date wont significantly change over a flight to affect mag field
75 //would be better to get the date from the sim if its simulated...
76 time_t rawtime;
77 time( &rawtime );
78 struct tm ptm;
79 #if defined(_MSC_VER) || defined(__MINGW32__)
80 gmtime_s(&ptm, &rawtime);
81 #else
82 gmtime_r(&rawtime, &ptm);
83 #endif
84
85 int year = ptm.tm_year;
86 if(year>100)
87 {
88 year-= 100;
89 }
90 //the months here are zero based TODO find out if the function expects 1s based
91 date = (yymmdd_to_julian_days(ptm.tm_year, ptm.tm_mon, ptm.tm_mday)); //Julian 1950-2049 yy,mm,dd
92 updateInertialMag();
93
94 Debug(0);
95}
96//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
97
98FGMagnetometer::~FGMagnetometer()
99{
100 Debug(1);
101}
102
103//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
104
105void FGMagnetometer::ResetPastStates(void)
106{
107 FGSensor::ResetPastStates();
108 counter = 0;
109}
110
111//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
112
113void FGMagnetometer::updateInertialMag(void)
114{
115 if (counter++ % INERTIAL_UPDATE_RATE == 0)//dont need to update every iteration
116 {
117 usedLat = (Propagate->GetGeodLatitudeRad());//radians, N and E lat and long are positive, S and W negative
118 usedLon = (Propagate->GetLongitude());//radians
119 usedAlt = (Propagate->GetGeodeticAltitude()*fttom*0.001);//km
120
121 //this should be done whenever the position changes significantly (in nTesla)
122 calc_magvar( usedLat, usedLon, usedAlt, date, field );
123 }
124}
125
126//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
127
128bool FGMagnetometer::Run(void )
129{
130 // There is no input assumed. This is a dedicated magnetic field sensor.
131
132 vRadius = MassBalance->StructuralToBody(vLocation);
133
134 updateInertialMag();
135
136 // Inertial magnetic field rotated to the body frame
137 vMag = Propagate->GetTl2b() * FGColumnVector3(field[3], field[4], field[5]);
138
139 // Allow for sensor orientation
140 vMag = mT * vMag;
141
142 Input = vMag(axis);
143
144 ProcessSensorSignal();
145
146 SetOutput();
147
148 return true;
149}
150
151//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
152// The bitmasked value choices are as follows:
153// unset: In this case (the default) JSBSim would only print
154// out the normally expected messages, essentially echoing
155// the config files as they are read. If the environment
156// variable is not set, debug_lvl is set to 1 internally
157// 0: This requests JSBSim not to output any messages
158// whatsoever.
159// 1: This value explicitly requests the normal JSBSim
160// startup messages
161// 2: This value asks for a message to be printed out when
162// a class is instantiated
163// 4: When this value is set, a message is displayed when a
164// FGModel object executes its Run() method
165// 8: When this value is set, various runtime state variables
166// are printed out periodically
167// 16: When set various parameters are sanity checked and
168// a message is printed out when they go out of bounds
169
170void FGMagnetometer::Debug(int from)
171{
172 string ax[4] = {"none", "X", "Y", "Z"};
173
174 if (debug_lvl <= 0) return;
175
176 if (debug_lvl & 1) { // Standard console startup message output
177 FGLogging log(LogLevel::DEBUG);
178 if (from == 0) { // Constructor
179 log << " Axis: " << ax[axis] << "\n";
180 }
181 }
182 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
183 FGLogging log(LogLevel::DEBUG);
184 if (from == 0) log << "Instantiated: FGMagnetometer\n";
185 if (from == 1) log << "Destroyed: FGMagnetometer\n";
186 }
187 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
188 }
189 if (debug_lvl & 8 ) { // Runtime state variables
190 }
191 if (debug_lvl & 16) { // Sanity checking
192 }
193 if (debug_lvl & 64) {
194 if (from == 0) { // Constructor
195 }
196 }
197}
198}
Main namespace for the JSBSim Flight Dynamics Model.
Definition FGFDMExec.cpp:71