]> git.mxchange.org Git - flightgear.git/blob - src/FDM/YASim/ControlMap.cpp
Use bool where the source and destination variable is bool.
[flightgear.git] / src / FDM / YASim / ControlMap.cpp
1 #include "Jet.hpp"
2 #include "Thruster.hpp"
3 #include "PropEngine.hpp"
4 #include "PistonEngine.hpp"
5 #include "TurbineEngine.hpp"
6 #include "Gear.hpp"
7 #include "Hook.hpp"
8 #include "Launchbar.hpp"
9 #include "Wing.hpp"
10 #include "Rotor.hpp"
11 #include "Math.hpp"
12 #include "Propeller.hpp"
13
14 #include "ControlMap.hpp"
15 namespace yasim {
16
17 ControlMap::~ControlMap()
18 {
19     int i;
20     for(i=0; i<_inputs.size(); i++) {
21         Vector* v = (Vector*)_inputs.get(i);
22         int j;
23         for(j=0; j<v->size(); j++)
24             delete (MapRec*)v->get(j);
25         delete v;
26     }
27
28     for(i=0; i<_outputs.size(); i++)
29         delete (OutRec*)_outputs.get(i);
30 }
31
32 int ControlMap::newInput()
33 {
34     Vector* v = new Vector();
35     return _inputs.add(v);
36 }
37
38 void ControlMap::addMapping(int input, int type, void* object, int options,
39                             float src0, float src1, float dst0, float dst1)
40 {
41     addMapping(input, type, object, options);
42
43     // The one we just added is last in the list (ugly, awful hack!)
44     Vector* maps = (Vector*)_inputs.get(input);
45     MapRec* m = (MapRec*)maps->get(maps->size() - 1);
46
47     m->src0 = src0;
48     m->src1 = src1;
49     m->dst0 = dst0;
50     m->dst1 = dst1;
51 }
52
53 void ControlMap::addMapping(int input, int type, void* object, int options)
54 {
55     // See if the output object already exists
56     OutRec* out = 0;
57     int i;
58     for(i=0; i<_outputs.size(); i++) {
59         OutRec* o = (OutRec*)_outputs.get(i);
60         if(o->object == object && o->type == type) {
61             out = o;
62             break;
63         }
64     }
65
66     // Create one if it doesn't
67     if(out == 0) {
68         out = new OutRec();
69         out->type = type;
70         out->object = object;
71         out->oldL = out->oldR = out->time = 0;
72         _outputs.add(out);
73     }
74     
75     // Make a new input record
76     MapRec* map = new MapRec();
77     map->out = out;
78     map->opt = options;
79     map->idx = out->maps.add(map);
80
81     // The default ranges differ depending on type!
82     map->src1 = map->dst1 = rangeMax(type);
83     map->src0 = map->dst0 = rangeMin(type);
84
85     // And add it to the approproate vectors.
86     Vector* maps = (Vector*)_inputs.get(input);
87     maps->add(map);
88 }
89
90 void ControlMap::reset()
91 {
92     // Set all the values to zero
93     for(int i=0; i<_outputs.size(); i++) {
94         OutRec* o = (OutRec*)_outputs.get(i);
95         for(int j=0; j<o->maps.size(); j++)
96             ((MapRec*)(o->maps.get(j)))->val = 0;
97     }
98 }
99
100 void ControlMap::setInput(int input, float val)
101 {
102     Vector* maps = (Vector*)_inputs.get(input);
103     for(int i=0; i<maps->size(); i++) {
104         MapRec* m = (MapRec*)maps->get(i);
105
106         float val2 = val;
107
108         // Do the scaling operation.  Clamp to [src0:src1], rescale to
109         // [0:1] within that range, then map to [dst0:dst1].
110         if(val2 < m->src0) val2 = m->src0;
111         if(val2 > m->src1) val2 = m->src1;
112         val2 = (val2 - m->src0) / (m->src1 - m->src0);
113         val2 = m->dst0 + val2 * (m->dst1 - m->dst0);
114
115         m->val = val2;
116     }
117 }
118
119 int ControlMap::getOutputHandle(void* obj, int type)
120 {
121     for(int i=0; i<_outputs.size(); i++) {
122         OutRec* o = (OutRec*)_outputs.get(i);
123         if(o->object == obj && o->type == type)
124             return i;
125     }
126     return 0;
127 }
128
129 void ControlMap::setTransitionTime(int handle, float time)
130 {
131     ((OutRec*)_outputs.get(handle))->time = time;
132 }
133
134 float ControlMap::getOutput(int handle)
135 {
136     return ((OutRec*)_outputs.get(handle))->oldL;
137 }
138
139 float ControlMap::getOutputR(int handle)
140 {
141     return ((OutRec*)_outputs.get(handle))->oldR;
142 }
143
144 void ControlMap::applyControls(float dt)
145 {
146     int outrec;
147     for(outrec=0; outrec<_outputs.size(); outrec++) {
148         OutRec* o = (OutRec*)_outputs.get(outrec);
149         
150         // Generate a summed value.  Note the check for "split"
151         // control axes like ailerons.
152         float lval = 0, rval = 0;
153         int i;
154         for(i=0; i<o->maps.size(); i++) {
155             MapRec* m = (MapRec*)o->maps.get(i);
156             float val = m->val;
157
158             if(m->opt & OPT_SQUARE)
159                 val = val * Math::abs(val);
160             if(m->opt & OPT_INVERT)
161                 val = -val;
162             lval += val;
163             if(m->opt & OPT_SPLIT)
164                 rval -= val;
165             else
166                 rval += val;
167         }
168
169         // If there is a finite transition time, clamp the values to
170         // the maximum travel allowed in this dt.
171         if(o->time > 0) {
172             float dl = lval - o->oldL;
173             float dr = rval - o->oldR;
174             float adl = Math::abs(dl);
175             float adr = Math::abs(dr);
176         
177             float max = (dt/o->time) * (rangeMax(o->type) - rangeMin(o->type));
178             if(adl > max) dl = dl*max/adl;
179             if(adr > max) dr = dr*max/adr;
180
181             lval = o->oldL + dl;
182             rval = o->oldR + dr;
183         }
184
185         o->oldL = lval;
186         o->oldR = rval;
187
188         void* obj = o->object;
189         switch(o->type) {
190         case THROTTLE: ((Thruster*)obj)->setThrottle(lval);        break;
191         case MIXTURE:  ((Thruster*)obj)->setMixture(lval);         break;
192         case CONDLEVER: ((TurbineEngine*)((PropEngine*)obj)->getEngine())->setCondLever(lval); break;
193         case STARTER:  ((Thruster*)obj)->setStarter(lval != 0.0);  break;
194         case MAGNETOS: ((PropEngine*)obj)->setMagnetos((int)lval); break;
195         case ADVANCE:  ((PropEngine*)obj)->setAdvance(lval);       break;
196         case PROPPITCH: ((PropEngine*)obj)->setPropPitch(lval);    break;
197         case PROPFEATHER: ((PropEngine*)obj)->setPropFeather((int)lval); break;
198         case REHEAT:   ((Jet*)obj)->setReheat(lval);               break;
199         case VECTOR:   ((Jet*)obj)->setRotation(lval);             break;
200         case BRAKE:    ((Gear*)obj)->setBrake(lval);               break;
201         case STEER:    ((Gear*)obj)->setRotation(lval);            break;
202         case EXTEND:   ((Gear*)obj)->setExtension(lval);           break;
203         case HEXTEND:  ((Hook*)obj)->setExtension(lval);           break;
204         case LEXTEND:  ((Launchbar*)obj)->setExtension(lval);      break;
205         case CASTERING:((Gear*)obj)->setCastering(lval != 0);      break;
206         case SLAT:     ((Wing*)obj)->setSlat(lval);                break;
207         case FLAP0:    ((Wing*)obj)->setFlap0(lval, rval);         break;
208         case FLAP1:    ((Wing*)obj)->setFlap1(lval, rval);         break;
209         case SPOILER:  ((Wing*)obj)->setSpoiler(lval, rval);       break;
210         case COLLECTIVE:   ((Rotor*)obj)->setCollective(lval);     break;
211         case CYCLICAIL:    ((Rotor*)obj)->setCyclicail(lval,rval); break;
212         case CYCLICELE:    ((Rotor*)obj)->setCyclicele(lval,rval); break;
213         case ROTORENGINEON: ((Rotor*)obj)->setEngineOn((int)lval); break;
214         case REVERSE_THRUST: ((Jet*)obj)->setReverse(lval != 0);   break;
215         case BOOST:
216             ((PistonEngine*)((Thruster*)obj)->getEngine())->setBoost(lval);
217             break;
218         case WASTEGATE:
219             ((PistonEngine*)((Thruster*)obj)->getEngine())->setWastegate(lval);
220             break;
221         }
222     }
223 }
224
225 float ControlMap::rangeMin(int type)
226 {
227     // The minimum of the range for each type of control
228     switch(type) {
229     case FLAP0:    return -1;  // [-1:1]
230     case FLAP1:    return -1;
231     case STEER:    return -1;
232     case CYCLICELE: return -1;
233     case CYCLICAIL: return -1;
234     case COLLECTIVE: return -1;
235     case MAGNETOS: return 0;   // [0:3]
236     default:       return 0;   // [0:1]
237     }
238 }
239
240 float ControlMap::rangeMax(int type)
241 {
242     // The maximum of the range for each type of control
243     switch(type) {
244     case FLAP0:    return 1; // [-1:1]
245     case FLAP1:    return 1;
246     case STEER:    return 1;
247     case MAGNETOS: return 3; // [0:3]
248     default:       return 1; // [0:1]
249     }
250 }
251
252 } // namespace yasim