AnimatLab  2
Test
MotorStimulus.cpp
Go to the documentation of this file.
1 
7 #include "StdAfx.h"
8 
9 #include "StdAfx.h"
10 #include "IMovableItemCallback.h"
11 #include "ISimGUICallback.h"
12 #include "AnimatBase.h"
13 
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include "Gain.h"
17 #include "Node.h"
18 #include "Link.h"
19 #include "IPhysicsMovableItem.h"
20 #include "IPhysicsBody.h"
21 #include "BoundingBox.h"
22 #include "MovableItem.h"
23 #include "BodyPart.h"
24 #include "Joint.h"
25 #include "MotorizedJoint.h"
26 #include "ReceptiveField.h"
27 #include "ContactSensor.h"
28 #include "RigidBody.h"
29 #include "Structure.h"
30 #include "NeuralModule.h"
31 #include "Adapter.h"
32 #include "NervousSystem.h"
33 #include "Organism.h"
34 #include "ActivatedItem.h"
35 #include "ActivatedItemMgr.h"
36 #include "DataChartMgr.h"
37 #include "ExternalStimulus.h"
38 #include "ExternalStimuliMgr.h"
39 #include "KeyFrame.h"
40 #include "SimulationRecorder.h"
41 #include "OdorType.h"
42 #include "Odor.h"
43 #include "Light.h"
44 #include "LightManager.h"
45 #include "Simulator.h"
46 
47 #include "MotorStimulus.h"
48 
49 namespace AnimatSim
50 {
51  namespace ExternalStimuli
52  {
60 {
61  m_lpJoint = NULL;
62  m_lpEval = NULL;
63  m_fltValue = 0;
64  m_fltValueReport = 0;
65  m_bDisableMotorWhenDone = false;
66  m_lpPosition = NULL;
67  m_lpVelocity = NULL;
68  m_iTargetID = DESIRED_VELOCITY_TYPE;
69 }
70 
78 {
79 
80 try
81 {
82  m_lpJoint = NULL;
83  if(m_lpEval) delete m_lpEval;
84 }
85 catch(...)
86 {Std_TraceMsg(0, "Caught Error in desctructor of MotorStimulus\r\n", "", -1, false, true);}
87 }
88 
97 void MotorStimulus::Equation(std::string strVal)
98 {
99  //Initialize the postfix evaluator.
100  if(m_lpEval)
101  {delete m_lpEval; m_lpEval = NULL;}
102 
103  m_strEquation = strVal;
104  m_lpEval = new CStdPostFixEval;
105 
106  m_lpEval->AddVariable("t");
107  m_lpEval->AddVariable("p");
108  m_lpEval->AddVariable("v");
109  m_lpEval->Equation(m_strEquation);
110 }
111 
112 void MotorStimulus::TargetID(int iID)
113 {
114  if(iID == DESIRED_VELOCITY_TYPE || iID == DESIRED_POSITION_TYPE)
115  m_iTargetID = iID;
116  else
117  THROW_PARAM_ERROR(Al_Err_lInvalidTargetID, Al_Err_strInvalidTargetID, "ID", iID);
118 }
119 
120 void MotorStimulus::TargetID(std::string strID)
121 {
122  std::string strId = Std_CheckString(strID);
123 
124  if(strId == "VELOCITY")
125  m_iTargetID = DESIRED_VELOCITY_TYPE;
126  else if(strId == "POSITION")
127  m_iTargetID = DESIRED_POSITION_TYPE;
128  else
129  THROW_PARAM_ERROR(Al_Err_lInvalidTargetID, Al_Err_strInvalidTargetID, "ID", strID);
130 }
131 
133 {
135 
136  m_fltValue = 0;
137  m_fltValueReport = 0;
138 }
139 
141 {
143 
144  //Lets try and get the joint we will be injecting.
145  m_lpJoint = dynamic_cast<MotorizedJoint *>(m_lpSim->FindJoint(m_strStructureID, m_strJointID));
146  if(!m_lpJoint)
147  THROW_PARAM_ERROR(Al_Err_lJointNotMotorized, Al_Err_strJointNotMotorized, "ID", m_strJointID);
148 
149  m_lpPosition = m_lpJoint->GetDataPointer("JOINTPOSITION");
150  m_lpVelocity = m_lpJoint->GetDataPointer("JOINTACTUALVELOCITY");
151 }
152 
154 {
156 
157  //int iTest = 0;
158  //if(m_lpSim->Time() >= 5)
159  // iTest = iTest;
160 
161  if(m_bEnabled)
162  {
163  m_lpJoint->EnableMotor(true);
164 
165  if(m_iTargetID == DESIRED_VELOCITY_TYPE)
166  m_lpJoint->DesiredVelocity(0);
167  }
168 }
169 
170 
172 {
173  //float fltVel=0;
174  //int iTest = 0;
175  //if(m_lpSim->Time() >= 5.75)
176  // iTest = iTest;
177 
178  try
179  {
180  if(m_bEnabled)
181  {
182  //IMPORTANT! This stimulus applies a stimulus to the physics engine, so it should ONLY be called once for every time the physcis
183  //engine steps. If you do not do this then the you will accumulate forces being applied during the neural steps, and the total
184  //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
185  //the physics step count is equal to the step interval.
187  {
188  m_lpEval->SetVariable("t", (m_lpSim->Time()-m_fltStartTime) );
189 
190  if(m_lpPosition)
191  m_lpEval->SetVariable("p", *m_lpPosition);
192 
193  if(m_lpVelocity)
194  m_lpEval->SetVariable("v", *m_lpVelocity);
195 
196  m_fltValueReport = m_fltValue = m_lpEval->Solve();
197  //fltVel = -sin(6.28*(m_lpSim->Time()-m_fltStartTime));
198 
199  if(!m_lpJoint->UsesRadians())
200  m_fltValue *= m_lpSim->InverseDistanceUnits();
201 
202  if(m_iTargetID == DESIRED_VELOCITY_TYPE)
203  m_lpJoint->DesiredVelocity(m_fltValue);
204  else if(m_iTargetID == DESIRED_POSITION_TYPE)
205  m_lpJoint->DesiredPosition(m_fltValue);
206  }
207  }
208  }
209  catch(...)
210  {
211  LOG_ERROR("Error Occurred while setting Joint Velocity");
212  }
213 }
214 
215 
217 {
219 
220  if(m_bEnabled)
221  {
222  if(m_iTargetID == DESIRED_VELOCITY_TYPE)
223  m_lpJoint->DesiredVelocity(0);
224 
225  if(m_bDisableMotorWhenDone)
226  m_lpJoint->EnableMotor(false);
227  }
228 }
229 
230 float *MotorStimulus::GetDataPointer(const std::string &strDataType)
231 {
232  float *lpData=NULL;
233  std::string strType = Std_CheckString(strDataType);
234 
235  if(strType == "VELOCITY" || strType == "VALUE")
236  lpData = &m_fltValueReport;
237  else
238  THROW_TEXT_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType, "StimulusName: " + STR(m_strName) + " DataType: " + strDataType);
239 
240  return lpData;
241 }
242 
243 bool MotorStimulus::SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError)
244 {
245  std::string strType = Std_CheckString(strDataType);
246 
247  if(ExternalStimulus::SetData(strDataType, strValue, false))
248  return true;
249 
250  if(strType == "VELOCITY" || strType == "EQUATION")
251  {
252  Equation(strValue);
253  return true;
254  }
255 
256  if(strType == "DISABLEWHENDONE")
257  {
258  DisableMotorWhenDone(Std_ToBool(strValue));
259  return true;
260  }
261 
262  if(strType == "TARGETID")
263  {
264  TargetID(strValue);
265  return true;
266  }
267 
268  //If it was not one of those above then we have a problem.
269  if(bThrowError)
270  THROW_PARAM_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType, "Data Type", strDataType);
271 
272  return false;
273 }
274 
275 void MotorStimulus::QueryProperties(CStdPtrArray<TypeProperty> &aryProperties)
276 {
277  ExternalStimulus::QueryProperties(aryProperties);
278 
279  aryProperties.Add(new TypeProperty("Velocity", AnimatPropertyType::Float, AnimatPropertyDirection::Both));
280  aryProperties.Add(new TypeProperty("Value", AnimatPropertyType::Float, AnimatPropertyDirection::Both));
281  aryProperties.Add(new TypeProperty("Equation", AnimatPropertyType::String, AnimatPropertyDirection::Set));
282  aryProperties.Add(new TypeProperty("DisableMotorWhenDone", AnimatPropertyType::Boolean, AnimatPropertyDirection::Set));
283  aryProperties.Add(new TypeProperty("TargetID", AnimatPropertyType::Integer, AnimatPropertyDirection::Set));
284 }
285 
286 void MotorStimulus::Load(CStdXml &oXml)
287 {
288  ActivatedItem::Load(oXml);
289 
290  oXml.IntoElem(); //Into Simulus Element
291 
292  m_strStructureID = oXml.GetChildString("StructureID");
293  if(Std_IsBlank(m_strStructureID))
294  THROW_ERROR(Al_Err_lIDBlank, Al_Err_strIDBlank);
295 
296  m_strJointID = oXml.GetChildString("JointID");
297  if(Std_IsBlank(m_strStructureID))
298  THROW_ERROR(Al_Err_lIDBlank, Al_Err_strIDBlank);
299 
300  if(oXml.FindChildElement("Velocity", false))
301  Equation(oXml.GetChildString("Velocity"));
302  else
303  Equation(oXml.GetChildString("Equation"));
304 
305  DisableMotorWhenDone(oXml.GetChildBool("DisableMotorWhenDone", m_bDisableMotorWhenDone));
306  TargetID(oXml.GetChildString("TargetID", "Velocity"));
307 
308  oXml.OutOfElem(); //OutOf Simulus Element
309 }
310 
311  } //ExternalStimuli
312 } //AnimatSim
313 
314 
315 
316 
Declares the external stimulus base class.
Base class file for all Animat simulation objects.
Declares the nervous system class.
void AddVariable(std::string strVarName)
Adds a variable.
virtual void Activate()
Activates this item.
Declares the simulation recorder class.
virtual bool FindChildElement(std::string strElementName, bool fThrowError=true)
Finds a child element by name.
Definition: StdXml.cpp:256
Root namespace for the base simulation library for AnimatLab.
virtual void Activate()
Activates this item.
Declares the body part class.
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 bool IntoElem()
Goes into the next element where the cursor is located.
Definition: StdXml.cpp:42
Class that stores information about types for QueryProperty information.
Definition: TypeProperty.h:35
virtual void QueryProperties(CStdPtrArray< TypeProperty > &aryProperties)
Queries this object for a list of properties that can be changed using SetData.
virtual float DesiredVelocity()
Gets the desired velocity.
Declares the key frame class.
Declares the joint class.
Declares the organism class.
virtual float * GetDataPointer(const std::string &strDataType)
Returns a float pointer to a data item of interest in this object.
Declares a light object.
virtual long PhysicsStepCount()
Gets the physics step count.
Definition: Simulator.cpp:1176
Declares the activated item class.
Declares a light manager object.
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 bounding box class.
Declares the gain base class.
virtual bool SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError=true)
Set a variable based on a string data type name.
void Equation(std::string strVal)
Sets the post-fix Equation.
A standard xml manipulation class.
Definition: StdXml.h:19
virtual void ResetSimulation()
Resets the simulation back to time 0.
virtual void ResetSimulation()
Resets the simulation back to time 0.
virtual bool EnableMotor()
Tells if the motor is enabled.
Declares the node class.
virtual std::string GetChildString(std::string strElementName)
Gets a string value from the element with the specified name.
Definition: StdXml.cpp:307
bool Std_ToBool(int iVal)
Converts a value toa bool.
virtual void Initialize()
Initializes this object.
Standard post fix evaluation 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 bool GetChildBool(std::string strElementName)
Gets a bool value from the element with the specified name.
Definition: StdXml.cpp:699
double Solve()
Solves the equation using the defined variable values.
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
void SetVariable(std::string strVarName, double dblVal)
Sets the value of a variable.
virtual bool OutOfElem()
Goes out of the element where the cursor is located.
Definition: StdXml.cpp:56
virtual void Deactivate()
Deactivates this item.
virtual float DesiredPosition()
Gets the desired Position.
bool Std_IsBlank(std::string strVal)
Trims a string and tests if a string is blank.
Declares the data chart manager class.
Declares the rigid body class.
std::string Std_CheckString(std::string strVal)
Converts a string to upper case and trims it.
virtual void StepSimulation()
Step the simulation for this object.
Declares the structure class.
Declares the odor type class.
virtual void Initialize()
Initializes this object.
virtual void QueryProperties(CStdPtrArray< TypeProperty > &aryProperties)
Queries this object for a list of properties that can be changed using SetData.
virtual bool UsesRadians()
Tells whether this joint uses radians or meters for its measurements.
Definition: Joint.cpp:97
Declares the vs motor velocity stimulus class.
virtual float InverseDistanceUnits()
Gets the inverse distance units.
Definition: Simulator.cpp:1742
Declares the odor class.
Declares the simulator class.
Declares the neural module class.
virtual float Time()
Gets the current simulation time in seconds.
Definition: Simulator.cpp:559
Declares the activated item manager class.
virtual void Deactivate()
Deactivates this item.
Declares the contact sensor class.
Declares the external stimuli manager class.
Declares the receptive field class.
std::string m_strName
The name for this object.
Definition: AnimatBase.h:61
virtual bool SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError=true)
Set a variable based on a string data type name.