NTRT Simulator
 All Classes Files Functions Variables Typedefs Friends Pages
tgBaseString.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 "tgBaseString.h"
28 #include "tgWorld.h"
29 // The C++ Standard Library
30 #include <cmath>
31 #include <iostream>
32 #include <stdexcept>
33 
34 using namespace std;
35 
37  double d,
38  bool h,
39  double rot,
40  double mf,
41  double tVel,
42  double mxAcc,
43  double mnAL,
44  double mnRL) :
45  stiffness(s),
46  damping(d),
47  hist(h),
48  rotation(rot),
49  maxTens(mf),
50  targetVelocity(tVel),
51  maxAcc(mxAcc),
52  minActualLength(mnAL),
53  minRestLength(mnRL)
54 {
56  if (s < 0.0)
57  {
58  throw std::invalid_argument("stiffness is negative.");
59  }
60  else if (d < 0.0)
61  {
62  throw std::invalid_argument("damping is negative.");
63  }
64  else if (mf < 0.0)
65  {
66  throw std::invalid_argument("max tension is negative.");
67  }
68  else if (maxAcc < 0.0)
69  {
70  throw std::invalid_argument("max acceleration is negative.");
71  }
72  else if (mnAL < 0.0)
73  {
74  throw std::invalid_argument("min Actual Length is negative.");
75  }
76  else if (mnRL < 0.0)
77  {
78  throw std::invalid_argument("min Rest Length is negative.");
79  }
80 }
81 
83 {
84  maxTens *= sf;
85  targetVelocity *= sf;
86  maxAcc *= sf;
87  minActualLength *= sf;
88  minRestLength *= sf;
89 }
90 
91 
92 
93 void tgBaseString::constructorAux()
94 {
95  if (m_config.targetVelocity < 0.0)
96  {
97  throw std::invalid_argument("Target velocity is negative.");
98  }
99  else if (m_config.maxAcc < 0.0)
100  {
101  throw std::invalid_argument("Maximum acceleration is negative.");
102  }
103  else if (m_config.minActualLength < 0.0)
104  {
105  throw std::invalid_argument("Minimum length is negative.");
106  }
107  else if (m_restLength < 0.0)
108  {
109  throw std::invalid_argument("Starting rest length is negative.");
110  }
111 }
113  tgBaseString::Config& config,
114  double restLength,
115  double actualLength) :
116  tgModel(tags),
118  m_config(config),
119  m_restLength(restLength),
120  m_preferredLength(m_restLength),
121  m_startLength(actualLength),
122  m_prevVelocity(0.0)
123 {
124  constructorAux();
125 
126  // Postcondition
127  assert(invariant());
128  assert(m_preferredLength == m_restLength);
129 }
130 
131 tgBaseString::tgBaseString(std::string space_separated_tags,
132  tgBaseString::Config& config,
133  double restLength,
134  double actualLength) :
135  tgModel(space_separated_tags),
136  m_pHistory(new BaseStringHistory()),
137  m_config(config),
138  m_restLength(restLength),
139  m_preferredLength(m_restLength),
140  m_startLength(actualLength),
141  m_prevVelocity(0.0)
142 {
143  constructorAux();
144 
145  // Postcondition
146  assert(invariant());
147  assert(m_preferredLength == m_restLength);
148 }
149 
151 {
152  delete m_pHistory;
153 }
154 
156 {
157  tgModel::setup(world);
158 }
159 
161 {
163 }
164 
165 void tgBaseString::step(double dt)
166 {
167  if (dt <= 0.0)
168  {
169  throw std::invalid_argument("dt is not positive.");
170  }
171  else
172  {
173  tgModel::step(dt);
174  }
175 }
176 
177 void tgBaseString::setRestLength(double newLength, float dt)
178 {
179  if (newLength < 0.0)
180  {
181  throw std::invalid_argument("Rest length is negative.");
182  }
183  else
184  {
185  m_preferredLength = newLength;
186  moveMotors(dt);
187  }
188 
189  // Postcondition
190  assert(invariant());
191  assert(m_preferredLength == newLength);
192 }
193 
194 bool tgBaseString::invariant() const
195 {
196  return
197  // Only know this about history here, can't confirm how child
198  // classes need to log
199  (m_pHistory != NULL) &&
200  (m_config.targetVelocity >= 0.0) &&
201  (m_config.maxAcc >= 0.0) &&
202  (m_config.minActualLength >= 0.0) &&
203  (m_preferredLength >= 0.0) &&
204  (m_startLength >= 0.0);
205 }
void scale(double sf)
virtual ~tgBaseString()
virtual void teardown()
Definition: tgModel.cpp:73
virtual void setup(tgWorld &world)
Definition: tgModel.cpp:62
Contains the definition of abstract base class tgBaseString. Assumes that the string is linear (F = -...
double m_restLength
Definition: tgBaseString.h:233
virtual void step(double dt)
Definition: tgModel.cpp:86
double m_startLength
Definition: tgBaseString.h:240
Config m_config
Definition: tgBaseString.h:223
virtual void step(double dt)
virtual void setup(tgWorld &wdorld)
virtual void moveMotors(double dt)=0
Contains the definition of class tgWorld $Id$.
Config(double s=1000.0, double d=10.0, bool h=false, double rot=0, double mf=1000.0, double tVel=100.0, double mxAcc=10000.0, double mnAL=0.1, double mnRL=0.1)
double m_prevVelocity
Definition: tgBaseString.h:245
BaseStringHistory *const m_pHistory
Definition: tgBaseString.h:226
virtual void teardown()
tgBaseString(const tgTags &tags, tgBaseString::Config &config, double restLength, double actualLength)
Definition: tgTags.h:43