AnimatLab  2
Test
EquationGain.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 "Gain.h"
13 #include "EquationGain.h"
14 
15 
16 namespace AnimatSim
17 {
18  namespace Gains
19  {
20 
28 {
29  m_lpEval = NULL;
30 }
31 
39 {
40 
41 try
42 {
43  if(m_lpEval)
44  delete m_lpEval;
45 }
46 catch(...)
47 {Std_TraceMsg(0, "Caught Error in desctructor of EquationGain\r\n", "", -1, false, true);}
48 }
49 
59 
68 void EquationGain::GainEquation(std::string strEquation)
69 {
70  CStdPostFixEval *lpEval = new CStdPostFixEval;
71 
72  try
73  {
74  lpEval->AddVariable("x");
75  lpEval->Equation(strEquation);
76  }
77  catch(CStdErrorInfo oError)
78  {
79  if(lpEval) delete lpEval;
80  RELAY_ERROR(oError);
81  }
82  catch(...)
83  {
84  if(lpEval) delete lpEval;
85  THROW_PARAM_ERROR(Std_Err_lSettingEquation, Std_Err_strSettingEquation, "Equation", strEquation);
86  }
87 
88  //Initialize the postfix evaluator.
89  if(m_lpEval)
90  {delete m_lpEval; m_lpEval = NULL;}
91  m_lpEval = lpEval;
92 }
93 
94 void EquationGain::Copy(CStdSerialize *lpSource)
95 {
96  Gain::Copy(lpSource);
97 
98  EquationGain *lpOrig = dynamic_cast<EquationGain *>(lpSource);
99 
101 
102  if(m_lpEval)
103  {
104  delete m_lpEval;
105  m_lpEval = NULL;
106  }
107 
108  //TODO Need to clone this.
109  //if(lpOrig->m_lpEval)
110  // m_lpEval = lpOrig->m_l
111 }
112 
113 CStdSerialize *EquationGain::Clone()
114 {
115  CStdSerialize *lpClone = new EquationGain();
116  lpClone->Copy(this);
117  return lpClone;
118 }
119 
120 float EquationGain::CalculateGain(float fltInput)
121 {
122  if(InLimits(fltInput))
123  {
124  m_lpEval->SetVariable("x", fltInput);
125  return m_lpEval->Solve();
126  }
127  else
128  return CalculateLimitOutput(fltInput);
129 }
130 
131 bool EquationGain::SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError)
132 {
133  if(Gain::SetData(strDataType, strValue, false))
134  return true;
135 
136  if(strDataType == "EQUATION")
137  {
138  GainEquation(strValue);
139  return true;
140  }
141 
142  //If it was not one of those above then we have a problem.
143  if(bThrowError)
144  THROW_PARAM_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType, "Data Type", strDataType);
145 
146  return false;
147 }
148 
149 void EquationGain::QueryProperties(CStdPtrArray<TypeProperty> &aryProperties)
150 {
151  Gain::QueryProperties(aryProperties);
152 
153  aryProperties.Add(new TypeProperty("Equation", AnimatPropertyType::String, AnimatPropertyDirection::Set));
154 }
155 
156 void EquationGain::Load(CStdXml &oXml)
157 {
158  Gain::Load(oXml);
159 
160  oXml.IntoElem(); //Into Adapter Element
161 
162  GainEquation(oXml.GetChildString("Equation"));
163 
164  oXml.OutOfElem(); //OutOf Adapter Element
165 }
166 
167  } //Gains
168 } //AnimatSim
Base class file for all Animat simulation objects.
Root namespace for the base simulation library for AnimatLab.
EquationGain()
Default constructor.
std::string m_strGainEquation
The post-fix gain equation.
Definition: EquationGain.h:28
Class that stores information about types for QueryProperty information.
Definition: TypeProperty.h:35
virtual std::string GainEquation()
Gets the post-fix gain equation.
bool InLimits(float fltInput)
Tells whether the input value is within the defined limit ranges.
Definition: Gain.h:63
Declares the gain base class.
CStdPostFixEval * m_lpEval
The pointer to the postfix equation evaluator.
Definition: EquationGain.h:31
virtual float CalculateGain(float fltInput)
Calculates the gain.
virtual bool SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError=true)
Set a variable based on a string data type name.
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.
float CalculateLimitOutput(float fltInput)
Calculates the output when the input is outside of the limit ranges.
Definition: Gain.h:81
virtual ~EquationGain()
Destructor.
virtual void QueryProperties(CStdPtrArray< TypeProperty > &aryProperties)
Queries this object for a list of properties that can be changed using SetData.
Definition: Gain.cpp:232
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: Gain.cpp:190
Declares the equation gain class.
virtual void QueryProperties(CStdPtrArray< TypeProperty > &aryProperties)
Queries this object for a list of properties that can be changed using SetData.