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