AnimatLab  2
Test
RobotPartInterface.cpp
1 #include "StdAfx.h"
2 #include "IMovableItemCallback.h"
3 #include "ISimGUICallback.h"
4 #include "IMotorizedJoint.h"
5 #include "AnimatBase.h"
6 
7 #include "Node.h"
8 #include "Link.h"
9 #include "IPhysicsMovableItem.h"
10 #include "IPhysicsBody.h"
11 #include "BoundingBox.h"
12 #include "MovableItem.h"
13 #include "BodyPart.h"
14 #include "Joint.h"
15 #include "ReceptiveField.h"
16 #include "ContactSensor.h"
17 #include "RigidBody.h"
18 #include "Structure.h"
19 #include "NeuralModule.h"
20 #include "Adapter.h"
21 #include "NervousSystem.h"
22 #include "Organism.h"
23 #include "ActivatedItem.h"
24 #include "ActivatedItemMgr.h"
25 #include "DataChartMgr.h"
26 #include "ExternalStimuliMgr.h"
27 #include "KeyFrame.h"
28 #include "SimulationRecorder.h"
29 #include "OdorType.h"
30 #include "Odor.h"
31 #include "Light.h"
32 #include "LightManager.h"
33 #include "Simulator.h"
34 #include "Gain.h"
35 
36 #include "RobotInterface.h"
37 #include "RobotIOControl.h"
38 #include "RobotPartInterface.h"
39 
40 
41 namespace AnimatSim
42 {
43  namespace Robotics
44  {
45 
46 RobotPartInterface::RobotPartInterface(void)
47 {
48  m_lpParentInterface = NULL;
49  m_lpParentIOControl = NULL;
50  m_lpPart = NULL;
51  m_lpProperty = NULL;
52  m_iIOComponentID = 0;
53  m_fltIOValue = 0;
54  m_iIOValue = 0;
56  m_lpGain = NULL;
57  m_bChanged= false;
58  m_fltStepIODuration = 0;
59 }
60 
61 RobotPartInterface::~RobotPartInterface(void)
62 {
63 
64 try
65 {
66  if(m_lpPart)
67  m_lpPart->RemoveRobotPartInterface(this);
68 
69  //We do not own any of these.
70  m_lpParentInterface = NULL;
71  m_lpParentIOControl = NULL;
72  m_lpPart = NULL;
73  m_lpProperty = NULL;
74  if(m_lpGain) delete m_lpGain;
75 }
76 catch(...)
77 {Std_TraceMsg(0, "Caught Error in desctructor of RobotPartInterface\r\n", "", -1, false, true);}
78 }
79 
80 void RobotPartInterface::ParentIOControl(RobotIOControl *lpParent)
81 {
82  m_lpParentIOControl = lpParent;
83 
84  if(m_lpParentIOControl)
85  m_lpParentInterface = m_lpParentIOControl->ParentInterface();
86 }
87 
88 RobotIOControl *RobotPartInterface::ParentIOControl() {return m_lpParentIOControl;}
89 
90 int RobotPartInterface::IOComponentID() {return m_iIOComponentID;}
91 
92 void RobotPartInterface::IOComponentID(int iID) {m_iIOComponentID = iID;}
93 
94 float RobotPartInterface::IOValue() {return m_fltIOValue;}
95 
96 void RobotPartInterface::IOValue(float fltVal)
97 {
98  m_fltIOValue = fltVal;
99  m_iIOValue = (int) fltVal;
100 }
101 
102 int RobotPartInterface::IOValueInt() {return m_iIOValue;}
103 
104 void RobotPartInterface::IOValueInt(int iVal)
105 {
106  m_iIOValue = iVal;
107  m_fltIOValue = (float) iVal;
108 }
109 
110 float RobotPartInterface::IOScaledValue() {return m_fltIOScaledValue;}
111 
112 void RobotPartInterface::IOScaledValue(float fltVal) {m_fltIOScaledValue = fltVal;}
113 
114 bool RobotPartInterface::Changed() {return m_bChanged;}
115 
116 void RobotPartInterface::Changed(bool bVal) {m_bChanged = bVal;}
117 
118 void RobotPartInterface::LinkedPartID(std::string strID)
119 {
120  m_strPartID = strID;
121  m_strPropertyName = "";
122  Initialize();
123 }
124 
125 std::string RobotPartInterface::LinkedPartID() {return m_strPartID;}
126 
127 void RobotPartInterface::PropertyName(std::string strName)
128 {
129  m_strPropertyName = strName;
130  Initialize();
131 }
132 
133 std::string RobotPartInterface::PropertyName() {return m_strPropertyName;}
134 
144 
158 {
159  return fltPos;
160 }
161 
175 {
176  return fltVel;
177 }
178 
188 
189 void RobotPartInterface::SetGain(Gain *lpGain)
190 {
191  if(m_lpGain)
192  {
193  delete m_lpGain;
194  m_lpGain = NULL;
195  }
196 
197  m_lpGain = lpGain;
198  m_lpGain->SetSystemPointers(m_lpSim, m_lpStructure, NULL, NULL, true);
199 }
200 
209 float RobotPartInterface::StepIODuration() {return m_fltStepIODuration;}
210 
219 void RobotPartInterface::AddGain(std::string strXml)
220 {
221  CStdXml oXml;
222  oXml.Deserialize(strXml);
223  oXml.FindElement("Root");
224  oXml.FindChildElement("Gain");
225 
226  SetGain(AnimatSim::Gains::LoadGain(m_lpSim, "Gain", oXml));
227 }
228 
229 #pragma region DataAccesMethods
230 
231 float *RobotPartInterface::GetDataPointer(const std::string &strDataType)
232 {
233  std::string strType = Std_CheckString(strDataType);
234 
235  if(strType == "IOVALUE")
236  return &m_fltIOValue;
237  else if(strType == "IOSCALEDVALUE")
238  return &m_fltIOScaledValue;
239  else if(strType == "STEPIODURATION")
240  return &m_fltStepIODuration;
241  else
242  THROW_TEXT_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType, "Robot Interface ID: " + STR(m_strName) + " DataType: " + strDataType);
243 
244  return NULL;
245 }
246 
247 bool RobotPartInterface::SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError)
248 {
249  std::string strType = Std_CheckString(strDataType);
250 
251  if(strType == "IOCOMPONENTID")
252  {
253  IOComponentID((int) atoi(strValue.c_str()));
254  return true;
255  }
256 
257  if(strType == "LINKEDPARTID")
258  {
259  LinkedPartID(strValue);
260  return true;
261  }
262 
263  if(strType == "PROPERTYNAME")
264  {
265  PropertyName(strValue);
266  return true;
267  }
268 
269  if(strType == "GAIN")
270  {
271  AddGain(strValue);
272  return true;
273  }
274 
275  if(strType == "ENABLED")
276  {
277  Enabled(Std_ToBool(strValue));
278  return true;
279  }
280 
281  if(AnimatBase::SetData(strDataType, strValue, false))
282  return true;
283 
284  //If it was not one of those above then we have a problem.
285  if(bThrowError)
286  THROW_PARAM_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType, "Data Type", strDataType);
287 
288  return false;
289 }
290 
291 void RobotPartInterface::QueryProperties(CStdPtrArray<TypeProperty> &aryProperties)
292 {
293  AnimatBase::QueryProperties(aryProperties);
294 
295  aryProperties.Add(new TypeProperty("IOValue", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
296  aryProperties.Add(new TypeProperty("IOScaledValue", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
297  aryProperties.Add(new TypeProperty("StepIODuration", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
298 
299  aryProperties.Add(new TypeProperty("IOComponentID", AnimatPropertyType::Integer, AnimatPropertyDirection::Set));
300  aryProperties.Add(new TypeProperty("Gain", AnimatPropertyType::Xml, AnimatPropertyDirection::Set));
301  aryProperties.Add(new TypeProperty("PropertyName", AnimatPropertyType::String, AnimatPropertyDirection::Set));
302  aryProperties.Add(new TypeProperty("LinkedPartID", AnimatPropertyType::String, AnimatPropertyDirection::Set));
303 }
304 
305 #pragma endregion
306 
319 {
320 }
321 
331 void RobotPartInterface::StepIO(int iPartIdx)
332 {
333 }
334 
344 {
345 }
346 
348 {
349  //We need to find the referenced body part and set its robot part interface to this one.
350  if(!Std_IsBlank(m_strPartID))
351  m_lpPart = dynamic_cast<BodyPart *>(m_lpSim->FindByID(m_strPartID));
352  else
353  {
354  m_lpPart = NULL;
355  m_lpProperty = NULL;
356  }
357 
358  if(m_lpPart)
359  {
360  if(!Std_IsBlank(m_strPropertyName))
361  m_lpProperty = m_lpPart->GetDataPointer(m_strPropertyName);
362  else
363  m_lpProperty = NULL;
364 
365  m_lpPart->AddRobotPartInterface(this);
366  }
367  else
368  m_lpProperty = NULL;
369 }
370 
372 {
374 
375  m_fltIOValue = 0;
376  m_iIOValue = 0;
377  m_fltIOScaledValue = 0;
378  m_bChanged= false;
379 }
380 
381 void RobotPartInterface::Load(CStdXml &oXml)
382 {
383  AnimatBase::Load(oXml);
384 
385  oXml.IntoElem(); //Into RigidBody Element
386 
387  LinkedPartID(oXml.GetChildString("LinkedPartID", ""));
388  PropertyName(oXml.GetChildString("PropertyName", ""));
389  IOComponentID(oXml.GetChildInt("IOComponentID", m_iIOComponentID));
390 
391  SetGain(AnimatSim::Gains::LoadGain(m_lpSim, "Gain", oXml));
392 
393  oXml.OutOfElem(); //OutOf RigidBody Element
394 }
395 
396  }
397 }
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
virtual float StepIODuration()
Gets the time duration required to perform one step of the IO for this part.
Base class file for all Animat simulation objects.
Declares the nervous system class.
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
Root namespace for the base simulation library for AnimatLab.
virtual void QueryProperties(CStdPtrArray< TypeProperty > &aryProperties)
Queries this object for a list of properties that can be changed using SetData.
Declares the body part class.
virtual float QuantizeServoVelocity(float fltVel)
If this is a servo controller interface then it will take a continuous velocity and return back a val...
virtual float * GetDataPointer(const std::string &strDataType)
Returns a float pointer to a data item of interest in this object.
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
virtual bool IsMotorControl()
Returns true if this part interface is for controlling a motor.
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 ParentInterface(RobotInterface *lpParent)
Sets the parent robot interface pointer.
Class that stores information about types for QueryProperty information.
Definition: TypeProperty.h:35
The Gain base class.
Definition: Gain.h:35
Declares the key frame class.
Declares the joint class.
Declares the organism class.
virtual int GetChildInt(std::string strElementName)
Gets an integer value from the element with the specified name.
Definition: StdXml.cpp:456
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
Declares a light object.
Declares the activated item class.
virtual void AddGain(std::string strXml)
Creates and adds a gain object.
Declares a light manager object.
virtual void SetupIO()
This method is called after all connections to whatever control board have been made. It is meant to be used to setup connection information specific to this part. For example, We connect to a Firmata microcontroller like an Arduino, and then do a setup that could take some time. We should not attempt to setup any of the pins until after the board itself has been setup. After that we need to loop through and setup all the parts. That is what this method is for.
Declares the bounding box class.
bool m_bChanged
True when the value changes.
Declares the gain base 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
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
Base class for all body parts and joints.
Definition: BodyPart.h:25
virtual std::string GetChildString(std::string strElementName)
Gets a string value from the element with the specified name.
Definition: StdXml.cpp:307
virtual float QuantizeServoPosition(float fltPos)
If this is a servo controller interface then it will take a continuous positon and return back a vali...
virtual AnimatBase * FindByID(std::string strID, bool bThrowError=true)
Searches for the object with the specified ID.
Definition: Simulator.cpp:4008
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 void ResetSimulation()
Resets the simulation back to time 0.
virtual void StepIO(int iPartIdx)
This method is used to send/recieve the actual IO. This will often be in a seperate thread than the S...
virtual bool OutOfElem()
Goes out of the element where the cursor is located.
Definition: StdXml.cpp:56
virtual bool SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError=true)
Set a variable based on a string data type name.
bool Std_IsBlank(std::string strVal)
Trims a string and tests if a string is blank.
Declares the data chart manager class.
virtual Gain * GetGain()
Gets the poitner to the gain function.
Declares the rigid body class.
std::string Std_CheckString(std::string strVal)
Converts a string to upper case and trims it.
Gain * m_lpGain
Pointer to the Gain that will be used to convert the source value into the target value...
Declares the structure class.
Declares the odor type class.
Declares the robotics inerface for animatlab.
int m_iIOValue
This stores the temporary IO value to be used later in IO calculations.
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.
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
virtual void ShutdownIO()
This method is called just before the IO thread is closed down. It gives the IO objects a chance to d...
Gain * LoadGain(Simulator *lpSim, std::string strName, CStdXml &oXml)
Loads a gain object.
Definition: Gain.cpp:287
Declares the receptive field class.
std::string m_strName
The name for this object.
Definition: AnimatBase.h:61