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