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 #include <string>
40 #include "helpers/FileHelpers.h"
41 #include <json/json.h>
42 
43 using namespace std;
44 
49 namespace
50 {
55  const struct Config
56  {
57  double density;
58  double radius;
59  double stiffness;
60  double damping;
61  double triangle_length;
62  double triangle_height;
63  double prism_height;
64  double pretension;
65  } c =
66  {
67  0.2, // density (mass / length^3)
68  0.31, // radius (length)
69  1000.0, // stiffness (mass / sec^2)
70  10.0, // damping (mass / sec)
71  10.0, // triangle_length (length)
72  10.0, // triangle_height (length)
73  20.0, // prism_height (length)
74  500.0 // Pretension (mass * length / sec^2)
75  };
76 } // namespace
77 
79  tgModel()
80 {
81 }
82 
84 {
85 
86 }
87 
88 void PrismModel::addNodes(tgStructure& tetra,
89  double edge,
90  double width,
91  double height)
92 {
93  // bottom right
94  tetra.addNode(-edge / 2.0, 0, 0); // 1
95  // bottom left
96  tetra.addNode( edge / 2.0, 0, 0); // 2
97  // bottom front
98  tetra.addNode(0, 0, width); // 3
99  // top right
100  tetra.addNode(-edge / 2.0, height, 0); // 4
101  // top left
102  tetra.addNode( edge / 2.0, height, 0); // 5
103  // top front
104  tetra.addNode(0, height, width); // 6
105 }
106 
107 void PrismModel::addRods(tgStructure& s)
108 {
109  s.addPair( 0, 4, "rod");
110  s.addPair( 1, 5, "rod");
111  s.addPair( 2, 3, "rod");
112 }
113 
114 void PrismModel::addActuators(tgStructure& s)
115 {
116  // Bottom Triangle
117  s.addPair(0, 1, "actuator");
118  s.addPair(1, 2, "actuator");
119  s.addPair(2, 0, "actuator");
120 
121  // Top
122  s.addPair(3, 4, "actuator");
123  s.addPair(4, 5, "actuator");
124  s.addPair(5, 3, "actuator");
125 
126  //Edges
127  s.addPair(0, 3, "actuator");
128  s.addPair(1, 4, "actuator");
129  s.addPair(2, 5, "actuator");
130 }
131 
132 void PrismModel::setup(tgWorld& world)
133 {
134  // Define the configurations of the rods and strings
135  const tgRod::Config rodConfig(c.radius, c.density);
136  const tgBasicActuator::Config actuatorConfig(c.stiffness, c.damping, c.pretension);
137 
138  // Create a structure that will hold the details of this model
139  tgStructure s;
140 
141  //BEGIN DESERIALIZING
142 
143  Json::Value root; // will contains the root value after parsing.
144  Json::Reader reader;
145 
146  std::string configPath = FileHelpers::getResourcePath("3_prism_serialize/config.json");
147  bool parsingSuccessful = reader.parse( FileHelpers::getFileString(configPath), root );
148  if ( !parsingSuccessful )
149  {
150  // report to the user the failure and their locations in the document.
151  std::cout << "Failed to parse configuration\n"
152  << reader.getFormattedErrorMessages();
153  return;
154  }
155  // Get the value of the member of root named 'encoding', return 'UTF-8' if there is no
156  // such member.
157  double triangle_height = root.get("triangle_height", "UTF-8").asDouble();
158  double triangle_length = root.get("triangle_length", "UTF-8").asDouble();
159  double prism_height = root.get("prism_height", "UTF-8").asDouble();
160  //END SERIALIZING
161 
162  // Add nodes to the structure
163  addNodes(s, triangle_length, triangle_height, prism_height);
164 
165  // Add rods to the structure
166  addRods(s);
167 
168  // Add actuators to the structure
169  addActuators(s);
170 
171  // Move the structure so it doesn't start in the ground
172  s.move(btVector3(0, 10, 0));
173 
174  // Create the build spec that uses tags to turn the structure into a real model
175  tgBuildSpec spec;
176  spec.addBuilder("rod", new tgRodInfo(rodConfig));
177  spec.addBuilder("actuator", new tgBasicActuatorInfo(actuatorConfig));
178 
179  // Create your structureInfo
180  tgStructureInfo structureInfo(s, spec);
181 
182  // Use the structureInfo to build ourselves
183  structureInfo.buildInto(*this, world);
184 
185  // We could now use tgCast::filter or similar to pull out the
186  // models (e.g. actuators) that we want to control.
187  allActuators = tgCast::filter<tgModel, tgSpringCableActuator> (getDescendants());
188 
189  // Notify controllers that setup has finished.
190  notifySetup();
191 
192  // Actually setup the children
193  tgModel::setup(world);
194 }
195 
196 void PrismModel::step(double dt)
197 {
198  // Precondition
199  if (dt <= 0.0)
200  {
201  throw std::invalid_argument("dt is not positive");
202  }
203  else
204  {
205  // Notify observers (controllers) of the step so that they can take action
206  notifyStep(dt);
207  tgModel::step(dt); // Step any children
208  }
209 }
210 
212 {
213  // Example: m_rod->getRigidBody()->dosomething()...
214  tgModel::onVisit(r);
215 }
216 
217 const std::vector<tgSpringCableActuator*>& PrismModel::getAllActuators() const
218 {
219  return allActuators;
220 }
221 
223 {
224  notifyTeardown();
226 }
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
A series of functions to assist with file input/output.
static std::string getResourcePath(std::string relPath)
Definition: FileHelpers.cpp:40
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 addNode(double x, double y, double z, std::string tags="")
Definition: tgStructure.cpp:70