NTRT Simulator  v1.1
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
Crater.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 "Crater.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  10.0, // width (dm?)
54  10.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  origin = btVector3(0,0,0);
64 }
65 
66 Crater::Crater(btVector3 center) : tgModel() {
67  origin = btVector3(center.getX(), center.getY(), center.getZ());
68 }
69 
71 
72 void Crater::setup(tgWorld& world) {
73  const tgBox::Config boxConfig(c.width, c.height, c.density, c.friction, c.rollFriction, c.restitution);
74 
75  // Start creating the structure
76  tgStructure s;
77  addNodes(s);
78 
79  // Create the build spec that uses tags to turn the structure into a real model
80  tgBuildSpec spec;
81  spec.addBuilder("box", new tgBoxInfo(boxConfig));
82 
83  // Create your structureInfo
84  tgStructureInfo structureInfo(s, spec);
85 
86  // Use the structureInfo to build ourselves
87  structureInfo.buildInto(*this, world);
88 
89  // call the onSetup methods of all observed things e.g. controllers
90  notifySetup();
91 
92  // Actually setup the children
93  tgModel::setup(world);
94 }
95 
96 void Crater::step(double dt) {
97  // Precondition
98  if (dt <= 0.0) {
99  throw std::invalid_argument("dt is not positive");
100  }
101  else { //Notify observers (controllers) of the step so that they can take action
102  notifyStep(dt);
103  tgModel::step(dt); // Step any children
104  }
105 }
106 
108  tgModel::onVisit(r);
109 }
110 
112  notifyTeardown();
114 }
115 
116 // Nodes: center points of opposing faces of rectangles
117 void Crater::addNodes(tgStructure& s) {
118  const int nBoxes = 4;
119 
120  // Accumulating rotation on boxes
121  btVector3 rotationPoint = origin;
122  btVector3 rotationAxis = btVector3(0, 1, 0); // y-axis
123  double rotationAngle = M_PI/2;
124 
125  addBoxNodes();
126  addBoxNodes();
127  addBoxNodes();
128  addBoxNodes();
129 
130  for(int i=0;i<nodes.size();i+=2) {
131  s.addNode(nodes[i]);
132  s.addNode(nodes[i+1]);
133  s.addRotation(rotationPoint, rotationAxis, rotationAngle);
134  s.addPair(i, i+1, "box");
135  }
136  s.move(btVector3(0, -5, 0)); // Sink boxes into the ground
137 }
138 
139 void Crater::addBoxNodes() {
140  tgNode node;
141  const double shift = 20; // Arbitrary
142  const double vshift = 2;
143  const double node_h = c.height/2 + vshift;
144  const double node_w = c.width/2;
145 
146  double x1 = -shift-node_w;
147  double x2 = shift+node_w;
148  double y1 = -node_h;
149  double y2 = node_h;
150  double z1 = -shift-node_w;
151  double z2 = shift+node_w;
152 
153  btVector3 rotationPoint = btVector3((x2-x1)/2, (y2-y1)/2, (z2-z1)/2); //Halfway between nodes
154  btVector3 rotationAxis = btVector3(0, 1, 0); // y-axis
155  double rotationAngle = M_PI/4;
156 
157  node = tgNode(x1, y1, z1, "node");
158  node.addRotation(rotationPoint, rotationAxis, rotationAngle);
159  nodes.push_back(node);
160 
161  node = tgNode(x2, y2, z2, "node");
162  node.addRotation(rotationPoint, rotationAxis, rotationAngle);
163  nodes.push_back(node);
164 
165 }
166 
virtual void onVisit(tgModelVisitor &r)
Definition: Crater.cpp:107
virtual ~Crater()
Definition: Crater.cpp:70
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
void addRotation(const btVector3 &fixedPoint, const btVector3 &fromOrientation, const btVector3 &toOrientation)
Definition: tgNode.h:62
virtual void step(double dt)
Definition: Crater.cpp:96
virtual void step(double dt)
Definition: tgModel.cpp:84
virtual void setup(tgWorld &world)
Definition: Crater.cpp:72
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:66
void addRotation(const btVector3 &fixedPoint, const btVector3 &axis, double angle)
Definition: tgStructure.cpp:99
Contains the definition of class Crater. Specifically, a crater is defined as a series of boxes which...
Definition of class tgNode.
Definition: tgNode.h:45
Definition of class tgStructure.
Definition of class tgStructureInfo.
void teardown()
Definition: Crater.cpp:111
Definition of class tgBuildSpec.
void notifyStep(double dt)
Crater()
Definition: Crater.cpp:62
void addNode(double x, double y, double z, std::string tags="")
Definition: tgStructure.cpp:56