AnimatLab  2
Test
RbHinge.cpp
Go to the documentation of this file.
1 
7 #include "StdAfx.h"
8 #include "RbMovableItem.h"
9 #include "RbBody.h"
10 #include "RbJoint.h"
11 #include "RbMotorizedJoint.h"
12 #include "RbRigidBody.h"
13 #include "RbJoint.h"
14 #include "RbHingeLimit.h"
15 #include "RbHinge.h"
16 #include "RbSimulator.h"
17 
18 namespace RoboticsAnimatSim
19 {
20  namespace Environment
21  {
22  namespace Joints
23  {
24 
25 
33 {
34  SetThisPointers();
35 
38  m_lpPosFlap = new RbHingeLimit();
39 
40  m_lpUpperLimit->LimitPos((float) (0.25*RB_PI), false);
41  m_lpLowerLimit->LimitPos((float) (-0.25*RB_PI), false);
42  m_lpPosFlap->LimitPos(Hinge::JointPosition(), false);
44 
45  m_lpUpperLimit->Color(1, 0, 0, 1);
46  m_lpLowerLimit->Color(1, 1, 1, 1);
47  m_lpPosFlap->Color(0, 0, 1, 1);
48 
51 }
52 
60 {
61  //ConstraintLimits are deleted in the base objects.
62  try
63  {
64  }
65  catch(...)
66  {Std_TraceMsg(0, "Caught Error in desctructor of RbHinge\r\n", "", -1, false, true);}
67 }
68 
69 void RbHinge::EnableLimits(bool bVal)
70 {
71  Hinge::EnableLimits(bVal);
72 
73  SetLimitValues();
74 }
75 
76 void RbHinge::SetLimitValues()
77 {
78  //if(m_btHinge)
79  //{
80  // if(m_bEnableLimits)
81  // {
82  // m_bJointLocked = false;
83 
84  // m_btHinge->setLinearLowerLimit(btVector3(0, 0, 0));
85  // m_btHinge->setLinearUpperLimit(btVector3(0, 0, 0));
86 
87  // //Disable rotation about the axis for the prismatic joint.
88  // m_btHinge->setAngularLowerLimit(btVector3(m_lpLowerLimit->LimitPos(),0,0));
89  // m_btHinge->setAngularUpperLimit(btVector3(m_lpUpperLimit->LimitPos(),0,0));
90 
91  // //float fltKp = m_lpUpperLimit->Stiffness();
92  // //float fltKd = m_lpUpperLimit->Damping();
93  // //float fltH = m_lpSim->PhysicsTimeStep()*1000;
94  //
95  // //float fltErp = 0.9; //(fltH*fltKp)/((fltH*fltKp) + fltKd);
96  // //float fltCfm = 0.1; //1/((fltH*fltKp) + fltKd);
97 
98  // //m_btHinge->setParam(BT_CONSTRAINT_STOP_CFM, fltCfm, -1);
99  // //m_btHinge->setParam(BT_CONSTRAINT_STOP_ERP, fltErp, -1);
100  // }
101  // else
102  // {
103  // //To disable limits in bullet we need the lower limit to be bigger than the upper limit
104  // m_bJointLocked = false;
105 
106  // m_btHinge->setLinearLowerLimit(btVector3(0, 0, 0));
107  // m_btHinge->setLinearUpperLimit(btVector3(0, 0, 0));
108 
109  // //Disable rotation about the axis for the prismatic joint.
110  // m_btHinge->setAngularLowerLimit(btVector3(1,0,0));
111  // m_btHinge->setAngularUpperLimit(btVector3(-1,0,0));
112  // }
113  //}
114 }
115 
116 void RbHinge::JointPosition(float fltPos)
117 {
118  m_fltPosition = fltPos;
119  if(m_lpPosFlap)
120  m_lpPosFlap->LimitPos(fltPos);
121 }
122 
123 
125 {
126 }
127 
128 #pragma region DataAccesMethods
129 
130 float *RbHinge::GetDataPointer(const std::string &strDataType)
131 {
132  float *lpData=NULL;
133  std::string strType = Std_CheckString(strDataType);
134 
135  if(strType == "JOINTROTATION")
136  return &m_fltPosition;
137  else if(strType == "JOINTPOSITION")
138  return &m_fltPosition;
139  else if(strType == "JOINTACTUALVELOCITY")
140  return &m_fltVelocity;
141  else if(strType == "JOINTFORCE")
142  return &m_fltForce;
143  else if(strType == "JOINTROTATIONDEG")
144  return &m_fltRotationDeg;
145  else if(strType == "JOINTDESIREDPOSITION")
146  return &m_fltReportSetPosition;
147  else if(strType == "JOINTDESIREDPOSITIONDEG")
148  return &m_fltDesiredPositionDeg;
149  else if(strType == "JOINTSETPOSITION")
150  return &m_fltReportSetPosition;
151  else if(strType == "JOINTDESIREDVELOCITY")
152  return &m_fltReportSetVelocity;
153  else if(strType == "JOINTSETVELOCITY")
154  return &m_fltReportSetVelocity;
155  else if(strType == "ENABLE")
156  return &m_fltEnabled;
157  else if(strType == "CONTACTCOUNT")
158  THROW_PARAM_ERROR(Al_Err_lMustBeContactBodyToGetCount, Al_Err_strMustBeContactBodyToGetCount, "JointID", m_strName);
159  else
160  {
161  lpData = RbMotorizedJoint::Physics_GetDataPointer(strType);
162  if(lpData) return lpData;
163 
164  lpData = Hinge::GetDataPointer(strType);
165  if(lpData) return lpData;
166 
167  THROW_TEXT_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType, "JointID: " + STR(m_strName) + " DataType: " + strDataType);
168  }
169 
170  return lpData;
171 }
172 
173 bool RbHinge::SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError)
174 {
175  //if(RbJoint::Physics_SetData(strDataType, strValue))
176  // return true;
177 
178  if(Hinge::SetData(strDataType, strValue, false))
179  return true;
180 
181  //If it was not one of those above then we have a problem.
182  if(bThrowError)
183  THROW_PARAM_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType, "Data Type", strDataType);
184 
185  return false;
186 }
187 
188 void RbHinge::QueryProperties(CStdPtrArray<TypeProperty> &aryProperties)
189 {
190  //RbJoint::Physics_QueryProperties(aryProperties);
191  Hinge::QueryProperties(aryProperties);
192 
193  aryProperties.Add(new TypeProperty("JointRotation", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
194  aryProperties.Add(new TypeProperty("JointPosition", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
195  aryProperties.Add(new TypeProperty("JointActualVelocity", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
196  aryProperties.Add(new TypeProperty("JointForce", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
197  aryProperties.Add(new TypeProperty("JointRotationDeg", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
198  aryProperties.Add(new TypeProperty("JointDesiredPosition", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
199  aryProperties.Add(new TypeProperty("JointDesiredPositionDeg", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
200  aryProperties.Add(new TypeProperty("JointSetPosition", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
201  aryProperties.Add(new TypeProperty("JointDesiredVelocity", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
202  aryProperties.Add(new TypeProperty("JointSetVelocity", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
203  aryProperties.Add(new TypeProperty("Enable", AnimatPropertyType::Boolean, AnimatPropertyDirection::Get));
204 }
205 
206 #pragma endregion
207 
209 {
210  UpdateData();
212 }
213 
214 void RbHinge::Physics_EnableLock(bool bOn, float fltPosition, float fltMaxLockForce)
215 {
216  if(bOn)
217  m_bJointLocked = true;
218  else if (m_bMotorOn)
219  Physics_EnableMotor(true, 0, fltMaxLockForce, false);
220  else
221  SetLimitValues();
222 }
223 
224 void RbHinge::Physics_EnableMotor(bool bOn, float fltDesiredVelocity, float fltMaxForce, bool bForceWakeup)
225 {
226  if(bOn)
227  {
228  if(!m_bMotorOn || bForceWakeup || m_bJointLocked)
229  {
230  }
231 
232  //I had to move these statements out of the if above. I kept running into one instance after another where I ran inot a problem if I did not do this every single time.
233  // It is really annoying and inefficient, but I cannot find another way to reiably guarantee that the motor will behave coorectly under all conditions without
234  // doing this every single time I set the motor velocity.
235  SetLimitValues();
236  m_lpThisJoint->WakeDynamics();
237  }
238  else
239  {
240  TurnMotorOff();
241 
242  if(m_bMotorOn || bForceWakeup || m_bJointLocked)
243  {
244  m_iAssistCountdown = 3;
246  m_lpThisJoint->WakeDynamics();
247  SetLimitValues();
248  }
249  }
250 
251  m_bMotorOn = bOn;
252 }
253 
254 void RbHinge::Physics_MaxForce(float fltVal)
255 {
256  //if(m_btJoint && m_btHinge)
257  // m_btHinge->getRotationalLimitMotor(0)->m_maxMotorForce = fltVal;
258 }
259 
260 void RbHinge::TurnMotorOff()
261 {
262  //if(m_btHinge)
263  //{
264  // if(m_lpFriction && m_lpFriction->Enabled())
265  // {
266  // //0.032 is a coefficient that produces friction behavior in bullet using the same coefficient values
267  // //that were specified in vortex engine. This way I get similar behavior between the two.
268  // float maxMotorImpulse = m_lpFriction->Coefficient()*0.032f*(m_lpThisAB->GetSimulator()->InverseMassUnits() * m_lpThisAB->GetSimulator()->InverseDistanceUnits());
269  // m_btHinge->getRotationalLimitMotor(0)->m_enableMotor = true;
270  // m_btHinge->getRotationalLimitMotor(0)->m_targetVelocity = 0;
271  // m_btHinge->getRotationalLimitMotor(0)->m_maxMotorForce = maxMotorImpulse;
272  // }
273  // else
274  // m_btHinge->getRotationalLimitMotor(0)->m_enableMotor = false;
275  //}
276 }
277 
278 void RbHinge::SetConstraintFriction()
279 {
280  if(!m_bJointLocked && !m_bMotorOn && m_bEnabled)
281  TurnMotorOff();
282 }
283 
285 {
286  Hinge::ResetSimulation();
287 
288  //m_btHinge->getRotationalLimitMotor(0)->m_currentPosition = 0;
289  //m_btHinge->getRotationalLimitMotor(0)->m_accumulatedImpulse = 0;
290 }
291 
292  } //Joints
293  } // Environment
294 } //RoboticsAnimatSim
virtual float LimitPos()
Gets the limit position.
virtual void ResetSimulation()
Resets the simulation back to time 0.
Definition: RbHinge.cpp:284
float m_fltEnabled
This is used for reporting the enabled state in a GetDataPointer call.
Definition: Node.h:35
ConstraintLimit * m_lpUpperLimit
Upper limit constring pointer.
Definition: Hinge.h:43
virtual void IsShowPosition(bool bVal)
Sets whether this contstraint is actually just being used to show the current position of the joint...
float m_fltRotationDeg
The rotation of the hinge in degrees.
Definition: Hinge.h:52
virtual void CreateJoint()
Creates the joint.
Definition: RbHinge.cpp:124
Declares the vortex hinge class.
float m_fltDesiredPositionDeg
The desired rotation of the hinge in degrees.
Definition: Hinge.h:55
ConstraintLimit * m_lpLowerLimit
Lower limit constring pointer.
Definition: Hinge.h:46
virtual void StepSimulation()
Step the simulation for this object.
Definition: RbHinge.cpp:208
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.
float m_fltForce
The current force being applied to the joint by the motor.
Definition: Joint.h:58
bool m_bEnabled
Tells if this item is enabled or not. If it is not enabled then it is not run.
Definition: AnimatBase.h:40
float m_fltReportSetPosition
This is the variable that is reported to AnimatLab on what the set position was.
ConstraintLimit * m_lpPosFlap
Pointer to a constraint that is used to represent the position flap.
Definition: Hinge.h:49
float m_fltReportSetVelocity
This is the variable that is reported to AnimatLab on what the set veloicty was.
virtual void SetVelocityToDesired()
Sets the desired velocity to use for the motor.
virtual float JointPosition()
Gets the joint position.
Definition: Joint.cpp:532
virtual void Color(float fltR, float fltG, float fltB, float fltA)
Sets the color to use when displaying this contraint.
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 void ClearAssistForces()
Clears the assist forces.
Classes for implementing the cm-labs vortex physics engine for AnimatLab.
virtual void UpdateData()
Called to collect any body data for this part.
Definition: Hinge.cpp:219
virtual bool EnableLimits()
Tells if ConstraintLimits are enabled.
Definition: Joint.cpp:139
std::string m_strName
The name for this object.
Definition: AnimatBase.h:61