2 #include "BodyEnvironment.hpp"
4 #include "RigidBody.hpp"
6 #include "Launchbar.hpp"
11 static const float YASIM_PI2 = 3.14159265358979323846/2;
12 static const float YASIM_PI = 3.14159265358979323846;
13 static const float RAD2DEG = 180/YASIM_PI;
15 Launchbar::Launchbar()
19 _launchbar_mount[i] = _holdback_mount[i] = _launchbar_force[i]
20 = _holdback_force[i] = 0;
22 _global_ground[i] = 0;
23 _global_ground[2] = 1;
24 _global_ground[3] = -1e5;
26 _holdback_length = 2.0;
38 void Launchbar::setLaunchbarMount(float* position)
41 for(i=0; i<3; i++) _launchbar_mount[i] = position[i];
44 void Launchbar::setHoldbackMount(float* position)
47 for(i=0; i<3; i++) _holdback_mount[i] = position[i];
50 void Launchbar::setLength(float length)
55 void Launchbar::setHoldbackLength(float length)
57 _holdback_length = length;
60 void Launchbar::setDownAngle(float ang)
65 void Launchbar::setUpAngle(float ang)
70 void Launchbar::setExtension(float extension)
72 _extension = extension;
75 void Launchbar::setLaunchCmd(bool cmd)
80 void Launchbar::setGlobalGround(double *global_ground)
83 for(i=0; i<4; i++) _global_ground[i] = global_ground[i];
86 void Launchbar::getLaunchbarMount(float* out)
89 for(i=0; i<3; i++) out[i] = _launchbar_mount[i];
92 float Launchbar::getLaunchbarPos(int i)
94 return _launchbar_mount[i];
97 void Launchbar::getHoldbackMount(float* out)
100 for(i=0; i<3; i++) out[i] = _holdback_mount[i];
103 float Launchbar::getHoldbackPos(int j)
105 return _holdback_mount[j];
108 float Launchbar::getHoldbackLength(void)
110 return _holdback_length;
113 float Launchbar::getLength(void)
118 float Launchbar::getDownAngle(void)
123 float Launchbar::getUpAngle(void)
128 float Launchbar::getAngle(void)
133 float Launchbar::getHoldbackAngle(void)
137 float Launchbar::getExtension(void)
142 void Launchbar::getForce(float* force1, float* off1,
143 float* force2, float* off2)
145 Math::set3(_launchbar_force, force1);
146 Math::set3(_launchbar_mount, off1);
147 Math::set3(_holdback_force, force2);
148 Math::set3(_holdback_mount, off2);
151 const char* Launchbar::getState(void)
165 bool Launchbar::getStrop(void)
170 float Launchbar::getCompressFraction()
175 float Launchbar::getHoldbackCompressFraction()
180 void Launchbar::getTipPosition(float* out)
182 // The launchbar tip in local coordinates.
184 _ang = _frac*(_down_ang - _up_ang ) + _up_ang ;
185 float ptip[3] = { _length*Math::cos(_ang), 0, -_length*Math::sin(_ang) };
186 Math::add3(_launchbar_mount, ptip, out);
189 float Launchbar::getTipPos(int i)
192 getTipPosition(pos_tip);
196 void Launchbar::getHoldbackTipPosition(float* out)
198 // The holdback tip in local coordinates.
199 _h_ang = _h_frac*(_down_ang - _up_ang) + _up_ang;
200 float htip[3] = { -_length*Math::cos(_h_ang), 0, -_length*Math::sin(_h_ang) };
201 Math::add3(_holdback_mount, htip, out);
204 float Launchbar::getHoldbackTipPos(int i)
207 getHoldbackTipPosition(pos_tip);
212 void Launchbar::getTipGlobalPosition(State* s, double* out)
214 // The launchbar tip in local coordinates.
216 getTipPosition(pos_tip);
217 // The launchbar tip in global coordinates.
218 s->posLocalToGlobal(pos_tip, out);
221 float Launchbar::getPercentPosOnCat(float* lpos, float off, float lends[2][3])
223 // Compute the forward direction of the cat.
225 Math::sub3(lends[1], lends[0], lforward);
227 Math::sub3(lpos, lends[0], ltopos);
228 float fwlen = Math::mag3(lforward);
230 return (Math::dot3(ltopos, lforward)/fwlen + off)/fwlen;
233 void Launchbar::getPosOnCat(float perc, float* lpos, float* lvel,
234 float lends[2][3], float lendvels[2][3])
241 // Compute the forward direction of the cat.
243 Math::sub3(lends[1], lends[0], lforward);
244 Math::mul3(perc, lforward, lpos);
245 Math::add3(lends[0], lpos, lpos);
248 Math::mul3(perc, lendvels[0], lvel);
249 Math::mul3(1.0-perc, lendvels[1], tmp);
250 Math::add3(tmp, lvel, lvel);
253 void Launchbar::calcForce(Ground *g_cb, RigidBody* body, State* s, float* lv, float* lrot)
255 // Init the return values
257 for(i=0; i<3; i++) _launchbar_force[i] = 0;
258 for(i=0; i<3; i++) _holdback_force[i] = 0;
260 if (_state != Unmounted)
263 // Don't bother if it's fully retracted
267 // For the first guess, the position fraction is equal to the
269 _frac = _h_frac = _extension;
271 // The ground plane transformed to the local frame.
273 s->planeGlobalToLocal(_global_ground, ground);
275 // The launchbar tip in local coordinates.
277 getTipPosition(ltip);
279 // Correct the extension value for no intersection.
281 // Check if the tip will intersect the ground or not. That is, compute
282 // the distance of the tip to the ground plane.
283 float tipdist = ground[3] - Math::dot3(ltip, ground);
287 // Compute the distance of the launchbar mount point from the
289 float mountdist = ground[3] - Math::dot3(_launchbar_mount, ground);
291 // Compute the distance of the launchbar mount point from the
292 // ground plane in the x-z plane. It holds:
293 // mountdist = mountdist_xz*cos(angle(normal_yz, e_z))
295 float mountdist_xz = _length;
296 if (ground[2] != 0) {
297 float nrm_yz = Math::sqrt(ground[1]*ground[1]+ground[2]*ground[2]);
298 mountdist_xz = -mountdist*nrm_yz/ground[2];
301 if (mountdist_xz < _length) {
302 // the launchbar points forward, so we need to change the signs here
303 float ang = -Math::asin(mountdist_xz/_length)
304 + Math::atan2(ground[2], ground[0]) + YASIM_PI2;
306 _frac = (ang - _up_ang)/(_down_ang - _up_ang);
312 // Now do it again for the holdback
314 // The holdback tip in local coordinates.
316 getHoldbackTipPosition(htip);
318 // Check if the tip will intersect the ground or not. That is, compute
319 // the distance of the tip to the ground plane.
320 float h_tipdist = ground[3] - Math::dot3(htip, ground);
321 if (0 <= h_tipdist) {
322 _h_frac = _extension;
324 // Compute the distance of the holdback mount point from the ground
326 float h_mountdist = ground[3] - Math::dot3(_holdback_mount, ground);
328 // Compute the distance of the holdback mount point from the ground
329 // plane in the x-z plane. It holds:
330 // mountdist = mountdist_xz*cos(angle(normal_yz, e_z))
332 float h_mountdist_xz = _holdback_length;
333 if (ground[2] != 0) {
334 float nrm_yz = Math::sqrt(ground[1]*ground[1]+ground[2]*ground[2]);
335 h_mountdist_xz = -h_mountdist*nrm_yz/ground[2];
338 if (h_mountdist_xz < _holdback_length) {
339 float h_ang = Math::asin(h_mountdist_xz/_holdback_length)
340 + Math::atan2(ground[2], ground[0]) + YASIM_PI2;
341 _h_frac = (h_ang - _up_ang)/(_down_ang - _up_ang);
343 _h_frac = _extension;
348 getTipPosition(llb_mount);
350 // The launchbar tip in global coordinates.
351 double launchbar_pos[3];
352 s->posLocalToGlobal(llb_mount, launchbar_pos);
354 double end[2][3]; float vel[2][3];
355 float dist = g_cb->getCatapult(launchbar_pos, end, vel);
356 // Work around a problem of flightgear returning totally screwed up
357 // scenery when switching views.
361 // Compute the positions of the catapult start and endpoints in the
362 // local coordinate system
364 s->posGlobalToLocal(end[0], lend[0]);
365 s->posGlobalToLocal(end[1], lend[1]);
367 // Transform the velocities of the endpoints to the
368 // local coordinate sytem.
370 s->velGlobalToLocal(vel[0], lvel[0]);
371 s->velGlobalToLocal(vel[1], lvel[1]);
373 // Compute the position of the launchbar tip relative to the cat.
374 float tip_pos_on_cat = getPercentPosOnCat(llb_mount, 0.0, lend);
375 float llbtip[3], lvlbtip[3];
376 getPosOnCat(tip_pos_on_cat, llbtip, lvlbtip, lend, lvel);
378 // Compute the direction from the launchbar mount at the gear
379 // to the lauchbar mount on the cat.
381 Math::sub3(llbtip, _launchbar_mount, llbdir);
382 float lblen = Math::mag3(llbdir);
383 Math::mul3(1.0/lblen, llbdir, llbdir);
385 // Check if we are near enough to the cat.
386 if (_state == Unmounted && dist < 0.6) {
387 // coarse approximation for the velocity of the launchbar.
388 // Might be sufficient because arresting at the cat makes only
389 // sense when the aircraft does not rotate much.
392 float lrot[3], lv[3];
393 Math::vmul33(s->orient, s->rot, lrot);
394 Math::vmul33(s->orient, s->v, lv);
395 body->pointVelocity(llb_mount, lrot, tmp);
396 Math::sub3(tmp, lvlbtip, lv_mount);
397 Math::add3(lv, lv_mount, lv_mount);
399 // We cannot arrest at the cat if we move too fast wrt the cat.
400 if (0.2 < Math::mag3(lv_mount))
403 // Compute the position of the holdback mount relative to the cat.
404 double dd[2][3]; float fd[2][3]; double ghldbkpos[3];
405 s->posLocalToGlobal(_holdback_mount, ghldbkpos);
406 float hbdist = g_cb->getCatapult(ghldbkpos, dd, fd);
408 // don't let the calculation go -ve here
409 if (_holdback_length*_holdback_length - hbdist*hbdist < 0)
411 float offset = -Math::sqrt(_holdback_length*_holdback_length
413 _pos_on_cat = getPercentPosOnCat(_holdback_mount, offset, lend);
415 // We cannot arrest if we are not at the start of the cat.
416 if (_pos_on_cat < 0.0 || 0.4 < _pos_on_cat)
419 // Now we are arrested at the cat.
420 // The force is applied at the next step.
425 // Get the actual distance from the holdback to its mountpoint
426 // on the cat. If it is longer than the holdback apply a force.
427 float lhldbk_cmount[3]; float lvhldbk_cmount[3];
428 getPosOnCat(_pos_on_cat, lhldbk_cmount, lvhldbk_cmount, lend, lvel);
429 // Compute the direction of holdback.
431 Math::sub3(lhldbk_cmount, _holdback_mount, lhldbkdir);
432 float hldbklen = Math::mag3(lhldbkdir);
433 Math::mul3(1/hldbklen, lhldbkdir, lhldbkdir);
435 if (_state == Arrested) {
436 // Now apply a constant tension from the catapult over the launchbar.
437 Math::mul3(2.0, llbdir, _launchbar_force);
439 // If the distance from the holdback mount at the aircraft to the
440 // holdback mount on the cat is larger than the holdback length itself,
441 // the holdback applies a force to the gear.
442 if (_holdback_length < hldbklen) {
443 // coarse approximation for the velocity of the holdback mount
445 // Might be sufficient because arresting at the cat makes only
446 // sense when the aircraft does not rotate much.
447 float lvhldbk_gmount[3];
448 float lrot[3], lv[3];
449 Math::vmul33(s->orient, s->rot, lrot);
450 Math::vmul33(s->orient, s->v, lv);
451 body->pointVelocity(_holdback_mount, lrot, lvhldbk_gmount);
452 Math::add3(lv, lvhldbk_gmount, lvhldbk_gmount);
454 // The velocity of the holdback mount at the gear wrt the
455 // holdback mount at the cat.
457 Math::sub3(lvhldbk_gmount, lvhldbk_cmount, lvhldbk);
459 // The spring force the holdback will apply to the gear
461 Math::mul3(1e1*(hldbklen - _holdback_length), lhldbkdir,
464 // The damping force here ...
465 Math::mul3(2e0, lvhldbk, tmp);
466 Math::sub3(_holdback_force, tmp, _holdback_force);
475 if (_state == Launch) {
476 // Now apply a constant tension from the catapult over the launchbar.
477 Math::mul3(25.0, llbdir, _launchbar_force);
484 if (_state == Completed) {
485 // Wait until the strop has cleared the deck
486 // This is a temporary fix until we come up with something better
494 // Scale by the mass. That keeps the stiffness in reasonable bounds.
495 float mass = body->getTotalMass();
496 Math::mul3(mass, _launchbar_force, _launchbar_force);
497 Math::mul3(mass, _holdback_force, _holdback_force);
500 }; // namespace yasim