AnimatLab  2
Test
Spring.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 "IPhysicsMovableItem.h"
14 #include "IPhysicsBody.h"
15 #include "BoundingBox.h"
16 #include "MovableItem.h"
17 #include "BodyPart.h"
18 #include "Joint.h"
19 #include "ReceptiveField.h"
20 #include "ContactSensor.h"
21 #include "RigidBody.h"
22 #include "LineBase.h"
23 #include "Spring.h"
24 #include "Structure.h"
25 #include "Organism.h"
26 #include "ActivatedItem.h"
27 #include "ActivatedItemMgr.h"
28 #include "DataChartMgr.h"
29 #include "ExternalStimuliMgr.h"
30 #include "KeyFrame.h"
31 #include "SimulationRecorder.h"
32 #include "OdorType.h"
33 #include "Odor.h"
34 #include "Light.h"
35 #include "LightManager.h"
36 #include "Simulator.h"
37 
38 namespace AnimatSim
39 {
40  namespace Environment
41  {
42  namespace Bodies
43  {
51 {
52  m_bInitEnabled = false;
55  m_fltStiffness = 5000;
57  m_fltDamping = 1000;
60  m_fltTension = 0;
61  m_fltEnergy = 0;
62  m_fltVelocity = 0;
63  m_fltAvgVelocity = 0;
64  m_fltStiffnessTension = 0;
65  m_fltDampingTension = 0;
66 
68 }
69 
77 {
78 }
79 
89 
91 
92 void Spring::NaturalLength(float fltVal, bool bUseScaling)
93 {
94  Std_IsAboveMin((float) 0, fltVal, true, "Spring.NaturalLength");
95 
97  if(bUseScaling)
99  else
100  m_fltNaturalLength = fltVal;
101 }
102 
104 
105 void Spring::Stiffness(float fltVal, bool bUseScaling)
106 {
107  Std_IsAboveMin((float) 0, fltVal, true, "Spring.Stiffness");
108 
109  m_fltStiffnessNotScaled = fltVal;
110  if(bUseScaling)
112  else
113  m_fltStiffness = fltVal;
114 }
115 
116 float Spring::Damping() {return m_fltDamping;}
117 
118 void Spring::Damping(float fltVal, bool bUseScaling)
119 {
120  Std_IsAboveMin((float) 0, fltVal, true, "Spring.Damping", true);
121 
122  m_fltDampingNotScaled = fltVal;
123  if(bUseScaling)
125  else
126  m_fltDamping = fltVal;
127 }
128 
138 
147 float Spring::Tension() {return m_fltTension;}
148 
157 float Spring::Energy() {return m_fltEnergy;}
158 
159 
169 
171 {
172  //Setup the circular cue for calculating rolling velocity average.
173  m_fltAvgVelocity = 0;
174  m_aryVelocityAvg.Clear();
175  for(int i=0; i<5; i++)
176  m_aryVelocityAvg.Add(0);
177 }
178 
179 // There are no parts or joints to create for muscle attachment points.
181 {
182 }
183 
185 {
187 
190 
192 
194  m_fltVelocity = 0;
195 }
196 
198 {
199  if(m_bEnabled)
200  {
204 
206 
209 
210  m_fltStiffnessTension = m_fltStiffnessNotScaled * m_fltDisplacement;
211  m_fltDampingTension = m_fltAvgVelocity*m_fltDamping;
212 
213  m_fltTension = m_fltStiffnessTension + m_fltDampingTension;
214  m_fltEnergy = 0.5f*m_fltStiffnessNotScaled*m_fltDisplacement*m_fltDisplacement;
215  }
216  else
217  {
218  m_fltDisplacement = 0;
219  m_fltTension = 0;
220  m_fltEnergy = 0;
221  m_fltVelocity = 0;
222  if(m_aryVelocityAvg.GetSize())
224  }
225 }
226 
227 void Spring::AddExternalNodeInput(int iTargetDataType, float fltInput)
228 {
229  if(m_aryAttachmentPoints.GetSize() == 2)
230  {
231  if(fltInput > 0 && m_bEnabled != !m_bInitEnabled)
233 
234  if(fltInput <= 0 && m_bEnabled != m_bInitEnabled)
236  }
237  else
238  m_bEnabled = false;
239 }
240 
241 
242 float *Spring::GetDataPointer(const std::string &strDataType)
243 {
244  std::string strType = Std_CheckString(strDataType);
245 
246  if(strType == "SPRINGLENGTH")
247  return &m_fltLength;
248 
249  if(strType == "DISPLACEMENT")
250  return &m_fltDisplacement;
251 
252  if(strType == "TENSION")
253  return &m_fltTension;
254 
255  if(strType == "STIFFNESSTENSION")
256  return &m_fltStiffnessTension;
257 
258  if(strType == "DAMPINGTENSION")
259  return &m_fltDampingTension;
260 
261  if(strType == "ENERGY")
262  return &m_fltEnergy;
263 
264  if(strType == "VELOCITY")
265  return &m_fltAvgVelocity;
266 
267  if(strType == "ENABLE")
268  return &m_fltEnabled;
269 
270  return LineBase::GetDataPointer(strDataType);
271 }
272 
273 bool Spring::SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError)
274 {
275  if(LineBase::SetData(strDataType, strValue, false))
276  return true;
277 
278  if(strDataType == "NATURALLENGTH")
279  {
280  NaturalLength((float) atof(strValue.c_str()));
281  return true;
282  }
283 
284  if(strDataType == "STIFFNESS")
285  {
286  Stiffness((float) atof(strValue.c_str()));
287  return true;
288  }
289 
290  if(strDataType == "DAMPING")
291  {
292  Damping((float) atof(strValue.c_str()));
293  return true;
294  }
295 
296  //If it was not one of those above then we have a problem.
297  if(bThrowError)
298  THROW_PARAM_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType, "Data Type", strDataType);
299 
300  return false;
301 }
302 
303 void Spring::QueryProperties(CStdPtrArray<TypeProperty> &aryProperties)
304 {
305  LineBase::QueryProperties(aryProperties);
306 
307  aryProperties.Add(new TypeProperty("SpringLength", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
308  aryProperties.Add(new TypeProperty("Displacement", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
309  aryProperties.Add(new TypeProperty("Tension", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
310  aryProperties.Add(new TypeProperty("StiffnessTension", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
311  aryProperties.Add(new TypeProperty("DampingTension", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
312  aryProperties.Add(new TypeProperty("Energy", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
313  aryProperties.Add(new TypeProperty("Velocity", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
314  aryProperties.Add(new TypeProperty("Enable", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
315 
316  aryProperties.Add(new TypeProperty("NaturalLength", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
317  aryProperties.Add(new TypeProperty("Stiffness", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
318  aryProperties.Add(new TypeProperty("Damping", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
319 }
320 
321 void Spring::Load(CStdXml &oXml)
322 {
323  if(!m_lpParent)
324  THROW_ERROR(Al_Err_lParentNotDefined, Al_Err_strParentNotDefined);
325 
326  LineBase::Load(oXml);
327 
328  oXml.IntoElem(); //Into RigidBody Element
329 
330  if(m_aryAttachmentPointIDs.GetSize() < 2)
331  m_bEnabled = false;
332 
333  NaturalLength(oXml.GetChildFloat("NaturalLength", m_fltNaturalLength));
334  Stiffness(oXml.GetChildFloat("Stiffness", m_fltStiffness));
335  Damping(oXml.GetChildFloat("Damping", m_fltDamping));
336 
338 
339  oXml.OutOfElem(); //OutOf RigidBody Element
340 }
341 
342  } //Joints
343  } //Environment
344 } //AnimatSim
virtual float Damping()
Gets the damping of the spring.
Definition: Spring.cpp:116
Base class file for all Animat simulation objects.
virtual float CalculateLength()
Calculates the length of the line.
Definition: LineBase.cpp:169
float m_fltStiffness
The stiffness of the spring.
Definition: Spring.h:38
virtual void PhysicsTimeStep(float fltVal)
Sets the integration time step for the physics engine.
Definition: Simulator.cpp:1126
Declares the simulation recorder class.
virtual float NaturalLength()
Gets the natural length of the spring.
Definition: Spring.cpp:90
float m_fltNaturalLength
The natural length of the spring.
Definition: Spring.h:32
virtual float Tension()
Gets the current tension of the spring.
Definition: Spring.cpp:147
Root namespace for the base simulation library for AnimatLab.
virtual float InverseMassUnits()
Gets the inverse mass units.
Definition: Simulator.cpp:1797
float m_fltPrevLength
Length of the line in the previous timestep.
Definition: LineBase.h:32
float m_fltDamping
The damping of the spring.
Definition: Spring.h:44
Declares the body part class.
virtual void ResetSimulation()
Resets the simulation back to time 0.
Definition: Spring.cpp:184
virtual void ResetSimulation()
Resets the simulation back to time 0.
Definition: RigidBody.cpp:1473
Simulator * m_lpSim
The pointer to a Simulation.
Definition: AnimatBase.h:43
float m_fltEnabled
This is used for reporting the enabled state in a GetDataPointer call.
Definition: Node.h:35
virtual bool IntoElem()
Goes into the next element where the cursor is located.
Definition: StdXml.cpp:42
virtual float Displacement()
Gets the curent displacement away from the natural length of the spring.
Definition: Spring.cpp:137
virtual float Energy()
Gets the current energy stored in the spring.
Definition: Spring.cpp:157
Class that stores information about types for QueryProperty information.
Definition: TypeProperty.h:35
Declares the key frame class.
float m_fltVelocity
The velocity of the spring length change.
Definition: Spring.h:59
float m_fltAvgVelocity
The rolling average velocity over the last 5 steps.
Definition: Spring.h:62
virtual void CreateParts()
Allows the rigid body to create its parts using the chosen physics engine.
Definition: Spring.cpp:180
Declares the joint class.
Declares the organism class.
bool Std_IsAboveMin(int iMinVal, int iVal, bool bThrowError, std::string strParamName, bool bInclusiveLimit)
Tests if a number is above a minimum value.
virtual bool Enabled()
Tells whether this node is enabled.
Definition: Node.cpp:84
Declares a light object.
Declares the activated item class.
virtual float DisplayMassUnits()
Gets the density mass units.
Definition: Simulator.cpp:1810
Declares a light manager object.
Declares the bounding box class.
virtual void ClearVelocityAverage()
Clears the velocity average circular queue.
Definition: Spring.cpp:170
Spring()
Default constructor.
Definition: Spring.cpp:50
A standard xml manipulation class.
Definition: StdXml.h:19
Declares the node class.
Declares the line base class.
virtual bool InitEnabled()
Tells whether the spring is enabled at startup of the simulation.
Definition: Spring.cpp:88
bool m_bInitEnabled
Keeps track of the initial state of the enabled flag.
Definition: Spring.h:29
float m_fltDampingNotScaled
The damping of the spring.
Definition: Spring.h:47
CStdArray< Attachment * > m_aryAttachmentPoints
A pointer to the primary attachment part.
Definition: LineBase.h:38
virtual float Stiffness()
Gets the stiffness of the spring.
Definition: Spring.cpp:103
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
float m_fltEnergy
The current energy contained in the spring.
Definition: Spring.h:56
virtual bool OutOfElem()
Goes out of the element where the cursor is located.
Definition: StdXml.cpp:56
float m_fltStiffnessNotScaled
The unscaled stiffness. This is used to calcuate the energy.
Definition: Spring.h:41
float m_fltLength
Current length of the line.
Definition: LineBase.h:29
Declares the data chart manager class.
Declares the rigid body class.
virtual float Velocity()
Gets the velocity of the length change of the spring.
Definition: Spring.cpp:168
virtual ~Spring()
Destructor.
Definition: Spring.cpp:76
std::string Std_CheckString(std::string strVal)
Converts a string to upper case and trims it.
virtual void CalculateTension()
Calculates the tension.
Definition: Spring.cpp:197
Declares the structure class.
Declares the odor type class.
virtual float InverseDistanceUnits()
Gets the inverse distance units.
Definition: Simulator.cpp:1742
Declares the odor class.
Declares the simulator class.
float m_fltDisplacement
The current displacement of the spring from its natural length.
Definition: Spring.h:50
Declares the activated item manager class.
Declares the contact sensor class.
Declares the external stimuli manager class.
CStdArray< std::string > m_aryAttachmentPointIDs
The ID's of the attachment points for this muscle. This is used during the load/initialization proces...
Definition: LineBase.h:35
float m_fltNaturalLengthNotScaled
The unscaled natural length. This is used to calcuate the tension and displacement.
Definition: Spring.h:35
CStdCircularArray< float > m_aryVelocityAvg
circular array for calculating the rolling average of the velocity.
Definition: Spring.h:65
virtual void AddExternalNodeInput(int iTargetDataType, float fltInput)
Adds an external node input.
Definition: Spring.cpp:227
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
Declares the spring class.
float m_fltTension
The current force being applied by the spring.
Definition: Spring.h:53