NTRT Simulator  Version: Master
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
T6Model.cpp
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 
25 // This module
26 #include "T6Model.h"
27 // This library
28 #include "core/tgBasicActuator.h"
29 #include "core/tgRod.h"
30 #include "tgcreator/tgBuildSpec.h"
32 #include "tgcreator/tgRodInfo.h"
33 #include "tgcreator/tgStructure.h"
35 // The Bullet Physics library
36 #include "LinearMath/btVector3.h"
37 // The C++ Standard Library
38 #include <stdexcept>
39 
40 namespace
41 {
42  // see tgBasicActuator and tgRod for a descripton of these rod parameters
43  // (specifically, those related to the motor moving the strings.)
44  // NOTE that any parameter that depends on units of length will scale
45  // with the current gravity scaling. E.g., with gravity as 98.1,
46  // the length units below are in decimeters.
47 
48  // Note: This current model of the SUPERball rod is 1.5m long by 3 cm radius,
49  // which is 0.00424 m^3.
50  // For SUPERball v1.5, mass = 3.5kg per strut, which comes out to
51  // 0.825 kg / (decimeter^3).
52 
53  // similarly, frictional parameters are for the tgRod objects.
54  const struct Config
55  {
56  double density;
57  double radius;
58  double stiffness;
59  double damping;
60  double rod_length;
61  double rod_space;
62  double friction;
63  double rollFriction;
64  double restitution;
65  double pretension;
66  bool hist;
67  double maxTens;
68  double targetVelocity;
69  } c =
70  {
71  0.688, // density (kg / length^3)
72  0.31, // radius (length)
73  613.0, // stiffness (kg / sec^2) was 1500
74  200.0, // damping (kg / sec)
75  16.84, // rod_length (length)
76  7.5, // rod_space (length)
77  0.99, // friction (unitless)
78  0.01, // rollFriction (unitless)
79  0.0, // restitution (?)
80  2452.0, // pretension -> set to 4 * 613, the previous value of the rest length controller
81  0, // History logging (boolean)
82  100000, // maxTens
83  10000, // targetVelocity
84 
85  // Use the below values for earlier versions of simulation.
86  // 1.006,
87  // 0.31,
88  // 300000.0,
89  // 3000.0,
90  // 15.0,
91  // 7.5,
92  };
93 } // namespace
94 
96 {
97 }
98 
100 {
101 }
102 
103 void T6Model::addNodes(tgStructure& s)
104 {
105  const double half_length = c.rod_length / 2;
106 
107  s.addNode(-c.rod_space, -half_length, 0); // 0
108  s.addNode(-c.rod_space, half_length, 0); // 1
109  s.addNode( c.rod_space, -half_length, 0); // 2
110  s.addNode( c.rod_space, half_length, 0); // 3
111  s.addNode(0, -c.rod_space, -half_length); // 4
112  s.addNode(0, -c.rod_space, half_length); // 5
113  s.addNode(0, c.rod_space, -half_length); // 6
114  s.addNode(0, c.rod_space, half_length); // 7
115  s.addNode(-half_length, 0, c.rod_space); // 8
116  s.addNode( half_length, 0, c.rod_space); // 9
117  s.addNode(-half_length, 0, -c.rod_space); // 10
118  s.addNode( half_length, 0, -c.rod_space); // 11
119 }
120 
121 void T6Model::addRods(tgStructure& s)
122 {
123  s.addPair( 0, 1, "rod");
124  s.addPair( 2, 3, "rod");
125  s.addPair( 4, 5, "rod");
126  s.addPair( 6, 7, "rod");
127  s.addPair( 8, 9, "rod");
128  s.addPair(10, 11, "rod");
129 }
130 
131 void T6Model::addActuators(tgStructure& s)
132 {
133  s.addPair(0, 4, "muscle");
134  s.addPair(0, 5, "muscle");
135  s.addPair(0, 8, "muscle");
136  s.addPair(0, 10, "muscle");
137 
138  s.addPair(1, 6, "muscle");
139  s.addPair(1, 7, "muscle");
140  s.addPair(1, 8, "muscle");
141  s.addPair(1, 10, "muscle");
142 
143  s.addPair(2, 4, "muscle");
144  s.addPair(2, 5, "muscle");
145  s.addPair(2, 9, "muscle");
146  s.addPair(2, 11, "muscle");
147 
148  s.addPair(3, 7, "muscle");
149  s.addPair(3, 6, "muscle");
150  s.addPair(3, 9, "muscle");
151  s.addPair(3, 11, "muscle");
152 
153  s.addPair(4, 2, "muscle");
154  s.addPair(4, 10, "muscle");
155  s.addPair(4, 11, "muscle");
156 
157  s.addPair(5, 8, "muscle");
158  s.addPair(5, 9, "muscle");
159 
160  s.addPair(6, 10, "muscle");
161  s.addPair(6, 11, "muscle");
162 
163  s.addPair(7, 8, "muscle");
164  s.addPair(7, 9, "muscle");
165 
166 }
167 
168 void T6Model::setup(tgWorld& world)
169 {
170 
171  const tgRod::Config rodConfig(c.radius, c.density, c.friction,
172  c.rollFriction, c.restitution);
173 
175  tgBasicActuator::Config muscleConfig(c.stiffness, c.damping, c.pretension, c.hist,
176  c.maxTens, c.targetVelocity);
177 
178  // Start creating the structure
179  tgStructure s;
180  addNodes(s);
181  addRods(s);
182  addActuators(s);
183  s.move(btVector3(0, 10, 0));
184 
185  // Add a rotation. This is needed if the ground slopes too much,
186  // otherwise glitches put a rod below the ground.
187  btVector3 rotationPoint = btVector3(0, 0, 0); // origin
188  btVector3 rotationAxis = btVector3(0, 1, 0); // y-axis
189  double rotationAngle = M_PI/2;
190  s.addRotation(rotationPoint, rotationAxis, rotationAngle);
191 
192  // Create the build spec that uses tags to turn the structure into a real model
193  tgBuildSpec spec;
194  spec.addBuilder("rod", new tgRodInfo(rodConfig));
195  spec.addBuilder("muscle", new tgBasicActuatorInfo(muscleConfig));
196 
197  // Create your structureInfo
198  tgStructureInfo structureInfo(s, spec);
199 
200  // Use the structureInfo to build ourselves
201  structureInfo.buildInto(*this, world);
202 
203  // We could now use tgCast::filter or similar to pull out the
204  // models (e.g. muscles) that we want to control.
205  allActuators = tgCast::filter<tgModel, tgBasicActuator> (getDescendants());
206 
207  // call the onSetup methods of all observed things e.g. controllers
208  notifySetup();
209 
210  // Actually setup the children
211  tgModel::setup(world);
212 }
213 
214 void T6Model::step(double dt)
215 {
216  // Precondition
217  if (dt <= 0.0)
218  {
219  throw std::invalid_argument("dt is not positive");
220  }
221  else
222  {
223  // Notify observers (controllers) of the step so that they can take action
224  notifyStep(dt);
225  tgModel::step(dt); // Step any children
226  }
227 }
228 
230 {
231  tgModel::onVisit(r);
232 }
233 
234 const std::vector<tgBasicActuator*>& T6Model::getAllActuators() const
235 {
236  return allActuators;
237 }
238 
239 void T6Model::teardown()
240 {
241  notifyTeardown();
243 }
virtual void teardown()
Definition: tgModel.cpp:68
virtual void setup(tgWorld &world)
Definition: tgModel.cpp:57
Definition of class tgRodInfo.
virtual void step(double dt)
Definition: tgModel.cpp:84
virtual void onVisit(tgModelVisitor &r)
Definition: T6Model.cpp:242
Definition of class tgBasicActuatorInfo.
virtual void onVisit(const tgModelVisitor &r) const
Definition: tgModel.cpp:107
virtual void setup(tgWorld &world)
Definition: T6Model.cpp:174
void addPair(int fromNodeIdx, int toNodeIdx, std::string tags="")
Definition: tgStructure.cpp:80
void addRotation(const btVector3 &fixedPoint, const btVector3 &axis, double angle)
void teardown()
Definition: T6Model.cpp:268
Contains the definition of class tgBasicActuator.
virtual void step(double dt)
Definition: T6Model.cpp:227
virtual ~T6Model()
Definition: T6Model.cpp:104
Definition of class tgStructure.
Definition of class tgStructureInfo.
T6Model()
Definition: T6Model.cpp:100
Contains the definition of class tgRod.
Definition of class tgBuildSpec.
void notifyStep(double dt)
const std::vector< tgBasicActuator * > & getAllActuators() const
Definition: T6Model.cpp:234
std::vector< tgModel * > getDescendants() const
Definition: tgModel.cpp:170
void addNode(double x, double y, double z, std::string tags="")
Definition: tgStructure.cpp:70