JSBSim Flight Dynamics Model  1.0 (02 March 2017)
An Open Source Flight Dynamics and Control Software Library in C++
FGFCSChannel.h
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2 
3  Header: FGGFCSChannel.h
4  Author: Jon S. Berndt
5  Date started: 10/11/12
6 
7  ------------- Copyright (C) 2012 Jon S. Berndt (jon@jsbsim.org) -------------
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 HISTORY
27 --------------------------------------------------------------------------------
28 10/11/12 JSB Created
29 
30 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
31 SENTRY
32 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
33 
34 #ifndef FGFCSCHANNEL_H
35 #define FGFCSCHANNEL_H
36 
37 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38 INCLUDES
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
40 
41 #include <iostream>
42 
43 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
44 DEFINITIONS
45 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
46 
47 #define ID_FCSCHANNEL "$Id: FGFCSChannel.h,v 1.10 2016/04/16 11:00:11 bcoconni Exp $"
48 
49 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
50 FORWARD DECLARATIONS
51 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
52 
53 namespace JSBSim {
54 
55 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
56 CLASS DOCUMENTATION
57 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
58 
72 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73 CLASS DECLARATION
74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
75 
76 typedef std::vector <FGFCSComponent*> FCSCompVec;
77 
78 class FGFCSChannel {
79 public:
81  FGFCSChannel(FGFCS* FCS, const std::string &name, int execRate,
82  FGPropertyNode* node=0)
83  : fcs(FCS), OnOffNode(node), Name(name)
84  {
85  ExecRate = execRate < 1 ? 1 : execRate;
86  // Set ExecFrameCountSinceLastRun so that each components are initialized
87  ExecFrameCountSinceLastRun = ExecRate;
88  }
89 
92  for (unsigned int i=0; i<FCSComponents.size(); i++) delete FCSComponents[i];
93  FCSComponents.clear();
94  }
96  std::string GetName() {return Name;}
97 
99  void Add(FGFCSComponent* comp) {
100  FCSComponents.push_back(comp);
101  comp->SetDtForFrameCount(ExecRate);
102  }
104  size_t GetNumComponents() {return FCSComponents.size();}
106  FGFCSComponent* GetComponent(unsigned int i) {
107  if (i >= GetNumComponents()) {
108  std::cerr << "Tried to get nonexistent component" << std::endl;
109  return 0;
110  } else {
111  return FCSComponents[i];
112  }
113  }
115  void Reset() {
116  for (unsigned int i=0; i<FCSComponents.size(); i++)
117  FCSComponents[i]->ResetPastStates();
118 
119  // Set ExecFrameCountSinceLastRun so that each components are initialized
120  // after a reset.
121  ExecFrameCountSinceLastRun = ExecRate;
122  }
124  void Execute() {
125  // If there is an on/off property supplied for this channel, check
126  // the value. If it is true, permit execution to continue. If not, return
127  // and do not execute the channel.
128  if (OnOffNode && !OnOffNode->getBoolValue()) return;
129 
130  if (fcs->GetDt() != 0.0) {
131  if (ExecFrameCountSinceLastRun >= ExecRate) {
132  ExecFrameCountSinceLastRun = 0;
133  }
134 
135  ++ExecFrameCountSinceLastRun;
136  }
137 
138  // channel will be run at rate 1 if trimming, or when the next execrate
139  // frame is reached
140  if (fcs->GetTrimStatus() || ExecFrameCountSinceLastRun >= ExecRate) {
141  for (unsigned int i=0; i<FCSComponents.size(); i++)
142  FCSComponents[i]->Run();
143  }
144  }
146  int GetRate(void) const { return ExecRate; }
147 
148  private:
149  FGFCS* fcs;
150  FCSCompVec FCSComponents;
151  FGConstPropertyNode_ptr OnOffNode;
152  std::string Name;
153 
154  int ExecRate; // rate at which this system executes, 0 or 1 every frame, 2 every second frame etc..
155  int ExecFrameCountSinceLastRun;
156 };
157 
158 }
159 
160 #endif
FGFCSChannel(FGFCS *FCS, const std::string &name, int execRate, FGPropertyNode *node=0)
Constructor.
Definition: FGFCSChannel.h:81
std::string GetName()
Retrieves the name of the channel.
Definition: FGFCSChannel.h:96
void Execute()
Executes all the components in a channel.
Definition: FGFCSChannel.h:124
FGFCSComponent * GetComponent(unsigned int i)
Retrieves a specific component.
Definition: FGFCSChannel.h:106
Class wrapper for property handling.
void Reset()
Reset the components that can be reset.
Definition: FGFCSChannel.h:115
~FGFCSChannel()
Destructor.
Definition: FGFCSChannel.h:91
void Add(FGFCSComponent *comp)
Adds a component to a channel.
Definition: FGFCSChannel.h:99
Encapsulates the Flight Control System (FCS) functionality.
Definition: FGFCS.h:193
int GetRate(void) const
Get the channel rate.
Definition: FGFCSChannel.h:146
Base class for JSBSim Flight Control System Components.
size_t GetNumComponents()
Returns the number of components in the channel.
Definition: FGFCSChannel.h:104