NTRT Simulator  Version: Master
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
tgWall.cpp
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 "tgWall.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  5.0, // width (dm?)
54  1.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 Wall::Wall(btVector3 center) : tgModel() {
67  origin = btVector3(center.getX(), center.getY(), center.getZ());
68 }
69 
71 
72 void Wall::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 Wall::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 Wall::addNodes(tgStructure& s) {
118  const int nBoxes = 1;
119 
120  // Accumulating rotation on boxes
121  btVector3 rotationPoint = origin;
122  btVector3 rotationAxis = btVector3(0, 1, 0); // y-axis
123  double rotationAngle = 0.0;
124 
125  addBoxNodes();
126 
127  for(std::size_t i=0;i<nodes.size();i+=2) {
128  s.addNode(nodes[i]);
129  s.addNode(nodes[i+1]);
130 
131  s.addPair(i, i+1, "box");
132  }
133  s.addRotation(rotationPoint, rotationAxis, rotationAngle);
134  s.move(origin); // Sink boxes into the ground
135 }
136 
137 void Wall::addBoxNodes() {
138  tgNode node;
139  node = tgNode(-100.0, 0.0, 0.0, "node");
140  //node.addRotation(rotationPoint, rotationAxis, rotationAngle);
141  nodes.push_back(node);
142 
143  node = tgNode(100.0, 0.0, 0.0, "node");
144  //node.addRotation(rotationPoint, rotationAxis, rotationAngle);
145  nodes.push_back(node);
146 
147 }
148 
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 setup(tgWorld &world)
Definition: tgWall.cpp:72
Wall()
Definition: tgWall.cpp:62
virtual void step(double dt)
Definition: tgModel.cpp:84
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 addRotation(const btVector3 &fixedPoint, const btVector3 &axis, double angle)
virtual void onVisit(tgModelVisitor &r)
Definition: tgWall.cpp:107
virtual void step(double dt)
Definition: tgWall.cpp:96
Definition of class tgNode.
Definition: tgNode.h:45
Definition of class tgStructure.
Definition of class tgStructureInfo.
virtual ~Wall()
Definition: tgWall.cpp:70
Definition of class tgBuildSpec.
void teardown()
Definition: tgWall.cpp:111
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