AnimatLab  2
Test
Organism.cpp
Go to the documentation of this file.
1 
7 #include "StdAfx.h"
8 #include "IMovableItemCallback.h"
9 #include "ISimGUICallback.h"
10 #include "AnimatBase.h"
11 
12 #include "Gain.h"
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 "Sensor.h"
25 #include "Attachment.h"
26 #include "Structure.h"
27 #include "Structure.h"
28 #include "NeuralModule.h"
29 #include "Adapter.h"
30 #include "NervousSystem.h"
31 #include "Organism.h"
32 #include "ActivatedItem.h"
33 #include "ActivatedItemMgr.h"
34 #include "DataChartMgr.h"
35 #include "ExternalStimuliMgr.h"
36 #include "KeyFrame.h"
37 #include "SimulationRecorder.h"
38 #include "OdorType.h"
39 #include "Odor.h"
40 #include "Light.h"
41 #include "LightManager.h"
42 #include "Simulator.h"
43 
50 Organism::Organism()
51 {
52  m_bDead = false;
53  m_lpNervousSystem = NULL;
54  m_lpRobot = NULL;
55 }
56 
64 {
65 
66 try
67 {
69  {
70  delete m_lpNervousSystem;
71  m_lpNervousSystem = NULL;
72  }
73 
74  if(m_lpRobot)
75  {
76  delete m_lpRobot;
77  m_lpRobot = NULL;
78  }
79 }
80 catch(...)
81 {Std_TraceMsg(0, "Caught Error in desctructor of Organism\r\n", "", -1, false, true);}
82 }
83 
93 {return m_bDead;}
94 
95 void Organism::Kill(bool bState)
96 {
97  Structure::Kill(bState);
98 
99  m_bDead = bState;
100 
101  if(m_lpBody)
102  m_lpBody->Kill(bState);
103 
105  m_lpNervousSystem->Kill(bState);
106 }
107 
109 {
111 
114 
115  if(m_lpRobot)
117 }
118 
120 {
122 
123  //Make sure to reset us from being killed if it happend during the sim.
124  Kill(false);
125 
126  if(m_lpNervousSystem)
128 
129  if(m_lpRobot)
131 }
132 
142 {
143  if(!m_bDead)
144  {
145  if(m_lpScript)
146  m_lpScript->BeforeStepPhysicsEngine();
147 
149 
150  if(m_lpScript)
151  m_lpScript->AfterStepPhysicsEngine();
152  }
153 }
154 
156 {
158 
159  if(m_lpRobot && m_lpRobot->Enabled())
161 }
162 
163 void Organism::MinTimeStep(float &fltMin)
164 {
165  m_lpNervousSystem->MinTimeStep(fltMin);
166 }
167 
168 #pragma region DataAccesMethods
169 
170 bool Organism::SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError)
171 {
172  std::string strType = Std_CheckString(strDataType);
173 
174  if(Structure::SetData(strDataType, strValue, false))
175  return true;
176 
177  //if(strType == "TIMESTEP")
178  //{
179  // TimeStep((float) atof(strValue.c_str()));
180  // return true;
181  //}
182 
183  //If it was not one of those above then we have a problem.
184  if(bThrowError)
185  THROW_PARAM_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType, "Data Type", strDataType);
186 
187  return false;
188 }
189 
190 
200 {
201  CStdXml oXml;
202  oXml.Deserialize(strXml);
203  oXml.FindElement("Root");
204  oXml.FindChildElement("RobotInterface");
205 
206  RobotInterface *lpInterface = LoadRobotInterface(oXml);
207 
208  lpInterface->Initialize();
209 
210  return lpInterface;
211 }
212 
223 void Organism::RemoveRobotInterface(std::string strID, bool bThrowError)
224 {
226  THROW_PARAM_ERROR(Al_Err_lRobotInterfaceIDNotFound, Al_Err_strRobotInterfaceIDNotFound, "ID", strID);
227 
228  delete m_lpRobot;
229  m_lpRobot = NULL;
230 }
231 
232 bool Organism::AddItem(const std::string &strItemType, const std::string &strXml, bool bThrowError, bool bDoNotInit)
233 {
234  std::string strType = Std_CheckString(strItemType);
235 
236  if(Structure::AddItem(strItemType, strXml, false))
237  return true;
238 
239  if(strType == "NEURALMODULE")
240  {
241  try
242  {
244  return true;
245  }
246  catch(CStdErrorInfo oError)
247  {
248  if(bThrowError)
249  RELAY_ERROR(oError);
250  }
251  }
252 
253  if(strType == "ROBOTINTERFACE")
254  {
255  AddRobotInterface(strXml);
256  return true;
257  }
258 
259  //If it was not one of those above then we have a problem.
260  if(bThrowError)
261  THROW_PARAM_ERROR(Al_Err_lInvalidItemType, Al_Err_strInvalidItemType, "Item Type", strItemType);
262 
263  return false;
264 }
265 
266 bool Organism::RemoveItem(const std::string &strItemType, const std::string &strID, bool bThrowError)
267 {
268  std::string strType = Std_CheckString(strItemType);
269 
270  if(Structure::RemoveItem(strItemType, strID, false))
271  return true;
272 
273  if(strType == "NEURALMODULE")
274  {
275  try
276  {
278  return true;
279  }
280  catch(CStdErrorInfo oError)
281  {
282  if(bThrowError)
283  RELAY_ERROR(oError);
284  }
285  }
286 
287  if(strType == "ROBOTINTERFACE")
288  {
289  RemoveRobotInterface(strID, bThrowError);
290  return true;
291  }
292 
293  //If it was not one of those above then we have a problem.
294  if(bThrowError)
295  THROW_PARAM_ERROR(Al_Err_lInvalidItemType, Al_Err_strInvalidItemType, "Item Type", strItemType);
296 
297  return false;
298 }
299 
300 #pragma endregion
301 
312 AnimatSim::Behavior::NervousSystem *Organism::GetNervousSystem()
313 {return m_lpNervousSystem;}
314 
317 
318 void Organism::SaveKeyFrameSnapshot(byte *aryBytes, long &lIndex)
319 {m_lpNervousSystem->SaveKeyFrameSnapshot(aryBytes, lIndex);}
320 
321 void Organism::LoadKeyFrameSnapshot(byte *aryBytes, long &lIndex)
322 {m_lpNervousSystem->LoadKeyFrameSnapshot(aryBytes, lIndex);}
323 
336 {
337  RobotInterface *lpInterface = NULL;
338  std::string strType;
339 
340 try
341 {
342  if(oXml.FindChildElement("RobotInterface", false))
343  {
344  if(m_lpRobot)
345  {
346  delete m_lpRobot;
347  m_lpRobot = NULL;
348  }
349 
350  oXml.IntoChildElement("RobotInterface");
351  std::string strModuleName = oXml.GetChildString("ModuleName", "");
352  std::string strType = oXml.GetChildString("Type");
353  oXml.OutOfElem(); //OutOf RobotInterface Element
354 
355  lpInterface = dynamic_cast<RobotInterface *>(m_lpSim->CreateObject(strModuleName, "RobotInterface", strType));
356  if(!lpInterface)
357  THROW_TEXT_ERROR(Al_Err_lConvertingClassToType, Al_Err_strConvertingClassToType, "RobotInterface");
358  lpInterface->SetSystemPointers(m_lpSim, this, m_lpModule, NULL, true);
359  lpInterface->Load(oXml);
360  m_lpRobot = lpInterface;
361  }
362 
363  return m_lpRobot;
364 }
365 catch(CStdErrorInfo oError)
366 {
367  if(m_lpRobot) delete m_lpRobot;
368  RELAY_ERROR(oError);
369  return NULL;
370 }
371 catch(...)
372 {
373  if(m_lpRobot) delete m_lpRobot;
374  THROW_ERROR(Std_Err_lUnspecifiedError, Std_Err_strUnspecifiedError);
375  return NULL;
376 }
377 }
378 
379 void Organism::Load(CStdXml &oXml)
380 {
381  Structure::Load(oXml);
382 
383  oXml.IntoElem(); //Into Structure Element
384 
385  //dwc convert. Need to have a method to remove a nervous system. It needs to remove any added
386  //modules from the list in the simulator.
389 
390  oXml.IntoChildElement("NervousSystem");
391 
392  m_lpNervousSystem->SetSystemPointers(m_lpSim, this, NULL, NULL, true);
393  m_lpNervousSystem->Load(oXml);
394 
395  oXml.OutOfElem(); //OutOf NervousSystem Element
396 
397  LoadRobotInterface(oXml);
398 
399  oXml.OutOfElem(); //OutOf Structure Element
400 }
NervousSystem * m_lpNervousSystem
The pointer to the nervous system.
Definition: Organism.h:35
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.
Nervous system for an organism.
Definition: NervousSystem.h:28
Declares the nervous system class.
Declares the simulation recorder 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.
Definition: Structure.cpp:578
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
AnimatSim::Behavior::NeuralModule * m_lpModule
The pointer to this items parentNeuralModule. If this is not relevant for this object then this is NU...
Definition: AnimatBase.h:49
virtual void StepNeuralEngine()
Step neural engine.
Definition: Organism.cpp:141
virtual void ResetSimulation()
Resets the simulation back to time 0.
Definition: Organism.cpp:119
virtual void SaveKeyFrameSnapshot(byte *aryBytes, long &lIndex)
Saves a key frame snapshot.
Declares the body part class.
virtual RobotInterface * LoadRobotInterface(CStdXml &oXml)
Loads a robot interface Control.
Definition: Organism.cpp:335
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 void Initialize()
Initializes this object.
virtual void RemoveNeuralModule(std::string strID)
Removes the neural module based on its ID.
Declares the key frame class.
virtual void Initialize()
Initializes this object.
Definition: Structure.cpp:301
virtual RobotInterface * AddRobotInterface(std::string strXml)
Creates and adds a robot interface control.
Definition: Organism.cpp:199
RigidBody * m_lpBody
The root rigid body object of this structure.
Definition: Structure.h:88
Declares the joint class.
Declares the organism class.
virtual void SaveKeyFrameSnapshot(byte *aryBytes, long &lIndex)
Saves a key frame snapshot.
Definition: Organism.cpp:318
virtual bool RemoveItem(const std::string &strItemType, const std::string &strID, bool bThrowError=true)
Removes a child item from this parent.
Definition: Structure.cpp:601
Declares a light object.
virtual void Enabled(bool bVal)
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:102
RobotInterface * m_lpRobot
Pointer to a robot interface node to allow the organism to be hooked to a robot.
Definition: Organism.h:41
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
virtual void SetSystemPointers(Simulator *lpSim, Structure *lpStructure, NeuralModule *lpModule, Node *lpNode, bool bVerify)
Sets the system pointers.
Declares a light manager object.
void AddNeuralModule(NeuralModule *lpModule)
Adds a neural module to the nervous system.
Declares the bounding box class.
virtual void StepSimulation()
Step the simulation for this object.
Declares the gain base class.
virtual void ResetSimulation()
Resets the simulation back to time 0.
Definition: Structure.cpp:249
virtual bool AddItem(const std::string &strItemType, const std::string &strXml, bool bThrowError=true, bool bDoNotInit=false)
Adds a new object to this parent.
Definition: Organism.cpp:232
A standard xml manipulation class.
Definition: StdXml.h:19
virtual long CalculateSnapshotByteSize()
Calculates the snapshot byte size.
Definition: Organism.cpp:315
Declares the node class.
virtual void StepPhysicsEngine()
Allows the organism and its parts to update at each time slice.
Definition: Organism.cpp:155
virtual std::string GetChildString(std::string strElementName)
Gets a string value from the element with the specified name.
Definition: StdXml.cpp:307
virtual void LoadKeyFrameSnapshot(byte *aryBytes, long &lIndex)
Loads a key frame snapshot.
bool m_bDead
Tells if the organism is dead or not.
Definition: Organism.h:38
virtual void StepPhysicsEngine()
Allows the organism and its parts to update at each time slice.
Definition: Structure.cpp:286
virtual long CalculateSnapshotByteSize()
Calculates the snapshot byte size.
virtual void Kill(bool bState=true)
Kills.
virtual void ResetSimulation()
Resets the simulation back to time 0.
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 LoadKeyFrameSnapshot(byte *aryBytes, long &lIndex)
Loads a key frame snapshot.
Definition: Organism.cpp:321
virtual void Kill(bool bState=true)
Kills.
Definition: Structure.cpp:308
ScriptProcessor * m_lpScript
Script processor for running python or other scripting systems related to this structure.
Definition: Structure.h:117
virtual ~Organism()
Destructor.
Definition: Organism.cpp:63
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: Structure.cpp:555
virtual bool OutOfElem()
Goes out of the element where the cursor is located.
Definition: StdXml.cpp:56
virtual void Initialize()
Initializes this object.
virtual bool IsDead()
Query if this object is dead.
Definition: Organism.cpp:92
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: Organism.cpp:170
Declares the sensor class.
virtual void ResetSimulation()
Resets the simulation back to time 0.
Declares the data chart manager class.
virtual void Initialize()
Initializes this object.
Definition: Organism.cpp:108
Declares the rigid body class.
virtual bool IntoChildElement(std::string strElementName, bool bThrowError=true)
Goes into the child element with the specified name.
Definition: StdXml.cpp:278
std::string Std_CheckString(std::string strVal)
Converts a string to upper case and trims it.
Declares the structure class.
virtual void RemoveRobotInterface(std::string strID, bool bThrowError=true)
Removes the robot interface with the specified ID.
Definition: Organism.cpp:223
Declares the odor type class.
virtual void Kill(bool bState=true)
Kills.
Definition: Organism.cpp:95
Declares the odor class.
Declares the simulator class.
Declares the neural module class.
Declares the activated item manager class.
Declares the contact sensor class.
Declares the external stimuli manager class.
Declares the attachment class.
virtual bool RemoveItem(const std::string &strItemType, const std::string &strID, bool bThrowError=true)
Removes a child item from this parent.
Definition: Organism.cpp:266
Declares the receptive field class.
virtual void Kill(bool bState=true)
Kills.
Definition: RigidBody.cpp:1461
virtual void StepSimulation()
Step the simulation for this object.
The Robotics interface configures a simulation to run on a microcontroller board. ...