AnimatLab  2
Test
CurrentStimulus.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"
35 #include "CurrentStimulus.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_lpNode = NULL;
57  m_lpExternalCurrent = NULL;
66  m_fltCurrentOn = (float) 10e-9;
67  m_fltCurrentOff = 0;
71  m_lCycleStart = 0;
72  m_lBurstStart = 0;
73  m_bCycleOn = true;
74  m_bBurstOn = true;
75  m_lpCurrentOnEval = NULL;
76  m_iType = AL_TONIC_CURRENT;
77 }
78 
86 {
87 
88 try
89 {
90  m_lpNode = NULL;
91  m_lpExternalCurrent = NULL;
93  delete m_lpCurrentOnEval;
94 
95 }
96 catch(...)
97 {Std_TraceMsg(0, "Caught Error in desctructor of CurrentStimulus\r\n", "", -1, false, true);}
98 }
99 
108 //int CurrentStimulus::Type() {return m_iType;}
109 
118 std::string CurrentStimulus::Type() {return "CurrentStimulus";}
119 
120 void CurrentStimulus::Type(std::string strType)
121 {
122  m_strType = Std_ToUpper(Std_Trim(strType));
123  if(m_strType == "TONIC")
124  m_iType = AL_TONIC_CURRENT;
125  else if(m_strType == "REPETITIVE")
126  m_iType = AL_REPETITIVE_CURRENT;
127  else if(m_strType == "BURST")
128  m_iType = AL_BURST_CURRENT;
129  else
130  THROW_PARAM_ERROR(Al_Err_lInvalidCurrentType, Al_Err_strInvalidCurrentType, "Type", strType);
131 }
132 
134 {
136 
137  //If we have always active then we need to reset some of the durations to make sure
138  //they last for a very long time.
139  if(m_bAlwaysActive)
140  {
141  if(m_iType == AL_TONIC_CURRENT)
142  {
143  m_fltCycleOnDuration = 10000;
144  m_fltBurstOnDuration = 10000;
145  }
146  else if(m_iType == AL_REPETITIVE_CURRENT)
147  m_fltBurstOnDuration = 10000;
148  }
149 }
159 
168 void CurrentStimulus::CurrentOn(float fltVal)
169 {m_fltCurrentOn = fltVal;}
170 
180 
189 void CurrentStimulus::CurrentOff(float fltVal) {m_fltCurrentOff = fltVal;}
190 
200 
210 
220 
230 {
231  Std_IsAboveMin((float) 0, fltVal, true, "CycleOnDuration", false);
232  m_fltCycleOnDuration = fltVal;
234 }
235 
245 
255 {
256  Std_IsAboveMin((float) 0, fltVal, true, "CycleOffDuration", true);
257  m_fltCycleOffDuration = fltVal;
259 }
260 
270 
280 {
281  Std_IsAboveMin((float) 0, fltVal, true, "BurstOnDuration", false);
282  m_fltBurstOnDuration = fltVal;
284 }
285 
295 
305 {
306  Std_IsAboveMin((float) 0, fltVal, true, "BurstOffDuration", true);
307  m_fltBurstOffDuration = fltVal;
309 }
310 
320 
329 void CurrentStimulus::TargetNodeID(std::string strID)
330 {
331  if(Std_IsBlank(strID))
332  THROW_PARAM_ERROR(Al_Err_lInvalidCurrentType, Al_Err_strInvalidCurrentType, "ID", strID);
333  m_strTargetNodeID = strID;
334 }
335 
346 
357 void CurrentStimulus::CurrentEquation(std::string strEquation)
358 {
359  m_strCurrentEquation = strEquation;
360 
361  if(!Std_IsBlank(strEquation))
362  {
363 
364  //Initialize the postfix evaluator.
365  if(m_lpCurrentOnEval)
366  {delete m_lpCurrentOnEval; m_lpCurrentOnEval = NULL;}
367 
369 
371  m_lpCurrentOnEval->Equation(strEquation);
374  }
375  else
376  {
377  //Remove the current eval.
378  if(m_lpCurrentOnEval)
379  {delete m_lpCurrentOnEval; m_lpCurrentOnEval = NULL;}
380 
382  }
383 
385 }
386 
387 
389 {
391 
392  //Lets try and get the node we will dealing with.
393  m_lpNode = dynamic_cast<Node *>(m_lpSim->FindByID(m_strTargetNodeID));
394  if(!m_lpNode)
395  THROW_PARAM_ERROR(Al_Err_lNodeNotFound, Al_Err_strNodeNotFound, "ID: ", m_strTargetNodeID);
396 
397  m_lpExternalCurrent = m_lpNode->GetDataPointer("ExternalCurrent");
398 
400  THROW_TEXT_ERROR(Al_Err_lDataPointNotFound, Al_Err_strDataPointNotFound,
401  ("Stimulus: " + m_strID + "Node: " + m_strTargetNodeID + " DataType: ExternalCurrent"));
402 }
403 
413 {
415  {
417  return 1e-9*m_lpCurrentOnEval->Solve();
418  }
419  else
420  return m_fltCurrentOn;
421 
422 }
423 
424 void CurrentStimulus::SetSliceData()
425 {
426  ActivatedItem::SetSliceData();
427 
432 }
433 
435 {
437 
438  //Start a cycle and a burst.
440 
443 
445 }
446 
448 {
449  long lCycleDiff = m_lpSim->TimeSlice() - m_lCycleStart;
450  long lBurstDiff = m_lpSim->TimeSlice() - m_lBurstStart;
451 
452  if(m_bBurstOn)
453  {
454  if( (m_bCycleOn && (lCycleDiff >= m_lCycleOnDuration)) )
455  {
456  m_bCycleOn = false;
458 
460  m_fltActiveCurrent = m_fltCurrentOff;
462  }
463  else if( (!m_bCycleOn && (lCycleDiff >= m_lCycleOffDuration)) )
464  {
465  m_bCycleOn = true;
467 
469  m_fltActiveCurrent = GetCurrentOn();
471  }
472  }
473 
474  if( (m_bBurstOn && (lBurstDiff >= m_lBurstOnDuration)) )
475  {
476  m_bCycleOn = false;
477  m_bBurstOn = false;
479 
481  m_fltActiveCurrent = m_fltCurrentBurstOff;
483  }
484  else if( (!m_bBurstOn && (lBurstDiff >= m_lBurstOffDuration)) )
485  {
486  m_bCycleOn = true;
487  m_bBurstOn = true;
490 
492  m_fltActiveCurrent = GetCurrentOn();
494  }
495  else if(m_iType == AL_TONIC_CURRENT && m_lpCurrentOnEval)
496  {
498  m_fltActiveCurrent = GetCurrentOn();
500  }
501 
502 }
503 
505 {
508 }
509 
511 {
513 
514  m_bCycleOn = true;
515  m_bBurstOn = true;
516  m_lCycleStart = 0;
517  m_lBurstStart = 0;
519 }
520 
521 float *CurrentStimulus::GetDataPointer(const std::string &strDataType)
522 {
523  float *lpData=NULL;
524  std::string strType = Std_CheckString(strDataType);
525 
526  if(strType == "ACTIVECURRENT")
527  lpData = &m_fltActiveCurrent;
528  else
529  THROW_TEXT_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType, "StimulusName: " + STR(m_strName) + " DataType: " + strDataType);
530 
531  return lpData;
532 }
533 
534 bool CurrentStimulus::SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError)
535 {
536  std::string strType = Std_CheckString(strDataType);
537 
538  if(ExternalStimulus::SetData(strDataType, strValue, false))
539  return true;
540 
541  if(strType == "CURRENTON")
542  {
543  CurrentOn((float) atof(strValue.c_str()));
544  return true;
545  }
546 
547  if(strType == "CURRENTOFF")
548  {
549  CurrentOff((float) atof(strValue.c_str()));
550  return true;
551  }
552 
553  if(strType == "CURRENTBURSTOFF")
554  {
555  CurrentBurstOff((float) atof(strValue.c_str()));
556  return true;
557  }
558 
559  if(strType == "CYCLEONDURATION")
560  {
561  CycleOnDuration((float) atof(strValue.c_str()));
562  return true;
563  }
564 
565  if(strType == "CYCLEOFFDURATION")
566  {
567  CycleOffDuration((float) atof(strValue.c_str()));
568  return true;
569  }
570 
571  if(strType == "BURSTONDURATION")
572  {
573  BurstOnDuration((float) atof(strValue.c_str()));
574  return true;
575  }
576 
577  if(strType == "BURSTOFFDURATION")
578  {
579  BurstOffDuration((float) atof(strValue.c_str()));
580  return true;
581  }
582 
583  if(strType == "EQUATION")
584  {
585  CurrentEquation(strValue);
586  return true;
587  }
588 
589  //If it was not one of those above then we have a problem.
590  if(bThrowError)
591  THROW_PARAM_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType, "Data Type", strDataType);
592 
593  return false;
594 }
595 
596 void CurrentStimulus::QueryProperties(CStdPtrArray<TypeProperty> &aryProperties)
597 {
598  ExternalStimulus::QueryProperties(aryProperties);
599 
600  aryProperties.Add(new TypeProperty("ActiveCurrent", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
601 
602  aryProperties.Add(new TypeProperty("CurrentOn", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
603  aryProperties.Add(new TypeProperty("CurrentOff", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
604  aryProperties.Add(new TypeProperty("CurrentBurstOff", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
605  aryProperties.Add(new TypeProperty("CycleOnDuration", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
606  aryProperties.Add(new TypeProperty("CycleOffDuration", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
607  aryProperties.Add(new TypeProperty("BurstOnDuration", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
608  aryProperties.Add(new TypeProperty("BurstOffDuration", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
609  aryProperties.Add(new TypeProperty("CurrentEquation", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
610 }
611 
612 void CurrentStimulus::Load(CStdXml &oXml)
613 {
614  ActivatedItem::Load(oXml);
615 
616  oXml.IntoElem(); //Into Simulus Element
617 
618  Type(oXml.GetChildString("CurrentType", "TONIC"));
619  TargetNodeID(oXml.GetChildString("TargetNodeID"));
620 
621  CycleOnDuration(oXml.GetChildFloat("CycleOnDuration", m_fltCycleOnDuration));
622  CycleOffDuration(oXml.GetChildFloat("CycleOffDuration", m_fltCycleOffDuration));
623  BurstOnDuration(oXml.GetChildFloat("BurstOnDuration", m_fltBurstOnDuration));
624  BurstOffDuration(oXml.GetChildFloat("BurstOffDuration", m_fltBurstOffDuration));
625 
626  CurrentOn(oXml.GetChildFloat("CurrentOn", m_fltCurrentOn));
627  CurrentOff(oXml.GetChildFloat("CurrentOff", m_fltCurrentOff));
628  CurrentBurstOff(oXml.GetChildFloat("CurrentBurstOff", m_fltCurrentBurstOff));
629 
630  CurrentEquation(oXml.GetChildString("CurrentOnEquation", ""));
631 
632  oXml.OutOfElem(); //OutOf Simulus Element
633 }
634 
635  } //ExternalStimuli
636 } //VortexAnimatSim
637 
638 
639 
640 
virtual float GetCurrentOn()
Calculates the on cycle current at this time step.
Declares the external stimulus base class.
float m_fltCurrentBurstOff
The current to apply when the burst is off.
Base class file for all Animat simulation objects.
Declares the nervous system class.
virtual std::string CurrentEquation()
Gets the post-fix current equation string. If this is null then an equation is not used...
void AddVariable(std::string strVarName)
Adds a variable.
virtual void Activate()
Activates this item.
Declares the simulation recorder class.
long m_lCycleOffDuration
Duration of the cycle off period in time slices.
virtual void Deactivate()
Deactivates this item.
Root namespace for the base simulation library for AnimatLab.
virtual float CurrentBurstOff()
Gets the off burst current value.
Declares the body part class.
float m_fltBurstOnDuration
Duration of the burst on period in time.
std::string m_strTargetNodeID
GUID ID of the neuron we are stimulating.
long m_lCycleOnDuration
Duration of the cycle on period in time slices.
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 Activate()
Activates this item.
bool m_bBurstOn
Tells whether a burst is on or not.
Simulator * m_lpSim
The pointer to a Simulation.
Definition: AnimatBase.h:43
virtual float CurrentOff()
Gets the ffn cycle current value.
virtual bool IntoElem()
Goes into the next element where the cursor is located.
Definition: StdXml.cpp:42
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
virtual void QueryProperties(CStdPtrArray< TypeProperty > &aryProperties)
Queries this object for a list of properties that can be changed using SetData.
virtual void QueryProperties(CStdPtrArray< TypeProperty > &aryProperties)
Queries this object for a list of properties that can be changed using SetData.
virtual void ResetSimulation()
Resets the simulation back to time 0.
Declares the key frame class.
virtual float BurstOnDuration()
Gets the on burst current duration in time.
float m_fltCycleOnDuration
Duration of the cycle on period in time.
Declares the joint class.
Declares the organism class.
virtual float CurrentOn()
Gets the on cycle current value.
virtual std::string Type()
Gets the integer value of current type. This is (Constant, repetitive, and bursting) ...
bool Std_IsAboveMin(int iMinVal, int iVal, bool bThrowError, std::string strParamName, bool bInclusiveLimit)
Tests if a number is above a minimum value.
std::string m_strID
The unique Id for this object.
Definition: AnimatBase.h:55
std::string m_strCurrentEquation
The post-fix string current equation.
std::string Std_Trim(std::string strVal)
Trims a string.
bool m_bCycleOn
Tells whether a cycle is on or not.
Declares a light object.
Declares the activated item class.
virtual float TimeStep()
Gets the smallest integration time step used within the simulation.
Definition: Simulator.cpp:951
Declares a light manager object.
virtual std::string TargetNodeID()
Gets the GUID ID of the node that is being stimulated.
Declares the bounding box class.
Declares the gain base class.
void Equation(std::string strVal)
Sets the post-fix Equation.
long m_lBurstStart
The time slice where the burst starts.
A standard xml manipulation class.
Definition: StdXml.h:19
Node * m_lpNode
The pointer to this items parent Node. If this is not relevant for this object then this is NULL...
Definition: AnimatBase.h:52
virtual void ResetSimulation()
Resets the simulation back to time 0.
float m_fltCurrentOff
The current to apply when the cycle is off.
virtual float * GetDataPointer(const std::string &strDataType)
Returns a float pointer to a data item of interest in this object.
Declares the node class.
float m_fltBurstOffDuration
Duration of the burst off period in time.
float m_fltInitialActiveCurrent
The initial active current that is used when reseting the simulation.
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.
Declares the current stimulus class.
double Solve()
Solves the equation using the defined variable values.
void SetVariable(std::string strVarName, double dblVal)
Sets the value of a variable.
Base class for body parts and neural network nodes.
Definition: Node.h:25
virtual void StepSimulation()
Step the simulation for this object.
long m_lBurstOffDuration
Duration of the burst off period in time slices.
virtual bool OutOfElem()
Goes out of the element where the cursor is located.
Definition: StdXml.cpp:56
virtual void Deactivate()
Deactivates this item.
float m_fltCycleOffDuration
Duration of the cycle off period in time.
int m_iType
Tells the type of current to apply. (Constant, Repetitive, Bursting)
bool Std_IsBlank(std::string strVal)
Trims a string and tests if a string is blank.
virtual float CycleOffDuration()
Gets the off cycle current duration in time.
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 long TimeSlice()
Gets the current time slice.
Definition: Simulator.cpp:613
Declares the structure class.
Declares the odor type class.
CStdPostFixEval * m_lpCurrentOnEval
Pointer to the post-fix equation evaluator.
virtual void Initialize()
Initializes this object.
virtual void Initialize()
Initializes this object.
virtual float BurstOffDuration()
Gets the off burst current duration in time.
long m_lBurstOnDuration
Duration of the burst on period in time slices.
float m_fltActiveCurrent
The active current at that time step.
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
std::string Std_ToUpper(std::string strVal)
Converts a string to upper case.
float m_fltCurrentOn
The current to apply when the cycle is on.
Declares the activated item manager class.
Declares the contact sensor class.
Declares the external stimuli manager class.
bool m_bAlwaysActive
Determines whether this item is always active during simulation.
Definition: ActivatedItem.h:59
long m_lCycleStart
The time slice where the cycle starts.
Declares the external input stimulus class.
std::string m_strType
The type for this object. Examples are Box, Plane, Neuron, etc..
Definition: AnimatBase.h:58
virtual bool AlwaysActive()
Gets whether this item is always active.
Declares the receptive field class.
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
virtual float CycleOnDuration()
Gets the on cycle current duration in time.
virtual bool SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError=true)
Set a variable based on a string data type name.