]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/filtersjb/FGKinemat.cpp
Updates.
[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;
53   double tmpDetent;
54   double tmpTime;
55
56   Detents.clear();
57   TransitionTimes.clear();
58   
59   OutputPct=0;
60
61   Type = AC_cfg->GetValue("TYPE");
62   Name = AC_cfg->GetValue("NAME");
63   AC_cfg->GetNextConfigLine();
64
65   while ((token = AC_cfg->GetValue()) != string("/COMPONENT")) {
66     *AC_cfg >> token;
67     if (token == "ID") {
68       *AC_cfg >> ID;
69     } else if (token == "INPUT") {
70       token = AC_cfg->GetValue("INPUT");
71       if (token.find("FG_") != token.npos) {
72         *AC_cfg >> token;
73         InputIdx = fcs->GetState()->GetParameterIndex(token);
74         InputType = itPilotAC;
75       }
76     } else if ( token == "DETENTS" ) {
77       *AC_cfg >> NumDetents;
78       for(int i=0;i<NumDetents;i++) {
79         *AC_cfg >> tmpDetent;
80         *AC_cfg >> tmpTime;
81         Detents.push_back(tmpDetent);
82         TransitionTimes.push_back(tmpTime);
83       }
84     } else if (token == "OUTPUT") {
85
86       IsOutput = true;
87       *AC_cfg >> sOutputIdx;
88       OutputIdx = fcs->GetState()->GetParameterIndex(sOutputIdx);
89     }
90   }
91
92   Debug(0);
93 }
94
95 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
96
97 FGKinemat::~FGKinemat()
98 {
99   Debug(1);
100 }
101
102 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
103
104 bool FGKinemat::Run(void ) {
105   double dt=fcs->GetState()->Getdt();
106   double output_transit_rate=0;
107
108   FGFCSComponent::Run(); // call the base class for initialization of Input
109   InputCmd = Input*Detents[NumDetents-1];
110   OutputPos = fcs->GetState()->GetParameter(OutputIdx);
111
112   if(InputCmd < Detents[0]) {
113     fi=0;
114     InputCmd=Detents[0];
115     lastInputCmd=InputCmd;
116     OutputPos=Detents[0];
117     Output=OutputPos;
118   } else if(InputCmd > Detents[NumDetents-1]) {
119     fi=NumDetents-1;
120     InputCmd=Detents[fi];
121     lastInputCmd=InputCmd;
122     OutputPos=Detents[fi];
123     Output=OutputPos;
124   } else {
125     //cout << "FGKinemat::Run Handle: " << InputCmd << " Position: " << OutputPos << endl;
126     if(dt <= 0)
127       OutputPos=InputCmd;
128     else {
129       if(InputCmd != lastInputCmd) {
130
131         InTransit=1;
132       }
133       if(InTransit) {
134
135         //fprintf(stderr,"InputCmd: %g, OutputPos: %g\n",InputCmd,OutputPos);
136         fi=0;
137         while(Detents[fi] < InputCmd) {
138           fi++;
139         }
140         if(OutputPos < InputCmd) {
141           if(TransitionTimes[fi] > 0)
142             output_transit_rate=(Detents[fi] - Detents[fi-1])/TransitionTimes[fi];
143           else
144             output_transit_rate=(Detents[fi] - Detents[fi-1])/5;
145         } else {
146           if(TransitionTimes[fi+1] > 0)
147             output_transit_rate=(Detents[fi] - Detents[fi+1])/TransitionTimes[fi+1];
148           else
149             output_transit_rate=(Detents[fi] - Detents[fi+1])/5;
150         }
151         if(fabs(OutputPos - InputCmd) > fabs(dt*output_transit_rate) )
152           OutputPos+=output_transit_rate*dt;
153         else {
154           InTransit=0;
155           OutputPos=InputCmd;
156         }
157       }
158     }
159     lastInputCmd = InputCmd;
160     Output = OutputPos;
161   }
162   
163   if( Detents[NumDetents-1] > 0 ) {
164     OutputPct = Output / Detents[NumDetents-1];
165   }
166   
167   if (IsOutput) SetOutput();
168
169   return true;
170 }
171
172 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
173 //    The bitmasked value choices are as follows:
174 //    unset: In this case (the default) JSBSim would only print
175 //       out the normally expected messages, essentially echoing
176 //       the config files as they are read. If the environment
177 //       variable is not set, debug_lvl is set to 1 internally
178 //    0: This requests JSBSim not to output any messages
179 //       whatsoever.
180 //    1: This value explicity requests the normal JSBSim
181 //       startup messages
182 //    2: This value asks for a message to be printed out when
183 //       a class is instantiated
184 //    4: When this value is set, a message is displayed when a
185 //       FGModel object executes its Run() method
186 //    8: When this value is set, various runtime state variables
187 //       are printed out periodically
188 //    16: When set various parameters are sanity checked and
189 //       a message is printed out when they go out of bounds
190
191 void FGKinemat::Debug(int from)
192 {
193   if (debug_lvl <= 0) return;
194
195   if (debug_lvl & 1) { // Standard console startup message output
196     if (from == 0) { // Constructor
197       cout << "      ID: " << ID << endl;
198       cout << "      INPUT: " << InputIdx << endl;
199       cout << "      DETENTS: " << NumDetents << endl;
200       for(int i=0;i<NumDetents;i++) {
201         cout << "        " << Detents[i] << " " << TransitionTimes[i] << endl;
202       }
203       if (IsOutput) cout << "      OUTPUT: " <<sOutputIdx << endl;
204     }
205   }
206   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
207     if (from == 0) cout << "Instantiated: FGKinemat" << endl;
208     if (from == 1) cout << "Destroyed:    FGKinemat" << endl;
209   }
210   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
211   }
212   if (debug_lvl & 8 ) { // Runtime state variables
213   }
214   if (debug_lvl & 16) { // Sanity checking
215   }
216   if (debug_lvl & 64) {
217     if (from == 0) { // Constructor
218       cout << IdSrc << endl;
219       cout << IdHdr << endl;
220     }
221   }
222 }
223