NTRT Simulator  Version: Master
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
CraterDeep.cpp
Go to the documentation of this file.
1 /*
2  * Copyright © 2014, 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 "CraterDeep.h"
27 // This library
28 #include "core/tgBox.h"
29 #include "tgcreator/tgBuildSpec.h"
30 #include "tgcreator/tgBoxInfo.h"
31 #include "tgcreator/tgStructure.h"
33 #include "tgcreator/tgNode.h"
34 // The Bullet Physics library
35 #include "LinearMath/btVector3.h"
36 // The C++ Standard Library
37 #include <stdexcept>
38 #include <vector>
39 
40 namespace
41 {
42  // similarly, frictional parameters are for the tgRod objects.
43  const struct Config
44  {
45  double width;
46  double height;
47  double density;
48  double friction;
49  double rollFriction;
50  double restitution;
51  } c =
52  {
53  15.0, // width (dm?)
54  15.0, // height (dm?)
55  0.0, // density (kg / length^3)
56  1.0, // friction (unitless)
57  0.01, // rollFriction (unitless)
58  0.2, // restitution (?)
59  };
60 } // namespace
61 
63 {
64  origin = btVector3(0,0,0);
65 }
66 
67 CraterDeep::CraterDeep(btVector3 center) : tgModel()
68 {
69  origin = btVector3(center.getX(), center.getY(), center.getZ());
70 }
71 
73 {
74 }
75 
76 void CraterDeep::setup(tgWorld& world) {
77 
78  const tgBox::Config boxConfig(c.width, c.height, c.density, c.friction, c.rollFriction, c.restitution);
79 
80  // Start creating the structure
81  tgStructure s;
82  addNodes(s);
83 
84  // Create the build spec that uses tags to turn the structure into a real model
85  tgBuildSpec spec;
86  spec.addBuilder("box", new tgBoxInfo(boxConfig));
87 
88  // Create your structureInfo
89  tgStructureInfo structureInfo(s, spec);
90 
91  // Use the structureInfo to build ourselves
92  structureInfo.buildInto(*this, world);
93 
94  // call the onSetup methods of all observed things e.g. controllers
95  notifySetup();
96 
97  // Actually setup the children
98  tgModel::setup(world);
99 }
100 
101 void CraterDeep::step(double dt) {
102  // Precondition
103  if (dt <= 0.0) {
104  throw std::invalid_argument("dt is not positive");
105  }
106  else { //Notify observers (controllers) of the step so that they can take action
107  notifyStep(dt);
108  tgModel::step(dt); // Step any children
109  }
110 }
111 
113  tgModel::onVisit(r);
114 }
115 
117  nodes.clear();
118  notifyTeardown();
120 }
121 
122 // Nodes: center points of opposing faces of rectangles
123 void CraterDeep::addNodes(tgStructure& s) {
124  const int nBoxes = 4;
125 
126  // Accumulating rotation on boxes
127  btVector3 rotationPoint = origin;
128  btVector3 rotationAxis = btVector3(0, 1, 0); // y-axis
129  double rotationAngle = M_PI/2;
130 
131  addBoxNodes();
132  addBoxNodes();
133  addBoxNodes();
134  addBoxNodes();
135 
136  for(int i=0;i<nodes.size();i+=2) {
137  s.addNode(nodes[i]);
138  s.addNode(nodes[i+1]);
139  s.addRotation(rotationPoint, rotationAxis, rotationAngle);
140  s.addPair(i, i+1, "box");
141  }
142  s.move(btVector3(0, -5, 0)); // Sink boxes into the ground
143 }
144 
145 void CraterDeep::addBoxNodes() {
146  tgNode node;
147 
148  double x1 = 20; // Smaller x values leads to a narrower crater
149  double x2 = 20;
150  double y1 = -10;
151  double y2 = 25;
152  double z1 = 0;
153  double z2 = 0;
154 
155  btVector3 rotationPoint = btVector3((x2-x1)/2, (y2-y1)/2, (z2-z1)/2); //Halfway between nodes
156  btVector3 rotationAxis = btVector3(0, 1, 0); // y-axis
157  double rotationAngle = 0; // Must != 0 for actual change
158 
159  node = tgNode(x1, y1, z1, "node");
160  node.addRotation(rotationPoint, rotationAxis, rotationAngle);
161  nodes.push_back(node);
162 
163  node = tgNode(x2, y2, z2, "node");
164  node.addRotation(rotationPoint, rotationAxis, rotationAngle);
165  nodes.push_back(node);
166 
167 }
168 
Create a box shape as an obstacle or add it to your tensegrity.
virtual void teardown()
Definition: tgModel.cpp:68
virtual void setup(tgWorld &world)
Definition: tgModel.cpp:57
virtual void onVisit(tgModelVisitor &r)
Definition: CraterDeep.cpp:112
void addRotation(const btVector3 &fixedPoint, const btVector3 &fromOrientation, const btVector3 &toOrientation)
Definition: tgNode.h:62
virtual ~CraterDeep()
Definition: CraterDeep.cpp:72
virtual void step(double dt)
Definition: tgModel.cpp:84
virtual void setup(tgWorld &world)
Definition: CraterDeep.cpp:76
virtual void onVisit(const tgModelVisitor &r) const
Definition: tgModel.cpp:107
Class that interfaces with Bullet to build the boxes.
void addPair(int fromNodeIdx, int toNodeIdx, std::string tags="")
Definition: tgStructure.cpp:80
void teardown()
Definition: CraterDeep.cpp:116
void addRotation(const btVector3 &fixedPoint, const btVector3 &axis, double angle)
Definition of class tgNode.
Definition: tgNode.h:45
Definition of class tgStructure.
Definition of class tgStructureInfo.
virtual void step(double dt)
Definition: CraterDeep.cpp:101
Definition of class tgBuildSpec.
Contains the definition of class CraterDeep. Specifically, this crater is defined as a series of boxe...
void notifyStep(double dt)
void buildInto(tgModel &model, tgWorld &world)
void addNode(double x, double y, double z, std::string tags="")
Definition: tgStructure.cpp:70