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