Debian Codegraph @

src/FDM/YASim/Model.cpp

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