]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/flight_control/FGAccelerometer.cpp
latest changes for JSBSim (1.0 prerelease)
[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
52 FGAccelerometer::FGAccelerometer(FGFCS* fcs, Element* element) : FGSensor(fcs, element)
53 {
54   Propagate = fcs->GetExec()->GetPropagate();
55   MassBalance = fcs->GetExec()->GetMassBalance();
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   vAccel = mT * (Propagate->GetUVWdot()
103                  + Propagate->GetPQRdot() * vRadius
104                  + Propagate->GetPQR() * (Propagate->GetPQR() * vRadius));
105   
106   Input = vAccel(axis);
107
108   Output = Input; // perfect accelerometer
109
110   // Degrade signal as specified
111
112   if (fail_stuck) {
113     Output = PreviousOutput;
114     return true;
115   }
116
117   if (lag != 0.0)            Lag();       // models accelerometer lag
118   if (noise_variance != 0.0) Noise();     // models noise
119   if (drift_rate != 0.0)     Drift();     // models drift over time
120   if (bias != 0.0)           Bias();      // models a finite bias
121
122   if (fail_low)  Output = -HUGE_VAL;
123   if (fail_high) Output =  HUGE_VAL;
124
125   if (bits != 0)             Quantize();  // models quantization degradation
126 //  if (delay != 0.0)          Delay();     // models system signal transport latencies
127
128   Clip(); // Is it right to clip an accelerometer?
129   return true;
130 }
131
132 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
133
134 void FGAccelerometer::CalculateTransformMatrix(void)
135 {
136   double cp,sp,cr,sr,cy,sy;
137
138   cp=cos(vOrient(ePitch)); sp=sin(vOrient(ePitch));
139   cr=cos(vOrient(eRoll));  sr=sin(vOrient(eRoll));
140   cy=cos(vOrient(eYaw));   sy=sin(vOrient(eYaw));
141
142   mT(1,1) =  cp*cy;
143   mT(1,2) =  cp*sy;
144   mT(1,3) = -sp;
145
146   mT(2,1) = sr*sp*cy - cr*sy;
147   mT(2,2) = sr*sp*sy + cr*cy;
148   mT(2,3) = sr*cp;
149
150   mT(3,1) = cr*sp*cy + sr*sy;
151   mT(3,2) = cr*sp*sy - sr*cy;
152   mT(3,3) = cr*cp;
153   
154   // This transform is different than for FGForce, where we want a native nozzle
155   // force in body frame. Here we calculate the body frame accel and want it in
156   // the transformed accelerometer frame. So, the next line is commented out.
157   // mT = mT.Inverse();
158 }
159
160 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
161 //    The bitmasked value choices are as follows:
162 //    unset: In this case (the default) JSBSim would only print
163 //       out the normally expected messages, essentially echoing
164 //       the config files as they are read. If the environment
165 //       variable is not set, debug_lvl is set to 1 internally
166 //    0: This requests JSBSim not to output any messages
167 //       whatsoever.
168 //    1: This value explicity requests the normal JSBSim
169 //       startup messages
170 //    2: This value asks for a message to be printed out when
171 //       a class is instantiated
172 //    4: When this value is set, a message is displayed when a
173 //       FGModel object executes its Run() method
174 //    8: When this value is set, various runtime state variables
175 //       are printed out periodically
176 //    16: When set various parameters are sanity checked and
177 //       a message is printed out when they go out of bounds
178
179 void FGAccelerometer::Debug(int from)
180 {
181   string ax[4] = {"none", "X", "Y", "Z"};
182
183   if (debug_lvl <= 0) return;
184
185   if (debug_lvl & 1) { // Standard console startup message output
186     if (from == 0) { // Constructor
187       cout << "        Axis: " << ax[axis] << endl;
188     }
189   }
190   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
191     if (from == 0) cout << "Instantiated: FGAccelerometer" << endl;
192     if (from == 1) cout << "Destroyed:    FGAccelerometer" << endl;
193   }
194   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
195   }
196   if (debug_lvl & 8 ) { // Runtime state variables
197   }
198   if (debug_lvl & 16) { // Sanity checking
199   }
200   if (debug_lvl & 64) {
201     if (from == 0) { // Constructor
202       cout << IdSrc << endl;
203       cout << IdHdr << endl;
204     }
205   }
206 }
207 }