]> git.mxchange.org Git - flightgear.git/blob - src/FDM/YASim/Model.cpp
c91fe7df6f2bd72e3ac7f690d27abfc41e3e34b1
[flightgear.git] / src / FDM / YASim / Model.cpp
1 #include "Atmosphere.hpp"
2 #include "Thruster.hpp"
3 #include "Math.hpp"
4 #include "RigidBody.hpp"
5 #include "Integrator.hpp"
6 #include "Propeller.hpp"
7 #include "PistonEngine.hpp"
8 #include "Gear.hpp"
9 #include "Surface.hpp"
10 #include "Rotor.hpp"
11 #include "Rotorpart.hpp"
12 #include "Rotorblade.hpp"
13 #include "Glue.hpp"
14
15 #include "Model.hpp"
16 namespace yasim {
17
18 #if 0
19 void printState(State* s)
20 {
21     State tmp = *s;
22     Math::vmul33(tmp.orient, tmp.v, tmp.v);
23     Math::vmul33(tmp.orient, tmp.acc, tmp.acc);
24     Math::vmul33(tmp.orient, tmp.rot, tmp.rot);
25     Math::vmul33(tmp.orient, tmp.racc, tmp.racc);
26
27     printf("\nNEW STATE (LOCAL COORDS)\n");
28     printf("pos: %10.2f %10.2f %10.2f\n", tmp.pos[0], tmp.pos[1], tmp.pos[2]);
29     printf("o:   ");
30     int i;
31     for(i=0; i<3; i++) {
32         if(i != 0) printf("     ");
33         printf("%6.2f %6.2f %6.2f\n",
34                tmp.orient[3*i+0], tmp.orient[3*i+1], tmp.orient[3*i+2]);
35     }
36     printf("v:   %6.2f %6.2f %6.2f\n", tmp.v[0], tmp.v[1], tmp.v[2]);
37     printf("acc: %6.2f %6.2f %6.2f\n", tmp.acc[0], tmp.acc[1], tmp.acc[2]);
38     printf("rot: %6.2f %6.2f %6.2f\n", tmp.rot[0], tmp.rot[1], tmp.rot[2]);
39     printf("rac: %6.2f %6.2f %6.2f\n", tmp.racc[0], tmp.racc[1], tmp.racc[2]);
40 }
41 #endif
42
43 Model::Model()
44 {
45     int i;
46     for(i=0; i<3; i++) _wind[i] = 0;
47
48     _integrator.setBody(&_body);
49     _integrator.setEnvironment(this);
50
51     // Default value of 30 Hz
52     _integrator.setInterval(1.0f/30.0f);
53
54     _agl = 0;
55     _crashed = false;
56     _turb = 0;
57 }
58
59 Model::~Model()
60 {
61     // FIXME: who owns these things?  Need a policy
62 }
63
64 void Model::getThrust(float* out)
65 {
66     float tmp[3];
67     out[0] = out[1] = out[2] = 0;
68     int i;
69     for(i=0; i<_thrusters.size(); i++) {
70         Thruster* t = (Thruster*)_thrusters.get(i);
71         t->getThrust(tmp);
72         Math::add3(tmp, out, out);
73     }
74 }
75
76 void Model::initIteration()
77 {
78     // Precompute torque and angular momentum for the thrusters
79     int i;
80     for(i=0; i<3; i++)
81         _gyro[i] = _torque[i] = 0;
82     for(i=0; i<_thrusters.size(); i++) {
83         Thruster* t = (Thruster*)_thrusters.get(i);
84         
85         // Get the wind velocity at the thruster location
86         float pos[3], v[3];
87         t->getPosition(pos);
88         localWind(pos, _s, v);
89
90         t->setWind(v);
91         t->setAir(_pressure, _temp, _rho);
92         t->integrate(_integrator.getInterval());
93
94         t->getTorque(v);
95         Math::add3(v, _torque, _torque);
96
97         t->getGyro(v);
98         Math::add3(v, _gyro, _gyro);
99     }
100
101     // Displace the turbulence coordinates according to the local wind.
102     if(_turb) {
103         float toff[3];
104         Math::mul3(_integrator.getInterval(), _wind, toff);
105         _turb->offset(toff);
106     }
107 }
108
109 // FIXME: This method looks to me like it's doing *integration*, not
110 // initialization.  Integration code should ideally go into
111 // calcForces.  Only very "unstiff" problems can be solved well like
112 // this (see the engine code for an example); I don't know if rotor
113 // dynamics qualify or not.
114 // -Andy
115 void Model::initRotorIteration()
116 {
117     int i;
118     float dt = _integrator.getInterval();
119     float lrot[3];
120     Math::vmul33(_s->orient, _s->rot, lrot);
121     Math::mul3(dt,lrot,lrot);
122     for(i=0; i<_rotors.size(); i++) {
123         Rotor* r = (Rotor*)_rotors.get(i);
124         r->inititeration(dt);
125     }
126     for(i=0; i<_rotorparts.size(); i++) {
127         Rotorpart* rp = (Rotorpart*)_rotorparts.get(i);
128         rp->inititeration(dt,lrot);
129     }
130     for(i=0; i<_rotorblades.size(); i++) {
131         Rotorblade* rp = (Rotorblade*)_rotorblades.get(i);
132         rp->inititeration(dt,lrot);
133     }
134 }
135
136 void Model::iterate()
137 {
138     initIteration();
139     initRotorIteration();
140     _body.recalc(); // FIXME: amortize this, somehow
141     _integrator.calcNewInterval();
142 }
143
144 bool Model::isCrashed()
145 {
146     return _crashed;
147 }
148
149 void Model::setCrashed(bool crashed)
150 {
151     _crashed = crashed;
152 }
153
154 float Model::getAGL()
155 {
156     return _agl;
157 }
158
159 State* Model::getState()
160 {
161     return _s;
162 }
163
164 void Model::setState(State* s)
165 {
166     _integrator.setState(s);
167     _s = _integrator.getState();
168 }
169
170 RigidBody* Model::getBody()
171 {
172     return &_body;
173 }
174
175 Integrator* Model::getIntegrator()
176 {
177     return &_integrator;
178 }
179
180 Surface* Model::getSurface(int handle)
181 {
182     return (Surface*)_surfaces.get(handle);
183 }
184
185 Rotorpart* Model::getRotorpart(int handle)
186 {
187     return (Rotorpart*)_rotorparts.get(handle);
188 }
189 Rotorblade* Model::getRotorblade(int handle)
190 {
191     return (Rotorblade*)_rotorblades.get(handle);
192 }
193 Rotor* Model::getRotor(int handle)
194 {
195     return (Rotor*)_rotors.get(handle);
196 }
197
198 int Model::addThruster(Thruster* t)
199 {
200     return _thrusters.add(t);
201 }
202
203 int Model::numThrusters()
204 {
205     return _thrusters.size();
206 }
207
208 Thruster* Model::getThruster(int handle)
209 {
210     return (Thruster*)_thrusters.get(handle);
211 }
212
213 void Model::setThruster(int handle, Thruster* t)
214 {
215     _thrusters.set(handle, t);
216 }
217
218 int Model::addSurface(Surface* surf)
219 {
220     return _surfaces.add(surf);
221 }
222
223 int Model::addRotorpart(Rotorpart* rpart)
224 {
225     return _rotorparts.add(rpart);
226 }
227 int Model::addRotorblade(Rotorblade* rblade)
228 {
229     return _rotorblades.add(rblade);
230 }
231 int Model::addRotor(Rotor* r)
232 {
233     return _rotors.add(r);
234 }
235
236 int Model::addGear(Gear* gear)
237 {
238     return _gears.add(gear);
239 }
240
241 void Model::setGroundEffect(float* pos, float span, float mul)
242 {
243     Math::set3(pos, _wingCenter);
244     _groundEffectSpan = span;
245     _groundEffect = mul;
246 }
247
248 // The first three elements are a unit vector pointing from the global
249 // origin to the plane, the final element is the distance from the
250 // origin (the radius of the earth, in most implementations).  So
251 // (v dot _ground)-_ground[3] gives the distance AGL.
252 void Model::setGroundPlane(double* planeNormal, double fromOrigin)
253 {
254     int i;
255     for(i=0; i<3; i++) _ground[i] = planeNormal[i];
256     _ground[3] = fromOrigin;
257 }
258
259 void Model::setAir(float pressure, float temp, float density)
260 {
261     _pressure = pressure;
262     _temp = temp;
263     _rho = density;
264 }
265
266 void Model::setWind(float* wind)
267 {
268     Math::set3(wind, _wind);
269 }
270
271 void Model::calcForces(State* s)
272 {
273     // Add in the pre-computed stuff.  These values aren't part of the
274     // Runge-Kutta integration (they don't depend on position or
275     // velocity), and are therefore constant across the four calls to
276     // calcForces.  They get computed before we begin the integration
277     // step.
278     _body.setGyro(_gyro);
279     _body.addTorque(_torque);
280     int i;
281     for(i=0; i<_thrusters.size(); i++) {
282         Thruster* t = (Thruster*)_thrusters.get(i);
283         float thrust[3], pos[3];
284         t->getThrust(thrust);
285         t->getPosition(pos);
286         _body.addForce(pos, thrust);
287     }
288
289     // Gravity, convert to a force, then to local coordinates
290     float grav[3];
291     Glue::geodUp(s->pos, grav);
292     Math::mul3(-9.8f * _body.getTotalMass(), grav, grav);
293     Math::vmul33(s->orient, grav, grav);
294     _body.addForce(grav);
295
296     // Do each surface, remembering that the local velocity at each
297     // point is different due to rotation.
298     float faero[3];
299     faero[0] = faero[1] = faero[2] = 0;
300     for(i=0; i<_surfaces.size(); i++) {
301         Surface* sf = (Surface*)_surfaces.get(i);
302
303         // Vsurf = wind - velocity + (rot cross (cg - pos))
304         float vs[3], pos[3];
305         sf->getPosition(pos);
306         localWind(pos, s, vs);
307
308         float force[3], torque[3];
309         sf->calcForce(vs, _rho, force, torque);
310         Math::add3(faero, force, faero);
311
312         _body.addForce(pos, force);
313         _body.addTorque(torque);
314     }
315     for(i=0; i<_rotorparts.size(); i++) {
316         Rotorpart* sf = (Rotorpart*)_rotorparts.get(i);
317
318         // Vsurf = wind - velocity + (rot cross (cg - pos))
319         float vs[3], pos[3];
320         sf->getPosition(pos);
321         localWind(pos, s, vs);
322
323         float force[3], torque[3];
324         sf->calcForce(vs, _rho, force, torque);
325         //Math::add3(faero, force, faero);
326
327         sf->getPositionForceAttac(pos);
328
329         _body.addForce(pos, force);
330         _body.addTorque(torque);
331     }
332     for(i=0; i<_rotorblades.size(); i++) {
333         Rotorblade* sf = (Rotorblade*)_rotorblades.get(i);
334
335         // Vsurf = wind - velocity + (rot cross (cg - pos))
336         float vs[3], pos[3];
337         sf->getPosition(pos);
338         localWind(pos, s, vs);
339
340         float force[3], torque[3];
341         sf->calcForce(vs, _rho, force, torque);
342         //Math::add3(faero, force, faero);
343
344         sf->getPositionForceAttac(pos);
345
346         _body.addForce(pos, force);
347         _body.addTorque(torque);
348     }
349
350     // Get a ground plane in local coordinates.  The first three
351     // elements are the normal vector, the final one is the distance
352     // from the local origin along that vector to the ground plane
353     // (negative for objects "above" the ground)
354     float ground[4];
355     ground[3] = localGround(s, ground);
356
357     // Account for ground effect by multiplying the vertical force
358     // component by an amount linear with the fraction of the wingspan
359     // above the ground.
360     float dist = ground[3] - Math::dot3(ground, _wingCenter);
361     if(dist > 0 && dist < _groundEffectSpan) {
362         float fz = Math::dot3(faero, ground);
363         fz *= (_groundEffectSpan - dist) / _groundEffectSpan;
364         fz *= _groundEffect;
365         Math::mul3(fz, ground, faero);
366         _body.addForce(faero);
367     }
368     
369     // Convert the velocity and rotation vectors to local coordinates
370     float lrot[3], lv[3];
371     Math::vmul33(s->orient, s->rot, lrot);
372     Math::vmul33(s->orient, s->v, lv);
373
374     // The landing gear
375     for(i=0; i<_gears.size(); i++) {
376         float force[3], contact[3];
377         Gear* g = (Gear*)_gears.get(i);
378         g->calcForce(&_body, lv, lrot, ground);
379         g->getForce(force, contact);
380         _body.addForce(contact, force);
381     }
382 }
383
384 void Model::newState(State* s)
385 {
386     _s = s;
387
388     // Some simple collision detection
389     float min = 1e8;
390     float ground[4], pos[3], cmpr[3];
391     ground[3] = localGround(s, ground);
392     int i;
393     for(i=0; i<_gears.size(); i++) {
394         Gear* g = (Gear*)_gears.get(i);
395
396         // Get the point of ground contact
397         g->getPosition(pos);
398         g->getCompression(cmpr);
399         Math::mul3(g->getCompressFraction(), cmpr, cmpr);
400         Math::add3(cmpr, pos, pos);
401         float dist = ground[3] - Math::dot3(pos, ground);
402
403         // Find the lowest one
404         if(dist < min)
405             min = dist;
406     }
407     _agl = min;
408     if(_agl < -1) // Allow for some integration slop
409         _crashed = true;
410 }
411
412 // Returns a unit "down" vector for the ground in out, and the
413 // distance from the local origin to the ground as the return value.
414 // So for a given position V, "dist - (V dot out)" will be the height
415 // AGL.
416 float Model::localGround(State* s, float* out)
417 {
418     // Get the ground's "down" vector, this can be in floats, because
419     // we don't need positioning accuracy.  The direction has plenty
420     // of accuracy after truncation.
421     out[0] = -(float)_ground[0];
422     out[1] = -(float)_ground[1];
423     out[2] = -(float)_ground[2];
424     Math::vmul33(s->orient, out, out);
425
426     // The distance from the ground to the Aircraft's origin:
427     double dist = (s->pos[0]*_ground[0]
428                    + s->pos[1]*_ground[1]
429                    + s->pos[2]*_ground[2] - _ground[3]);
430
431     return (float)dist;
432 }
433
434 // Calculates the airflow direction at the given point and for the
435 // specified aircraft velocity.
436 void Model::localWind(float* pos, State* s, float* out)
437 {
438     float tmp[3], lwind[3], lrot[3], lv[3];
439
440     // Get a global coordinate for our local position, and calculate
441     // turbulence.
442     // FIXME: modify turbulence for altitude, attenuating the vertical
443     // component near the ground.
444     if(_turb) {
445         double gpos[3];
446         Math::tmul33(s->orient, pos, tmp);
447         for(int i=0; i<3; i++) gpos[i] = s->pos[i] + tmp[i];
448         _turb->getTurbulence(gpos, lwind);
449         Math::add3(_wind, lwind, lwind);
450     } else {
451         Math::set3(_wind, lwind);
452     }
453
454     // Convert to local coordinates
455     Math::vmul33(s->orient, lwind, lwind);
456     Math::vmul33(s->orient, s->rot, lrot);
457     Math::vmul33(s->orient, s->v, lv);
458
459     _body.pointVelocity(pos, lrot, out); // rotational velocity
460     Math::mul3(-1, out, out);      //  (negated)
461     Math::add3(lwind, out, out);    //  + wind
462     Math::sub3(out, lv, out);       //  - velocity
463 }
464
465 }; // namespace yasim