]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/filtersjb/FGFlaps.cpp
builddir -> srcdir so builds can be done outside the master source directory.
[flightgear.git] / src / FDM / JSBSim / filtersjb / FGFlaps.cpp
1 /*******************************************************************************
2  
3  Module:       FGFlap.cpp
4  Author:       Tony Peden, for flight control system authored by Jon S. Berndt
5  Date started: 5/11/00
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 "FGFlaps.h"
41
42 /*******************************************************************************
43 ************************************ CODE **************************************
44 *******************************************************************************/
45
46 // *****************************************************************************
47 //  Function:   Constructor
48 //  Purpose:
49 //  Parameters: void
50 //  Comments:
51
52 FGFlaps::FGFlaps(FGFCS* fcs, FGConfigFile* AC_cfg) : FGFCSComponent(fcs),
53 AC_cfg(AC_cfg) {
54   string token;
55   float tmpDetent;
56   float tmpTime;
57
58   Detents.clear();
59   TransitionTimes.clear();
60
61   Type = AC_cfg->GetValue("TYPE");
62   Name = AC_cfg->GetValue("NAME");
63   AC_cfg->GetNextConfigLine();
64
65   while ((token = AC_cfg->GetValue()) != "/COMPONENT") {
66     *AC_cfg >> token;
67     if (token == "ID") {
68       *AC_cfg >> ID;
69       cout << "      ID: " << ID << endl;
70     } else if (token == "INPUT") {
71       token = AC_cfg->GetValue("INPUT");
72       cout << "      INPUT: " << token << endl;
73       if (token.find("FG_") != token.npos) {
74         *AC_cfg >> token;
75         InputIdx = fcs->GetState()->GetParameterIndex(token);
76         InputType = itPilotAC;
77       }
78     } else if ( token == "DETENTS" ) {
79       *AC_cfg >> NumDetents;
80       cout << "      DETENTS: " << NumDetents << endl;
81       for(int i=0;i<NumDetents;i++) {
82         *AC_cfg >> tmpDetent;
83         *AC_cfg >> tmpTime;
84         Detents.push_back(tmpDetent);
85         TransitionTimes.push_back(tmpTime);
86         cout << "        " << Detents[i] << " " << TransitionTimes[i] << endl;
87       }
88     } else if (token == "OUTPUT") {
89
90       IsOutput = true;
91       *AC_cfg >> sOutputIdx;
92       cout << "      OUTPUT: " <<sOutputIdx << endl;
93       OutputIdx = fcs->GetState()->GetParameterIndex(sOutputIdx);
94     }
95   }
96 }
97
98 // *****************************************************************************
99 //  Function:   Run
100 //  Purpose:
101 //  Parameters: void
102 //  Comments:
103
104 bool FGFlaps::Run(void ) {
105   float dt=fcs->GetState()->Getdt();
106   float flap_transit_rate=0;
107
108   FGFCSComponent::Run(); // call the base class for initialization of Input
109   Flap_Handle=Input*Detents[NumDetents-1];
110   Flap_Position=fcs->GetState()->GetParameter(OutputIdx);
111
112   if(Flap_Handle < Detents[0]) {
113     fi=0;
114     Flap_Handle=Detents[0];
115     lastFlapHandle=Flap_Handle;
116     Flap_Position=Detents[0];
117     Output=Flap_Position;
118   } else if(Flap_Handle > Detents[NumDetents-1]) {
119     fi=NumDetents-1;
120     Flap_Handle=Detents[fi];
121     lastFlapHandle=Flap_Handle;
122     Flap_Position=Detents[fi];
123     Output=Flap_Position;
124   } else {
125     //cout << "FGFlaps::Run Handle: " << Flap_Handle << " Position: " << Flap_Position << endl;
126     if(dt <= 0)
127       Flap_Position=Flap_Handle;
128     else {
129       if(Flap_Handle != lastFlapHandle) {
130
131         Flaps_In_Transit=1;
132       }
133       if(Flaps_In_Transit) {
134
135         //fprintf(stderr,"Flap_Handle: %g, Flap_Position: %g\n",Flap_Handle,Flap_Position);
136         fi=0;
137         while(Detents[fi] < Flap_Handle) {
138           fi++;
139         }
140         if(Flap_Position < Flap_Handle) {
141           if(TransitionTimes[fi] > 0)
142             flap_transit_rate=(Detents[fi] - Detents[fi-1])/TransitionTimes[fi];
143           else
144             flap_transit_rate=(Detents[fi] - Detents[fi-1])/5;
145         } else {
146           if(TransitionTimes[fi+1] > 0)
147             flap_transit_rate=(Detents[fi] - Detents[fi+1])/TransitionTimes[fi+1];
148           else
149             flap_transit_rate=(Detents[fi] - Detents[fi+1])/5;
150         }
151         if(fabs(Flap_Position - Flap_Handle) > dt*flap_transit_rate)
152           Flap_Position+=flap_transit_rate*dt;
153         else {
154           Flaps_In_Transit=0;
155           Flap_Position=Flap_Handle;
156         }
157       }
158     }
159     lastFlapHandle=Flap_Handle;
160     Output=Flap_Position;
161   }
162   //cout << "FGFlaps::Run Handle: " << Flap_Handle << " Position: " << Flap_Position << " Output: " << Output << endl;
163   if (IsOutput) {
164     //cout << "Calling SetOutput()" << endl;
165     SetOutput();
166   }
167   //cout << "Out FGFlap::Run" << endl;
168   return true;
169 }
170