NTRT Simulator  v1.1
 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  const int nBoxes = 4;
120 
121  // Accumulating rotation on boxes
122  btVector3 rotationPoint = origin;
123  btVector3 rotationAxis = btVector3(0, 1, 0); // y-axis
124  double rotationAngle = M_PI/2;
125 
126  addBoxNodes();
127  addBoxNodes();
128  addBoxNodes();
129  addBoxNodes();
130 
131  for(int i=0;i<nodes.size();i+=2) {
132  s.addNode(nodes[i]);
133  s.addNode(nodes[i+1]);
134  s.addRotation(rotationPoint, rotationAxis, rotationAngle);
135  s.addPair(i, i+1, "box");
136  }
137  s.move(btVector3(0, -5, 0)); // Sink boxes into the ground
138 }
139 
140 void tgCraterShallow::addBoxNodes() {
141  tgNode node;
142  const double shift = 20; // Arbitrary
143  const double vshift = 2;
144  const double node_h = c.height/2 + vshift;
145  const double node_w = c.width/2;
146 
147  double x1 = -shift-node_w;
148  double x2 = shift+node_w;
149  double y1 = -node_h;
150  double y2 = node_h;
151  double z1 = -shift-node_w;
152  double z2 = shift+node_w;
153 
154  btVector3 rotationPoint = btVector3((x2-x1)/2, (y2-y1)/2, (z2-z1)/2); //Halfway between nodes
155  btVector3 rotationAxis = btVector3(0, 1, 0); // y-axis
156  double rotationAngle = M_PI/4;
157 
158  node = tgNode(x1, y1, z1, "node");
159  node.addRotation(rotationPoint, rotationAxis, rotationAngle);
160  nodes.push_back(node);
161 
162  node = tgNode(x2, y2, z2, "node");
163  node.addRotation(rotationPoint, rotationAxis, rotationAngle);
164  nodes.push_back(node);
165 
166 }
167 
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:66
virtual void step(double dt)
void addRotation(const btVector3 &fixedPoint, const btVector3 &axis, double angle)
Definition: tgStructure.cpp:99
Definition of class tgNode.
Definition: tgNode.h:45
Definition of class tgStructure.
Definition of class tgStructureInfo.
Definition of class tgBuildSpec.
void addNode(double x, double y, double z, std::string tags="")
Definition: tgStructure.cpp:56