29#include "trax/Configuration.h"
30#include "trax/TrackData.h"
31#include "trax/Units.h"
44 template<
typename Valtype>
45 class TrackJoint :
public TrackData<Valtype>{
47 TrackJoint()
noexcept;
48 virtual ~TrackJoint(){}
127 void MotorTarget( Valtype targetVelocity )
noexcept;
161 void ThresholdP( Valtype threshold )
noexcept;
170 void ThresholdT( Valtype threshold )
noexcept;
178 void ThresholdN( Valtype threshold )
noexcept;
186 void ThresholdB( Valtype threshold )
noexcept;
196 void Flange( Valtype flange )
noexcept;
224 void SetDerailed(
bool bDerailed =
true )
noexcept;
259 Valtype& fmax )
const noexcept
264 Constraint1( Jm, Rm, Jt, Rt, E, fmin, fmax );
267 Constraint2( Jm, Rm, Jt, Rt, E, fmin, fmax );
270 Constraint3( Jm, Rm, Jt, Rt, E, fmin, fmax );
273 Constraint4( Rm, Rt, E, fmin, fmax );
276 Constraint5( Jm, Rm, Jt, Rt, E, fmin, fmax );
279 Constraint6( Jm, Rm, Jt, Rt, VTarget, fmin, fmax );
282 assert( !
"Dimension index invalid!" );
288 Constraint1( Jm, Rm, E, fmin, fmax );
291 Constraint2( Jm, Rm, E, fmin, fmax );
294 Constraint3( Jm, Rm, E, fmin, fmax );
297 Constraint4( Rm, E, fmin, fmax );
300 Constraint5( Jm, Rm, E, fmin, fmax );
303 Constraint6( Jm, Rm, VTarget, fmin, fmax );
306 assert( !
"Dimension index invalid!" );
330 virtual bool IsMovableBodySleeping() const noexcept = 0;
332 virtual
void WakeUp() const = 0;
334 virtual
void UpdateBodies() = 0;
336 virtual
void UpdateTrackBody(
std::shared_ptr<const struct
Body> pToBody ) = 0;
338 virtual
void release() = 0;
340 virtual
void markDirty() noexcept = 0;
356 Valtype motor_force_min;
357 Valtype motor_force_max;
358 bool motor_force_limited;
371 unsigned short m_Dims, m_LimDims;
372 bool bDerailed = false;
374 Valtype normal_force_min;
375 Valtype normal_force_max;
376 bool normal_force_limited;
377 Valtype binormal_force_min;
378 Valtype binormal_force_max;
379 bool binormal_force_limited;
380 Valtype normal_torque_max;
381 bool normal_torque_limited;
385 bool thresholdPViolated;
387 bool thresholdTViolated;
389 bool thresholdNViolated;
391 bool thresholdBViolated;
397 static const Valtype maxval;
415 inline
void Constraint1(
422 Valtype& f1max ) const noexcept
437 f1min = normal_force_min;
438 f1max = normal_force_max;
441 inline void Constraint1(
446 Valtype& f1max )
const noexcept
451 f1min = normal_force_min;
452 f1max = normal_force_max;
460 inline void Constraint2(
461 spat::Vector<Valtype>& Jm2,
462 spat::Vector<Valtype>& Rm2,
463 spat::Vector<Valtype>& Jt2,
464 spat::Vector<Valtype>& Rt2,
467 Valtype& f2max )
const noexcept
482 f2min = binormal_force_min;
483 f2max = binormal_force_max;
486 inline void Constraint2(
487 spat::Vector<Valtype>& Jm2,
488 spat::Vector<Valtype>& Rm2,
491 Valtype& f2max )
const noexcept
496 f2min = binormal_force_min;
497 f2max = binormal_force_max;
504 inline void Constraint3(
505 spat::Vector<Valtype>& Jm3,
506 spat::Vector<Valtype>& Rm3,
507 spat::Vector<Valtype>& Jt3,
508 spat::Vector<Valtype>& Rt3,
511 Valtype& f3max )
const noexcept
522 Rm3 =
wF.T -
t * dA %
wF.T;
524 Rt3 = -
wF.T +
t * dF %
wF.T;
525 E3 = (eN + eB) *
wF.T;
530 inline void Constraint3(
531 spat::Vector<Valtype>& Jm3,
532 spat::Vector<Valtype>& Rm3,
535 Valtype& f3max )
const noexcept
538 Rm3 =
wF.T -
t * dA %
wF.T;
539 E3 = (eN + eB) *
wF.T;
549 inline void Constraint4(
550 spat::Vector<Valtype>& Rm4,
551 spat::Vector<Valtype>& Rt4,
554 Valtype& f4max )
const noexcept
564 E4 = (eT + eN + eB) *
F.N;
565 f4min = -normal_torque_max;
566 f4max = normal_torque_max;
570 inline void Constraint4(
571 spat::Vector<Valtype>& Rm4,
574 Valtype& f4max )
const noexcept
577 E4 = (eT + eN + eB) *
F.N;
578 f4min = -normal_torque_max;
579 f4max = normal_torque_max;
587 inline void Constraint5(
588 spat::Vector<Valtype>& Jm5,
589 spat::Vector<Valtype>& Rm5,
590 spat::Vector<Valtype>& Jt5,
591 spat::Vector<Valtype>& Rt5,
594 Valtype& f5max )
const noexcept
605 Rm5 =
F.B -
c * dA %
wF.T;
607 Rt5 = -
F.B +
c * dF %
wF.T;
608 E5 = (eT + eN + eB) *
F.B;
613 inline void Constraint5(
614 spat::Vector<Valtype>& Jm5,
615 spat::Vector<Valtype>& Rm5,
618 Valtype& f5max )
const noexcept
621 Rm5 =
F.B -
c * dA %
wF.T;
622 E5 = (eT + eN + eB) *
F.B;
632 inline void Constraint6(
633 spat::Vector<Valtype>& Jm6,
634 spat::Vector<Valtype>& Rm6,
635 spat::Vector<Valtype>& Jt6,
636 spat::Vector<Valtype>& Rt6,
639 Valtype& f6max )
const noexcept
654 f6min = motor_force_min;
655 f6max = motor_force_max;
658 inline void Constraint6(
659 spat::Vector<Valtype>& Jm6,
660 spat::Vector<Valtype>& Rm6,
663 Valtype& f6max )
const noexcept
668 f6min = motor_force_min;
669 f6max = motor_force_max;
673template<
typename Valtype>
674const Valtype TrackJoint<Valtype>::maxval =
infinite;
676template<
typename Valtype>
677inline TrackJoint<Valtype>::TrackJoint() noexcept
682 motor_force_limited {
true},
692 normal_force_min {-maxval},
693 normal_force_max {maxval},
694 normal_force_limited {
false},
695 binormal_force_min {0},
696 binormal_force_max {maxval},
697 binormal_force_limited {
true},
698 normal_torque_max {maxval},
699 normal_torque_limited {
false},
701 torque_limited {
false},
702 thresholdPViolated {
false},
704 thresholdTViolated {
false},
706 thresholdNViolated {
false},
708 thresholdBViolated {
false},
716 m_MovingBodyCOM.Init();
717 m_TrackBodyCOM.Init();
718 m_MovingBodyFrame.Init();
719 m_TrackBodyFrame.Init();
720 m_LocalAnchor.Init();
723template<
typename Valtype>
725 assert( step >= 0
_s );
729template<
typename Valtype>
734template<
typename Valtype>
736 m_MovingBodyFrame = bodyFrame;
738 m_MovingBodyFrame.ToParent( A );
739 Fm = m_MovingBodyCOM;
740 m_MovingBodyFrame.ToParent( Fm );
752template<
typename Valtype>
759 if( eP.Length() > thresholdP ){
760 thresholdPViolated =
true;
763 std::clog << Verbosity::detailed <<
"Trackjoint: thresholdPViolated violated! (" << eP.Length() <<
" > " << thresholdP <<
")" << std::endl;
767 thresholdPViolated =
false;
769 if( eN.Length() > thresholdT &&
770 eB.Length() > thresholdT )
772 thresholdTViolated =
true;
775 std::clog << Verbosity::detailed <<
"Trackjoint: thresholdTViolated violated!" << std::endl;
779 thresholdTViolated =
false;
781 if( eT.Length() > thresholdN &&
782 eB.Length() > thresholdN ){
783 thresholdNViolated =
true;
786 std::clog << Verbosity::detailed <<
"Trackjoint: thresholdNViolated violated!" << std::endl;
790 thresholdNViolated =
false;
792 if( eT.Length() > thresholdB &&
793 eN.Length() > thresholdB )
795 thresholdBViolated =
true;
798 std::clog << Verbosity::detailed <<
"Trackjoint: thresholdBViolated violated!" << std::endl;
802 thresholdBViolated =
false;
804 if( motor_force_min < 0 || motor_force_max > 0 ){
806 m_LimDims = (normal_force_limited ? 1u : 0) +
807 (binormal_force_limited ? 1u : 0) +
808 (normal_torque_limited ? 1u : 0) +
809 (torque_limited ? 2u : 0) +
810 (motor_force_limited ? 1u : 0);
814 m_LimDims = (normal_force_limited ? 1u : 0) +
815 (binormal_force_limited ? 1u : 0) +
816 (normal_torque_limited ? 1u : 0) +
817 (torque_limited ? 2u : 0);
820 if( binormal_force_min >= Valtype{0} && eP *
wF.B < -flange )
825 std::clog << Verbosity::detailed <<
"Trackjoint: levitating!" << std::endl;
834 bMovingTrack =
false;
838template<
typename Valtype>
840 return m_MovingBodyFrame;
843template<
typename Valtype>
845 m_MovingBodyCOM = localCom;
848template<
typename Valtype>
850 m_TrackBodyFrame = bodyFrame;
852 m_TrackBodyFrame.ToParent( Ft );
861template<
typename Valtype>
863 return m_TrackBodyFrame;
866template<
typename Valtype>
868 m_TrackBodyCOM = localCom;
871template<
typename Valtype>
873 m_LocalAnchor = anchor;
877template<
typename Valtype>
879 return m_LocalAnchor;
882template<
typename Valtype>
887template<
typename Valtype>
890 throw std::invalid_argument(
"NormalForceLimits min can't be greater than max!" );
892 normal_force_min = min;
893 normal_force_max = max;
895 normal_force_limited = normal_force_min > -maxval ||
896 normal_force_max < maxval;
901template<
typename Valtype>
904 throw std::invalid_argument(
"BinormalForceLimits min can't be greater than max!" );
906 binormal_force_min = min;
907 binormal_force_max = max;
909 binormal_force_limited = binormal_force_min > -maxval ||
910 binormal_force_max < maxval;
915template<
typename Valtype>
918 throw std::invalid_argument(
"NormalTorqueLimit max can not be negative!" );
920 normal_torque_max = max;
921 normal_torque_limited = normal_torque_max < maxval;
926template<
typename Valtype>
929 throw std::invalid_argument(
"TorqueLimit max can not be negative!" );
932 torque_limited = torque_max < maxval;
937template<
typename Valtype>
943template<
typename Valtype>
946 std::ostringstream stream;
947 stream <<
"Track_Joint: motor_force_min (" << min <<
") can't be greater than motor_force_max (" << max <<
")!";
948 throw std::invalid_argument( stream.str() );
951 motor_force_min = min;
952 motor_force_max = max;
954 motor_force_limited = motor_force_min > -maxval ||
955 motor_force_max < maxval;
960template<
typename Valtype>
962 if( force < motor_force_min ){
963 std::ostringstream stream;
964 stream <<
"Track_Joint: motor force (" << force <<
") can not be smaller than motor_force_min (" << motor_force_min <<
")!";
965 throw std::invalid_argument( stream.str() );
968 motor_force_max = force;
969 motor_force_limited = motor_force_min > -maxval ||
970 motor_force_max < maxval;
975template<
typename Valtype>
977 if( force > motor_force_max ){
978 std::ostringstream stream;
979 stream <<
"Track_Joint motor force (" << force <<
") can not be greater than motor_force_max (" << motor_force_max <<
")!";
980 throw std::invalid_argument( stream.str() );
983 motor_force_min = force;
984 motor_force_limited = motor_force_min > -maxval ||
985 motor_force_max < maxval;
990template<
typename Valtype>
996template<
typename Valtype>
999 return motor_force_max;
1002template<
typename Valtype>
1005 return motor_force_min;
1008template<
typename Valtype>
1010 thresholdP = threshold;
1014template<
typename Valtype>
1016 thresholdT = std::abs(std::sin( threshold ));
1020template<
typename Valtype>
1022 thresholdN = std::abs(std::sin( threshold ));
1026template<
typename Valtype>
1028 thresholdB = std::abs(std::sin( threshold ));
1032template<
typename Valtype>
1037template<
typename Valtype>
1040 erp = std::clamp( _erp,
static_cast<Valtype
>(0),
static_cast<Valtype
>(1) );
1043template<
typename Valtype>
1049template<
typename Valtype>
1053 bDerailed = _bDerailed;
1057template<
typename Valtype>
1062template<
typename Valtype>
1067template<
typename Valtype>
1069 return dim == 2 ||
dim == 3 ||
dim == 4;
1072template<
typename Valtype>
1077template<
typename Valtype>
1082 return normal_force_limited;
1084 return binormal_force_limited;
1086 return torque_limited;
1088 return normal_torque_limited;
1090 return torque_limited;
1092 return motor_force_limited;
void ThresholdT(Valtype threshold) noexcept
Sets the threshold to switch off the joint when violated.
Definition TrackJoint.h:1015
void SetTrackBodyCOM(const spat::Frame< Valtype > &localCom) noexcept
Set the track bodie's center of mass relative to the track bodie's pose.
Definition TrackJoint.h:867
void Precalculate(const spat::Frame< Valtype > &bodyFrame) noexcept
Set the global pose of the body moving along the track and precalculates internal values like the glo...
Definition TrackJoint.h:735
Valtype MotorTarget() const noexcept
Definition TrackJoint.h:991
void MotorForceLimits(Valtype min, Valtype max)
Sets the limits for the motor force to be applied at the anchor point.
Definition TrackJoint.h:944
void NormalForceLimits(Valtype min, Valtype max)
Sets the limits for the normal force to be applied at the anchor point.
Definition TrackJoint.h:888
void SetTimeStep(Time step) noexcept
Sets the time step for this trackjoint.
Definition TrackJoint.h:724
void SetMovingBodyCOM(const spat::Frame< Valtype > &localCom) noexcept
Set the moving bodie's center of mass relative to the bodie's pose.
Definition TrackJoint.h:844
void ThresholdN(Valtype threshold) noexcept
Sets the threshold to switch off the joint when violated.
Definition TrackJoint.h:1021
void BinormalForceLimits(Valtype min, Valtype max)
Sets the limits for the binormal force to be applied at the anchor point.
Definition TrackJoint.h:902
const spat::Frame< Valtype > & GlobalAnchor() const noexcept
Definition TrackJoint.h:883
bool SetTrackBody(const spat::Frame< Valtype > &bodyFrame) noexcept
Set the global pose of the body moving the track system.
Definition TrackJoint.h:849
Time GetTimeStep() const noexcept
Definition TrackJoint.h:730
unsigned short DimensionsTotal() const noexcept
Definition TrackJoint.h:1063
bool IsConstraintLimited(unsigned short dim) const noexcept
Definition TrackJoint.h:1078
spat::Vector< Valtype > MovingBodyGlobalCOM2AnchorOffset() const noexcept
Definition TrackJoint.h:322
bool IsDriveConstraint(unsigned short dim) const noexcept
Definition TrackJoint.h:1073
Valtype ErrorReductionParameter() const noexcept
Definition TrackJoint.h:1044
bool IsDerailed() const noexcept
Definition TrackJoint.h:1058
const spat::Frame< Valtype > & GetMovingBody() const noexcept
Definition TrackJoint.h:839
Valtype MotorForceMin() const noexcept
Definition TrackJoint.h:1003
const spat::Frame< Valtype > & Anchor() const noexcept
Definition TrackJoint.h:878
const spat::Frame< Valtype > & GetTrackBody() const noexcept
Definition TrackJoint.h:862
bool SetMovingBody(const spat::Frame< Valtype > &bodyFrame) noexcept
Set the global pose of the body moving along the track.
Definition TrackJoint.h:753
void Flange(Valtype flange) noexcept
If there is no holding force in binormal direction (BinormalForceLimitMin >= 0) the flange height wil...
Definition TrackJoint.h:1033
void ThresholdB(Valtype threshold) noexcept
Sets the threshold to switch off the joint when violated.
Definition TrackJoint.h:1027
void NormalTorqueLimit(Valtype max)
Sets the limit for the normal torque to be applied at the anchor point [-max,max].
Definition TrackJoint.h:916
void SetDerailed(bool bDerailed=true) noexcept
Sets the status of the track joint to derailed.
Definition TrackJoint.h:1050
bool IsAngularConstraint(unsigned short dim) const noexcept
Definition TrackJoint.h:1068
Valtype MotorForceMax() const noexcept
Definition TrackJoint.h:997
void ThresholdP(Valtype threshold) noexcept
Sets the threshold to switch off the joint when violated.
Definition TrackJoint.h:1009
void Jacobian(unsigned short dim, spat::Vector< Valtype > &Jm, spat::Vector< Valtype > &Rm, spat::Vector< Valtype > &Jt, spat::Vector< Valtype > &Rt, Valtype &VTarget, Valtype &E, Valtype &fmin, Valtype &fmax) const noexcept
Fills the matrix values for a specific constraint.
Definition TrackJoint.h:250
void TorqueLimit(Valtype max)
Sets the limit for the tangent and binormal torque to be applied at the anchor point [-max,...
Definition TrackJoint.h:927
The namespace provides classes and methods for the dim library.
Definition DimensionedValues.h:178
constexpr Real infinite
Positive infinity value.
Definition DimensionedValues.h:349
constexpr Real _s(Time t) noexcept
Dimensionated Values conversion functions.
Definition DimensionedValues.h:1535
Value< Dimension< 0, 0, 1 > > Time
Time.
Definition DimensionedValues.h:329
The namespace provides classes and methods for spatial computations.
Definition Box.h:32
Namespace of all the trax track libraries classes and methods.
Definition Collection.h:17
constexpr Time fixed_timestep
The fixed timestep to use with the simulation.
Definition Units.h:136
constexpr Length default_derailing_distance
A default value for the acceptable distance between a track position and something beeing still consi...
Definition Units.h:128
A Frame ("TNBFrame") describes a location in 3d space and an orientation using a right handed coordin...
Definition Frame.h:52
Implements a 3D - vector in cartesian coordinates.
Definition Vector.h:48
@ null
Vector with all parameters zeroed.
Definition Vector.h:58
A dynamic object in physical space.
Definition Body.h:44
Full geometrical data at a point on a track.
Definition TrackData.h:36
void Init() noexcept
Initializes all values.
Definition TrackData.h:58
spat::Frame< Valtype > F
TNB Frame of the curve.
Definition TrackData.h:37
Valtype t
torsion
Definition TrackData.h:40
spat::Frame< Valtype > wF
twisted TNB Frame
Definition TrackData.h:38
Valtype c
curvature
Definition TrackData.h:39