]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/flight_control/FGAccelerometer.cpp
Merge branch 'jsd/atmos' into topic/atmos-merge
[flightgear.git] / src / FDM / JSBSim / models / flight_control / FGAccelerometer.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGAccelerometer.cpp
4  Author:       Jon Berndt
5  Date started: 9 July 2005
6
7  ------------- Copyright (C) 2005 -------------
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 "FGAccelerometer.h"
41
42 namespace JSBSim {
43
44 static const char *IdSrc = "$Id$";
45 static const char *IdHdr = ID_ACCELEROMETER;
46
47 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
48 CLASS IMPLEMENTATION
49 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
50
51 FGAccelerometer::FGAccelerometer(FGFCS* fcs, Element* element) : FGSensor(fcs, element)
52 {
53   Propagate = fcs->GetExec()->GetPropagate();
54   MassBalance = fcs->GetExec()->GetMassBalance();
55   Inertial = fcs->GetExec()->GetInertial();
56   
57   Element* location_element = element->FindElement("location");
58   if (location_element) vLocation = location_element->FindElementTripletConvertTo("IN");
59   else {cerr << "No location given for accelerometer. " << endl; exit(-1);}
60
61   vRadius = MassBalance->StructuralToBody(vLocation);
62
63   Element* orient_element = element->FindElement("orientation");
64   if (orient_element) vOrient = orient_element->FindElementTripletConvertTo("RAD");
65   else {cerr << "No orientation given for accelerometer. " << endl;}
66
67   Element* axis_element = element->FindElement("axis");
68   if (axis_element) {
69     string sAxis = element->FindElementValue("axis");
70     if (sAxis == "X" || sAxis == "x") {
71       axis = 1;
72     } else if (sAxis == "Y" || sAxis == "y") {
73       axis = 2;
74     } else if (sAxis == "Z" || sAxis == "z") {
75       axis = 3;
76     } else {
77       cerr << "  Incorrect/no axis specified for accelerometer; assuming X axis" << endl;
78       axis = 1;
79     }
80   }
81
82   CalculateTransformMatrix();
83
84   Debug(0);
85 }
86
87 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88
89 FGAccelerometer::~FGAccelerometer()
90 {
91   Debug(1);
92 }
93
94 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
95
96 bool FGAccelerometer::Run(void )
97 {
98   // There is no input assumed. This is a dedicated acceleration sensor.
99   
100   vRadius = MassBalance->StructuralToBody(vLocation);
101     
102   //gravitational forces
103   vAccel = Propagate->GetTl2b() * FGColumnVector3(0, 0, Inertial->gravity());
104
105   //aircraft forces
106   vAccel += (Propagate->GetUVWdot()
107               + Propagate->GetPQRdot() * vRadius
108               + Propagate->GetPQR() * (Propagate->GetPQR() * vRadius));
109
110   // transform to the specified orientation
111   vAccel = mT * vAccel;
112
113   Input = vAccel(axis);
114
115   Output = Input; // perfect accelerometer
116
117   // Degrade signal as specified
118
119   if (fail_stuck) {
120     Output = PreviousOutput;
121     return true;
122   }
123
124   if (lag != 0.0)            Lag();       // models accelerometer lag
125   if (noise_variance != 0.0) Noise();     // models noise
126   if (drift_rate != 0.0)     Drift();     // models drift over time
127   if (bias != 0.0)           Bias();      // models a finite bias
128   if (gain != 0.0)           Gain();      // models a gain
129
130   if (fail_low)  Output = -HUGE_VAL;
131   if (fail_high) Output =  HUGE_VAL;
132
133   if (bits != 0)             Quantize();  // models quantization degradation
134 //  if (delay != 0.0)          Delay();     // models system signal transport latencies
135
136   Clip(); // Is it right to clip an accelerometer?
137   return true;
138 }
139
140 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141
142 void FGAccelerometer::CalculateTransformMatrix(void)
143 {
144   double cp,sp,cr,sr,cy,sy;
145
146   cp=cos(vOrient(ePitch)); sp=sin(vOrient(ePitch));
147   cr=cos(vOrient(eRoll));  sr=sin(vOrient(eRoll));
148   cy=cos(vOrient(eYaw));   sy=sin(vOrient(eYaw));
149
150   mT(1,1) =  cp*cy;
151   mT(1,2) =  cp*sy;
152   mT(1,3) = -sp;
153
154   mT(2,1) = sr*sp*cy - cr*sy;
155   mT(2,2) = sr*sp*sy + cr*cy;
156   mT(2,3) = sr*cp;
157
158   mT(3,1) = cr*sp*cy + sr*sy;
159   mT(3,2) = cr*sp*sy - sr*cy;
160   mT(3,3) = cr*cp;
161
162   // This transform is different than for FGForce, where we want a native nozzle
163   // force in body frame. Here we calculate the body frame accel and want it in
164   // the transformed accelerometer frame. So, the next line is commented out.
165   // mT = mT.Inverse();
166 }
167
168 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
169 //    The bitmasked value choices are as follows:
170 //    unset: In this case (the default) JSBSim would only print
171 //       out the normally expected messages, essentially echoing
172 //       the config files as they are read. If the environment
173 //       variable is not set, debug_lvl is set to 1 internally
174 //    0: This requests JSBSim not to output any messages
175 //       whatsoever.
176 //    1: This value explicity requests the normal JSBSim
177 //       startup messages
178 //    2: This value asks for a message to be printed out when
179 //       a class is instantiated
180 //    4: When this value is set, a message is displayed when a
181 //       FGModel object executes its Run() method
182 //    8: When this value is set, various runtime state variables
183 //       are printed out periodically
184 //    16: When set various parameters are sanity checked and
185 //       a message is printed out when they go out of bounds
186
187 void FGAccelerometer::Debug(int from)
188 {
189   string ax[4] = {"none", "X", "Y", "Z"};
190
191   if (debug_lvl <= 0) return;
192
193   if (debug_lvl & 1) { // Standard console startup message output
194     if (from == 0) { // Constructor
195       cout << "        Axis: " << ax[axis] << endl;
196     }
197   }
198   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
199     if (from == 0) cout << "Instantiated: FGAccelerometer" << endl;
200     if (from == 1) cout << "Destroyed:    FGAccelerometer" << endl;
201   }
202   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
203   }
204   if (debug_lvl & 8 ) { // Runtime state variables
205   }
206   if (debug_lvl & 16) { // Sanity checking
207   }
208   if (debug_lvl & 64) {
209     if (from == 0) { // Constructor
210       cout << IdSrc << endl;
211       cout << IdHdr << endl;
212     }
213   }
214 }
215 }