NTRT Simulator  Version: Master
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
tgCompressionSpringActuator.cpp
Go to the documentation of this file.
1 /*
2  * Copyright © 2012, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The NASA Tensegrity Robotics Toolkit (NTRT) v1 platform is licensed
7  * under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * http://www.apache.org/licenses/LICENSE-2.0.
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
15  * either express or implied. See the License for the specific language
16  * governing permissions and limitations under the License.
17 */
18 
27 // This Module
30 #include "tgModelVisitor.h"
31 #include "tgWorld.h"
32 // The Bullet Physics Library
33 #include "LinearMath/btQuickprof.h"
34 
35 // The C++ Standard Library
36 #include <cmath>
37 #include <deque> // For history
38 #include <iostream>
39 #include <stdexcept>
40 
41 using namespace std;
42 
43 // Constructor for the Config struct. Assigns variables but also does some
44 // checks on what's passed in.
46  double s,
47  double d,
48  double rL,
49  bool moveCPA,
50  bool moveCPB) :
51  isFreeEndAttached(iFEA),
52  stiffness(s),
53  damping(d),
54  restLength(rL),
55  moveCablePointAToEdge(moveCPA),
56  moveCablePointBToEdge(moveCPB)
57 {
59  if (s < 0.0)
60  {
61  throw std::invalid_argument("stiffness is negative.");
62  }
63  else if (d < 0.0)
64  {
65  throw std::invalid_argument("damping is negative.");
66  }
67  else if (rL < 0.0)
68  {
69  throw std::invalid_argument("Rest length is negative. Compression springs have a positive rest length.");
70  }
71 }
72 
73 // A helper function for the constructor.
74 // @TO-DO: Where is the "correct" place for these non-negative checks?
75 void tgCompressionSpringActuator::constructorAux()
76 {
77  if (m_compressionSpring == NULL)
78  {
79  throw std::invalid_argument("Pointer to tgBulletCompressionSpring is NULL.");
80  }
81  else if (m_config.stiffness < 0.0)
82  {
83  throw std::invalid_argument("Stiffness is less than zero.");
84  }
85  else if (m_config.damping < 0.0)
86  {
87  throw std::invalid_argument("Damping coefficient is negative.");
88  }
89  else if (m_config.restLength < 0.0)
90  {
91  throw std::invalid_argument("Rest length is less than zero..");
92  }
93 }
94 
99  const tgTags& tags,
101  tgModel(tags),
102  m_compressionSpring(compressionSpring),
103  m_config(config),
104  m_prevVelocity(0.0)
105 {
106  // call the helper function that does some checks for non-negativeness
107  constructorAux();
108 
109  // Postcondition
110  assert(invariant());
111  // making sure that no changes to the aux constructor function end up changing around
112  // the compression spring pointer.
113  assert(m_compressionSpring == compressionSpring);
114 }
115 
117 {
118  // Debugging
119  #if(0)
120  std::cout << "Destroying tgCompressionSpringActuator" << std::endl;
121  #endif
122 
123  // This is the only location where the compression spring should be
124  // deleted. Do not re-delete it in any of its child classes.
125  // Note also that the pointer may not be NULL in other locations in
126  // other objects, since the pointer is being passed around, and will
127  // only change locally but not globally.
128  // @TODO: make this a pointer to a pointer so we can check everywhere?
129  delete m_compressionSpring;
130  m_compressionSpring = NULL;
131 }
132 
134 {
135  // This needs to be called here in case the controller needs to cast
136  notifySetup();
137  tgModel::setup(world);
138 }
139 
141 {
142  // Do not notify teardown. Copied from tgBasicActuator/tgSpringCableActuator.
144 }
145 
152 {
153 #ifndef BT_NO_PROFILE
154  BT_PROFILE("tgCompressionSpringActuator::step");
155 #endif //BT_NO_PROFILE
156  if (dt <= 0.0)
157  {
158  throw std::invalid_argument("dt is not positive.");
159  }
160  else
161  {
162  // Want to update any controls before applying forces
163  notifyStep(dt);
165  tgModel::step(dt);
166  }
167 }
168 
169 // Renders the spring in the NTRT window
171 {
172 #ifndef BT_NO_PROFILE
173  BT_PROFILE("tgCompressionSpringActuator::onVisit");
174 #endif //BT_NO_PROFILE
175  r.render(*this);
176 }
177 
178 // Returns the force in the underlying compression spring.
179 // This should be polymorphic: if the spring is actually a Unidirectional spring,
180 // for example, this method should call the getSpringForce method of that spring.
182  // Just return the force of the underlying spring.
184 }
185 
190 {
191  // do nothing. See tgBasic Actuator for an example implementation.
192 }
193 void tgCompressionSpringActuator::setControlInput(double input, double dt)
194 {
195  // do nothing. See tgBasic Actuator for an example implementation.
196 }
197 
198 // Finally, the invariant for assertions.
199 bool tgCompressionSpringActuator::invariant() const
200 {
201  return
202  (m_compressionSpring != NULL);
203 }
virtual void teardown()
Definition: tgModel.cpp:68
virtual void setup(tgWorld &world)
Definition: tgModel.cpp:57
Contains the definition of class tgCompressionSpringActuator. This class assumes a linear spring...
Config(bool iFEA=false, double s=1000.0, double d=10.0, double rL=0.0, bool moveCPA=true, bool moveCPB=true)
virtual void setup(tgWorld &world)
tgBulletCompressionSpring * m_compressionSpring
virtual const double getSpringForce() const
virtual void step(double dt)
Definition: tgModel.cpp:84
tgCompressionSpringActuator(tgBulletCompressionSpring *compressionSpring, const tgTags &tags, tgCompressionSpringActuator::Config &config)
virtual void render(const tgRod &rod) const
Contains the definition of interface class tgModelVisitor.
Contains the definition of class tgWorld $Id$.
Definitions of class tgBulletCompressionSpring.
virtual void onVisit(const tgModelVisitor &r) const
virtual void setControlInput(double input)
Definition: tgTags.h:44