| 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 ([email protected]) ------------- |
| 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 | FORWARD DECLARATIONS |
| 45 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 46 | |
| 47 | namespace JSBSim { |
| 48 | |
| 49 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 50 | CLASS DOCUMENTATION |
| 51 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 52 | |
| 53 | /** Represents a <channel> in a control system definition. |
| 54 | The <channel> may be defined within a <system>, <autopilot> or <flight_control> |
| 55 | element. Channels are a way to group sets of components that perform |
| 56 | a specific purpose or algorithm. |
| 57 | Created within a <system> tag, the channel is defined as follows |
| 58 | <channel name="name" [execute="property"] [execrate="rate"]> |
| 59 | name is the name of the channel - in the old way this would also be used to bind elements |
| 60 | execute [optional] is the property that defines when to execute this channel; an on/off switch |
| 61 | execrate [optional] is the rate at which the channel should execute. |
| 62 | A value of 0 or 1 will execute the channel every frame, a value of 2 |
| 63 | every other frame (half rate), a value of 4 is every 4th frame (quarter rate) |
| 64 | */ |
| 65 | |
| 66 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 67 | CLASS DECLARATION |
| 68 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 69 | |
| 70 | typedef std::vector <FGFCSComponent*> FCSCompVec; |
| 71 | |
| 72 | class FGFCSChannel { |
| 73 | public: |
| 74 | /// Constructor |
| 75 | FGFCSChannel(FGFCS* FCS, const std::string &name, int execRate, |
| 76 | FGPropertyNode* node=0) |
| 77 | : fcs(FCS), OnOffNode(node), Name(name) |
| 78 | { |
| 79 | ExecRate = execRate < 1 ? 1 : execRate; |
| 80 | // Set ExecFrameCountSinceLastRun so that each components are initialized |
| 81 | ExecFrameCountSinceLastRun = ExecRate; |
| 82 | } |
| 83 | |
| 84 | /// Destructor |
| 85 | ~FGFCSChannel() { |
| 86 | for (unsigned int i=0; i<FCSComponents.size(); i++) delete FCSComponents[i]; |
| 87 | FCSComponents.clear(); |
| 88 | } |
| 89 | /// Retrieves the name of the channel |
| 90 | std::string GetName() {return Name;} |
| 91 | |
| 92 | /// Adds a component to a channel |
| 93 | void Add(FGFCSComponent* comp) { |
| 94 | FCSComponents.push_back(comp); |
| 95 | } |
| 96 | /// Returns the number of components in the channel. |
| 97 | size_t GetNumComponents() {return FCSComponents.size();} |
| 98 | /// Retrieves a specific component. |
| 99 | FGFCSComponent* GetComponent(unsigned int i) { |
| 100 | if (i >= GetNumComponents()) { |
| 101 | std::cerr << "Tried to get nonexistent component" << std::endl; |
| 102 | return 0; |
| 103 | } else { |
| 104 | return FCSComponents[i]; |
| 105 | } |
| 106 | } |
| 107 | /// Reset the components that can be reset |
| 108 | void Reset() { |
| 109 | for (unsigned int i=0; i<FCSComponents.size(); i++) |
| 110 | FCSComponents[i]->ResetPastStates(); |
| 111 | |
| 112 | // Set ExecFrameCountSinceLastRun so that each components are initialized |
| 113 | // after a reset. |
| 114 | ExecFrameCountSinceLastRun = ExecRate; |
| 115 | } |
| 116 | /// Executes all the components in a channel. |
| 117 | void Execute() { |
| 118 | // If there is an on/off property supplied for this channel, check |
| 119 | // the value. If it is true, permit execution to continue. If not, return |
| 120 | // and do not execute the channel. |
| 121 | if (OnOffNode && !OnOffNode->getBoolValue()) return; |
| 122 | |
| 123 | if (fcs->GetDt() != 0.0) { |
| 124 | if (ExecFrameCountSinceLastRun >= ExecRate) { |
| 125 | ExecFrameCountSinceLastRun = 0; |
| 126 | } |
| 127 | |
| 128 | ++ExecFrameCountSinceLastRun; |
| 129 | } |
| 130 | |
| 131 | // channel will be run at rate 1 if trimming, or when the next execrate |
| 132 | // frame is reached |
| 133 | if (fcs->GetTrimStatus() || ExecFrameCountSinceLastRun >= ExecRate) { |
| 134 | for (unsigned int i=0; i<FCSComponents.size(); i++) |
| 135 | FCSComponents[i]->Run(); |
| 136 | } |
| 137 | } |
| 138 | /// Get the channel rate |
| 139 | int GetRate(void) const { return ExecRate; } |
| 140 | |
| 141 | private: |
| 142 | FGFCS* fcs; |
| 143 | FCSCompVec FCSComponents; |
| 144 | FGConstPropertyNode_ptr OnOffNode; |
| 145 | std::string Name; |
| 146 | |
| 147 | int ExecRate; // rate at which this system executes, 0 or 1 every frame, 2 every second frame etc.. |
| 148 | int ExecFrameCountSinceLastRun; |
| 149 | }; |
| 150 | |
| 151 | } |
| 152 | |
| 153 | #endif |