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