| 1 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2 | |
| 3 | Header: FGSwitch.h |
| 4 | Author: Jon S. Berndt |
| 5 | Date started: 12/23/2002 |
| 6 | |
| 7 | ------------- Copyright (C) 2002 [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 |
| 11 | Software Foundation; either version 2 of the License, or (at your option) any |
| 12 | later 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 |
| 20 | with this program; if not, write to the Free Software Foundation, Inc., 59 |
| 21 | Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 22 | |
| 23 | Further information about the GNU Lesser General Public License can also be |
| 24 | found on the world wide web at http://www.gnu.org. |
| 25 | |
| 26 | HISTORY |
| 27 | -------------------------------------------------------------------------------- |
| 28 | |
| 29 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 30 | SENTRY |
| 31 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 32 | |
| 33 | #ifndef FGSWITCH_H |
| 34 | #define FGSWITCH_H |
| 35 | |
| 36 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 37 | INCLUDES |
| 38 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 39 | |
| 40 | #include "FGFCSComponent.h" |
| 41 | #include "math/FGParameterValue.h" |
| 42 | |
| 43 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 44 | FORWARD DECLARATIONS |
| 45 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 46 | |
| 47 | namespace JSBSim { |
| 48 | |
| 49 | class FGCondition; |
| 50 | |
| 51 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 52 | CLASS DOCUMENTATION |
| 53 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 54 | |
| 55 | /** Encapsulates a switch for the flight control system. |
| 56 | |
| 57 | The switch component models a switch - either on/off or a multi-choice rotary |
| 58 | switch. The switch can represent a physical cockpit switch, or can represent a |
| 59 | logical switch, where several conditions might need to be satisfied before a |
| 60 | particular state is reached. The value of the switch - the output value for the |
| 61 | component - is chosen depending on the state of the switch. Each switch is |
| 62 | comprised of one or more tests. Each test has a value associated with it. The |
| 63 | first test that evaluates to true will set the output value of the switch |
| 64 | according to the value parameter belonging to that test. Each test contains one |
| 65 | or more conditions, which each must be logically related (if there are more than |
| 66 | one) given the value of the logic attribute, and which takes the form: |
| 67 | |
| 68 | property conditional property|value |
| 69 | |
| 70 | e.g. |
| 71 | |
| 72 | qbar ge 21.0 |
| 73 | |
| 74 | or, |
| 75 | |
| 76 | roll_rate == pitch_rate |
| 77 | |
| 78 | Within a test, additional tests can be specified, which allows for |
| 79 | complex groupings of logical comparisons. Each test contains |
| 80 | additional conditions, as well as possibly additional tests. |
| 81 | |
| 82 | @code |
| 83 | <switch name="switch1"> |
| 84 | <default value="{property|value}"/> |
| 85 | <test logic="{AND|OR}" value="{property|value}"> |
| 86 | {property} {conditional} {property|value} |
| 87 | <test logic="{AND|OR}"> |
| 88 | {property} {conditional} {property|value} |
| 89 | ... |
| 90 | </test> |
| 91 | ... |
| 92 | </test> |
| 93 | <test logic="{AND|OR}" value="{property|value}"> |
| 94 | {property} {conditional} {property|value} |
| 95 | ... |
| 96 | </test> |
| 97 | ... |
| 98 | [<output> {property} </output>] |
| 99 | </switch> |
| 100 | @endcode |
| 101 | |
| 102 | Here's an example: |
| 103 | |
| 104 | @code |
| 105 | <switch name="roll a/p autoswitch"> |
| 106 | <default value="0.0"/> |
| 107 | <test value="fcs/roll-ap-error-summer"> |
| 108 | ap/attitude_hold == 1 |
| 109 | </test> |
| 110 | </switch> |
| 111 | @endcode |
| 112 | |
| 113 | Note: In the "logic" attribute, "AND" is the default logic, if none is supplied. |
| 114 | |
| 115 | The above example specifies that the default value of the component (i.e. the |
| 116 | output property of the component, addressed by the property, |
| 117 | ap/roll-ap-autoswitch) is 0.0. |
| 118 | |
| 119 | If or when the attitude hold switch is selected (property ap/attitude_hold takes |
| 120 | the value 1), the value of the switch component will be whatever value |
| 121 | fcs/roll-ap-error-summer is. |
| 122 | |
| 123 | @author Jon S. Berndt |
| 124 | */ |
| 125 | |
| 126 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 127 | CLASS DECLARATION |
| 128 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 129 | |
| 130 | class FGSwitch : public FGFCSComponent |
| 131 | { |
| 132 | public: |
| 133 | /** Constructor |
| 134 | @param fcs a pointer to the parent FGFCS class |
| 135 | @param element a pointer to the Element (from the config file XML tree) |
| 136 | that represents this switch component */ |
| 137 | FGSwitch(FGFCS* fcs, Element* element); |
| 138 | |
| 139 | /// Destructor |
| 140 | ~FGSwitch(); |
| 141 | |
| 142 | /** Executes the switch logic. |
| 143 | @return true - always*/ |
| 144 | bool Run(void) override; |
| 145 | |
| 146 | private: |
| 147 | |
| 148 | struct Test { |
| 149 | FGCondition* condition; |
| 150 | bool Default; |
| 151 | FGParameter_ptr OutputValue; |
| 152 | |
| 153 | // constructor for the test structure |
| 154 | Test(void) : condition(nullptr), Default(false) {} |
| 155 | |
| 156 | void setTestValue(const std::string &value, const std::string &Name, |
| 157 | FGPropertyManager* pm) |
| 158 | { |
| 159 | if (value.empty()) { |
| 160 | std::cerr << "No VALUE supplied for switch component: " << Name |
| 161 | << std::endl; |
| 162 | } else |
| 163 | OutputValue = new FGParameterValue(value, pm); |
| 164 | } |
| 165 | |
| 166 | std::string GetOutputName(void) const {return OutputValue->GetName();} |
| 167 | }; |
| 168 | |
| 169 | std::vector <Test*> tests; |
| 170 | bool initialized = false; |
| 171 | |
| 172 | void VerifyProperties(void); |
| 173 | void Debug(int from) override; |
| 174 | }; |
| 175 | } |
| 176 | #endif |