| 1 | #ifndef _CONTROL_MAP_HPP |
| 2 | #define _CONTROL_MAP_HPP |
| 3 | |
| 4 | #include <string> |
| 5 | |
| 6 | #include <simgear/props/props.hxx> |
| 7 | #include "yasim-common.hpp" |
| 8 | #include "Vector.hpp" |
| 9 | |
| 10 | namespace yasim { |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | class ControlMap { |
| 16 | public: |
| 17 | ~ControlMap(); |
| 18 | |
| 19 | //! keep this enum in sync with the static vector ControlNames in ControlMap.cpp ! |
| 20 | enum ControlType { |
| 21 | THROTTLE, |
| 22 | MIXTURE, |
| 23 | CONDLEVER, |
| 24 | STARTER, |
| 25 | MAGNETOS, |
| 26 | ADVANCE, |
| 27 | REHEAT, |
| 28 | PROP, |
| 29 | BRAKE, |
| 30 | STEER, |
| 31 | EXTEND, |
| 32 | HEXTEND, |
| 33 | LEXTEND, |
| 34 | LACCEL, |
| 35 | INCIDENCE, |
| 36 | FLAP0, |
| 37 | FLAP1, |
| 38 | SLAT, |
| 39 | SPOILER, |
| 40 | VECTOR, |
| 41 | FLAP0EFFECTIVENESS, |
| 42 | FLAP1EFFECTIVENESS, |
| 43 | BOOST, |
| 44 | CASTERING, |
| 45 | PROPPITCH, |
| 46 | PROPFEATHER, |
| 47 | COLLECTIVE, |
| 48 | CYCLICAIL, |
| 49 | CYCLICELE, |
| 50 | ROTORGEARENGINEON, |
| 51 | TILTYAW, |
| 52 | TILTPITCH, |
| 53 | TILTROLL, |
| 54 | ROTORBRAKE, |
| 55 | ROTORENGINEMAXRELTORQUE, |
| 56 | ROTORRELTARGET, |
| 57 | ROTORBALANCE, |
| 58 | REVERSE_THRUST, |
| 59 | WASTEGATE, |
| 60 | WINCHRELSPEED, |
| 61 | HITCHOPEN, |
| 62 | PLACEWINCH, |
| 63 | FINDAITOW, |
| 64 | }; //! keep this enum in sync with the static vector ControlNames in ControlMap.cpp ! |
| 65 | |
| 66 | |
| 67 | enum { |
| 68 | OPT_SPLIT = 0x01, |
| 69 | OPT_INVERT = 0x02, |
| 70 | OPT_SQUARE = 0x04 |
| 71 | }; |
| 72 | |
| 73 | struct PropHandle { |
| 74 | std::string name; |
| 75 | int handle {0}; |
| 76 | }; |
| 77 | // to identify controls per wing section we need wing object + section id |
| 78 | struct ObjectID { |
| 79 | void* object {nullptr}; |
| 80 | int subObj {0}; |
| 81 | }; |
| 82 | |
| 83 | // map control name to int (enum) |
| 84 | static ControlType parseControl(const char* name); |
| 85 | static ControlType getControlByName(const std::string& name); |
| 86 | static std::string getControlName(ControlType c); |
| 87 | // create ID from object and optional sub index (e.g. for wing section) |
| 88 | static ObjectID getObjectID(void* object, int subObj = 0); |
| 89 | |
| 90 | // add input property for a control to an object |
| 91 | |
| 92 | // same with limits. Input values are clamped to [src0:src1] and then mapped to |
| 93 | // [dst0:dst1] before being set on the objects control. |
| 94 | void addMapping(const char* inputProp, ControlType control, ObjectID id, int options, float src0, float src1, float dst0, float dst1); |
| 95 | |
| 96 | // Resets our accumulated input values. Call before any |
| 97 | // setInput() invokations. |
| 98 | void reset(); |
| 99 | |
| 100 | // Sets the specified input (as returned by getPropertyHandle()) to the |
| 101 | // specified value. |
| 102 | void setInput(int propHandle, float value); |
| 103 | |
| 104 | /// Calculates and applies the settings received since the last reset(). |
| 105 | /// dt defaults to a large value used at solve time. |
| 106 | void applyControls(float dt=1e6); |
| 107 | |
| 108 | // Returns the input/output range appropriate for the given |
| 109 | // control. Ailerons go from -1 to 1, while throttles are never |
| 110 | // lower than zero, etc... |
| 111 | static float rangeMin(ControlType control); |
| 112 | static float rangeMax(ControlType control); |
| 113 | |
| 114 | // Each output record is identified by both an object/type tuple |
| 115 | // and a numeric handle. |
| 116 | int getOutputHandle(ObjectID id, ControlType control); |
| 117 | |
| 118 | // Sets the transition time for the control output to swing |
| 119 | // through its full range. |
| 120 | void setTransitionTime(int handle, float time); |
| 121 | |
| 122 | // Retrieves the current value of the control output. Controls |
| 123 | // with OPT_SPLIT settable on inputs will have a separately |
| 124 | // computed "right side" value. |
| 125 | float getOutput(int handle); |
| 126 | float getOutputR(int handle); |
| 127 | |
| 128 | // register property name, return handle |
| 129 | int getInputPropertyHandle(const char* name); |
| 130 | int numProperties() { return _properties.size(); } |
| 131 | PropHandle* getProperty(const int i); |
| 132 | |
| 133 | private: |
| 134 | //output data for a control of an object |
| 135 | struct OutRec { |
| 136 | int id {0}; |
| 137 | ControlType control; |
| 138 | ObjectID oid; |
| 139 | Vector maps; |
| 140 | float transitionTime {0}; |
| 141 | float oldValueLeft {0}; |
| 142 | float oldValueRight {0}; |
| 143 | }; |
| 144 | struct MapRec { |
| 145 | int id {0}; |
| 146 | int opt {0}; |
| 147 | float val {0}; |
| 148 | float src0 {0}; |
| 149 | float src1 {0}; |
| 150 | float dst0 {0}; |
| 151 | float dst1 {0}; |
| 152 | }; |
| 153 | |
| 154 | // A list of (sub)Vectors containing a bunch of MapRec objects for |
| 155 | // each input handle. |
| 156 | Vector _inputs; |
| 157 | |
| 158 | // An unordered list of output settings. |
| 159 | Vector _outputs; |
| 160 | |
| 161 | Vector _properties; // list of PropHandle* |
| 162 | |
| 163 | OutRec* getOutRec(ObjectID id, ControlType control); |
| 164 | }; |
| 165 | |
| 166 | }; // namespace yasim |
| 167 | #endif // _CONTROL_MAP_HPP |