NTRT Simulator
 All Classes Files Functions Variables Typedefs Friends Pages
PrismModel.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 
25 // This module
26 #include "PrismModel.h"
27 // This library
29 #include "core/tgLinearString.h"
30 #include "core/tgRod.h"
31 #include "tgcreator/tgBuildSpec.h"
33 #include "tgcreator/tgRodInfo.h"
34 #include "tgcreator/tgStructure.h"
36 // The Bullet Physics library
37 #include "LinearMath/btVector3.h"
38 // The C++ Standard Library
39 #include <stdexcept>
40 
45 namespace
46 {
51  const struct Config
52  {
53  double density;
54  double radius;
55  double stiffness;
56  double damping;
57  double triangle_length;
58  double triangle_height;
59  double prism_height;
60  double pretension;
61  } c =
62  {
63  0.05, // density (mass / length^3)
64  0.31, // radius (length)
65  1000.0, // stiffness (mass / sec^2)
66  10.0, // damping (mass / sec)
67  10.0, // triangle_length (length)
68  10.0, // triangle_height (length)
69  20.0, // prism_height (length)
70  0.05 // Pretension (percentage)
71  };
72 } // namespace
73 
75 m_pStringController(new PretensionController(c.pretension)),
76 tgModel()
77 {
78 }
79 
81 {
82  delete m_pStringController;
83 }
84 
85 void PrismModel::addNodes(tgStructure& tetra,
86  double edge,
87  double width,
88  double height)
89 {
90  // bottom right
91  tetra.addNode(-edge / 2.0, 0, 0); // 1
92  // bottom left
93  tetra.addNode( edge / 2.0, 0, 0); // 2
94  // bottom front
95  tetra.addNode(0, 0, width); // 3
96  // top right
97  tetra.addNode(-edge / 2.0, height, 0); // 4
98  // top left
99  tetra.addNode( edge / 2.0, height, 0); // 5
100  // top front
101  tetra.addNode(0, height, width); // 6
102 }
103 
104 void PrismModel::addRods(tgStructure& s)
105 {
106  s.addPair( 0, 4, "rod");
107  s.addPair( 1, 5, "rod");
108  s.addPair( 2, 3, "rod");
109 }
110 
111 void PrismModel::addMuscles(tgStructure& s)
112 {
113  // Bottom Triangle
114  s.addPair(0, 1, "muscle");
115  s.addPair(1, 2, "muscle");
116  s.addPair(2, 0, "muscle");
117 
118  // Top
119  s.addPair(3, 4, "muscle");
120  s.addPair(4, 5, "muscle");
121  s.addPair(5, 3, "muscle");
122 
123  //Edges
124  s.addPair(0, 3, "muscle");
125  s.addPair(1, 4, "muscle");
126  s.addPair(2, 5, "muscle");
127 }
128 
130 {
131  // Define the configurations of the rods and strings
132  const tgRod::Config rodConfig(c.radius, c.density);
133  const tgLinearString::Config muscleConfig(c.stiffness, c.damping);
134 
135  // Create a structure that will hold the details of this model
136  tgStructure s;
137 
138  // Add nodes to the structure
139  addNodes(s, c.triangle_length, c.triangle_height, c.prism_height);
140 
141  // Add rods to the structure
142  addRods(s);
143 
144  // Add muscles to the structure
145  addMuscles(s);
146 
147  // Move the structure so it doesn't start in the ground
148  s.move(btVector3(0, 10, 0));
149 
150  // Create the build spec that uses tags to turn the structure into a real model
151  tgBuildSpec spec;
152  spec.addBuilder("rod", new tgRodInfo(rodConfig));
153  spec.addBuilder("muscle", new tgLinearStringInfo(muscleConfig));
154 
155  // Create your structureInfo
156  tgStructureInfo structureInfo(s, spec);
157 
158  // Use the structureInfo to build ourselves
159  structureInfo.buildInto(*this, world);
160 
161  // We could now use tgCast::filter or similar to pull out the
162  // models (e.g. muscles) that we want to control.
163  allMuscles = tgCast::filter<tgModel, tgLinearString> (getDescendants());
164 
165  // Then attach the pretension controller to each of these muscles to keep
166  // the tensegrity's shape
167  for (std::size_t i = 0; i < allMuscles.size(); i++)
168  {
169  allMuscles[i]->attach(m_pStringController);
170  }
171 
172  // Notify controllers that setup has finished.
173  notifySetup();
174 
175  // Actually setup the children
176  tgModel::setup(world);
177 }
178 
179 void PrismModel::step(double dt)
180 {
181  // Precondition
182  if (dt <= 0.0)
183  {
184  throw std::invalid_argument("dt is not positive");
185  }
186  else
187  {
188  // Notify observers (controllers) of the step so that they can take action
189  notifyStep(dt);
190  tgModel::step(dt); // Step any children
191  }
192 }
193 
195 {
196  // Example: m_rod->getRigidBody()->dosomething()...
197  tgModel::onVisit(r);
198 }
199 
200 const std::vector<tgLinearString*>& PrismModel::getAllMuscles() const
201 {
202  return allMuscles;
203 }
204 
206 {
207  notifyTeardown();
209 }
virtual void step(double dt)
Definition: PrismModel.cpp:179
virtual void teardown()
Definition: tgModel.cpp:73
virtual void setup(tgWorld &world)
Definition: tgModel.cpp:62
Definition of class tgRodInfo.
const std::vector< tgLinearString * > & getAllMuscles() const
Definition: PrismModel.cpp:200
virtual void step(double dt)
Definition: tgModel.cpp:86
virtual void teardown()
Definition: PrismModel.cpp:205
virtual void onVisit(const tgModelVisitor &r) const
Definition: tgModel.cpp:109
void addPair(int fromNodeIdx, int toNodeIdx, std::string tags="")
Definition: tgStructure.cpp:65
Defines a 3 strut 9 string tensegrity model.
Contains the definition of class tgLinearString.
virtual void onVisit(tgModelVisitor &r)
Definition: PrismModel.cpp:194
Definition of class tgStructure.
Definition of class tgStructureInfo.
Definition of class tgLinearStringInfo.
Contains the definition of class tgRod $Id$.
Definition of class tgBuildSpec.
virtual void setup(tgWorld &world)
Definition: PrismModel.cpp:129
virtual ~PrismModel()
Definition: PrismModel.cpp:80
Contains the definition of the class PretensonController.
void notifyStep(double dt)
std::vector< tgModel * > getDescendants() const
Definition: tgModel.cpp:174
void addNode(double x, double y, double z, std::string tags="")
Definition: tgStructure.cpp:55