9 #include "BlMotorizedJoint.h"
10 #include "BlRigidBody.h"
11 #include "BlSimulator.h"
18 BlMotorizedJoint::BlMotorizedJoint()
20 m_lpThisMotorJoint = NULL;
22 m_bJointLocked =
false;
23 m_fltPredictedPos = 0;
24 m_fltNextPredictedPos = 0;
27 BlMotorizedJoint::~BlMotorizedJoint()
31 void BlMotorizedJoint::SetThisPointers()
33 BlJoint::SetThisPointers();
35 m_lpThisMotorJoint =
dynamic_cast<MotorizedJoint *
>(
this);
36 if(!m_lpThisMotorJoint)
37 THROW_TEXT_ERROR(Bl_Err_lThisPointerNotDefined, Bl_Err_strThisPointerNotDefined,
"m_lpThisMotorJoint, " + m_lpThisAB->Name());
39 m_lpThisMotorJoint->PhysicsMotorJoint(
this);
44 void BlMotorizedJoint::CalculateServoVelocity()
49 float fltTargetPos = m_lpThisJoint->GetPositionWithinLimits(m_lpThisMotorJoint->DesiredPosition());
53 float fltError = fltTargetPos - GetCurrentBtPositionScaled();
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());
98 void BlMotorizedJoint::Physics_SetVelocityToDesired()
100 if(m_lpThisMotorJoint->EnableMotor())
103 if(MotorType == eJointMotorType::PositionControl ||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);
122 float fltHalfPercVel = fabs(fltSetVelocity * 0.01);
132 m_fltPredictedPos = m_fltNextPredictedPos;
133 m_fltNextPredictedPos = m_fltPredictedPos + (fltSetVelocity*m_lpThisAB->GetSimulator()->PhysicsTimeStep());
136 float fltJointVel = m_lpThisJoint->JointVelocity();
138 if(!m_lpThisJoint->UsesRadians())
139 fltJointVel *= m_lpThisAB->GetSimulator()->InverseDistanceUnits();;
141 float fltVelDiff = fabs(fltJointVel - fltSetVelocity);
144 if(m_btJoint && fltVelDiff > 1e-4)
146 if(fabs(fltSetVelocity) > 1e-4 && m_btJoint)
147 Physics_EnableMotor(
true, fltSetVelocity, fltMaxForce,
false);
148 else if(!m_bJointLocked)
149 Physics_EnableLock(
true, GetCurrentBtPosition(), fltMaxForce);
152 m_lpThisMotorJoint->PrevVelocity(fltSetVelocity);
156 void BlMotorizedJoint::Physics_CollectExtraData()
159 if(m_btJoint && lpSim && m_lpThisMotorJoint && m_lpThisJoint && m_lpThisJoint->NeedsRobotSynch())
161 float fDisUnits = m_lpThisAB->GetSimulator()->DistanceUnits();
162 float fMassUnits = m_lpThisAB->GetSimulator()->MassUnits();
163 CStdFPoint vVal, vAssist;
165 float fltRatio = fMassUnits * fDisUnits;
166 vAssist = m_lpThisMotorJoint->MotorAssistForceToAReport();
167 vVal[0] = (m_btJointFeedback.m_appliedForceBodyA[0] * fltRatio) + vAssist[0];
168 vVal[1] = (m_btJointFeedback.m_appliedForceBodyA[1] * fltRatio) + vAssist[1];
169 vVal[2] = (m_btJointFeedback.m_appliedForceBodyA[2] * fltRatio) + vAssist[2];
170 m_lpThisMotorJoint->MotorForceToA(vVal);
172 vAssist = m_lpThisMotorJoint->MotorAssistForceToBReport();
173 vVal[0] = (m_btJointFeedback.m_appliedForceBodyB[0] * fltRatio) + vAssist[0];
174 vVal[1] = (m_btJointFeedback.m_appliedForceBodyB[1] * fltRatio) + vAssist[0];
175 vVal[2] = (m_btJointFeedback.m_appliedForceBodyB[2] * fltRatio) + vAssist[0];
176 m_lpThisMotorJoint->MotorForceToB(vVal);
178 fltRatio = fMassUnits * fDisUnits * fDisUnits;
179 vAssist = m_lpThisMotorJoint->MotorAssistTorqueToAReport();
180 vVal[0] = (m_btJointFeedback.m_appliedTorqueBodyA[0] * fltRatio) + vAssist[0];
181 vVal[1] = (m_btJointFeedback.m_appliedTorqueBodyA[1] * fltRatio) + vAssist[0];
182 vVal[2] = (m_btJointFeedback.m_appliedTorqueBodyA[2] * fltRatio) + vAssist[0];
183 m_lpThisMotorJoint->MotorTorqueToA(vVal);
185 vAssist = m_lpThisMotorJoint->MotorAssistTorqueToBReport();
186 vVal[0] = (m_btJointFeedback.m_appliedTorqueBodyB[0] * fltRatio) + vAssist[0];
187 vVal[1] = (m_btJointFeedback.m_appliedTorqueBodyB[1] * fltRatio) + vAssist[0];
188 vVal[2] = (m_btJointFeedback.m_appliedTorqueBodyB[2] * fltRatio) + vAssist[0];
189 m_lpThisMotorJoint->MotorTorqueToB(vVal);
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.