NTRT Simulator  Version: Master
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
tgUnidirComprSprActuator.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 
28 // This Module
30 // NTRT core library files
32 #include "core/tgModelVisitor.h"
33 #include "core/tgWorld.h"
34 // The Bullet Physics Library
35 #include "LinearMath/btQuickprof.h"
36 
37 // The C++ Standard Library
38 #include <cmath>
39 #include <deque> // For history
40 #include <iostream>
41 #include <stdexcept>
42 
43 using namespace std;
44 
45 // The constructor for this class' Config struct,
46 // now containing the additional parameter for direction.
47 // (This is NOT a declaration of the config struct, that's in the .h file.
48 // Remember that in C++, structs have constructor methods.)
49 // Note that this Config struct includes the config struct for its superclass.
51  double s,
52  double d,
53  double rL,
54  bool moveCPA,
55  bool moveCPB,
56  btVector3 * dir) :
57  tgCompressionSpringActuator::Config::Config(iFEA, s, d, rL, moveCPA, moveCPB),
58  direction(dir)
59 {
60  // Direction cannot be null.
61  if( dir == NULL)
62  {
63  throw std::invalid_argument("Pointer to btVector3 direction is NULL, inside constructor for tgUnidirComprSprActuator Config.");
64  }
65 
66  // Debugging
67  #if (0)
68  std::cout << "tgUnidirComprSprActuator Config constructor. Direction is:" << std::endl;
69  std::cout << "(" << direction->x() << ",";
70  std::cout << direction->y() << ",";
71  std::cout << direction->z() << ")" << std::endl;
72  #endif
73 }
74 
75 // A helper function for the constructor.
76 // @TO-DO: Where is the "correct" place for these non-negative checks?
77 void tgUnidirComprSprActuator::constructorAux()
78 {
79  if (m_compressionSpring == NULL)
80  {
81  throw std::invalid_argument("Pointer to tgBulletUnidirComprSpr is NULL.");
82  }
83 
84  // Direction cannot be null.
85  if( m_config.direction == NULL)
86  {
87  throw std::invalid_argument("Pointer to btVector3 direction is NULL, inside constructor aux for tgUnidirComprSprActuator.");
88  }
89 
90  // Debugging
91  #if (0)
92  std::cout << "tgUnidirComprSprActuator constructor aux. Direction is:" << std::endl;
93  std::cout << "(" << m_config.direction->x() << ",";
94  std::cout << m_config.direction->y() << ",";
95  std::cout << m_config.direction->z() << ")" << std::endl;
96  #endif
97 }
98 
108  tgBulletUnidirComprSpr* compressionSpring,
109  const tgTags& tags,
111  m_config(config),
112  tgCompressionSpringActuator(compressionSpring, tags, config)
113 {
114  // call the helper function that does some checks
115  constructorAux();
116 
117  // Postcondition
118  assert(invariant());
119  // making sure that no changes to the aux constructor function end up
120  // changing around the compression spring pointer.
121  assert(m_compressionSpring == compressionSpring);
122 }
123 
125 {
126  //Debugging
127  #if(0)
128  std::cout << "Destroying tgUnidirComprSprActuator" << std::endl;
129  std::cout << "This class does not delete m_compressionSpring." << std::endl;
130  #endif
131 
132 }
133 
135 {
136  // This needs to be called here in case the controller needs to cast
137  notifySetup();
138  tgModel::setup(world);
139 }
140 
142 {
143  // Copied from tgBasicActuator/tgSpringCableActuator.
145 }
146 
153 {
154 #ifndef BT_NO_PROFILE
155  BT_PROFILE("tgUnidirComprSprActuator::step");
156 #endif //BT_NO_PROFILE
157  if (dt <= 0.0)
158  {
159  throw std::invalid_argument("dt is not positive.");
160  }
161  else
162  {
163  // Want to update any controls before applying forces
164  notifyStep(dt);
165  // This should call the step function within
166  // tgBulletUnidirComprSpr instead of the one inside
167  // tgBulletCompressionSpring.
169  tgModel::step(dt);
170  }
171 }
172 
173 // Renders the spring in the NTRT window
175 {
176 #ifndef BT_NO_PROFILE
177  BT_PROFILE("tgUnidirComprSprActuator::onVisit");
178 #endif //BT_NO_PROFILE
179  r.render(*this);
180 }
181 
182 // Finally, the invariant for assertions.
183 bool tgUnidirComprSprActuator::invariant() const
184 {
185  return
186  (m_compressionSpring != NULL &&
187  m_config.direction != NULL);
188 }
virtual void teardown()
Definition: tgModel.cpp:68
virtual void setup(tgWorld &world)
Definition: tgModel.cpp:57
Definitions of class tgBulletUnidirComprSpr, a version of tgBulletCompressionSpring that only applies...
tgBulletCompressionSpring * m_compressionSpring
virtual void onVisit(const tgModelVisitor &r) const
Contains the definition of class tgUnidirComprSprActuator. This class assumes a linear spring...
virtual void step(double dt)
Definition: tgModel.cpp:84
virtual void render(const tgRod &rod) const
Contains the definition of interface class tgModelVisitor.
Config(bool iFEA=false, double s=1000.0, double d=10.0, double rL=0.0, bool moveCPA=true, bool moveCPB=true, btVector3 *dir=(new btVector3(0, 1, 0)))
Contains the definition of class tgWorld $Id$.
tgUnidirComprSprActuator(tgBulletUnidirComprSpr *compressionSpring, const tgTags &tags, tgUnidirComprSprActuator::Config &config)
virtual void setup(tgWorld &world)
Definition: tgTags.h:44
tgUnidirComprSprActuator::Config m_config