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