AnimatLab  2
Test
PidControl.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 "KeyFrame.h"
34 #include "SimulationRecorder.h"
35 #include "OdorType.h"
36 #include "Odor.h"
37 #include "Light.h"
38 #include "LightManager.h"
39 #include "Simulator.h"
40 #include "PidControl.h"
41 
42 namespace AnimatSim
43 {
44 
52 {
53  m_bEnabled = true;
55  m_fltEnabled = 0;
56 }
57 
58 PidControl::PidControl(float fltSetpoint, float fltGain, float fltIntegralAct, float fltDerivativeAct,
59  bool bComplexError, bool bAntiResetWindup, bool bRampLimit,
60  float fltRangeMax, float fltRangeMin, float fltARWBound, float fltRampGradient) :
61 CStdPID(fltSetpoint, fltGain, fltIntegralAct, fltDerivativeAct, bComplexError, bAntiResetWindup,
62  bRampLimit, fltRangeMax, fltRangeMin, fltARWBound, fltRampGradient)
63 {
64 }
65 
66 
74 {
75 
76 try
77 {
78 }
79 catch(...)
80 {Std_TraceMsg(0, "Caught Error in desctructor of PidControl\r\n", "", -1, false, true);}
81 }
82 
95 
106 void PidControl::Enabled(bool bValue)
107 {
108  m_bEnabled = bValue;
109  m_fltEnabled = (float) m_bEnabled;
110 
111  //If the sim is running then we do not set the history flag. Only set it if changed while the sim is not running.
112  if(!m_lpSim->SimRunning())
114 }
115 
116 float *PidControl::GetDataPointer(const std::string &strDataType)
117 {
118  std::string strType = Std_CheckString(strDataType);
119 
120  float *lpData = NULL;
121 
122  if(strType == "ENABLE")
123  return &m_fltEnabled;
124 
125  return AnimatBase::GetDataPointer(strDataType);
126 }
127 
128 bool PidControl::SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError)
129 {
130  std::string strType = Std_CheckString(strDataType);
131 
132  if(AnimatBase::SetData(strDataType, strValue, false))
133  return true;
134 
135  if(strType == "ENABLED")
136  {
137  Enabled(Std_ToBool(strValue));
138  return true;
139  }
140 
141  if(strType == "KP")
142  {
143  Gain((float) atof(strValue.c_str()));
144  return true;
145  }
146 
147  if(strType == "KI")
148  {
149  IntegralAct((float) atof(strValue.c_str()));
150  return true;
151  }
152 
153  if(strType == "KD")
154  {
155  DerivativeAct((float) atof(strValue.c_str()));
156  return true;
157  }
158 
159  if(strType == "COMPLEXERROR")
160  {
161  ComplexError(Std_ToBool(strValue));
162  return true;
163  }
164 
165  if(strType == "ANTIRESETWINDUP")
166  {
167  AntiResetWindup(Std_ToBool(strValue));
168  return true;
169  }
170 
171  if(strType == "RAMPLIMIT")
172  {
173  RampLimit(Std_ToBool(strValue));
174  return true;
175  }
176 
177  if(strType == "RANGEMAX")
178  {
179  RangeMax((float) atof(strValue.c_str()));
180  return true;
181  }
182 
183  if(strType == "RANGEMIN")
184  {
185  RangeMin((float) atof(strValue.c_str()));
186  return true;
187  }
188 
189  if(strType == "ARWBOUND")
190  {
191  ARWBound((float) atof(strValue.c_str()));
192  return true;
193  }
194 
195  if(strType == "RAMPGRADIENT")
196  {
197  RampGradient((float) atof(strValue.c_str()));
198  return true;
199  }
200 
201  //If it was not one of those above then we have a problem.
202  if(bThrowError)
203  THROW_PARAM_ERROR(Al_Err_lInvalidItemType, Al_Err_strInvalidItemType, "Data Type", strDataType);
204 
205  return false;
206 }
207 
208 void PidControl::QueryProperties(CStdPtrArray<TypeProperty> &aryProperties)
209 {
210  AnimatBase::QueryProperties(aryProperties);
211 
212  aryProperties.Add(new TypeProperty("Enable", AnimatPropertyType::Boolean, AnimatPropertyDirection::Get));
213 
214  aryProperties.Add(new TypeProperty("Enabled", AnimatPropertyType::Boolean, AnimatPropertyDirection::Both));
215  aryProperties.Add(new TypeProperty("Kp", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
216  aryProperties.Add(new TypeProperty("Ki", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
217  aryProperties.Add(new TypeProperty("Kd", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
218  aryProperties.Add(new TypeProperty("ComplexError", AnimatPropertyType::Boolean, AnimatPropertyDirection::Set));
219  aryProperties.Add(new TypeProperty("AntiResetWindup", AnimatPropertyType::Boolean, AnimatPropertyDirection::Set));
220  aryProperties.Add(new TypeProperty("RampLimit", AnimatPropertyType::Boolean, AnimatPropertyDirection::Set));
221  aryProperties.Add(new TypeProperty("RangeMax", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
222  aryProperties.Add(new TypeProperty("RangeMin", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
223  aryProperties.Add(new TypeProperty("ARWBound", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
224  aryProperties.Add(new TypeProperty("RampGradient", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
225 }
226 
228 {
229  ResetVars();
230 }
231 
232 void PidControl::Load(CStdXml &oXml)
233 {
234  AnimatBase::Load(oXml);
235 
236  oXml.IntoElem(); //Into PidControl Element
237 
238  //Load Source Data
239  Setpoint(oXml.GetChildFloat("Setpoint", 0));
240  Gain(oXml.GetChildFloat("Kp", m_fltGain));
241  IntegralAct(oXml.GetChildFloat("Ki", m_fltIntegralAct));
242  DerivativeAct(oXml.GetChildFloat("Kd", m_fltDerivativeAct));
243  ComplexError(oXml.GetChildBool("ComplexError", m_bComplexError));
244  AntiResetWindup(oXml.GetChildBool("AntiResetWindup", m_bAntiResetWindup));
245  RampLimit(oXml.GetChildBool("RampLimit", m_bRampLimit));
246  RangeMax(oXml.GetChildFloat("RangeMax", m_fltRangeMax));
247  RangeMin(oXml.GetChildFloat("RangeMin", m_fltRangeMin));
248  ARWBound(oXml.GetChildFloat("ARWBound", m_fltARWBound));
249  RampGradient(oXml.GetChildFloat("RampGradient", m_fltRampGradient));
250 
251  Enabled(oXml.GetChildBool("Enabled", m_bEnabled));
252 
253  oXml.OutOfElem(); //OutOf PidControl Element
254 }
255 
256 } //AnimatSim
Base class file for all Animat simulation objects.
Declares the nervous system class.
Declares the simulation recorder class.
Root namespace for the base simulation library for AnimatLab.
Declares the body part class.
virtual void ResetSimulation()
Resets the simulation back to time 0.
Definition: PidControl.cpp:227
Simulator * m_lpSim
The pointer to a Simulation.
Definition: AnimatBase.h:43
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
The Gain base class.
Definition: Gain.h:35
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: PidControl.cpp:128
bool m_bInitEnabled
Keeps track of the enabled state at sim startup.
Definition: PidControl.h:15
Declares the key frame class.
Declares the joint class.
Declares the organism class.
virtual bool SimRunning()
Gets whether the simulation is running.
Definition: Simulator.cpp:673
Declares a light object.
Declares the activated item class.
PidControl()
Default constructor.
Definition: PidControl.cpp:51
Implements a basic PID control algorithm.
Definition: StdPID.h:17
Declares a light manager object.
Declares the bounding box class.
Declares the gain base class.
A standard xml manipulation class.
Definition: StdXml.h:19
virtual void QueryProperties(CStdPtrArray< TypeProperty > &aryProperties)
Queries this object for a list of properties that can be changed using SetData.
Definition: PidControl.cpp:208
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
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
virtual bool Enabled()
Tells whether this node is enabled.
Definition: PidControl.cpp:94
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 ~PidControl()
Destructor.
Definition: PidControl.cpp:73
virtual bool OutOfElem()
Goes out of the element where the cursor is located.
Definition: StdXml.cpp:56
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.
Declares the odor type class.
virtual float * GetDataPointer(const std::string &strDataType)
Returns a float pointer to a data item of interest in this object.
Definition: PidControl.cpp:116
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
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