2 #include "BodyEnvironment.hpp"
4 #include "RigidBody.hpp"
6 #include "Launchbar.hpp"
10 static const float YASIM_PI2 = 3.14159265358979323846/2;
11 static const float YASIM_PI = 3.14159265358979323846;
12 static const float RAD2DEG = 180/YASIM_PI;
14 Launchbar::Launchbar()
18 _launchbar_mount[i] = _holdback_mount[i] = _launchbar_force[i]
19 = _holdback_force[i] = 0;
21 _global_ground[i] = 0;
22 _global_ground[2] = 1;
23 _global_ground[3] = -1e5;
25 _holdback_length = 2.0;
37 void Launchbar::setLaunchbarMount(float* position)
40 for(i=0; i<3; i++) _launchbar_mount[i] = position[i];
43 void Launchbar::setHoldbackMount(float* position)
46 for(i=0; i<3; i++) _holdback_mount[i] = position[i];
49 void Launchbar::setLength(float length)
54 void Launchbar::setHoldbackLength(float length)
56 _holdback_length = length;
59 void Launchbar::setDownAngle(float ang)
64 void Launchbar::setUpAngle(float ang)
69 void Launchbar::setExtension(float extension)
71 _extension = extension;
74 void Launchbar::setLaunchCmd(bool cmd)
79 void Launchbar::setGlobalGround(double *global_ground)
82 for(i=0; i<4; i++) _global_ground[i] = global_ground[i];
85 void Launchbar::getLaunchbarMount(float* out)
88 for(i=0; i<3; i++) out[i] = _launchbar_mount[i];
91 float Launchbar::getLaunchbarPos(int i)
93 return _launchbar_mount[i];
96 void Launchbar::getHoldbackMount(float* out)
99 for(i=0; i<3; i++) out[i] = _holdback_mount[i];
102 float Launchbar::getHoldbackPos(int j)
104 return _holdback_mount[j];
107 float Launchbar::getHoldbackLength(void)
109 return _holdback_length;
112 float Launchbar::getLength(void)
117 float Launchbar::getDownAngle(void)
122 float Launchbar::getUpAngle(void)
127 float Launchbar::getAngle(void)
132 float Launchbar::getHoldbackAngle(void)
136 float Launchbar::getExtension(void)
141 void Launchbar::getForce(float* force1, float* off1,
142 float* force2, float* off2)
144 Math::set3(_launchbar_force, force1);
145 Math::set3(_launchbar_mount, off1);
146 Math::set3(_holdback_force, force2);
147 Math::set3(_holdback_mount, off2);
150 const char* Launchbar::getState(void)
164 bool Launchbar::getStrop(void)
169 float Launchbar::getCompressFraction()
174 float Launchbar::getHoldbackCompressFraction()
179 void Launchbar::getTipPosition(float* out)
181 // The launchbar tip in local coordinates.
183 _ang = _frac*(_down_ang - _up_ang ) + _up_ang ;
184 float ptip[3] = { _length*Math::cos(_ang), 0, -_length*Math::sin(_ang) };
185 Math::add3(_launchbar_mount, ptip, out);
188 float Launchbar::getTipPos(int i)
191 getTipPosition(pos_tip);
195 void Launchbar::getHoldbackTipPosition(float* out)
197 // The holdback tip in local coordinates.
198 _h_ang = _h_frac*(_down_ang - _up_ang) + _up_ang;
199 float htip[3] = { -_length*Math::cos(_h_ang), 0, -_length*Math::sin(_h_ang) };
200 Math::add3(_holdback_mount, htip, out);
203 float Launchbar::getHoldbackTipPos(int i)
206 getHoldbackTipPosition(pos_tip);
211 void Launchbar::getTipGlobalPosition(State* s, double* out)
213 // The launchbar tip in local coordinates.
215 getTipPosition(pos_tip);
216 // The launchbar tip in global coordinates.
217 s->posLocalToGlobal(pos_tip, out);
220 float Launchbar::getPercentPosOnCat(float* lpos, float off, float lends[2][3])
222 // Compute the forward direction of the cat.
224 Math::sub3(lends[1], lends[0], lforward);
226 Math::sub3(lpos, lends[0], ltopos);
227 float fwlen = Math::mag3(lforward);
229 return (Math::dot3(ltopos, lforward)/fwlen + off)/fwlen;
232 void Launchbar::getPosOnCat(float perc, float* lpos, float* lvel,
233 float lends[2][3], float lendvels[2][3])
240 // Compute the forward direction of the cat.
242 Math::sub3(lends[1], lends[0], lforward);
243 Math::mul3(perc, lforward, lpos);
244 Math::add3(lends[0], lpos, lpos);
247 Math::mul3(perc, lendvels[0], lvel);
248 Math::mul3(1.0-perc, lendvels[1], tmp);
249 Math::add3(tmp, lvel, lvel);
252 void Launchbar::calcForce(Ground *g_cb, RigidBody* body, State* s, float* lv, float* lrot)
254 // Init the return values
256 for(i=0; i<3; i++) _launchbar_force[i] = 0;
257 for(i=0; i<3; i++) _holdback_force[i] = 0;
259 if (_state != Unmounted)
262 // Don't bother if it's fully retracted
266 // For the first guess, the position fraction is equal to the
268 _frac = _h_frac = _extension;
270 // The ground plane transformed to the local frame.
272 s->planeGlobalToLocal(_global_ground, ground);
274 // The launchbar tip in local coordinates.
276 getTipPosition(ltip);
278 // Correct the extension value for no intersection.
280 // Check if the tip will intersect the ground or not. That is, compute
281 // the distance of the tip to the ground plane.
282 float tipdist = ground[3] - Math::dot3(ltip, ground);
286 // Compute the distance of the launchbar mount point from the
288 float mountdist = ground[3] - Math::dot3(_launchbar_mount, ground);
290 // Compute the distance of the launchbar mount point from the
291 // ground plane in the x-z plane. It holds:
292 // mountdist = mountdist_xz*cos(angle(normal_yz, e_z))
294 float mountdist_xz = _length;
295 if (ground[2] != 0) {
296 float nrm_yz = Math::sqrt(ground[1]*ground[1]+ground[2]*ground[2]);
297 mountdist_xz = -mountdist*nrm_yz/ground[2];
300 if (mountdist_xz < _length) {
301 // the launchbar points forward, so we need to change the signs here
302 float ang = -Math::asin(mountdist_xz/_length)
303 + Math::atan2(ground[2], ground[0]) + YASIM_PI2;
305 _frac = (ang - _up_ang)/(_down_ang - _up_ang);
311 // Now do it again for the holdback
313 // The holdback tip in local coordinates.
315 getHoldbackTipPosition(htip);
317 // Check if the tip will intersect the ground or not. That is, compute
318 // the distance of the tip to the ground plane.
319 float h_tipdist = ground[3] - Math::dot3(htip, ground);
320 if (0 <= h_tipdist) {
321 _h_frac = _extension;
323 // Compute the distance of the holdback mount point from the ground
325 float h_mountdist = ground[3] - Math::dot3(_holdback_mount, ground);
327 // Compute the distance of the holdback mount point from the ground
328 // plane in the x-z plane. It holds:
329 // mountdist = mountdist_xz*cos(angle(normal_yz, e_z))
331 float h_mountdist_xz = _holdback_length;
332 if (ground[2] != 0) {
333 float nrm_yz = Math::sqrt(ground[1]*ground[1]+ground[2]*ground[2]);
334 h_mountdist_xz = -h_mountdist*nrm_yz/ground[2];
337 if (h_mountdist_xz < _holdback_length) {
338 float h_ang = Math::asin(h_mountdist_xz/_holdback_length)
339 + Math::atan2(ground[2], ground[0]) + YASIM_PI2;
340 _h_frac = (h_ang - _up_ang)/(_down_ang - _up_ang);
342 _h_frac = _extension;
347 getTipPosition(llb_mount);
349 // The launchbar tip in global coordinates.
350 double launchbar_pos[3];
351 s->posLocalToGlobal(llb_mount, launchbar_pos);
353 double end[2][3]; float vel[2][3];
354 float dist = g_cb->getCatapult(launchbar_pos, end, vel);
355 // Work around a problem of flightgear returning totally screwed up
356 // scenery when switching views.
360 // Compute the positions of the catapult start and endpoints in the
361 // local coordinate system
363 s->posGlobalToLocal(end[0], lend[0]);
364 s->posGlobalToLocal(end[1], lend[1]);
366 // Transform the velocities of the endpoints to the
367 // local coordinate sytem.
369 s->velGlobalToLocal(vel[0], lvel[0]);
370 s->velGlobalToLocal(vel[1], lvel[1]);
372 // Compute the position of the launchbar tip relative to the cat.
373 float tip_pos_on_cat = getPercentPosOnCat(llb_mount, 0.0, lend);
374 float llbtip[3], lvlbtip[3];
375 getPosOnCat(tip_pos_on_cat, llbtip, lvlbtip, lend, lvel);
377 // Compute the direction from the launchbar mount at the gear
378 // to the lauchbar mount on the cat.
380 Math::sub3(llbtip, _launchbar_mount, llbdir);
381 float lblen = Math::mag3(llbdir);
382 Math::mul3(1.0/lblen, llbdir, llbdir);
384 // Check if we are near enough to the cat.
385 if (_state == Unmounted && dist < 0.6) {
386 // coarse approximation for the velocity of the launchbar.
387 // Might be sufficient because arresting at the cat makes only
388 // sense when the aircraft does not rotate much.
391 float lrot[3], lv[3];
392 Math::vmul33(s->orient, s->rot, lrot);
393 Math::vmul33(s->orient, s->v, lv);
394 body->pointVelocity(llb_mount, lrot, tmp);
395 Math::sub3(tmp, lvlbtip, lv_mount);
396 Math::add3(lv, lv_mount, lv_mount);
398 // We cannot arrest at the cat if we move too fast wrt the cat.
399 if (0.2 < Math::mag3(lv_mount))
402 // Compute the position of the holdback mount relative to the cat.
403 double dd[2][3]; float fd[2][3]; double ghldbkpos[3];
404 s->posLocalToGlobal(_holdback_mount, ghldbkpos);
405 float hbdist = g_cb->getCatapult(ghldbkpos, dd, fd);
407 // don't let the calculation go -ve here
408 if (_holdback_length*_holdback_length - hbdist*hbdist < 0)
410 float offset = -Math::sqrt(_holdback_length*_holdback_length
412 _pos_on_cat = getPercentPosOnCat(_holdback_mount, offset, lend);
414 // We cannot arrest if we are not at the start of the cat.
415 if (_pos_on_cat < 0.0 || 0.4 < _pos_on_cat)
418 // Now we are arrested at the cat.
419 // The force is applied at the next step.
424 // Get the actual distance from the holdback to its mountpoint
425 // on the cat. If it is longer than the holdback apply a force.
426 float lhldbk_cmount[3]; float lvhldbk_cmount[3];
427 getPosOnCat(_pos_on_cat, lhldbk_cmount, lvhldbk_cmount, lend, lvel);
428 // Compute the direction of holdback.
430 Math::sub3(lhldbk_cmount, _holdback_mount, lhldbkdir);
431 float hldbklen = Math::mag3(lhldbkdir);
432 Math::mul3(1/hldbklen, lhldbkdir, lhldbkdir);
434 if (_state == Arrested) {
435 // Now apply a constant tension from the catapult over the launchbar.
436 Math::mul3(2.0, llbdir, _launchbar_force);
438 // If the distance from the holdback mount at the aircraft to the
439 // holdback mount on the cat is larger than the holdback length itself,
440 // the holdback applies a force to the gear.
441 if (_holdback_length < hldbklen) {
442 // coarse approximation for the velocity of the holdback mount
444 // Might be sufficient because arresting at the cat makes only
445 // sense when the aircraft does not rotate much.
446 float lvhldbk_gmount[3];
447 float lrot[3], lv[3];
448 Math::vmul33(s->orient, s->rot, lrot);
449 Math::vmul33(s->orient, s->v, lv);
450 body->pointVelocity(_holdback_mount, lrot, lvhldbk_gmount);
451 Math::add3(lv, lvhldbk_gmount, lvhldbk_gmount);
453 // The velocity of the holdback mount at the gear wrt the
454 // holdback mount at the cat.
456 Math::sub3(lvhldbk_gmount, lvhldbk_cmount, lvhldbk);
458 // The spring force the holdback will apply to the gear
460 Math::mul3(1e1*(hldbklen - _holdback_length), lhldbkdir,
463 // The damping force here ...
464 Math::mul3(2e0, lvhldbk, tmp);
465 Math::sub3(_holdback_force, tmp, _holdback_force);
474 if (_state == Launch) {
475 // Now apply a constant tension from the catapult over the launchbar.
476 Math::mul3(25.0, llbdir, _launchbar_force);
483 if (_state == Completed) {
484 // Wait until the strop has cleared the deck
485 // This is a temporary fix until we come up with something better
493 // Scale by the mass. That keeps the stiffness in reasonable bounds.
494 float mass = body->getTotalMass();
495 Math::mul3(mass, _launchbar_force, _launchbar_force);
496 Math::mul3(mass, _holdback_force, _holdback_force);
499 }; // namespace yasim