AnimatLab  2
Test
VsMotorVelocityStimulus.cpp
Go to the documentation of this file.
1 
7 #include "stdafx.h"
8 
9 #include "VsMovableItem.h"
10 #include "VsBody.h"
11 #include "VsJoint.h"
12 #include "VsMotorizedJoint.h"
13 #include "VsRigidBody.h"
14 #include "VsSimulator.h"
15 
17 #include "VsDragger.h"
18 
19 namespace VortexAnimatSim
20 {
21  namespace ExternalStimuli
22  {
30 {
31  m_lpJoint = NULL;
32  m_lpEval = NULL;
33  m_fltVelocity = 0;
34  m_fltVelocityReport = 0;
35  m_bDisableMotorWhenDone = false;
36  m_lpPosition = NULL;
37  m_lpVelocity = NULL;
38  m_iTargetID = DESIRED_VELOCITY_TYPE;
39 }
40 
48 {
49 
50 try
51 {
52  m_lpJoint = NULL;
53  if(m_lpEval) delete m_lpEval;
54 }
55 catch(...)
56 {Std_TraceMsg(0, "Caught Error in desctructor of VsMotorVelocityStimulus\r\n", "", -1, false, true);}
57 }
58 
67 void VsMotorVelocityStimulus::VelocityEquation(std::string strVal)
68 {
69  //Initialize the postfix evaluator.
70  if(m_lpEval)
71  {delete m_lpEval; m_lpEval = NULL;}
72 
73  m_strVelocityEquation = strVal;
74  m_lpEval = new CStdPostFixEval;
75 
76  m_lpEval->AddVariable("t");
77  m_lpEval->AddVariable("p");
78  m_lpEval->AddVariable("v");
79  m_lpEval->Equation(m_strVelocityEquation);
80 }
81 
82 void VsMotorVelocityStimulus::TargetID(int iID)
83 {
84  if(iID == DESIRED_VELOCITY_TYPE || iID == DESIRED_POSITION_TYPE)
85  m_iTargetID = iID;
86  else
87  THROW_PARAM_ERROR(Al_Err_lInvalidTargetID, Al_Err_strInvalidTargetID, "ID", iID);
88 }
89 
90 void VsMotorVelocityStimulus::TargetID(std::string strID)
91 {
92  std::string strId = Std_CheckString(strID);
93 
94  if(strId == "VELOCITY")
95  m_iTargetID = DESIRED_VELOCITY_TYPE;
96  else if(strId == "POSITION")
97  m_iTargetID = DESIRED_POSITION_TYPE;
98  else
99  THROW_PARAM_ERROR(Al_Err_lInvalidTargetID, Al_Err_strInvalidTargetID, "ID", strID);
100 }
101 
103 {
104  ExternalStimulus::ResetSimulation();
105 
106  m_fltVelocity = 0;
107  m_fltVelocityReport = 0;
108 }
109 
111 {
112  ExternalStimulus::Initialize();
113 
114  //Lets try and get the joint we will be injecting.
115  m_lpJoint = dynamic_cast<MotorizedJoint *>(m_lpSim->FindJoint(m_strStructureID, m_strJointID));
116  if(!m_lpJoint)
117  THROW_PARAM_ERROR(Al_Err_lJointNotMotorized, Al_Err_strJointNotMotorized, "ID", m_strJointID);
118 
119  m_lpPosition = m_lpJoint->GetDataPointer("JOINTPOSITION");
120  m_lpVelocity = m_lpJoint->GetDataPointer("JOINTACTUALVELOCITY");
121 }
122 
124 {
125  ExternalStimulus::Activate();
126 
127  if(m_bEnabled)
128  {
129  m_lpJoint->EnableMotor(true);
130 
131  if(m_iTargetID == DESIRED_VELOCITY_TYPE)
132  m_lpJoint->DesiredVelocity(0);
133  }
134 }
135 
136 
138 {
139  //float fltVel=0;
140 
141  try
142  {
143  if(m_bEnabled)
144  {
145  //IMPORTANT! This stimulus applies a stimulus to the physics engine, so it should ONLY be called once for every time the physcis
146  //engine steps. If you do not do this then the you will accumulate forces being applied during the neural steps, and the total
147  //for you apply will be greater than what it should be. To get around this we will only call the code in step simulation if
148  //the physics step count is equal to the step interval.
150  {
151  m_lpEval->SetVariable("t", (m_lpSim->Time()-m_fltStartTime) );
152 
153  if(m_lpPosition)
154  m_lpEval->SetVariable("p", *m_lpPosition);
155 
156  if(m_lpVelocity)
157  m_lpEval->SetVariable("v", *m_lpVelocity);
158 
159  m_fltVelocityReport = m_fltVelocity = m_lpEval->Solve();
160  //fltVel = -sin(6.28*(m_lpSim->Time()-m_fltStartTime));
161 
162  if(!m_lpJoint->UsesRadians())
163  m_fltVelocity *= m_lpSim->InverseDistanceUnits();
164 
165  if(m_iTargetID == DESIRED_VELOCITY_TYPE)
166  m_lpJoint->DesiredVelocity(m_fltVelocity);
167  else if(m_iTargetID == DESIRED_POSITION_TYPE)
168  m_lpJoint->DesiredPosition(m_fltVelocity);
169  }
170  }
171  }
172  catch(...)
173  {
174  LOG_ERROR("Error Occurred while setting Joint Velocity");
175  }
176 }
177 
178 
180 {
181  ExternalStimulus::Deactivate();
182 
183  if(m_bEnabled)
184  {
185  if(m_iTargetID == DESIRED_VELOCITY_TYPE)
186  m_lpJoint->DesiredVelocity(0);
187 
188  if(m_bDisableMotorWhenDone)
189  m_lpJoint->EnableMotor(false);
190  }
191 }
192 
193 float *VsMotorVelocityStimulus::GetDataPointer(const std::string &strDataType)
194 {
195  float *lpData=NULL;
196  std::string strType = Std_CheckString(strDataType);
197 
198  if(strType == "VELOCITY" || strType == "VALUE")
199  lpData = &m_fltVelocityReport;
200  else
201  THROW_TEXT_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType, "StimulusName: " + STR(m_strName) + " DataType: " + strDataType);
202 
203  return lpData;
204 }
205 
206 bool VsMotorVelocityStimulus::SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError)
207 {
208  std::string strType = Std_CheckString(strDataType);
209 
210  if(ExternalStimulus::SetData(strDataType, strValue, false))
211  return true;
212 
213  if(strType == "VELOCITY" || strType == "EQUATION")
214  {
215  VelocityEquation(strValue);
216  return true;
217  }
218 
219  if(strType == "DISABLEWHENDONE")
220  {
221  DisableMotorWhenDone(Std_ToBool(strValue));
222  return true;
223  }
224 
225  if(strType == "TARGETID")
226  {
227  TargetID(strValue);
228  return true;
229  }
230 
231  //If it was not one of those above then we have a problem.
232  if(bThrowError)
233  THROW_PARAM_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType, "Data Type", strDataType);
234 
235  return false;
236 }
237 
238 void VsMotorVelocityStimulus::QueryProperties(CStdPtrArray<TypeProperty> &aryProperties)
239 {
240  ExternalStimulus::QueryProperties(aryProperties);
241 
242  aryProperties.Add(new TypeProperty("Velocity", AnimatPropertyType::Float, AnimatPropertyDirection::Both));
243  aryProperties.Add(new TypeProperty("Equation", AnimatPropertyType::String, AnimatPropertyDirection::Set));
244  aryProperties.Add(new TypeProperty("DisableMotorWhenDone", AnimatPropertyType::Boolean, AnimatPropertyDirection::Set));
245  aryProperties.Add(new TypeProperty("TargetID", AnimatPropertyType::Integer, AnimatPropertyDirection::Set));
246 }
247 
248 void VsMotorVelocityStimulus::Load(CStdXml &oXml)
249 {
250  ActivatedItem::Load(oXml);
251 
252  oXml.IntoElem(); //Into Simulus Element
253 
254  m_strStructureID = oXml.GetChildString("StructureID");
255  if(Std_IsBlank(m_strStructureID))
256  THROW_ERROR(Al_Err_lIDBlank, Al_Err_strIDBlank);
257 
258  m_strJointID = oXml.GetChildString("JointID");
259  if(Std_IsBlank(m_strStructureID))
260  THROW_ERROR(Al_Err_lIDBlank, Al_Err_strIDBlank);
261 
262  if(oXml.FindChildElement("Velocity", false))
263  VelocityEquation(oXml.GetChildString("Velocity"));
264  else
265  VelocityEquation(oXml.GetChildString("Equation"));
266 
267  DisableMotorWhenDone(oXml.GetChildBool("DisableMotorWhenDone", m_bDisableMotorWhenDone));
268  TargetID(oXml.GetChildString("TargetID", "Velocity"));
269 
270  oXml.OutOfElem(); //OutOf Simulus Element
271 }
272 
273  } //ExternalStimuli
274 } //VortexAnimatSim
275 
276 
277 
278 
virtual void StepSimulation()
Step the simulation for this object.
Simulator * m_lpSim
The pointer to a Simulation.
Definition: AnimatBase.h:43
virtual short PhysicsStepInterval()
Gets the physics step interval.
Definition: Simulator.cpp:1097
virtual void ResetSimulation()
Resets the simulation back to time 0.
virtual bool SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError=true)
Set a variable based on a string data type name.
virtual long PhysicsStepCount()
Gets the physics step count.
Definition: Simulator.cpp:1176
virtual Joint * FindJoint(std::string strStructureID, std::string strJointID, bool bThrowError=true)
Finds a joint with the specified ID in the specified structure.
Definition: Simulator.cpp:3954
Declares the vs motor velocity stimulus class.
virtual float * GetDataPointer(const std::string &strDataType)
Returns a float pointer to a data item of interest in this object.
bool Std_ToBool(int iVal)
Converts a value toa bool.
Classes for implementing the cm-labs vortex physics engine for AnimatLab.
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.
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
bool Std_IsBlank(std::string strVal)
Trims a string and tests if a string is blank.
std::string Std_CheckString(std::string strVal)
Converts a string to upper case and trims it.
virtual float InverseDistanceUnits()
Gets the inverse distance units.
Definition: Simulator.cpp:1742
virtual float Time()
Gets the current simulation time in seconds.
Definition: Simulator.cpp:559
std::string m_strName
The name for this object.
Definition: AnimatBase.h:61