]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/filtersjb/FGKinemat.cpp
More tweaks to the automake/conf configuration scripts.
[flightgear.git] / src / FDM / JSBSim / filtersjb / FGKinemat.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2  
3  Module:       FGKinemat.cpp
4  Author:       Tony Peden, for flight control system authored by Jon S. Berndt
5  Date started: 12/02/01
6  
7  ------------- Copyright (C) 2000 Anthony K. Peden -------------
8  
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU 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 General Public License for more
17  details.
18  
19  You should have received a copy of the GNU 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 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 "FGKinemat.h"
41
42 static const char *IdSrc = "$Id$";
43 static const char *IdHdr = ID_FLAPS;
44
45 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
46 CLASS IMPLEMENTATION
47 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
48
49 FGKinemat::FGKinemat(FGFCS* fcs, FGConfigFile* AC_cfg) : FGFCSComponent(fcs),
50                                                          AC_cfg(AC_cfg)
51 {
52   string token,sOutputIdx;
53   double tmpDetent;
54   double tmpTime;
55
56   Detents.clear();
57   TransitionTimes.clear();
58   
59   OutputPct=0;
60   InTransit=0;
61
62   Type = AC_cfg->GetValue("TYPE");
63   Name = AC_cfg->GetValue("NAME");
64   AC_cfg->GetNextConfigLine();
65
66   while ((token = AC_cfg->GetValue()) != string("/COMPONENT")) {
67     *AC_cfg >> token;
68     if (token == "ID") {
69       *AC_cfg >> ID;
70     } else if (token == "INPUT") {
71       token = AC_cfg->GetValue("INPUT");
72       if (token.find("FG_") != token.npos) {
73         *AC_cfg >> token;
74         InputNode = PropertyManager->GetNode( 
75                     fcs->GetState()->GetPropertyName(token) );
76         InputType = itPilotAC;
77       } else {
78         *AC_cfg >> InputIdx;
79         InputType = itFCS;
80       }
81     } else if ( token == "DETENTS" ) {
82       *AC_cfg >> NumDetents;
83       for (int i=0;i<NumDetents;i++) {
84         *AC_cfg >> tmpDetent;
85         *AC_cfg >> tmpTime;
86         Detents.push_back(tmpDetent);
87         TransitionTimes.push_back(tmpTime);
88       }
89     } else if (token == "OUTPUT") {
90
91       IsOutput = true;
92       *AC_cfg >> sOutputIdx;
93       OutputNode = PropertyManager->GetNode( 
94                      fcs->GetState()->GetPropertyName(sOutputIdx) );
95     }
96   }
97
98   Debug(0);
99 }
100
101 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
102
103 FGKinemat::~FGKinemat()
104 {
105   Debug(1);
106 }
107
108 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109
110 bool FGKinemat::Run(void ) {
111   double dt=fcs->GetState()->Getdt();
112   double output_transit_rate=0;
113
114   FGFCSComponent::Run(); // call the base class for initialization of Input
115   InputCmd = Input*Detents[NumDetents-1];
116   OutputPos = OutputNode->getDoubleValue();
117   
118   if (InputCmd < Detents[0]) {
119     fi=0;
120     InputCmd=Detents[0];
121     lastInputCmd=InputCmd;
122     OutputPos=Detents[0];
123     Output=OutputPos;
124   } else if (InputCmd > Detents[NumDetents-1]) {
125     fi=NumDetents-1;
126     InputCmd=Detents[fi];
127     lastInputCmd=InputCmd;
128     OutputPos=Detents[fi];
129     Output=OutputPos;
130   } else {
131     //cout << "FGKinemat::Run Handle: " << InputCmd << " Position: " << OutputPos << endl;
132     if (dt <= 0)
133       OutputPos=InputCmd;
134     else {
135       if (InputCmd != lastInputCmd) {
136         InTransit=1;
137       }
138       //cout << "FGKinemat::Run, InTransit: " << InTransit << endl;
139       if (InTransit) {
140         //fprintf(stderr,"InputCmd: %g, OutputPos: %g\n",InputCmd,OutputPos);
141         fi=0;
142         while (Detents[fi] < InputCmd) {
143           fi++;
144         }
145         if (OutputPos < InputCmd) {
146           if (TransitionTimes[fi] > 0)
147             output_transit_rate=(Detents[fi] - Detents[fi-1])/TransitionTimes[fi];
148           else
149             output_transit_rate=(Detents[fi] - Detents[fi-1])/5;
150           //cout << "FGKinemat::Run, output_transit_rate: " << output_transit_rate << endl;  
151         } else {
152           if (TransitionTimes[fi+1] > 0)
153             output_transit_rate=(Detents[fi] - Detents[fi+1])/TransitionTimes[fi+1];
154           else
155             output_transit_rate=(Detents[fi] - Detents[fi+1])/5;
156         }
157         if (fabs(OutputPos - InputCmd) > fabs(dt*output_transit_rate) ) {
158           OutputPos+=output_transit_rate*dt;
159           //cout << "FGKinemat::Run, OutputPos: " << OutputPos 
160           //     << " dt: " << dt << endl;
161         } else {
162           InTransit=0;
163           OutputPos=InputCmd;
164         }
165       }
166     }
167     lastInputCmd = InputCmd;
168     Output = OutputPos;
169   }
170   
171   if ( Detents[NumDetents-1] > 0 ) {
172     OutputPct = Output / Detents[NumDetents-1];
173   }
174   
175   if (IsOutput) SetOutput();
176
177   return true;
178 }
179
180 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
181 //    The bitmasked value choices are as follows:
182 //    unset: In this case (the default) JSBSim would only print
183 //       out the normally expected messages, essentially echoing
184 //       the config files as they are read. If the environment
185 //       variable is not set, debug_lvl is set to 1 internally
186 //    0: This requests JSBSim not to output any messages
187 //       whatsoever.
188 //    1: This value explicity requests the normal JSBSim
189 //       startup messages
190 //    2: This value asks for a message to be printed out when
191 //       a class is instantiated
192 //    4: When this value is set, a message is displayed when a
193 //       FGModel object executes its Run() method
194 //    8: When this value is set, various runtime state variables
195 //       are printed out periodically
196 //    16: When set various parameters are sanity checked and
197 //       a message is printed out when they go out of bounds
198
199 void FGKinemat::Debug(int from)
200 {
201   if (debug_lvl <= 0) return;
202
203   if (debug_lvl & 1) { // Standard console startup message output
204     if (from == 0) { // Constructor
205       cout << "      ID: " << ID << endl;
206       cout << "      INPUT: " << InputNode->getName() << endl;
207       cout << "      DETENTS: " << NumDetents << endl;
208       for (int i=0;i<NumDetents;i++) {
209         cout << "        " << Detents[i] << " " << TransitionTimes[i] << endl;
210       }
211       if (IsOutput) cout << "      OUTPUT: " << OutputNode->getName() << endl;
212     }
213   }
214   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
215     if (from == 0) cout << "Instantiated: FGKinemat" << endl;
216     if (from == 1) cout << "Destroyed:    FGKinemat" << endl;
217   }
218   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
219   }
220   if (debug_lvl & 8 ) { // Runtime state variables
221   }
222   if (debug_lvl & 16) { // Sanity checking
223   }
224   if (debug_lvl & 64) {
225     if (from == 0) { // Constructor
226       cout << IdSrc << endl;
227       cout << IdHdr << endl;
228     }
229   }
230 }
231