9 #include "BlMotorizedJoint.h"
10 #include "BlPrismaticLimit.h"
11 #include "BlRigidBody.h"
13 #include "BlSimulator.h"
65 {
Std_TraceMsg(0,
"Caught Error in desctructor of BlPrismatic/\r\n",
"", -1,
false,
true);}
71 Prismatic::EnableLimits(bVal);
76 void BlPrismatic::SetLimitValues()
78 if(
m_lpSim && m_btJoint && m_btPrismatic)
80 GetLimitsFromRelaxations(m_vLowerLinear, m_vUpperLinear, m_vLowerAngular, m_vUpperAngular);
82 float fltErp = 0, fltCfm=0;
86 m_bJointLocked =
false;
95 fltErp = (fltH*fltKp)/((fltH*fltKp) + fltKd);
96 fltCfm = 1/((fltH*fltKp) + fltKd);
101 m_bJointLocked =
false;
103 m_vLowerLinear[0] = 1;
104 m_vUpperLinear[0] = -1;
107 m_btPrismatic->setParam(BT_CONSTRAINT_STOP_CFM, fltCfm, 0);
108 m_btPrismatic->setParam(BT_CONSTRAINT_STOP_ERP, fltErp, 0);
111 float fltHigh = m_btPrismatic->getTranslationalLimitMotor()->m_restitution = fltBounce;
113 m_btPrismatic->setLinearLowerLimit(m_vLowerLinear);
114 m_btPrismatic->setLinearUpperLimit(m_vUpperLinear);
116 m_btPrismatic->setAngularLowerLimit(m_vLowerAngular);
117 m_btPrismatic->setAngularUpperLimit(m_vUpperAngular);
123 Prismatic::TimeStepModified();
135 void BlPrismatic::SetAlpha()
144 void BlPrismatic::DeleteJointGraphics()
146 OsgPrismaticLimit *lpUpperLimit =
dynamic_cast<OsgPrismaticLimit *
>(
m_lpUpperLimit);
147 OsgPrismaticLimit *lpLowerLimit =
dynamic_cast<OsgPrismaticLimit *
>(
m_lpLowerLimit);
148 OsgPrismaticLimit *lpPosFlap =
dynamic_cast<OsgPrismaticLimit *
>(
m_lpPosFlap);
150 OsgPrismatic::DeletePrismaticGraphics(m_osgJointMT, lpUpperLimit, lpLowerLimit, lpPosFlap);
157 void BlPrismatic::CreateJointGraphics()
159 OsgPrismaticLimit *lpUpperLimit =
dynamic_cast<OsgPrismaticLimit *
>(
m_lpUpperLimit);
160 OsgPrismaticLimit *lpLowerLimit =
dynamic_cast<OsgPrismaticLimit *
>(
m_lpLowerLimit);
161 OsgPrismaticLimit *lpPosFlap =
dynamic_cast<OsgPrismaticLimit *
>(
m_lpPosFlap);
165 m_osgJointMT, lpUpperLimit,
166 lpLowerLimit, lpPosFlap);
169 void BlPrismatic::SetupPhysics()
172 DeletePhysics(
false);
176 btTransform mtJointRelParent, mtJointRelChild;
177 CStdFPoint vRot(0, 0, osg::PI);
178 CalculateRelativeJointMatrices(vRot, mtJointRelParent, mtJointRelChild);
182 GetBlSimulator()->DynamicsWorld()->addConstraint(m_btPrismatic,
true);
183 m_btPrismatic->setDbgDrawSize(btScalar(5.f));
188 m_btJoint = m_btPrismatic;
189 m_bt6DofJoint = m_btPrismatic;
199 if(m_lpBlParent && m_lpBlParent->Part())
200 m_lpBlParent->Part()->setSleepingThresholds(0, 0);
202 if(m_lpBlChild && m_lpBlChild->Part())
203 m_lpBlChild->Part()->setSleepingThresholds(0, 0);
205 m_btPrismatic->setJointFeedback(&m_btJointFeedback);
207 Prismatic::Initialize();
208 BlJoint::Initialize();
218 #pragma region DataAccesMethods
220 float *BlPrismatic::GetDataPointer(
const std::string &strDataType)
225 if(strType ==
"JOINTROTATION")
227 else if(strType ==
"JOINTPOSITION")
229 else if(strType ==
"JOINTACTUALVELOCITY")
231 else if(strType ==
"JOINTFORCE")
233 else if(strType ==
"JOINTDESIREDPOSITION")
235 else if(strType ==
"JOINTSETPOSITION")
237 else if(strType ==
"JOINTDESIREDVELOCITY")
239 else if(strType ==
"JOINTSETVELOCITY")
241 else if(strType ==
"ENABLE")
243 else if(strType ==
"CONTACTCOUNT")
244 THROW_PARAM_ERROR(Al_Err_lMustBeContactBodyToGetCount, Al_Err_strMustBeContactBodyToGetCount,
"JointID",
m_strName);
247 lpData = BlMotorizedJoint::Physics_GetDataPointer(strType);
248 if(lpData)
return lpData;
250 lpData = Prismatic::GetDataPointer(strType);
251 if(lpData)
return lpData;
253 THROW_TEXT_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType,
"JointID: " + STR(
m_strName) +
" DataType: " + strDataType);
259 bool BlPrismatic::SetData(
const std::string &strDataType,
const std::string &strValue,
bool bThrowError)
261 if(BlJoint::Physics_SetData(strDataType, strValue))
264 if(Prismatic::SetData(strDataType, strValue,
false))
269 THROW_PARAM_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType,
"Data Type", strDataType);
274 void BlPrismatic::QueryProperties(CStdPtrArray<TypeProperty> &aryProperties)
276 BlJoint::Physics_QueryProperties(aryProperties);
277 Prismatic::QueryProperties(aryProperties);
279 aryProperties.Add(
new TypeProperty(
"JointRotation", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
280 aryProperties.Add(
new TypeProperty(
"JointPosition", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
281 aryProperties.Add(
new TypeProperty(
"JointActualVelocity", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
282 aryProperties.Add(
new TypeProperty(
"JointForce", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
283 aryProperties.Add(
new TypeProperty(
"JointRotationDeg", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
284 aryProperties.Add(
new TypeProperty(
"JointDesiredPosition", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
285 aryProperties.Add(
new TypeProperty(
"JointSetPosition", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
286 aryProperties.Add(
new TypeProperty(
"JointDesiredVelocity", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
287 aryProperties.Add(
new TypeProperty(
"JointSetVelocity", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
288 aryProperties.Add(
new TypeProperty(
"Enable", AnimatPropertyType::Boolean, AnimatPropertyDirection::Get));
305 bool BlPrismatic::JointIsLocked()
307 btVector3 vLower, vUpper;
308 m_btPrismatic->getLinearLowerLimit(vLower);
309 m_btPrismatic->getLinearUpperLimit(vUpper);
310 if( (vLower[0] == vUpper[0]) || (vLower[0] > vUpper[0]) )
317 void BlPrismatic::Physics_EnableLock(
bool bOn,
float fltPosition,
float fltMaxLockForce)
319 if (m_btJoint && m_btPrismatic)
323 m_bJointLocked =
true;
325 m_vLowerLinear[0] = m_vUpperLinear[0] = fltPosition;
327 m_btPrismatic->setLinearLowerLimit(m_vLowerLinear);
328 m_btPrismatic->setLinearUpperLimit(m_vUpperLinear);
330 m_btPrismatic->enableSpring(0,
false);
331 m_btPrismatic->getTranslationalLimitMotor()->m_enableMotor[0] =
true;
332 m_btPrismatic->getTranslationalLimitMotor()->m_targetVelocity[0] = 0;
333 m_btPrismatic->getTranslationalLimitMotor()->m_maxMotorForce[0] = fltMaxLockForce;
336 Physics_EnableMotor(
true, 0, fltMaxLockForce,
false);
342 void BlPrismatic::Physics_EnableMotor(
bool bOn,
float fltDesiredVelocity,
float fltMaxForce,
bool bForceWakeup)
344 if (m_btJoint && m_btPrismatic)
357 m_lpThisJoint->WakeDynamics();
360 m_btPrismatic->enableSpring(0,
false);
361 m_btPrismatic->getTranslationalLimitMotor()->m_enableMotor[0] =
true;
362 m_btPrismatic->getTranslationalLimitMotor()->m_targetVelocity[0] = -fltDesiredVelocity;
363 m_btPrismatic->getTranslationalLimitMotor()->m_maxMotorForce[0] = fltMaxForce;
369 if(m_bMotorOn || bForceWakeup || m_bJointLocked || JointIsLocked())
373 m_lpThisJoint->WakeDynamics();
382 void BlPrismatic::Physics_MaxForce(
float fltVal)
384 if(m_btJoint && m_btPrismatic)
385 m_btPrismatic->getTranslationalLimitMotor()->m_maxMotorForce[0] = fltVal;
388 float BlPrismatic::GetCurrentBtPosition()
390 if(m_btJoint && m_btPrismatic)
392 btVector3 vDiff = m_btPrismatic->getTranslationalLimitMotor()->m_currentLinearDiff;
399 void BlPrismatic::TurnMotorOff()
406 m_btPrismatic->enableSpring(0,
true);
414 float maxMotorImpulse =
m_lpFriction->
Coefficient()*0.032f*(m_lpThisAB->GetSimulator()->InverseMassUnits() * m_lpThisAB->GetSimulator()->InverseDistanceUnits());
415 m_btPrismatic->enableSpring(0,
false);
416 m_btPrismatic->getTranslationalLimitMotor()->m_enableMotor[0] =
true;
417 m_btPrismatic->getTranslationalLimitMotor()->m_targetVelocity[0] = 0;
418 m_btPrismatic->getTranslationalLimitMotor()->m_maxMotorForce[0] = maxMotorImpulse;
422 m_btPrismatic->enableSpring(0,
false);
423 m_btPrismatic->getTranslationalLimitMotor()->m_enableMotor[0] =
false;
426 m_btPrismatic->getTranslationalLimitMotor()->m_accumulatedImpulse = btVector3(0, 0, 0);
427 m_btPrismatic->getTranslationalLimitMotor()->m_targetVelocity = btVector3(0, 0, 0);
428 m_btPrismatic->getTranslationalLimitMotor()->m_currentLimit[0] = 0;
429 m_btPrismatic->getTranslationalLimitMotor()->m_currentLimit[1] = 0;
430 m_btPrismatic->getTranslationalLimitMotor()->m_currentLimit[2] = 0;
431 m_btPrismatic->getTranslationalLimitMotor()->m_currentLimitError = btVector3(0, 0, 0);
435 void BlPrismatic::AxisConstraintSpringEnableChanged(
bool bEnabled)
438 m_btPrismatic->enableSpring(0, bEnabled);
441 void BlPrismatic::SetConstraintFriction()
443 if(m_btPrismatic && !m_bJointLocked && !m_bMotorOn &&
m_bEnabled)
449 Prismatic::ResetSimulation();
451 for(
int iIdx=0; iIdx<3; iIdx++)
453 m_btPrismatic->getRotationalLimitMotor(iIdx)->m_currentPosition = 0;
454 m_btPrismatic->getRotationalLimitMotor(iIdx)->m_accumulatedImpulse = 0;
455 m_btPrismatic->getRotationalLimitMotor(iIdx)->m_currentLimit = 0;
456 m_btPrismatic->getRotationalLimitMotor(iIdx)->m_currentLimitError = 0;
457 m_btPrismatic->getRotationalLimitMotor(iIdx)->m_targetVelocity = 0;
460 m_btPrismatic->getTranslationalLimitMotor()->m_currentLinearDiff = btVector3(0, 0, 0);
461 m_btPrismatic->getTranslationalLimitMotor()->m_accumulatedImpulse = btVector3(0, 0, 0);
462 m_btPrismatic->getTranslationalLimitMotor()->m_targetVelocity = btVector3(0, 0, 0);
463 m_btPrismatic->getTranslationalLimitMotor()->m_currentLimit[0] = 0;
464 m_btPrismatic->getTranslationalLimitMotor()->m_currentLimit[1] = 0;
465 m_btPrismatic->getTranslationalLimitMotor()->m_currentLimit[2] = 0;
466 m_btPrismatic->getTranslationalLimitMotor()->m_currentLimitError = btVector3(0, 0, 0);
471 if(m_btPrismatic) m_btPrismatic->enableFeedback(
true);
475 bool BlPrismatic::NeedApplyAssist()
484 float fltPos = m_btPrismatic->getTranslationalLimitMotor()->m_currentLinearDiff[0];
485 float fltLow = m_btPrismatic->getTranslationalLimitMotor()->m_lowerLimit[0];
486 float fltHigh = m_btPrismatic->getTranslationalLimitMotor()->m_upperLimit[0];
488 float fltLow1Perc = fabs(fltLow)*0.001;
489 float fltHigh1Perc = fabs(fltHigh)*0.001;
492 if(fltSetVel > 0 && fabs(fltHigh-fltPos)>=fltHigh1Perc)
496 if(fltSetVel < 0 && fabs(fltPos-fltLow)>=fltLow1Perc)
510 if(NeedApplyAssist())
514 float fDisUnits = m_lpThisAB->GetSimulator()->InverseDistanceUnits();
515 float fMassUnits = m_lpThisAB->GetSimulator()->InverseMassUnits();
516 float fltRatio = fMassUnits * fDisUnits;
520 float fltInput = m_lpThisJoint->JointVelocity() * fDisUnits;
530 btVector3 vMotorAxis = m_btPrismatic->GetLinearForceAxis(0);
532 btVector3 vBodyAForceReport = -fltForceMag * vMotorAxis;
533 btVector3 vBodyBForceReport = fltForceMag * vMotorAxis;
534 btVector3 vBodyAForce = fltRatio * vBodyAForceReport;
535 btVector3 vBodyBForce = fltRatio * vBodyBForceReport;
538 for(
int i=0; i<3; i++)
546 m_btParent->applyCentralForce(vBodyAForce);
547 m_btChild->applyCentralForce(vBodyBForce);
float BoxSize()
Gets the width of the flaps used to display the hinge in the environment.
Declares the vs prismatic class.
virtual void ApplyMotorAssist()
Applies the motor assist.
virtual void PhysicsTimeStep(float fltVal)
Sets the integration time step for the physics engine.
float m_fltMaxForceNotScaled
The un=scaled maximum force.
virtual ~BlPrismatic()
Destructor.
virtual float LimitPos()
Gets the limit position.
virtual void StepSimulation()
Step the simulation for this object.
virtual void UpdateData()
Called to collect any body data for this part.
virtual float Restitution()
Gets the restitution coefficient of the constraint.
virtual void Alpha(float fltA)=0
Sets the alpha color value for this constraint.
virtual void ResetSimulation()
Resets the simulation back to time 0.
Simulator * m_lpSim
The pointer to a Simulation.
CStdFPoint m_vMotorAssistForceToAReport
Force vector that the motor assist is applying to body A. (un-scaled units).
float m_fltEnabled
This is used for reporting the enabled state in a GetDataPointer call.
virtual float Coefficient()
Gets the Coefficient of friction for this constraint.
PidControl * m_lpAssistPid
The PID controller for the motor assist system.
ConstraintFriction * m_lpFriction
The friction for this joint.
CStdFPoint m_vMotorAssistForceToA
Force vector that the motor assist is applying to body A. (scaled units).
virtual float Stiffness()
Gets the stiffness of the constraint.
BlPrismatic()
Default constructor.
virtual Simulator * GetSimulator()
Gets the simulator pointer.
virtual void IsShowPosition(bool bVal)
Sets whether this contstraint is actually just being used to show the current position of the joint...
Classes for implementing the cm-labs vortex physics engine for AnimatLab.
virtual bool Enabled()
Tells whether this node is enabled.
ConstraintLimit * m_lpUpperLimit
Upper limit constring pointer.
virtual void EnableFeedback()
Enables joint feedback.
virtual float Damping()
Gets the damping value of the contraint.
BlConstraintRelaxation * m_aryBlRelaxations[6]
The bullet relaxation for the primary displacement relaxation.
virtual bool EnableMotor()
Tells if the motor is enabled.
CStdFPoint m_vMotorAssistForceToBReport
Force vector that the motor assist is applying to body B. (un-scaled units).
RigidBody * m_lpChild
The child rigid body for this joint.
bool m_bEnableLimits
If true then any ConstraintLimits for this joint are enabled.
CStdFPoint m_vMotorAssistForceToB
Force vector that the motor assist is applying to body B. (scaled units).
void Std_TraceMsg(const int iLevel, std::string strMessage, std::string strSourceFile, int iSourceLine, bool bLogToFile, bool bPrintHeader)
Traces a message to the debugger window.
ConstraintLimit * m_lpLowerLimit
Lower limit constring pointer.
float m_fltForce
The current force being applied to the joint by the motor.
virtual bool Enabled()
Tells whether this node is enabled.
bool m_bEnabled
Tells if this item is enabled or not. If it is not enabled then it is not run.
float m_fltReportSetPosition
This is the variable that is reported to AnimatLab on what the set position was.
virtual void TimeStepModified()
If the time step is modified then we need to recalculate the length of the delay buffer.
virtual bool Enabled()
Tells whether this item is enabled or not. This is not actually used for all objects, only specific ones. I am putting it in the base class though to prevent numerous duplications.
float m_fltMotorAssistMagnitudeReport
The reportable motor assist Force magnitude.
float m_fltAlpha
The current alpha transparency for this body part.
float m_fltReportSetVelocity
This is the variable that is reported to AnimatLab on what the set veloicty was.
ConstraintLimit * m_lpPosFlap
Pointer to a constraint that is used to represent the position flap.
virtual void SetVelocityToDesired()
Sets the desired velocity to use for the motor.
virtual float JointPosition()
Gets the joint position.
virtual void DeleteGraphics()=0
Deletes up the graphics for the constraint.
virtual void Color(float fltR, float fltG, float fltB, float fltA)
Sets the color to use when displaying this contraint.
float CylinderRadius()
Gets the radius cylinder of the cylinder used to display the axis of the prismatic joint in the envir...
virtual void IsLowerLimit(bool bVal)
Sets whether this is a lower limit or not..
std::string Std_CheckString(std::string strVal)
Converts a string to upper case and trims it.
virtual float SetVelocity()
Gets the velocity that is actually set using the physics method.
virtual void ClearAssistForces()
Clears the assist forces.
virtual bool EnableLimits()
Tells if ConstraintLimits are enabled.
virtual void CreateJoint()
Creates the joint.
float m_fltMotorAssistMagnitude
The motor assist Force magnitude.
ConstraintRelaxation * m_aryRelaxations[6]
The relaxations for the constraints.
std::string m_strName
The name for this object.