1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 Module: FGAccelerometer.cpp
5 Date started: 9 July 2005
7 ------------- Copyright (C) 2005 -------------
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
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
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.
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.
26 FUNCTIONAL DESCRIPTION
27 --------------------------------------------------------------------------------
30 --------------------------------------------------------------------------------
32 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
33 COMMENTS, REFERENCES, and NOTES
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
40 #include "FGAccelerometer.h"
44 static const char *IdSrc = "$Id$";
45 static const char *IdHdr = ID_ACCELEROMETER;
47 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
49 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
51 FGAccelerometer::FGAccelerometer(FGFCS* fcs, Element* element) : FGSensor(fcs, element)
53 Propagate = fcs->GetExec()->GetPropagate();
54 MassBalance = fcs->GetExec()->GetMassBalance();
55 Inertial = fcs->GetExec()->GetInertial();
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);}
61 vRadius = MassBalance->StructuralToBody(vLocation);
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;}
67 Element* axis_element = element->FindElement("axis");
69 string sAxis = element->FindElementValue("axis");
70 if (sAxis == "X" || sAxis == "x") {
72 } else if (sAxis == "Y" || sAxis == "y") {
74 } else if (sAxis == "Z" || sAxis == "z") {
77 cerr << " Incorrect/no axis specified for accelerometer; assuming X axis" << endl;
82 CalculateTransformMatrix();
87 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
89 FGAccelerometer::~FGAccelerometer()
94 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
96 bool FGAccelerometer::Run(void )
98 // There is no input assumed. This is a dedicated acceleration sensor.
100 vRadius = MassBalance->StructuralToBody(vLocation);
102 //gravitational forces
103 vAccel = Propagate->GetTl2b() * FGColumnVector3(0, 0, Inertial->gravity());
106 vAccel += (Propagate->GetUVWdot()
107 + Propagate->GetPQRdot() * vRadius
108 + Propagate->GetPQR() * (Propagate->GetPQR() * vRadius));
110 // transform to the specified orientation
111 vAccel = mT * vAccel;
113 Input = vAccel(axis);
115 Output = Input; // perfect accelerometer
117 // Degrade signal as specified
120 Output = PreviousOutput;
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
130 if (fail_low) Output = -HUGE_VAL;
131 if (fail_high) Output = HUGE_VAL;
133 if (bits != 0) Quantize(); // models quantization degradation
134 // if (delay != 0.0) Delay(); // models system signal transport latencies
136 Clip(); // Is it right to clip an accelerometer?
140 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142 void FGAccelerometer::CalculateTransformMatrix(void)
144 double cp,sp,cr,sr,cy,sy;
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));
154 mT(2,1) = sr*sp*cy - cr*sy;
155 mT(2,2) = sr*sp*sy + cr*cy;
158 mT(3,1) = cr*sp*cy + sr*sy;
159 mT(3,2) = cr*sp*sy - sr*cy;
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();
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
176 // 1: This value explicity requests the normal JSBSim
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
187 void FGAccelerometer::Debug(int from)
189 string ax[4] = {"none", "X", "Y", "Z"};
191 if (debug_lvl <= 0) return;
193 if (debug_lvl & 1) { // Standard console startup message output
194 if (from == 0) { // Constructor
195 cout << " Axis: " << ax[axis] << endl;
198 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
199 if (from == 0) cout << "Instantiated: FGAccelerometer" << endl;
200 if (from == 1) cout << "Destroyed: FGAccelerometer" << endl;
202 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
204 if (debug_lvl & 8 ) { // Runtime state variables
206 if (debug_lvl & 16) { // Sanity checking
208 if (debug_lvl & 64) {
209 if (from == 0) { // Constructor
210 cout << IdSrc << endl;
211 cout << IdHdr << endl;