8 #include "VsMovableItem.h"
11 #include "VsMotorizedJoint.h"
12 #include "VsRigidBody.h"
13 #include "VsSimulator.h"
14 #include "VsDragger.h"
21 VsMotorizedJoint::VsMotorizedJoint()
23 m_lpThisMotorJoint = NULL;
27 VsMotorizedJoint::~VsMotorizedJoint()
31 void VsMotorizedJoint::SetThisPointers()
33 VsJoint::SetThisPointers();
35 m_lpThisMotorJoint =
dynamic_cast<MotorizedJoint *
>(
this);
36 if(!m_lpThisMotorJoint)
37 THROW_TEXT_ERROR(Vs_Err_lThisPointerNotDefined, Vs_Err_strThisPointerNotDefined,
"m_lpThisMotorJoint, " + m_lpThisAB->Name());
39 m_lpThisMotorJoint->PhysicsMotorJoint(
this);
44 void VsMotorizedJoint::CalculateServoVelocity()
49 float fltTargetPos = m_lpThisJoint->GetPositionWithinLimits(m_lpThisMotorJoint->DesiredPosition());
53 float fltError = fltTargetPos - GetCurrentVxJointPos();
54 m_lpThisMotorJoint->SetPosition(fltTargetPos);
62 if((MotorType == eJointMotorType::PositionControl || MotorType == eJointMotorType::PositionVelocityControl) && m_lpThisMotorJoint->ReachedSetPosition() )
65 m_lpThisMotorJoint->DesiredVelocity(0);
67 else if(MotorType == eJointMotorType::PositionVelocityControl)
70 float fltDesiredVel = fabs(m_lpThisMotorJoint->DesiredVelocity()) *
Std_Sign(fltError);
72 float fltPosError = fabs(fltError);
73 if(fltPosError > 0 && fltPosError < 0.05)
75 float fltDesiredVel2 = fltDesiredVel * (fabs(fltError)*m_lpThisMotorJoint->ServoGain());
77 if(fabs(fltDesiredVel2) <= fabs(fltDesiredVel))
78 fltDesiredVel = fltDesiredVel2;
81 m_lpThisMotorJoint->DesiredVelocity(fltDesiredVel);
83 if(fabs(fltError) < 1e-4)
84 m_lpThisMotorJoint->ReachedSetPosition(
true);
86 else if(MotorType == eJointMotorType::PositionControl)
88 if(m_lpThisJoint->EnableLimits())
90 float fltProp = fltError / m_lpThisJoint->GetLimitRange();
91 m_lpThisMotorJoint->DesiredVelocity(fltProp * m_lpThisMotorJoint->ServoGain());
94 m_lpThisMotorJoint->DesiredVelocity(fltError * m_lpThisMotorJoint->MaxVelocity());
99 void VsMotorizedJoint::Physics_SetVelocityToDesired()
101 if(m_lpThisMotorJoint->EnableMotor())
103 if(m_lpThisMotorJoint->MotorType() == eJointMotorType::PositionControl || m_lpThisMotorJoint->MotorType() == eJointMotorType::PositionVelocityControl)
104 CalculateServoVelocity();
106 float fltDesiredVel = m_lpThisMotorJoint->DesiredVelocity();
107 float fltMaxVel = m_lpThisMotorJoint->MaxVelocity();
108 float fltMaxForce = m_lpThisMotorJoint->MaxForce();
110 if(fltDesiredVel>fltMaxVel)
111 fltDesiredVel = fltMaxVel;
113 if(fltDesiredVel < -fltMaxVel)
114 fltDesiredVel = -fltMaxVel;
116 float fltSetVelocity = fltDesiredVel;
118 m_lpThisMotorJoint->SetVelocity(fltSetVelocity);
119 m_lpThisMotorJoint->DesiredVelocity(0);
120 m_lpThisMotorJoint->DesiredPosition(0);
123 if(m_vxJoint && fabs(m_lpThisJoint->JointVelocity() - fltSetVelocity) > 1e-4)
125 if(fabs(fltSetVelocity) > 1e-4 && m_vxJoint)
127 if(m_vxJoint->getControl(m_iCoordID) == Vx::VxConstraint::CoordinateControlEnum::kControlLocked)
128 Physics_EnableMotor(
true, fltSetVelocity, fltMaxForce,
false);
130 m_vxJoint->setMotorDesiredVelocity(m_iCoordID, fltSetVelocity);
134 if(m_vxJoint->getControl(m_iCoordID) != Vx::VxConstraint::CoordinateControlEnum::kControlLocked)
135 m_lpThisMotorJoint->EnableLock(
true, m_vxJoint->getCoordinateCurrentPosition(m_iCoordID), fltMaxForce);
139 m_lpThisMotorJoint->PrevVelocity(fltSetVelocity);
143 void VsMotorizedJoint::Physics_EnableLock(
bool bOn,
float fltPosition,
float fltMaxLockForce)
150 m_vxJoint->setLockParameters(m_iCoordID, fltPosition, fltMaxLockForce);
152 m_vxJoint->setControl(m_iCoordID, VxConstraint::CoordinateControlEnum::kControlLocked);
155 Physics_EnableMotor(
true, 0, fltMaxLockForce,
false);
157 m_vxJoint->setControl(m_iCoordID, VxConstraint::CoordinateControlEnum::kControlFree);
161 void VsMotorizedJoint::Physics_EnableMotor(
bool bOn,
float fltDesiredVelocity,
float fltMaxForce,
bool bForceWakeup)
167 if(m_vxJoint->getControl(m_iCoordID) != Vx::VxConstraint::CoordinateControlEnum::kControlMotorized)
168 m_vxJoint->setControl(m_iCoordID, Vx::VxConstraint::CoordinateControlEnum::kControlMotorized);
170 m_vxJoint->setMotorMaximumForce(m_iCoordID, fltMaxForce);
171 m_vxJoint->setMotorDesiredVelocity(m_iCoordID, fltDesiredVelocity);
174 m_vxJoint->setControl(m_iCoordID, Vx::VxConstraint::CoordinateControlEnum::kControlFree);
180 void VsMotorizedJoint::Physics_MaxForce(
float fltVal)
183 m_vxJoint->setMotorMaximumForce(m_iCoordID, fltVal);
int Std_Sign(float fltVal)
Determines the sign of a number.
Classes for implementing the cm-labs vortex physics engine for AnimatLab.
eJointMotorType
The motor control type for this joint.