AnimatLab  2
Test
RobotInterface.cpp
Go to the documentation of this file.
1 
7 #include "StdAfx.h"
8 #include "IMovableItemCallback.h"
9 #include "ISimGUICallback.h"
10 #include "IMotorizedJoint.h"
11 #include "AnimatBase.h"
12 
13 #include "Node.h"
14 #include "Link.h"
15 #include "IPhysicsMovableItem.h"
16 #include "IPhysicsBody.h"
17 #include "BoundingBox.h"
18 #include "MovableItem.h"
19 #include "BodyPart.h"
20 #include "Joint.h"
21 #include "ReceptiveField.h"
22 #include "ContactSensor.h"
23 #include "RigidBody.h"
24 #include "Structure.h"
25 #include "NeuralModule.h"
26 #include "Adapter.h"
27 #include "NervousSystem.h"
28 #include "Organism.h"
29 #include "ActivatedItem.h"
30 #include "ActivatedItemMgr.h"
31 #include "DataChartMgr.h"
32 #include "ExternalStimuliMgr.h"
33 #include "KeyFrame.h"
34 #include "SimulationRecorder.h"
35 #include "OdorType.h"
36 #include "Odor.h"
37 #include "Light.h"
38 #include "LightManager.h"
39 #include "Simulator.h"
40 
41 #include "RobotInterface.h"
42 #include "RobotIOControl.h"
43 #include "RobotPartInterface.h"
44 
45 
46 namespace AnimatSim
47 {
48  namespace Robotics
49  {
50 
58 {
59  m_bSynchSim = true;
60  m_fltPhysicsTimeStep = 0.02f;
61 }
62 
70 {
71 
72 try
73 {
74  m_aryIOControls.RemoveAll();
75 }
76 catch(...)
77 {Std_TraceMsg(0, "Caught Error in desctructor of RobotInterface\r\n", "", -1, false, true);}
78 }
79 
88 CStdPtrArray<RobotIOControl> *RobotInterface::IOControls() {return &m_aryIOControls;}
89 
99 
109 {
110  Std_IsAboveMin((float) 0, fltStep, true, "PhysicsTimeStep", false);
111  m_fltPhysicsTimeStep = fltStep;
112 }
113 
123 
133 {
134  if(m_lpSim->InSimulation())
135  {
136  m_bSynchSim = bVal;
137 
138  if(m_lpSim)
139  m_lpSim->RobotAdpaterSynch(bVal);
140  }
141  else
142  m_lpSim->RobotAdpaterSynch(false);
143 }
144 
145 #pragma region DataAccesMethods
146 
147 float *RobotInterface::GetDataPointer(const std::string &strDataType)
148 {
149  std::string strType = Std_CheckString(strDataType);
150 
151  //if(strType == "LIMITPOS")
152  // return &m_fltLimitPos;
153  //else
154  THROW_TEXT_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType, "Robot Interface ID: " + STR(m_strName) + " DataType: " + strDataType);
155 
156  return NULL;
157 }
158 
159 bool RobotInterface::SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError)
160 {
161  std::string strType = Std_CheckString(strDataType);
162 
163  if(strType == "ENABLED")
164  {
165  Enabled(Std_ToBool(strValue));
166  return true;
167  }
168 
169  if(strType == "PHYSICSTIMESTEP")
170  {
171  PhysicsTimeStep((float) atof(strValue.c_str()));
172  return true;
173  }
174 
175  if(strType == "SYNCHSIM")
176  {
177  SynchSim(Std_ToBool(strValue));
178  return true;
179  }
180 
181  if(AnimatBase::SetData(strType, strValue, false))
182  return true;
183 
184  //If it was not one of those above then we have a problem.
185  if(bThrowError)
186  THROW_PARAM_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType, "Data Type", strDataType);
187 
188  return false;
189 }
190 
191 void RobotInterface::QueryProperties(CStdPtrArray<TypeProperty> &aryProperties)
192 {
193  AnimatBase::QueryProperties(aryProperties);
194 
195 }
196 
197 bool RobotInterface::AddItem(const std::string &strItemType, const std::string &strXml, bool bThrowError, bool bDoNotInit)
198 {
199  std::string strType = Std_CheckString(strItemType);
200 
201  if(strType == "ROBOTIOCONTROL")
202  {
203  AddIOControl(strXml);
204  return true;
205  }
206 
207  //If it was not one of those above then we have a problem.
208  if(bThrowError)
209  THROW_PARAM_ERROR(Al_Err_lInvalidItemType, Al_Err_strInvalidItemType, "Item Type", strItemType);
210 
211  return false;
212 }
213 
214 bool RobotInterface::RemoveItem(const std::string &strItemType, const std::string &strID, bool bThrowError)
215 {
216  std::string strType = Std_CheckString(strItemType);
217 
218  if(strType == "ROBOTIOCONTROL")
219  {
220  RemoveIOControl(strID);
221  return true;
222  }
223 
224  //If it was not one of those above then we have a problem.
225  if(bThrowError)
226  THROW_PARAM_ERROR(Al_Err_lInvalidItemType, Al_Err_strInvalidItemType, "Item Type", strItemType);
227 
228  return false;
229 }
230 
240 {
241  CStdXml oXml;
242  oXml.Deserialize(strXml);
243  oXml.FindElement("Root");
244  oXml.FindChildElement("RobotIOControl");
245 
246  RobotIOControl *lpControl = LoadIOControl(oXml);
247 
248  lpControl->Initialize();
249 
250  return lpControl;
251 }
252 
263 void RobotInterface::RemoveIOControl(std::string strID, bool bThrowError)
264 {
265  int iPos = FindChildListPos(strID, bThrowError);
266 
267  RobotIOControl *lpControl = m_aryIOControls[iPos];
268 
269  if(lpControl)
270  lpControl->ShutdownIO();
271 
272  m_aryIOControls.RemoveAt(iPos);
273 }
274 
275 
289 int RobotInterface::FindChildListPos(std::string strID, bool bThrowError)
290 {
291  std::string sID = Std_ToUpper(Std_Trim(strID));
292 
293  int iCount = m_aryIOControls.GetSize();
294  for(int iIndex=0; iIndex<iCount; iIndex++)
295  if(m_aryIOControls[iIndex]->ID() == sID)
296  return iIndex;
297 
298  if(bThrowError)
299  THROW_PARAM_ERROR(Al_Err_lIOControlIDNotFound, Al_Err_strIOControlIDNotFound, "ID", strID);
300 
301  return -1;
302 }
303 
304 #pragma endregion
305 
307 {
309 
310  if(m_bEnabled)
311  {
312  int iCount = m_aryIOControls.GetSize();
313  for(int iIndex=0; iIndex<iCount; iIndex++)
314  m_aryIOControls[iIndex]->Initialize();
315  }
316 }
317 
319 {
321 
322  if(m_bEnabled)
323  {
324  int iCount = m_aryIOControls.GetSize();
325  for(int iIndex=0; iIndex<iCount; iIndex++)
326  m_aryIOControls[iIndex]->ResetSimulation();
327  }
328 }
329 
331 {
333 
334  if(m_bEnabled)
335  {
336  int iCount = m_aryIOControls.GetSize();
337  for(int iIndex=0; iIndex<iCount; iIndex++)
339  }
340 }
341 
343 {
344  //If we are running in simulation mode then do not step the interfaces.
345  if(!m_bEnabled)
346  return;
347 
349 
350  int iCount = m_aryIOControls.GetSize();
351  for(int iIndex=0; iIndex<iCount; iIndex++)
352  if(m_aryIOControls[iIndex]->Enabled())
353  m_aryIOControls[iIndex]->StepSimulation();
354 }
355 
356 void RobotInterface::Load(CStdXml &oXml)
357 {
358  AnimatBase::Load(oXml);
359 
360  oXml.IntoElem(); //Into RigidBody Element
361 
362  PhysicsTimeStep(oXml.GetChildFloat("PhysicsTimeStep", m_fltPhysicsTimeStep));
363  SynchSim(oXml.GetChildBool("SynchSim", m_bSynchSim));
364 
365  if(oXml.FindChildElement("IOControls", false))
366  {
367  oXml.IntoElem(); //Into ChildBodies Element
368  int iChildCount = oXml.NumberOfChildren();
369 
370  for(int iIndex=0; iIndex<iChildCount; iIndex++)
371  {
372  oXml.FindChildByIndex(iIndex);
373  LoadIOControl(oXml);
374  }
375  oXml.OutOfElem(); //OutOf ChildBodies Element
376  }
377 
378  oXml.OutOfElem(); //OutOf RigidBody Element
379 }
380 
393 {
394  RobotIOControl *lpChild = NULL;
395  std::string strType;
396 
397 try
398 {
399  oXml.IntoElem(); //Into Child Element
400  std::string strModule = oXml.GetChildString("ModuleName", "");
401  strType = oXml.GetChildString("Type");
402  oXml.OutOfElem(); //OutOf Child Element
403 
404  lpChild = dynamic_cast<RobotIOControl *>(m_lpSim->CreateObject(strModule, "RobotIOControl", strType));
405  if(!lpChild)
406  THROW_TEXT_ERROR(Al_Err_lConvertingClassToType, Al_Err_strConvertingClassToType, "RobotIOControl");
407 
408  lpChild->ParentInterface(this);
409  lpChild->SetSystemPointers(m_lpSim, m_lpStructure, NULL, NULL, true);
410 
411  lpChild->Load(oXml);
412 
413  m_aryIOControls.Add(lpChild);
414 
415  return lpChild;
416 }
417 catch(CStdErrorInfo oError)
418 {
419  if(lpChild) delete lpChild;
420  RELAY_ERROR(oError);
421  return NULL;
422 }
423 catch(...)
424 {
425  if(lpChild) delete lpChild;
426  THROW_ERROR(Std_Err_lUnspecifiedError, Std_Err_strUnspecifiedError);
427  return NULL;
428 }
429 }
430 
431  }
432 }
virtual float * GetDataPointer(const std::string &strDataType)
Returns a float pointer to a data item of interest in this object.
virtual void ResetSimulation()
Resets the simulation back to time 0.
Definition: AnimatBase.cpp:587
virtual void Deserialize(std::string &strXml)
Deserializes a string into an xml document.
Definition: StdXml.cpp:162
Base class file for all Animat simulation objects.
Declares the nervous system class.
CStdPtrArray< RobotIOControl > m_aryIOControls
Declares the simulation recorder class.
Declares the Robot IO control interface base class.
virtual void SetSystemPointers(Simulator *lpSim, Structure *lpStructure, NeuralModule *lpModule, Node *lpNode, bool bVerify)
Sets the system pointers.
virtual bool FindChildElement(std::string strElementName, bool fThrowError=true)
Finds a child element by name.
Definition: StdXml.cpp:256
virtual bool InSimulation()
Used to determine if we are running in a simulation, or in a real control mode.
Definition: Simulator.cpp:1673
Root namespace for the base simulation library for AnimatLab.
virtual ~RobotInterface(void)
Destructor.
virtual void Initialize()
Initializes this object.
Declares the body part class.
virtual bool AddItem(const std::string &strItemType, const std::string &strXml, bool bThrowError=true, bool bDoNotInit=false)
Adds a new object to this parent.
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 bool FindElement(std::string strElementName, bool fThrowError=true)
Finds an element with the specified name.
Definition: StdXml.cpp:179
Simulator * m_lpSim
The pointer to a Simulation.
Definition: AnimatBase.h:43
Information about the standard error.
Definition: StdErrorInfo.h:19
virtual std::string ID()
Gets the unique GUID ID of this object.
Definition: AnimatBase.cpp:167
virtual bool IntoElem()
Goes into the next element where the cursor is located.
Definition: StdXml.cpp:42
virtual float PhysicsTimeStep()
Gets the physics time step used within the robot framwork.
virtual void ParentInterface(RobotInterface *lpParent)
Sets the parent robot interface pointer.
virtual void Initialize()
Initializes this object.
Definition: AnimatBase.cpp:573
virtual void AfterResetSimulation()
Called after a simulation reset for some objects.
Definition: AnimatBase.cpp:598
Declares the key frame class.
virtual RobotIOControl * AddIOControl(std::string strXml)
Creates and adds a robot IO control.
Declares the joint class.
Declares the organism class.
RobotInterface(void)
Default constructor.
bool Std_IsAboveMin(int iMinVal, int iVal, bool bThrowError, std::string strParamName, bool bInclusiveLimit)
Tests if a number is above a minimum value.
AnimatSim::Environment::Structure * m_lpStructure
The pointer to this items parent Structure. If this is not relevant for this object then this is NULL...
Definition: AnimatBase.h:46
virtual void ShutdownIO()
This method is called just before the IO thread is closed down. It gives the IO objects a chance to d...
std::string Std_Trim(std::string strVal)
Trims a string.
Declares a light object.
virtual void AfterResetSimulation()
Called after a simulation reset for some objects.
Declares the activated item class.
virtual CStdSerialize * CreateObject(std::string strModule, std::string strClassName, std::string strType, bool bThrowError=true)
Creates an object using a class factory.
Definition: Simulator.cpp:3440
Declares a light manager object.
virtual bool RobotAdpaterSynch()
Gets whether we need to synch the physics adapters in a simulation to the robot physics time step...
Definition: Simulator.cpp:1645
Declares the bounding box class.
virtual bool Enabled()
Tells whether this item is enabled or not. This is not actually used for all objects, only specific ones. I am putting it in the base class though to prevent numerous duplications.
Definition: AnimatBase.cpp:113
A Robot IO controller base class.
A standard xml manipulation class.
Definition: StdXml.h:19
virtual void QueryProperties(CStdPtrArray< TypeProperty > &aryProperties)
Queries this object for a list of properties that can be changed using SetData.
virtual void StepSimulation()
Step the simulation for this object.
Definition: AnimatBase.cpp:643
virtual bool SynchSim()
Gets whether we need to delay stepping of the physics adapters in the simulation to more closely matc...
Declares the node class.
virtual void QueryProperties(CStdPtrArray< TypeProperty > &aryProperties)
Queries this object for a list of properties that can be changed using SetData.
Definition: AnimatBase.cpp:447
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 Load(StdUtils::CStdXml &oXml)
Loads the item using an XML data packet.
Definition: AnimatBase.cpp:771
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
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
virtual int NumberOfChildren()
Gets the number of children of the current element.
Definition: StdXml.cpp:202
virtual RobotIOControl * LoadIOControl(CStdXml &oXml)
Loads a child IO Control.
virtual bool OutOfElem()
Goes out of the element where the cursor is located.
Definition: StdXml.cpp:56
virtual void Initialize()
Initializes this object.
virtual void ResetSimulation()
Resets the simulation back to time 0.
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 RemoveIOControl(std::string strID, bool bThrowError=true)
Removes the rigid body with the specified ID.
Declares the structure class.
Declares the odor type class.
virtual int FindChildListPos(std::string strID, bool bThrowError=true)
Finds the array index for the child part with the specified ID.
Declares the robotics inerface for animatlab.
Declares the odor class.
Declares the simulator class.
Declares the neural module class.
virtual bool FindChildByIndex(int iIndex, bool bThrowError=true)
Finds a child element by index.
Definition: StdXml.cpp:225
virtual CStdPtrArray< RobotIOControl > * IOControls()
Gets the array of IO controls.
std::string Std_ToUpper(std::string strVal)
Converts a string to upper case.
Declares the activated item manager class.
Declares the contact sensor class.
Declares the external stimuli manager 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.
Definition: AnimatBase.cpp:371
Declares the receptive field class.
virtual void StepSimulation()
Step the simulation for this object.
virtual bool RemoveItem(const std::string &strItemType, const std::string &strID, bool bThrowError=true)
Removes a child item from this parent.
std::string m_strName
The name for this object.
Definition: AnimatBase.h:61
virtual float GetChildFloat(std::string strElementName)
Gets a float value from the element with the specified name.
Definition: StdXml.cpp:617