NTRT Simulator  Version: Master
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
tgBasicActuator.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 
26 // This Module
27 #include "tgBulletSpringCable.h"
28 #include "tgBasicActuator.h"
29 #include "tgModelVisitor.h"
30 #include "tgWorld.h"
31 // The Bullet Physics Library
32 #include "LinearMath/btQuickprof.h"
33 
34 // The C++ Standard Library
35 #include <cmath>
36 #include <deque> // For history
37 #include <iostream>
38 #include <stdexcept>
39 
40 using namespace std;
41 
42 void tgBasicActuator::constructorAux()
43 {
44  // Precondition
45  assert(m_pHistory != NULL);
46  prevVel = 0.0;
47  if (m_springCable == NULL)
48  {
49  throw std::invalid_argument("Pointer to tgBulletSpringCable is NULL.");
50  }
51  else if (m_config.targetVelocity < 0.0)
52  {
53  throw std::invalid_argument("Target velocity is negative.");
54  }
55  else if (m_config.minActualLength < 0.0)
56  {
57  throw std::invalid_argument("Minimum length is negative.");
58  }
59  else
60  {
61  logHistory();
62  }
63 }
65  const tgTags& tags,
67  tgSpringCableActuator(muscle, tags, config),
68  m_preferredLength(m_restLength)
69 {
70  constructorAux();
71 
72  // Postcondition
73  assert(invariant());
74  assert(m_preferredLength == m_restLength);
75 }
76 
78 {
79  //std::cout << "deleting linear string" << std::endl;
80  // Should have already torn down.
81 }
82 
84 {
85  // This needs to be called here in case the controller needs to cast
86  notifySetup();
87  tgModel::setup(world);
88 }
89 
91 {
92  // Do not notify teardown. The controller has already been deleted.
94 }
95 
96 void tgBasicActuator::step(double dt)
97 {
98 #ifndef BT_NO_PROFILE
99  BT_PROFILE("tgBasicActuator::step");
100 #endif //BT_NO_PROFILE
101  if (dt <= 0.0)
102  {
103  throw std::invalid_argument("dt is not positive.");
104  }
105  else
106  {
107  // Want to update any controls before applying forces
108  notifyStep(dt);
109  m_springCable->step(dt);
110  logHistory();
111  tgModel::step(dt);
112  }
113 }
114 
116 {
117 #ifndef BT_NO_PROFILE
118  BT_PROFILE("tgBasicActuator::onVisit");
119 #endif //BT_NO_PROFILE
120  r.render(*this);
121 }
122 
123 void tgBasicActuator::logHistory()
124 {
126 
127  if (m_config.hist)
128  {
134  }
135 }
136 
138 {
139  if (input < 0.0)
140  {
141  throw std::invalid_argument("Rest length is negative.");
142  }
143  else
144  {
145  m_preferredLength = input;
146  }
147 }
148 
149 void tgBasicActuator::setControlInput(double input, double dt)
150 {
151  if (input < 0.0)
152  {
153  throw std::invalid_argument("Rest length is negative.");
154  }
155  else
156  {
157  m_preferredLength = input;
158 
159  // moveMotors can change m_preferred length, so this goes here for now
160  assert(m_preferredLength == input);
161 
162  moveMotors(dt);
163  }
164 
165  // Postcondition
166  assert(invariant());
167 
168 }
169 
171 {
172  // @todo add functions from muscle2P Bounded
173 
174 
175  const double stiffness = m_springCable->getCoefK();
176  // @todo: write invariant that checks this;
177  assert(stiffness > 0.0);
178 
179  // Reverse the sign if restLength >= preferredLength
180  // Velocity limiter
181  double stepSize = m_config.targetVelocity * dt;
182  const double actualLength = m_springCable->getActualLength();
183 
184  // First, change preferred length so we don't go over max tension
185  if ((actualLength - m_preferredLength) * stiffness
186  > m_config.maxTens)
187  {
188  m_preferredLength = actualLength - m_config.maxTens / stiffness;
189  }
190 
191  double diff = m_preferredLength - m_restLength;
192  const double fabsDiff = abs(diff);
193 
194  /*
195  * actualLength must be greater than minActualLength to shorten
196  * diff > 0 means can always lengthen.
197  */
198  if ((actualLength > m_config.minActualLength) ||
199  (diff > 0))
200  {
201  if (abs(diff) > stepSize)
202  {
203  m_restLength += (diff/fabsDiff)*stepSize;
204  }
205  else
206  {
207  m_restLength += diff;
208  }
209  }
210 
211  m_restLength =
212  (m_restLength > m_config.minRestLength) ? m_restLength : m_config.minRestLength;
213  #if (0)
214  std::cout << "RL: " << m_restLength << " M2P RL: " << m_springCable->getRestLength() << std::endl;
215 
216 
217  std::cout << "RL: " << m_restLength
218  << " Vel: " << (m_restLength -m_springCable->getRestLength()) / dt
219  << " prev Vel: " << prevVel
220  << " force " << (actualLength - m_restLength)*stiffness << std::endl;
221  prevVel = (m_restLength -m_springCable->getRestLength()) / dt ;
222  #endif
223  m_springCable->setRestLength(m_restLength);
224 
225 }
226 
227 bool tgBasicActuator::invariant() const
228 {
229  return
230  (m_springCable != NULL) &&
231  (m_pHistory != NULL) &&
232  (m_config.targetVelocity >= 0.0) &&
233  (m_config.minActualLength >= 0.0) &&
234  (m_preferredLength >= 0.0);
235 }
virtual void setRestLength(const double newRestLength)
virtual void teardown()
Definition: tgModel.cpp:68
virtual void setup(tgWorld &world)
Definition: tgModel.cpp:57
tgBasicActuator(tgBulletSpringCable *muscle, const tgTags &tags, tgSpringCableActuator::Config &config)
virtual void moveMotors(double dt)
virtual const double getVelocity() const
virtual void step(double dt)
Definition: tgModel.cpp:84
virtual void setControlInput(double input)
virtual void step(double dt)
virtual void setup(tgWorld &world)
virtual const double getRestLength() const
virtual void render(const tgRod &rod) const
virtual ~tgBasicActuator()
Contains the definition of interface class tgModelVisitor.
Contains the definition of class tgBasicActuator.
virtual const double getCoefK() const
Definition: tgSpringCable.h:95
Contains the definition of class tgWorld $Id$.
virtual const double getTension() const =0
virtual const double getDamping() const
virtual void teardown()
Definitions of classes tgBulletSpringCable $Id$.
virtual const double getActualLength() const =0
virtual void onVisit(const tgModelVisitor &r) const
SpringCableActuatorHistory *const m_pHistory
virtual void step(double dt)=0
Definition: tgTags.h:44