AnimatLab  2
Test
RemoteControlLinkage.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 "Gain.h"
12 #include "BoundingBox.h"
13 #include "MovableItem.h"
14 #include "BodyPart.h"
15 #include "Joint.h"
16 #include "ReceptiveField.h"
17 #include "ContactSensor.h"
18 #include "RigidBody.h"
19 #include "Structure.h"
20 #include "NeuralModule.h"
21 #include "Adapter.h"
22 #include "NervousSystem.h"
23 #include "Organism.h"
24 #include "ActivatedItem.h"
25 #include "ActivatedItemMgr.h"
26 #include "DataChartMgr.h"
27 #include "ExternalStimuliMgr.h"
28 #include "KeyFrame.h"
29 #include "SimulationRecorder.h"
30 #include "OdorType.h"
31 #include "Odor.h"
32 #include "Light.h"
33 #include "LightManager.h"
34 #include "Simulator.h"
35 
36 #include "RobotInterface.h"
37 #include "RobotIOControl.h"
38 #include "RemoteControlLinkage.h"
39 #include "RemoteControl.h"
40 
41 namespace AnimatSim
42 {
43  namespace Robotics
44  {
45 
46 
47 void RemoteControlData::ClearStartStops()
48 {
49  if((fabs(m_fltStart) > 0 || fabs(m_fltStop) > 0))
50  {
52  {
54  //if(m_fltStart > 0)
55  // OutputDebugString("Cleared Start\r\n");
56  //if(m_fltStop > 0)
57  // OutputDebugString("Cleared Stop\r\n");
58 
59  m_fltStart = 0;
60  m_fltStop = 0;
61  m_iSimStepped = 0;
62  }
63  else
64  m_iSimStepped++;
65  }
66 }
67 
68 void RemoteControlData::CheckStartedStopped()
69 {
70  if(m_fltValue == m_fltPrev)
71  m_iCount++;
72  else
73  m_iCount = 0;
74 
75  if(m_iCount == 3)
76  {
77  if(!m_bStarted && m_fltValue != 0)
78  {
79  m_iStartDir = Std_Sign(m_fltValue);
80  m_fltStart = 1*m_iStartDir;
81  m_bStarted = true;
83  //OutputDebugString("Start\r\n");
84  }
85  else if(m_bStarted && m_fltValue == 0)
86  {
87  m_fltStop = m_iStartDir;
88  m_bStarted = false;
90  //OutputDebugString("Stop\r\n");
91  }
92 
93  m_iCount = 0;
94  }
95 
96  m_fltPrev = m_fltValue;
97 
99  //if(m_fltValue > 0)
100  //{
101  //std::string strVal = "Val: " + STR(m_fltValue) + " Prev: " + STR(m_fltPrev) + " Count: " + STR(m_iCount) + " Started: " + STR(m_bStarted) + " Start: " + STR((int) m_fltStart) + " Stop: " + STR((int) m_fltStop) + "\r\n";
102  //OutputDebugString(strVal.c_str());
103  //}
104 }
105 
106 RemoteControlLinkage::RemoteControlLinkage(void)
107 {
109  m_lpSource = NULL;
110  m_lpTarget = NULL;
111  m_lpSourceData = NULL;
112  m_lpTargetData = NULL;
113  m_iTargetDataType = -1;
114  m_fltAppliedValue = 0;
115  m_bInLink = true;
116  m_iPropertyID = -1;
117 }
118 
119 RemoteControlLinkage::~RemoteControlLinkage(void)
120 {
121 try
122 {
123  m_lpSource = NULL;
124  m_lpTarget = NULL;
125  m_lpSourceData = NULL;
126  m_lpTargetData = NULL;
127 }
128 catch(...)
129 {Std_TraceMsg(0, "Caught Error in desctructor of RemoteControlLinkage\r\n", "", -1, false, true);}
130 }
131 
132 void RemoteControlLinkage::ParentRemoteControl(RemoteControl *lpParent) {m_lpParentRemoteControl = lpParent;}
133 
134 RemoteControl *RemoteControlLinkage::ParentRemoteControl() {return m_lpParentRemoteControl;}
135 
136 
146 
155 void RemoteControlLinkage::SourceID(std::string strID)
156 {
157  m_strSourceID = strID;
158  Initialize();
159 }
160 
170 
179 void RemoteControlLinkage::TargetID(std::string strID)
180 {
181  m_strTargetID = strID;
182  Initialize();
183 }
184 
194 
203 void RemoteControlLinkage::SourceDataTypeID(std::string strTypeID)
204 {
205  m_strSourceDataTypeID = strTypeID;
206  Initialize();
207 }
208 
218 
227 void RemoteControlLinkage::TargetDataTypeID(std::string strTypeID)
228 {
229  m_strTargetDataTypeID = strTypeID;
230  Initialize();
231 }
232 
233 int RemoteControlLinkage::PropertyID() {return m_iPropertyID;}
234 
235 void RemoteControlLinkage::PropertyID(int iID, bool bCreateDataTypes)
236 {
237  if(m_lpParentRemoteControl && !m_lpParentRemoteControl->UseRemoteDataTypes())
238  {
239  m_iPropertyID = iID;
240  m_Data.m_iButtonID = iID;
241  }
242 }
243 
253 
262 void RemoteControlLinkage::PropertyName(std::string strTypeID)
263 {
264  m_strPropertyName = strTypeID;
265 
266  std::string strProp = Std_CheckString(m_strPropertyName);
267  int iLen = strProp.length();
268  if(((iLen-5) >= 0) && strProp.substr(strProp.length() - 5, 5) == "START")
269  m_Data.m_strProperty = m_strPropertyName.substr(0, m_strPropertyName.length() - 5);
270  else if(((iLen-4) >= 0) && strProp.substr(strProp.length() - 4, 4) == "STOP")
271  m_Data.m_strProperty = m_strPropertyName.substr(0, m_strPropertyName.length() - 4);
272  else
274 
276  {
277  if(m_lpParentRemoteControl->DataIDMap()->find(m_Data.m_strProperty) != m_lpParentRemoteControl->DataIDMap()->end())
278  m_Data.m_iButtonID = (int) m_lpParentRemoteControl->DataIDMap()->find(m_Data.m_strProperty)->second;
279  }
280 }
281 
291 
301 {
302  m_bInLink = bVal;
303 }
304 
305 float RemoteControlLinkage::AppliedValue() {return m_fltAppliedValue;}
306 
307 void RemoteControlLinkage::AppliedValue(float fltVal) {m_fltAppliedValue = fltVal;}
308 
309 #pragma region DataAccesMethods
310 
311 float *RemoteControlLinkage::GetDataPointer(const std::string &strDataType)
312 {
313  std::string strType = Std_CheckString(strDataType);
314 
315  if(strType == "APPLIEDCURRENT" || strType == "APPLIEDVALUE")
316  return &m_fltAppliedValue;
317  else
318  THROW_TEXT_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType, "Robot Interface ID: " + STR(m_strName) + " DataType: " + strDataType);
319 
320  return NULL;
321 }
322 
323 bool RemoteControlLinkage::SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError)
324 {
325  std::string strType = Std_CheckString(strDataType);
326 
327  if(AnimatBase::SetData(strType, strValue, false))
328  return true;
329 
330  if(strType == "ENABLED")
331  {
332  Enabled(Std_ToBool(strValue));
333  return true;
334  }
335  else if(strType == "SOURCEDATATYPEID")
336  {
337  SourceDataTypeID(strValue);
338  return true;
339  }
340  else if(strType == "TARGETDATATYPEID")
341  {
342  TargetDataTypeID(strValue);
343  return true;
344  }
345  else if(strType == "SOURCEID")
346  {
347  SourceID(strValue);
348  return true;
349  }
350  else if(strType == "TARGETID")
351  {
352  TargetID(strValue);
353  return true;
354  }
355  else if(strType == "PROPERTYNAME")
356  {
357  PropertyName(strValue);
358  return true;
359  }
360  else if(strType == "PROPERTYID")
361  {
362  PropertyID(atoi(strValue.c_str()));
363  return true;
364  }
365 
366  //If it was not one of those above then we have a problem.
367  if(bThrowError)
368  THROW_PARAM_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType, "Data Type", strDataType);
369 
370  return false;
371 }
372 
373 void RemoteControlLinkage::QueryProperties(CStdPtrArray<TypeProperty> &aryProperties)
374 {
375  AnimatBase::QueryProperties(aryProperties);
376 
377  aryProperties.Add(new TypeProperty("Enabled", AnimatPropertyType::Boolean, AnimatPropertyDirection::Both));
378  aryProperties.Add(new TypeProperty("SourceDataTypeID", AnimatPropertyType::String, AnimatPropertyDirection::Set));
379  aryProperties.Add(new TypeProperty("TargetDataTypeID", AnimatPropertyType::String, AnimatPropertyDirection::Set));
380  aryProperties.Add(new TypeProperty("SourceID", AnimatPropertyType::String, AnimatPropertyDirection::Set));
381  aryProperties.Add(new TypeProperty("TargetID", AnimatPropertyType::String, AnimatPropertyDirection::Set));
382  aryProperties.Add(new TypeProperty("PropertyName", AnimatPropertyType::String, AnimatPropertyDirection::Set));
383  aryProperties.Add(new TypeProperty("PropertyID", AnimatPropertyType::Integer, AnimatPropertyDirection::Set));
384  aryProperties.Add(new TypeProperty("AppliedValue", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
385 }
386 
387 #pragma endregion
388 
389 
402 {
403 }
404 
413 {
414 }
415 
425 {
426 }
427 
429 {
431  {
432  m_lpSource = dynamic_cast<AnimatBase *>(m_lpSim->FindByID(m_strSourceID));
433  if(!m_lpSource)
434  THROW_PARAM_ERROR(Al_Err_lNodeNotFound, Al_Err_strNodeNotFound, "ID: ", m_strSourceID);
435 
437  (!m_bInLink || (m_bInLink && m_lpParentRemoteControl->FindLinkageWithPropertyName(m_strSourceDataTypeID, false))))
439  else
440  m_lpSourceData = NULL;
441  }
442  else
443  {
444  m_lpSource = NULL;
445  m_lpSourceData = NULL;
446  }
447 
449  {
450  m_lpTarget = dynamic_cast<AnimatBase *>(m_lpSim->FindByID(m_strTargetID));
451  if(!m_lpTarget)
452  THROW_PARAM_ERROR(Al_Err_lNodeNotFound, Al_Err_strNodeNotFound, "ID: ", m_strTargetID);
453 
455  (m_bInLink || (!m_bInLink && m_lpParentRemoteControl->FindLinkageWithPropertyName(m_strTargetDataTypeID, false))))
457  else
458  m_lpTargetData = NULL;
459  }
460  else
461  {
462  m_lpTarget = NULL;
463  m_lpTargetData = NULL;
464  }
465 }
466 
467 void RemoteControlLinkage::ApplyValue(float fltData)
468 {
470  {
472  //int i=5; //Std_ToLower(m_strID) == "079087db-7a2b-4e2b-82ab-cdd407ad3d85")
473  //if(GetSimulator()->Time() >= 0.2 && fabs(fltData) > 0)
474  // i=6;
475 
476  //std::string strVal = "WalkV: " + STR((float) *m_lpSourceData) + "\r\n";
477  //OutputDebugString(strVal.c_str());
478 
479  //Remove any previously applied current from this linkage.
481 
482  //Calculate the new current to apply.
483  m_fltAppliedValue = CalculateAppliedValue(fltData);
484 
485  //Add the new applied current
487  }
488 }
489 
491 {
492  m_fltAppliedValue = 0;
493 }
494 
496 {
498  {
499  if(m_bInLink)
500  ApplyValue(*m_lpSourceData);
501  else
502  m_Data.m_fltValue = *m_lpSourceData;
503  }
504 }
505 
506 void RemoteControlLinkage::Load(CStdXml &oXml)
507 {
508  AnimatBase::Load(oXml);
509 
510  oXml.IntoElem(); //Into Link Element
511 
512  InLink(oXml.GetChildBool("InLink", true));
513  SourceID(oXml.GetChildString("SourceID", ""));
514  TargetID(oXml.GetChildString("TargetID", ""));
515  SourceDataTypeID(oXml.GetChildString("SourceDataTypeID", ""));
516  TargetDataTypeID(oXml.GetChildString("TargetDataTypeID", ""));
517  PropertyName(oXml.GetChildString("PropertyName", ""));
518  PropertyID(oXml.GetChildInt("PropertyID", -1));
519 
520  oXml.OutOfElem(); //OutOf Link Element
521 }
522 
523 
524 
525  }
526 }
virtual void QueryProperties(CStdPtrArray< TypeProperty > &aryProperties)
Queries this object for a list of properties that can be changed using SetData.
std::string m_strSourceDataTypeID
ID of the source data type. This is only used during loading.
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.
Root namespace for the base simulation library for AnimatLab.
RemoteControlData m_Data
This is a set of data that is associated with this linkage.
Declares the body part class.
AnimatBase * m_lpSource
Source object we are inserting from.
virtual std::string TargetID()
Gets the target ID.
Simulator * m_lpSim
The pointer to a Simulation.
Definition: AnimatBase.h:43
float * m_lpTargetData
Pointer to the external value of the linked target.
std::string m_strProperty
The array index of this button.
virtual bool IntoElem()
Goes into the next element where the cursor is located.
Definition: StdXml.cpp:42
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 void StepSimulation()
Step the simulation for this object.
virtual float * GetDataPointer(const std::string &strDataType)
Returns a float pointer to a data item of interest in this object.
Definition: AnimatBase.cpp:340
Class that stores information about types for QueryProperty information.
Definition: TypeProperty.h:35
Declares the key frame class.
int Std_Sign(float fltVal)
Determines the sign of a number.
virtual void ResetSimulation()
Resets the simulation back to time 0.
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
virtual std::string SourceDataTypeID()
Gets the target data type.
Declares a light object.
int m_iTargetDataType
integer index of the target data type
Declares the activated item class.
Declares a light manager object.
AnimatBase * m_lpTarget
Target object we are inserting into.
Declares the bounding box class.
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 standard xml manipulation class.
Definition: StdXml.h:19
Declares the node class.
virtual std::string SourceID()
Gets the source ID.
std::string m_strPropertyName
Name of the property that will be used for the data type if required.
virtual std::string TargetDataTypeID()
Gets the target data type.
std::string m_strSourceID
ID of the source. This is only used during loading.
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
int m_iChangeSimStepCount
The number of simulation time slices to keep a start/stop signal active.
virtual void StepIO()
This method is called from within the IO thread. It calls StepIO for each part.
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 bool GetChildBool(std::string strElementName)
Gets a bool value from the element with the specified name.
Definition: StdXml.cpp:699
Animat base class.
Definition: AnimatBase.h:36
virtual float * GetDataPointer(const std::string &strDataType)
Returns a float pointer to a data item of interest in this object.
float m_fltAppliedValue
The total value applied during a time step.
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 bool OutOfElem()
Goes out of the element where the cursor is located.
Definition: StdXml.cpp:56
virtual void SetupIO()
This method is called after all connections to whatever control board have been made. It calls each parts SetupIO method. 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.
float * m_lpSourceData
Pointer to the source data variable.
virtual void Initialize()
Initializes this object.
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.
std::string m_strTargetDataTypeID
ID of the target data type. This is only used during loading.
Declares the structure class.
Declares the odor type class.
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 m_strTargetID
ID of the target. This is only used during loading.
Declares the robotics inerface for animatlab.
virtual std::string PropertyName()
Gets the property name.
RemoteControl * m_lpParentRemoteControl
Pointer tho the parent remote control.
Declares the odor class.
Declares the simulator class.
Declares the neural module class.
virtual bool InLink()
Gets the inlink value.
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.
std::string m_strName
The name for this object.
Definition: AnimatBase.h:61