]> git.mxchange.org Git - flightgear.git/blob - src/FDM/YASim/Model.cpp
Helicopter update from Maik:
[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 "Hook.hpp"
10 #include "Launchbar.hpp"
11 #include "Surface.hpp"
12 #include "Rotor.hpp"
13 #include "Rotorpart.hpp"
14 #include "Glue.hpp"
15 #include "Ground.hpp"
16
17 #include "Model.hpp"
18 namespace yasim {
19
20 #if 0
21 void printState(State* s)
22 {
23     State tmp = *s;
24     Math::vmul33(tmp.orient, tmp.v, tmp.v);
25     Math::vmul33(tmp.orient, tmp.acc, tmp.acc);
26     Math::vmul33(tmp.orient, tmp.rot, tmp.rot);
27     Math::vmul33(tmp.orient, tmp.racc, tmp.racc);
28
29     printf("\nNEW STATE (LOCAL COORDS)\n");
30     printf("pos: %10.2f %10.2f %10.2f\n", tmp.pos[0], tmp.pos[1], tmp.pos[2]);
31     printf("o:   ");
32     int i;
33     for(i=0; i<3; i++) {
34         if(i != 0) printf("     ");
35         printf("%6.2f %6.2f %6.2f\n",
36                tmp.orient[3*i+0], tmp.orient[3*i+1], tmp.orient[3*i+2]);
37     }
38     printf("v:   %6.2f %6.2f %6.2f\n", tmp.v[0], tmp.v[1], tmp.v[2]);
39     printf("acc: %6.2f %6.2f %6.2f\n", tmp.acc[0], tmp.acc[1], tmp.acc[2]);
40     printf("rot: %6.2f %6.2f %6.2f\n", tmp.rot[0], tmp.rot[1], tmp.rot[2]);
41     printf("rac: %6.2f %6.2f %6.2f\n", tmp.racc[0], tmp.racc[1], tmp.racc[2]);
42 }
43 #endif
44
45 Model::Model()
46 {
47     int i;
48     for(i=0; i<3; i++) _wind[i] = 0;
49
50     _integrator.setBody(&_body);
51     _integrator.setEnvironment(this);
52
53     // Default value of 30 Hz
54     _integrator.setInterval(1.0f/30.0f);
55
56     _agl = 0;
57     _crashed = false;
58     _turb = 0;
59     _ground_cb = new Ground();
60     _hook = 0;
61     _launchbar = 0;
62
63     _groundEffectSpan = 0;
64     _groundEffect = 0;
65     for(i=0; i<3; i++) _wingCenter[i] = 0;
66
67     _global_ground[0] = 0; _global_ground[1] = 0; _global_ground[2] = 1;
68     _global_ground[3] = -100000;
69
70 }
71
72 Model::~Model()
73 {
74     // FIXME: who owns these things?  Need a policy
75     delete _ground_cb;
76     delete _hook;
77     delete _launchbar;
78 }
79
80 void Model::getThrust(float* out)
81 {
82     float tmp[3];
83     out[0] = out[1] = out[2] = 0;
84     int i;
85     for(i=0; i<_thrusters.size(); i++) {
86         Thruster* t = (Thruster*)_thrusters.get(i);
87         t->getThrust(tmp);
88         Math::add3(tmp, out, out);
89     }
90 }
91
92 void Model::initIteration()
93 {
94     // Precompute torque and angular momentum for the thrusters
95     int i;
96     for(i=0; i<3; i++)
97         _gyro[i] = _torque[i] = 0;
98
99     // Need a local altitude for the wind calculation
100     float lground[4];
101     _s->planeGlobalToLocal(_global_ground, lground);
102     float alt = Math::abs(lground[3]);
103
104     for(i=0; i<_thrusters.size(); i++) {
105         Thruster* t = (Thruster*)_thrusters.get(i);
106         
107         // Get the wind velocity at the thruster location
108         float pos[3], v[3];
109         t->getPosition(pos);
110         localWind(pos, _s, v, alt);
111
112         t->setWind(v);
113         t->setAir(_pressure, _temp, _rho);
114         t->integrate(_integrator.getInterval());
115
116         t->getTorque(v);
117         Math::add3(v, _torque, _torque);
118
119         t->getGyro(v);
120         Math::add3(v, _gyro, _gyro);
121     }
122
123     // Displace the turbulence coordinates according to the local wind.
124     if(_turb) {
125         float toff[3];
126         Math::mul3(_integrator.getInterval(), _wind, toff);
127         _turb->offset(toff);
128     }
129
130     
131 }
132
133 // This function initializes some variables for the rotor calculation
134 // Furthermore it integrates in "void Rotorpart::inititeration
135 // (float dt,float *rot)" the "rotor orientation" by omega*dt for the 
136 // 3D-visualization of the heli only. and it compensates the rotation 
137 // of the fuselage. The rotor does not follow the rotation of the fuselage.
138 // Therefore its rotation must be subtracted from the orientation of the 
139 // rotor.
140 // Maik
141 void Model::initRotorIteration()
142 {
143     float dt = _integrator.getInterval();
144     float lrot[3];
145     if (!_rotorgear.isInUse()) return;
146     Math::vmul33(_s->orient, _s->rot, lrot);
147     Math::mul3(dt,lrot,lrot);
148     _rotorgear.initRotorIteration(lrot,dt);
149 }
150
151 void Model::iterate()
152 {
153     initIteration();
154     initRotorIteration();
155     _body.recalc(); // FIXME: amortize this, somehow
156     _integrator.calcNewInterval();
157 }
158
159 bool Model::isCrashed()
160 {
161     return _crashed;
162 }
163
164 void Model::setCrashed(bool crashed)
165 {
166     _crashed = crashed;
167 }
168
169 float Model::getAGL()
170 {
171     return _agl;
172 }
173
174 State* Model::getState()
175 {
176     return _s;
177 }
178
179 void Model::setState(State* s)
180 {
181     _integrator.setState(s);
182     _s = _integrator.getState();
183 }
184
185 RigidBody* Model::getBody()
186 {
187     return &_body;
188 }
189
190 Integrator* Model::getIntegrator()
191 {
192     return &_integrator;
193 }
194
195 Surface* Model::getSurface(int handle)
196 {
197     return (Surface*)_surfaces.get(handle);
198 }
199
200 Rotorgear* Model::getRotorgear(void)
201 {
202     return &_rotorgear;
203 }
204
205 int Model::addThruster(Thruster* t)
206 {
207     return _thrusters.add(t);
208 }
209
210 Hook* Model::getHook(void)
211 {
212     return _hook;
213 }
214
215 Launchbar* Model::getLaunchbar(void)
216 {
217     return _launchbar;
218 }
219
220 int Model::numThrusters()
221 {
222     return _thrusters.size();
223 }
224
225 Thruster* Model::getThruster(int handle)
226 {
227     return (Thruster*)_thrusters.get(handle);
228 }
229
230 void Model::setThruster(int handle, Thruster* t)
231 {
232     _thrusters.set(handle, t);
233 }
234
235 int Model::addSurface(Surface* surf)
236 {
237     return _surfaces.add(surf);
238 }
239
240 int Model::addGear(Gear* gear)
241 {
242     return _gears.add(gear);
243 }
244
245 void Model::addHook(Hook* hook)
246 {
247     _hook = hook;
248 }
249
250 void Model::addLaunchbar(Launchbar* launchbar)
251 {
252     _launchbar = launchbar;
253 }
254
255 void Model::setGroundCallback(Ground* ground_cb)
256 {
257     delete _ground_cb;
258     _ground_cb = ground_cb;
259 }
260
261 Ground* Model::getGroundCallback(void)
262 {
263     return _ground_cb;
264 }
265
266 void Model::setGroundEffect(float* pos, float span, float mul)
267 {
268     Math::set3(pos, _wingCenter);
269     _groundEffectSpan = span;
270     _groundEffect = mul;
271 }
272
273 void Model::setAir(float pressure, float temp, float density)
274 {
275     _pressure = pressure;
276     _temp = temp;
277     _rho = density;
278 }
279
280 void Model::setWind(float* wind)
281 {
282     Math::set3(wind, _wind);
283 }
284
285 void Model::updateGround(State* s)
286 {
287     float dummy[3];
288     _ground_cb->getGroundPlane(s->pos, _global_ground, dummy);
289
290     int i;
291     // The landing gear
292     for(i=0; i<_gears.size(); i++) {
293         Gear* g = (Gear*)_gears.get(i);
294
295         // Get the point of ground contact
296         float pos[3], cmpr[3];
297         g->getPosition(pos);
298         g->getCompression(cmpr);
299
300         Math::mul3(g->getCompressFraction(), cmpr, cmpr);
301         Math::add3(cmpr, pos, pos);
302         // Transform the local coordinates of the contact point to
303         // global coordinates.
304         double pt[3];
305         s->posLocalToGlobal(pos, pt);
306
307         // Ask for the ground plane in the global coordinate system
308         double global_ground[4];
309         float global_vel[3];
310         _ground_cb->getGroundPlane(pt, global_ground, global_vel);
311         g->setGlobalGround(global_ground, global_vel);
312     }
313     for(i=0; i<_rotorgear.getRotors()->size(); i++) {
314         Rotor* r = (Rotor*)_rotorgear.getRotors()->get(i);
315         r->findGroundEffectAltitude(_ground_cb,s);
316     }
317
318     // The arrester hook
319     if(_hook) {
320         double pt[3];
321         _hook->getTipGlobalPosition(s, pt);
322         double global_ground[4];
323         _ground_cb->getGroundPlane(pt, global_ground, dummy);
324         _hook->setGlobalGround(global_ground);
325     }
326
327     // The launchbar/holdback
328     if(_launchbar) {
329         double pt[3];
330         _launchbar->getTipGlobalPosition(s, pt);
331         double global_ground[4];
332         _ground_cb->getGroundPlane(pt, global_ground, dummy);
333         _launchbar->setGlobalGround(global_ground);
334     }
335 }
336
337 void Model::calcForces(State* s)
338 {
339     // Add in the pre-computed stuff.  These values aren't part of the
340     // Runge-Kutta integration (they don't depend on position or
341     // velocity), and are therefore constant across the four calls to
342     // calcForces.  They get computed before we begin the integration
343     // step.
344     _body.setGyro(_gyro);
345     _body.addTorque(_torque);
346     int i,j;
347     for(i=0; i<_thrusters.size(); i++) {
348         Thruster* t = (Thruster*)_thrusters.get(i);
349         float thrust[3], pos[3];
350         t->getThrust(thrust);
351         t->getPosition(pos);
352         _body.addForce(pos, thrust);
353     }
354
355     // Get a ground plane in local coordinates.  The first three
356     // elements are the normal vector, the final one is the distance
357     // from the local origin along that vector to the ground plane
358     // (negative for objects "above" the ground)
359     float ground[4];
360     s->planeGlobalToLocal(_global_ground, ground);
361     float alt = Math::abs(ground[3]);
362
363     // Gravity, convert to a force, then to local coordinates
364     float grav[3];
365     Glue::geodUp(s->pos, grav);
366     Math::mul3(-9.8f * _body.getTotalMass(), grav, grav);
367     Math::vmul33(s->orient, grav, grav);
368     _body.addForce(grav);
369
370     // Do each surface, remembering that the local velocity at each
371     // point is different due to rotation.
372     float faero[3];
373     faero[0] = faero[1] = faero[2] = 0;
374     for(i=0; i<_surfaces.size(); i++) {
375         Surface* sf = (Surface*)_surfaces.get(i);
376
377         // Vsurf = wind - velocity + (rot cross (cg - pos))
378         float vs[3], pos[3];
379         sf->getPosition(pos);
380         localWind(pos, s, vs, alt);
381
382         float force[3], torque[3];
383         sf->calcForce(vs, _rho, force, torque);
384         Math::add3(faero, force, faero);
385
386         _body.addForce(pos, force);
387         _body.addTorque(torque);
388     }
389     for (j=0; j<_rotorgear.getRotors()->size();j++)
390     {
391         Rotor* r = (Rotor *)_rotorgear.getRotors()->get(j);
392         float vs[3], pos[3];
393         r->getPosition(pos);
394         localWind(pos, s, vs, alt);
395         r->calcLiftFactor(vs, _rho,s);
396         float tq=0; 
397         // total torque of rotor (scalar) for calculating new rotor rpm
398
399         for(i=0; i<r->_rotorparts.size(); i++) {
400             float torque_scalar=0;
401             Rotorpart* rp = (Rotorpart*)r->_rotorparts.get(i);
402
403             // Vsurf = wind - velocity + (rot cross (cg - pos))
404             float vs[3], pos[3];
405             rp->getPosition(pos);
406             localWind(pos, s, vs, alt,true);
407
408             float force[3], torque[3];
409             rp->calcForce(vs, _rho, force, torque, &torque_scalar);
410             tq+=torque_scalar;
411             rp->getPositionForceAttac(pos);
412
413             _body.addForce(pos, force);
414             _body.addTorque(torque);
415         }
416         r->setTorque(tq);
417     }
418     if (_rotorgear.isInUse())
419     {
420         float torque[3];
421         _rotorgear.calcForces(torque);
422         _body.addTorque(torque);
423     }
424
425     // Account for ground effect by multiplying the vertical force
426     // component by an amount linear with the fraction of the wingspan
427     // above the ground.
428     if ((_groundEffectSpan != 0) && (_groundEffect != 0 ))
429     {
430         float dist = ground[3] - Math::dot3(ground, _wingCenter);
431         if(dist > 0 && dist < _groundEffectSpan) {
432         float fz = Math::dot3(faero, ground);
433             fz *= (_groundEffectSpan - dist) / _groundEffectSpan;
434             fz *= _groundEffect;
435         Math::mul3(fz, ground, faero);
436         _body.addForce(faero);
437         }
438     }
439     
440     // Convert the velocity and rotation vectors to local coordinates
441     float lrot[3], lv[3];
442     Math::vmul33(s->orient, s->rot, lrot);
443     Math::vmul33(s->orient, s->v, lv);
444
445     // The landing gear
446     for(i=0; i<_gears.size(); i++) {
447         float force[3], contact[3];
448         Gear* g = (Gear*)_gears.get(i);
449
450         g->calcForce(&_body, s, lv, lrot);
451         g->getForce(force, contact);
452         _body.addForce(contact, force);
453     }
454
455     // The arrester hook
456     if(_hook) {
457         _hook->calcForce(_ground_cb, &_body, s, lv, lrot);
458         float force[3], contact[3];
459         _hook->getForce(force, contact);
460         _body.addForce(contact, force);
461     }
462
463     // The launchbar/holdback
464     if(_launchbar) {
465         _launchbar->calcForce(_ground_cb, &_body, s, lv, lrot);
466         float forcelb[3], contactlb[3], forcehb[3], contacthb[3];
467         _launchbar->getForce(forcelb, contactlb, forcehb, contacthb);
468         _body.addForce(contactlb, forcelb);
469         _body.addForce(contacthb, forcehb);
470     }
471 }
472
473 void Model::newState(State* s)
474 {
475     _s = s;
476
477     // Some simple collision detection
478     float min = 1e8;
479     int i;
480     for(i=0; i<_gears.size(); i++) {
481         Gear* g = (Gear*)_gears.get(i);
482
483         // Get the point of ground contact
484         float pos[3], cmpr[3];
485         g->getPosition(pos);
486         g->getCompression(cmpr);
487         Math::mul3(g->getCompressFraction(), cmpr, cmpr);
488         Math::add3(cmpr, pos, pos);
489
490         // The plane transformed to local coordinates.
491         double global_ground[4];
492         g->getGlobalGround(global_ground);
493         float ground[4];
494         s->planeGlobalToLocal(global_ground, ground);
495         float dist = ground[3] - Math::dot3(pos, ground);
496
497         // Find the lowest one
498         if(dist < min)
499             min = dist;
500     }
501     _agl = min;
502     if(_agl < -1) // Allow for some integration slop
503         _crashed = true;
504 }
505
506 // Calculates the airflow direction at the given point and for the
507 // specified aircraft velocity.
508 void Model::localWind(float* pos, State* s, float* out, float alt, bool is_rotor)
509 {
510     float tmp[3], lwind[3], lrot[3], lv[3];
511
512     // Get a global coordinate for our local position, and calculate
513     // turbulence.
514     if(_turb) {
515         double gpos[3]; float up[3];
516         Math::tmul33(s->orient, pos, tmp);
517         for(int i=0; i<3; i++) {
518             gpos[i] = s->pos[i] + tmp[i];
519         }
520         Glue::geodUp(gpos, up);
521         _turb->getTurbulence(gpos, alt, up, lwind);
522         Math::add3(_wind, lwind, lwind);
523     } else {
524         Math::set3(_wind, lwind);
525     }
526
527     // Convert to local coordinates
528     Math::vmul33(s->orient, lwind, lwind);
529     Math::vmul33(s->orient, s->rot, lrot);
530     Math::vmul33(s->orient, s->v, lv);
531
532     _body.pointVelocity(pos, lrot, out); // rotational velocity
533     Math::mul3(-1, out, out);      //  (negated)
534     Math::add3(lwind, out, out);    //  + wind
535     Math::sub3(out, lv, out);       //  - velocity
536
537     //add the downwash of the rotors if it is not self a rotor
538     if (_rotorgear.isInUse()&&!is_rotor)
539     {
540         _rotorgear.getDownWash(pos,lv,tmp);
541         Math::add3(out,tmp, out);    //  + downwash
542     }
543
544
545 }
546
547 }; // namespace yasim