]> git.mxchange.org Git - flightgear.git/blob - src/FDM/YASim/Model.cpp
Updated to YASim-0.1.1
[flightgear.git] / src / FDM / YASim / Model.cpp
1 #include <stdio.h>
2
3 #include "Atmosphere.hpp"
4 #include "Thruster.hpp"
5 #include "Math.hpp"
6 #include "RigidBody.hpp"
7 #include "Integrator.hpp"
8 #include "Propeller.hpp"
9 #include "PistonEngine.hpp"
10 #include "Gear.hpp"
11 #include "Surface.hpp"
12 #include "Glue.hpp"
13
14 #include "Model.hpp"
15 namespace yasim {
16
17 void printState(State* s)
18 {
19     State tmp = *s;
20     Math::vmul33(tmp.orient, tmp.v, tmp.v);
21     Math::vmul33(tmp.orient, tmp.acc, tmp.acc);
22     Math::vmul33(tmp.orient, tmp.rot, tmp.rot);
23     Math::vmul33(tmp.orient, tmp.racc, tmp.racc);
24
25     printf("\nNEW STATE (LOCAL COORDS)\n");
26     printf("pos: %10.2f %10.2f %10.2f\n", tmp.pos[0], tmp.pos[1], tmp.pos[2]);
27     printf("o:   ");
28     for(int i=0; i<3; i++) {
29         if(i != 0) printf("     ");
30         printf("%6.2f %6.2f %6.2f\n",
31                tmp.orient[3*i+0], tmp.orient[3*i+1], tmp.orient[3*i+2]);
32     }
33     printf("v:   %6.2f %6.2f %6.2f\n", tmp.v[0], tmp.v[1], tmp.v[2]);
34     printf("acc: %6.2f %6.2f %6.2f\n", tmp.acc[0], tmp.acc[1], tmp.acc[2]);
35     printf("rot: %6.2f %6.2f %6.2f\n", tmp.rot[0], tmp.rot[1], tmp.rot[2]);
36     printf("rac: %6.2f %6.2f %6.2f\n", tmp.racc[0], tmp.racc[1], tmp.racc[2]);
37 }
38
39 Model::Model()
40 {
41     for(int i=0; i<3; i++) _wind[i] = 0;
42
43     _integrator.setBody(&_body);
44     _integrator.setEnvironment(this);
45
46     // Default value of 30 Hz
47     _integrator.setInterval(1.0/30.0);
48 }
49
50 Model::~Model()
51 {
52     // FIXME: who owns these things?  Need a policy
53 }
54
55 void Model::getThrust(float* out)
56 {
57     float tmp[3];
58     out[0] = out[1] = out[2] = 0;
59     for(int i=0; i<_thrusters.size(); i++) {
60         Thruster* t = (Thruster*)_thrusters.get(i);
61         t->getThrust(tmp);
62         Math::add3(tmp, out, out);
63     }
64 }
65
66 void Model::initIteration()
67 {
68     // Precompute torque and angular momentum for the thrusters
69     for(int i=0; i<3; i++)
70         _gyro[i] = _torque[i] = 0;
71     for(int i=0; i<_thrusters.size(); i++) {
72         Thruster* t = (Thruster*)_thrusters.get(i);
73         
74         // Get the wind velocity at the thruster location
75         float pos[3], v[3];
76         t->getPosition(pos);
77         localWind(pos, _s, v);
78
79         t->setWind(v);
80         t->setAir(_P, _T);
81         t->integrate(_integrator.getInterval());
82
83         t->getTorque(v);
84         Math::add3(v, _torque, _torque);
85
86         t->getGyro(v);
87         Math::add3(v, _gyro, _gyro);
88     }
89 }
90
91 void Model::iterate()
92 {
93     initIteration();
94     _body.recalc(); // FIXME: amortize this, somehow
95     _integrator.calcNewInterval();
96 }
97
98 State* Model::getState()
99 {
100     return _s;
101 }
102
103 void Model::setState(State* s)
104 {
105     _integrator.setState(s);
106     _s = _integrator.getState();
107 }
108
109 RigidBody* Model::getBody()
110 {
111     return &_body;
112 }
113
114 Integrator* Model::getIntegrator()
115 {
116     return &_integrator;
117 }
118
119 Surface* Model::getSurface(int handle)
120 {
121     return (Surface*)_surfaces.get(handle);
122 }
123
124 int Model::addThruster(Thruster* t)
125 {
126     return _thrusters.add(t);
127 }
128
129 int Model::numThrusters()
130 {
131     return _thrusters.size();
132 }
133
134 Thruster* Model::getThruster(int handle)
135 {
136     return (Thruster*)_thrusters.get(handle);
137 }
138
139 void Model::setThruster(int handle, Thruster* t)
140 {
141     _thrusters.set(handle, t);
142 }
143
144 int Model::addSurface(Surface* surf)
145 {
146     return _surfaces.add(surf);
147 }
148
149 int Model::addGear(Gear* gear)
150 {
151     return _gears.add(gear);
152 }
153
154 void Model::setGroundEffect(float* pos, float span, float mul)
155 {
156     Math::set3(pos, _wingCenter);
157     _groundEffectSpan = span;
158     _groundEffect = mul;
159 }
160
161 // The first three elements are a unit vector pointing from the global
162 // origin to the plane, the final element is the distance from the
163 // origin (the radius of the earth, in most implementations).  So
164 // (v dot _ground)-_ground[3] gives the distance AGL.
165 void Model::setGroundPlane(double* planeNormal, double fromOrigin)
166 {
167     for(int i=0; i<3; i++) _ground[i] = planeNormal[i];
168     _ground[3] = fromOrigin;
169 }
170
171 void Model::setAir(float pressure, float temp)
172 {
173     _P = pressure;
174     _T = temp;
175     _rho = Atmosphere::calcDensity(pressure, temp);
176 }
177
178 void Model::setWind(float* wind)
179 {
180     Math::set3(wind, _wind);
181 }
182
183 void Model::calcForces(State* s)
184 {
185     // Add in the pre-computed stuff.  These values aren't part of the
186     // Runge-Kutta integration (they don't depend on position or
187     // velocity), and are therefore constant across the four calls to
188     // calcForces.  They get computed before we begin the integration
189     // step.
190     _body.setGyro(_gyro);
191     _body.addTorque(_torque);
192     for(int i=0; i<_thrusters.size(); i++) {
193         Thruster* t = (Thruster*)_thrusters.get(i);
194         float thrust[3], pos[3];
195         t->getThrust(thrust);
196         t->getPosition(pos);
197         _body.addForce(pos, thrust);
198     }
199
200     // Gravity, convert to a force, then to local coordinates
201     float grav[3];
202     Glue::geodUp(s->pos, grav);
203     Math::mul3(-9.8 * _body.getTotalMass(), grav, grav);
204     Math::vmul33(s->orient, grav, grav);
205     _body.addForce(grav);
206
207     // Do each surface, remembering that the local velocity at each
208     // point is different due to rotation.
209     float faero[3];
210     faero[0] = faero[1] = faero[2] = 0;
211     for(int i=0; i<_surfaces.size(); i++) {
212         Surface* sf = (Surface*)_surfaces.get(i);
213
214         // Vsurf = wind - velocity + (rot cross (cg - pos))
215         float vs[3], pos[3];
216         sf->getPosition(pos);
217         localWind(pos, s, vs);
218
219         float force[3], torque[3];
220         sf->calcForce(vs, _rho, force, torque);
221         Math::add3(faero, force, faero);
222         _body.addForce(pos, force);
223         _body.addTorque(torque);
224     }
225
226     // Get a ground plane in local coordinates.  The first three
227     // elements are the normal vector, the final one is the distance
228     // from the local origin along that vector to the ground plane
229     // (negative for objects "above" the ground)
230     float ground[4];
231     ground[3] = localGround(s, ground);
232
233     // Account for ground effect by multiplying the vertical force
234     // component by an amount linear with the fraction of the wingspan
235     // above the ground.
236     float dist = ground[4] - Math::dot3(ground, _wingCenter);
237     if(dist > 0 && dist < _groundEffectSpan) {
238         float fz = Math::dot3(faero, ground);
239         Math::mul3(fz * _groundEffect * dist/_groundEffectSpan,
240                    ground, faero);
241         _body.addForce(faero);
242     }
243     
244     // Convert the velocity and rotation vectors to local coordinates
245     float lrot[3], lv[3];
246     Math::vmul33(s->orient, s->rot, lrot);
247     Math::vmul33(s->orient, s->v, lv);
248
249     // The landing gear
250     for(int i=0; i<_gears.size(); i++) {
251         float force[3], contact[3];
252         Gear* g = (Gear*)_gears.get(i);
253         g->calcForce(&_body, lv, lrot, ground);
254         g->getForce(force, contact);
255         _body.addForce(contact, force);
256     }
257 }
258
259 void Model::newState(State* s)
260 {
261     _s = s;
262
263     //printState(s);
264
265     // Some simple collision detection
266     float ground[4], pos[3], cmpr[3];
267     ground[3] = localGround(s, ground);
268     for(int i=0; i<_gears.size(); i++) {
269         Gear* g = (Gear*)_gears.get(i);
270         g->getPosition(pos);
271         g->getCompression(cmpr);
272         Math::add3(cmpr, pos, pos);
273         float dist = ground[3] - Math::dot3(pos, ground);
274         if(dist < 0) {
275             printf("CRASH: gear %d\n", i);
276             *(int*)0=0;
277         }
278     }
279 }
280
281 // Returns a unit "down" vector for the ground in out, and the
282 // distance from the local origin to the ground as the return value.
283 // So for a given position V, "dist - (V dot out)" will be the height
284 // AGL.
285 float Model::localGround(State* s, float* out)
286 {
287     // Get the ground's "down" vector, this can be in floats, because
288     // we don't need positioning accuracy.  The direction has plenty
289     // of accuracy after truncation.
290     out[0] = -(float)_ground[0];
291     out[1] = -(float)_ground[1];
292     out[2] = -(float)_ground[2];
293     Math::vmul33(s->orient, out, out);
294
295     // The distance from the ground to the Aircraft's origin:
296     double dist = (s->pos[0]*_ground[0]
297                    + s->pos[1]*_ground[1]
298                    + s->pos[2]*_ground[2] - _ground[3]);
299
300     return (float)dist;
301 }
302
303 // Calculates the airflow direction at the given point and for the
304 // specified aircraft velocity.
305 void Model::localWind(float* pos, State* s, float* out)
306 {
307     // Most of the input is in global coordinates.  Fix that.
308     float lwind[3], lrot[3], lv[3];
309     Math::vmul33(s->orient, _wind, lwind);
310     Math::vmul33(s->orient, s->rot, lrot);
311     Math::vmul33(s->orient, s->v, lv);
312
313     _body.pointVelocity(pos, lrot, out); // rotational velocity
314     Math::mul3(-1, out, out);      //  (negated)
315     Math::add3(lwind, out, out);    //  + wind
316     Math::sub3(out, lv, out);       //  - velocity
317 }
318
319 }; // namespace yasim