AnimatLab  2
Test
RbMotorizedJoint.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 "RbSimulator.h"
14 
15 namespace RoboticsAnimatSim
16 {
17  namespace Environment
18  {
19 
20 RbMotorizedJoint::RbMotorizedJoint()
21 {
22  m_lpThisMotorJoint = NULL;
23  m_bMotorOn = false;
24  m_bJointLocked = false;
25  m_fltPredictedPos = 0;
26  m_fltNextPredictedPos = 0;
27 }
28 
29 RbMotorizedJoint::~RbMotorizedJoint()
30 {
31 }
32 
33 void RbMotorizedJoint::SetThisPointers()
34 {
35  RbJoint::SetThisPointers();
36 
37  m_lpThisMotorJoint = dynamic_cast<MotorizedJoint *>(this);
38  if(!m_lpThisMotorJoint)
39  THROW_TEXT_ERROR(Rb_Err_lThisPointerNotDefined, Rb_Err_strThisPointerNotDefined, "m_lpThisMotorJoint, " + m_lpThisAB->Name());
40 
41  m_lpThisMotorJoint->PhysicsMotorJoint(this);
42 }
43 
44 //If this is a servo motor then the "velocity" signal is not really a velocity signal in this case.
45 //It is the desired position and we must convert it to the velocity needed to reach and maintian that position.
46 void RbMotorizedJoint::CalculateServoVelocity()
47 {
48  float fltTargetPos = m_lpThisJoint->GetPositionWithinLimits(m_lpThisMotorJoint->DesiredVelocity());
49  float fltError = fltTargetPos - m_lpThisJoint->JointPosition();
50 
51  if(m_lpThisJoint->EnableLimits())
52  {
53  float fltProp = fltError / m_lpThisJoint->GetLimitRange();
54  m_lpThisMotorJoint->DesiredVelocity(fltProp * m_lpThisMotorJoint->ServoGain());
55  }
56  else
57  m_lpThisMotorJoint->DesiredVelocity(fltError * m_lpThisMotorJoint->MaxVelocity());
58 }
59 
60 void RbMotorizedJoint::Physics_SetVelocityToDesired()
61 {
63  //int i=5;
64  //if(Std_ToLower(m_lpThisMotorJoint->ID()) == "c868a91a-285d-4d00-91b2-6410aead9fe8" && GetSimulator()->Time() > 0.5) //
65  // i=6;
66 
67  if(m_lpThisMotorJoint->EnableMotor())
68  {
69  if(m_lpThisMotorJoint->MotorType() == eJointMotorType::PositionControl)
70  {
71  m_lpThisMotorJoint->SetPosition(m_lpThisMotorJoint->DesiredPosition());
72  m_lpThisMotorJoint->DesiredVelocity(0);
73  m_lpThisMotorJoint->DesiredPosition(0);
74  }
75  else
76  {
77  float fltSetPos = m_lpThisMotorJoint->DesiredPosition();
78  float fltSetVel = m_lpThisMotorJoint->DesiredVelocity();
79 
80  if(m_lpThisMotorJoint->MotorType() == eJointMotorType::PositionVelocityControl)
81  m_lpThisMotorJoint->SetPosition(fltSetPos);
82 
83  m_lpThisMotorJoint->SetVelocity(fltSetVel);
84  m_lpThisMotorJoint->DesiredVelocity(0);
85  m_lpThisMotorJoint->DesiredPosition(0);
86 
87  m_lpThisMotorJoint->PrevVelocity(fltSetVel);
88  }
89  }
90 }
91 
92 void RbMotorizedJoint::Physics_ResetSimulation()
93 {
94  m_fltPredictedPos = 0;
95  m_fltNextPredictedPos = 0;
96 }
97 
98 void RbMotorizedJoint::Physics_CollectExtraData()
99 {
100  // OsgSimulator *lpSim = GetOsgSimulator();
101  //if(m_btJoint && lpSim && m_lpThisMotorJoint)
102  //{
103  // float fDisUnits = m_lpThisAB->GetSimulator()->DistanceUnits();
104  // float fMassUnits = m_lpThisAB->GetSimulator()->MassUnits();
105  // CStdFPoint vVal, vAssist;
106 
107  // float fltRatio = fMassUnits * fDisUnits;
108  // vAssist = m_lpThisMotorJoint->MotorAssistForceToAReport();
109  // vVal[0] = (m_btJointFeedback.m_appliedForceBodyA[0] * fltRatio) + vAssist[0];
110  // vVal[1] = (m_btJointFeedback.m_appliedForceBodyA[1] * fltRatio) + vAssist[1];
111  // vVal[2] = (m_btJointFeedback.m_appliedForceBodyA[2] * fltRatio) + vAssist[2];
112  // m_lpThisMotorJoint->MotorForceToA(vVal);
113 
114  // vAssist = m_lpThisMotorJoint->MotorAssistForceToBReport();
115  // vVal[0] = (m_btJointFeedback.m_appliedForceBodyB[0] * fltRatio) + vAssist[0];
116  // vVal[1] = (m_btJointFeedback.m_appliedForceBodyB[1] * fltRatio) + vAssist[0];
117  // vVal[2] = (m_btJointFeedback.m_appliedForceBodyB[2] * fltRatio) + vAssist[0];
118  // m_lpThisMotorJoint->MotorForceToB(vVal);
119 
120  // fltRatio = fMassUnits * fDisUnits * fDisUnits;
121  // vAssist = m_lpThisMotorJoint->MotorAssistTorqueToAReport();
122  // vVal[0] = (m_btJointFeedback.m_appliedTorqueBodyA[0] * fltRatio) + vAssist[0];
123  // vVal[1] = (m_btJointFeedback.m_appliedTorqueBodyA[1] * fltRatio) + vAssist[0];
124  // vVal[2] = (m_btJointFeedback.m_appliedTorqueBodyA[2] * fltRatio) + vAssist[0];
125  // m_lpThisMotorJoint->MotorTorqueToA(vVal);
126 
127  // vAssist = m_lpThisMotorJoint->MotorAssistTorqueToBReport();
128  // vVal[0] = (m_btJointFeedback.m_appliedTorqueBodyB[0] * fltRatio) + vAssist[0];
129  // vVal[1] = (m_btJointFeedback.m_appliedTorqueBodyB[1] * fltRatio) + vAssist[0];
130  // vVal[2] = (m_btJointFeedback.m_appliedTorqueBodyB[2] * fltRatio) + vAssist[0];
131  // m_lpThisMotorJoint->MotorTorqueToB(vVal);
132  //}
133 }
134 
135 
136  } // Environment
137 } //RoboticsAnimatSim
Classes for implementing the cm-labs vortex physics engine for AnimatLab.