AnimatLab  2
Test
PropertyControlStimulus.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 "Node.h"
13 #include "Link.h"
14 #include "IPhysicsMovableItem.h"
15 #include "IPhysicsBody.h"
16 #include "BoundingBox.h"
17 #include "MovableItem.h"
18 #include "BodyPart.h"
19 #include "Gain.h"
20 #include "Adapter.h"
21 #include "Joint.h"
22 #include "ReceptiveField.h"
23 #include "ContactSensor.h"
24 #include "RigidBody.h"
25 #include "Structure.h"
26 #include "NeuralModule.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 "ExternalStimulus.h"
34 #include "ExternalInputStimulus.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 
44 namespace AnimatSim
45 {
46  namespace ExternalStimuli
47  {
55 {
56  m_lpEval = NULL;
57  m_lpTargetObject = NULL;
58  m_fltSetThreshold = 0.5;
59  m_fltPreviousSetVal = 0;
60  m_fltInitialValue = 0;
61  m_fltFinalValue = 1;
62 }
63 
71 {
72 
73 try
74 {
75  m_ePropertyType = AnimatPropertyType::Invalid;
76  m_lpTargetObject = NULL;
77  if(m_lpEval) delete m_lpEval;
78 }
79 catch(...)
80 {Std_TraceMsg(0, "Caught Error in desctructor of PropertyControlStimulus\r\n", "", -1, false, true);}
81 }
82 
83 std::string PropertyControlStimulus::Type() {return "PropertyControlStimulus";}
84 
94 
103 void PropertyControlStimulus::TargetID(std::string strID)
104 {
105  if(Std_IsBlank(strID))
106  THROW_ERROR(Al_Err_lBodyIDBlank, Al_Err_strBodyIDBlank);
107  m_strTargetID = strID;
108 
109  //Reset the property name when switching objects.
110  m_ePropertyType = AnimatPropertyType::Invalid;
111  m_strPropertyName = "";
112 
113  //If we have already been initialized once then we need to re-call this.
114  if(m_bInitialized)
115  Initialize();
116 }
117 
126 void PropertyControlStimulus::Equation(std::string strVal)
127 {
128  //Initialize the postfix evaluator.
129  if(m_lpEval)
130  {delete m_lpEval; m_lpEval = NULL;}
131 
132  m_strEquation = strVal;
133  m_lpEval = new CStdPostFixEval;
134 
135  m_lpEval->AddVariable("t");
136  m_lpEval->Equation(m_strEquation);
137 }
138 
139 
148 void PropertyControlStimulus::PropertyName(std::string strPropName)
149 {
150  //Reset the property name so we can get the property type setup correctly.
151  //If it is not set then we need to assume that they will set it later.
152  //Make sure the property type is set to invalid so the step sim method knows this.
153  if(m_lpTargetObject && !Std_IsBlank(strPropName))
154  {
155  if(Std_Trim(strPropName).length() == 0)
156  THROW_PARAM_ERROR(Al_Err_lPropertyNameBlank, Al_Err_strPropertyNameBlank, "Adapter ID", m_strID);
157 
158  if(!m_lpTargetObject->HasProperty(strPropName))
159  THROW_PARAM_ERROR(Al_Err_lTargetDoesNotHaveProperty, Al_Err_strTargetDoesNotHaveProperty, "Property name", strPropName);
160 
161  AnimatPropertyType ePropertyType = m_lpTargetObject->PropertyType(strPropName);
162  if(!(ePropertyType != AnimatPropertyType::Boolean || ePropertyType != AnimatPropertyType::Integer || ePropertyType != AnimatPropertyType::Float))
163  THROW_PARAM_ERROR(Al_Err_lTargetInvalidPropertyType, Al_Err_strTargetInvalidPropertyType, "Property name", strPropName);
164 
165  m_ePropertyType = ePropertyType;
166  }
167  else
168  m_ePropertyType = AnimatPropertyType::Invalid;
169 
170  m_strPropertyName = strPropName;
171 }
172 
182 {return m_strPropertyName;}
183 
196 {
197  if(fltThreshold < 0)
198  THROW_PARAM_ERROR(Al_Err_lInvalidSetThreshold, Al_Err_strInvalidSetThreshold, "Threshold", fltThreshold);
199 
200  m_fltSetThreshold = fltThreshold;
201 }
202 
212 {return m_fltSetThreshold;}
213 
223 {
224  m_fltInitialValue = fltVal;
225  m_fltPreviousSetVal = fltVal;
226 }
227 
237 {return m_fltInitialValue;}
238 
248 {
249  m_fltFinalValue = fltVal;
250 }
251 
261 {return m_fltFinalValue;}
262 
272 
274 {
276 
278  if(!m_lpTargetObject)
279  THROW_PARAM_ERROR(Al_Err_lNodeNotFound, Al_Err_strNodeNotFound, "ID: ", m_strTargetID);
280 
281  PropertyName(m_strPropertyName);
282 
283  m_fltPreviousSetVal = m_fltInitialValue;
284 }
285 
287 {
289 
290  if(m_bEnabled)
291  {
292  m_fltPreviousSetVal = m_fltInitialValue;
293  if(m_ePropertyType != AnimatPropertyType::Invalid)
294  m_lpTargetObject->SetData(m_strPropertyName, STR(m_fltPreviousSetVal));
295  }
296 }
297 
298 void PropertyControlStimulus::SetPropertyValue(float fltVal)
299 {
300  //If they have not set the property name yet then we cannot set the property value
301  if(m_ePropertyType != AnimatPropertyType::Invalid)
302  {
303  float fltDiff = fltVal - m_fltPreviousSetVal;
304  if(fabs(fltDiff) > m_fltSetThreshold)
305  {
306  m_fltPreviousSetVal = fltVal;
307 
308  if(m_ePropertyType == AnimatPropertyType::Boolean)
309  {
310  if(fltDiff > 0)
311  m_lpTargetObject->SetData(m_strPropertyName, "1");
312  else
313  m_lpTargetObject->SetData(m_strPropertyName, "0");
314  }
315  else if(m_ePropertyType == AnimatPropertyType::Integer)
316  {
317  int iVal = (int) fltVal;
318  m_lpTargetObject->SetData(m_strPropertyName, STR(iVal));
319  }
320  else
321  m_lpTargetObject->SetData(m_strPropertyName, STR(fltVal));
322  }
323  }
324 }
325 
327 {
328 
329  try
330  {
331  if(m_bEnabled)
332  {
333  //IMPORTANT! This stimulus applies a stimulus to the physics engine, so it should ONLY be called once for every time the physcis
334  //engine steps. If you do not do this then the you will accumulate forces being applied during the neural steps, and the total
335  //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
336  //the physics step count is equal to the step interval.
338  {
339  m_lpEval->SetVariable("t", (m_lpSim->Time()-m_fltStartTime) );
340 
341  float fltInput = m_lpEval->Solve();
342 
343  SetPropertyValue(fltInput);
344  }
345  }
346  }
347  catch(...)
348  {
349  LOG_ERROR("Error Occurred while setting Joint Velocity");
350  }
351 }
352 
354 {
356 
357  if(m_bEnabled)
358  {
359  m_fltPreviousSetVal = m_fltInitialValue;
360  if(m_ePropertyType != AnimatPropertyType::Invalid)
361  m_lpTargetObject->SetData(m_strPropertyName, STR(m_fltFinalValue));
362  }
363 }
364 
365 bool PropertyControlStimulus::SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError)
366 {
367  std::string strType = Std_CheckString(strDataType);
368 
369  if(ExternalStimulus::SetData(strDataType, strValue, false))
370  return true;
371 
372  if(strType == "TARGETID")
373  {
374  TargetID(strValue);
375  return true;
376  }
377 
378  if(strType == "PROPERTYNAME")
379  {
380  PropertyName(strValue);
381  return true;
382  }
383 
384  if(strType == "SETTHRESHOLD")
385  {
386  SetThreshold(atof(strValue.c_str()));
387  return true;
388  }
389 
390  if(strType == "INITIALVALUE")
391  {
392  InitialValue(atof(strValue.c_str()));
393  return true;
394  }
395 
396  if(strType == "FINALVALUE")
397  {
398  FinalValue(atof(strValue.c_str()));
399  return true;
400  }
401 
402  if(strType == "EQUATION")
403  {
404  Equation(strValue);
405  return true;
406  }
407 
408  //If it was not one of those above then we have a problem.
409  if(bThrowError)
410  THROW_PARAM_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType, "Data Type", strDataType);
411 
412  return false;
413 }
414 
415 void PropertyControlStimulus::QueryProperties(CStdPtrArray<TypeProperty> &aryProperties)
416 {
417  ExternalStimulus::QueryProperties(aryProperties);
418 
419  aryProperties.Add(new TypeProperty("TargetID", AnimatPropertyType::String, AnimatPropertyDirection::Set));
420  aryProperties.Add(new TypeProperty("PropertyName", AnimatPropertyType::String, AnimatPropertyDirection::Set));
421  aryProperties.Add(new TypeProperty("SetThreshold", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
422  aryProperties.Add(new TypeProperty("InitialValue", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
423  aryProperties.Add(new TypeProperty("FinalValue", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
424  aryProperties.Add(new TypeProperty("Equation", AnimatPropertyType::String, AnimatPropertyDirection::Set));
425 }
426 
427 void PropertyControlStimulus::Load(CStdXml &oXml)
428 {
429  ActivatedItem::Load(oXml);
430 
431  oXml.IntoElem(); //Into Simulus Element
432 
433  TargetID(oXml.GetChildString("TargetID"));
434  PropertyName(oXml.GetChildString("PropertyName", m_strPropertyName));
435  SetThreshold(oXml.GetChildFloat("SetThreshold", m_fltSetThreshold));
436  InitialValue(oXml.GetChildFloat("InitialValue", m_fltInitialValue));
437  FinalValue(oXml.GetChildFloat("FinalValue", m_fltFinalValue));
438  Equation(oXml.GetChildString("Equation", m_strEquation));
439 
440  oXml.OutOfElem(); //OutOf Simulus Element
441 }
442 
443  } //ExternalStimuli
444 } //VortexAnimatSim
445 
446 
447 
448 
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 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.
Root namespace for the base simulation library for AnimatLab.
Declares the body part class.
Simulator * m_lpSim
The pointer to a Simulation.
Definition: AnimatBase.h:43
virtual void QueryProperties(CStdPtrArray< TypeProperty > &aryProperties)
Queries this object for a list of properties that can be changed using SetData.
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
virtual bool HasProperty(const std::string &strName)
Queries this object if it has a property with the given name.
Definition: AnimatBase.cpp:464
virtual std::string TargetID()
Gets the GUID ID of the target node that will be enabled.
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.
Declares the key frame class.
Declares the joint class.
Declares the organism class.
std::string m_strID
The unique Id for this object.
Definition: AnimatBase.h:55
std::string Std_Trim(std::string strVal)
Trims a string.
std::string m_strTargetID
GUID ID of the target node to enable.
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 float FinalValue()
Gets the final value used to set this property when the simulation ends.
Declares a stimulus class that can set any property on any object in the system.
Declares the bounding box class.
virtual std::string Type()
returns the string type name of this object.
Declares the gain base class.
void Equation(std::string strVal)
Sets the post-fix Equation.
A standard xml manipulation class.
Definition: StdXml.h:19
virtual float InitialValue()
Gets vthe initial value used to set this property when the simulation starts.
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
virtual AnimatBase * FindByID(std::string strID, bool bThrowError=true)
Searches for the object with the specified ID.
Definition: Simulator.cpp:4008
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.
Animat base class.
Definition: AnimatBase.h:36
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.
bool m_bInitialized
true if item has been initialized
Definition: ActivatedItem.h:62
virtual bool OutOfElem()
Goes out of the element where the cursor is located.
Definition: StdXml.cpp:56
virtual void Deactivate()
Deactivates this item.
virtual std::string PropertyName()
Gets the name of the property that this adapter will be setting.
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.
Declares the structure class.
AnimatBase * m_lpTargetObject
Pointer to the target node.
Declares the odor type class.
virtual void Initialize()
Initializes this object.
virtual AnimatBase * TargetObject()
Gets the target object.
virtual float SetThreshold()
Gets the threshold value used for determining when to set the value on the target object...
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.
Declares the contact sensor class.
Declares the external stimuli manager class.
Declares the external input stimulus 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 float GetChildFloat(std::string strElementName)
Gets a float value from the element with the specified name.
Definition: StdXml.cpp:617
virtual bool SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError=true)
Set a variable based on a string data type name.