NTRT Simulator  Version: Master
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
tgStairs.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 "tgStairs.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 #include <numeric> // RAND_MAX
40 
41 tgStairs::Config::Config(btVector3 origin,
42  btScalar friction,
43  btScalar restitution,
44  size_t nBlocks,
45  double stairWidth,
46  double stepWidth,
47  double stepHeight,
48  double angle) :
49 m_origin(origin),
50 m_friction(friction),
51 m_restitution(restitution),
52 m_nBlocks(nBlocks),
53 m_length(stairWidth),
54 m_width(stepWidth),
55 m_height(stepHeight),
56 m_angle(angle)
57 {
58  assert(m_friction >= 0.0);
59  assert(m_restitution >= 0.0);
60  assert(m_nBlocks >= 0.0);
61  assert(m_length >= 0.0);
62  assert(m_width >= 0.0);
63  assert(m_height >= 0.0);
64 }
65 
67 tgModel(),
68 m_config()
69 {
70 }
71 
73 tgModel(),
74 m_config(config)
75 {
76 }
77 
79 
80 void tgStairs::setup(tgWorld& world) {
81  // Density and roll friction are set to zero (respectively)
82  const tgBox::Config boxConfig(m_config.m_width / 2.0, m_config.m_height / 2.0, 0.0, m_config.m_friction, 0.0, m_config.m_restitution);
83 
84  // Start creating the structure
85  tgStructure s;
86  addNodes(s);
87 
88  // Create the build spec that uses tags to turn the structure into a real model
89  tgBuildSpec spec;
90  spec.addBuilder("box", new tgBoxInfo(boxConfig));
91 
92  // Create your structureInfo
93  tgStructureInfo structureInfo(s, spec);
94 
95  // Use the structureInfo to build ourselves
96  structureInfo.buildInto(*this, world);
97 
98  // Actually setup the children
99  tgModel::setup(world);
100 }
101 
102 void tgStairs::step(double dt) {
103  // Precondition
104  if (dt <= 0.0) {
105  throw std::invalid_argument("dt is not positive");
106  }
107  else {
108  tgModel::step(dt); // Step any children
109  }
110 }
111 
113  tgModel::onVisit(r);
114 }
115 
118 }
119 
120 // Nodes: center points of opposing faces of rectangles
121 void tgStairs::addNodes(tgStructure& s) {
122 
123  tgNode position1(0.0, 0.0, 0.0);
124  tgNode position2(0.0, 0.0, m_config.m_length);
125  btVector3 offset(m_config.m_width, m_config.m_height, 0.0);
126 
127  for(size_t i = 0; i < 2 * m_config.m_nBlocks; i += 2) {
128  s.addNode(position1);
129  s.addNode(position2);
130 
131  s.addPair(i, i+1, "box");
132 
133  position1 += offset;
134  position2 += offset;
135  }
136 
137  btVector3 point(0.0, 0.0, 0.0);
138  btVector3 axis(0.0, 1.0, 0.0);
139  s.addRotation(point, axis, m_config.m_angle);
140 
141  s.move(m_config.m_origin); // Set center of field to desired origin position
142 }
143 
btScalar m_restitution
Definition: tgStairs.h: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
tgStairs()
Definition: tgStairs.cpp:66
virtual ~tgStairs()
Definition: tgStairs.cpp:78
double m_width
Definition: tgStairs.h:79
btVector3 m_origin
Definition: tgStairs.h:64
void teardown()
Definition: tgStairs.cpp:116
virtual void step(double dt)
Definition: tgModel.cpp:84
Contains the definition of class tgStairs. A random field of blocks used to test tensegrities.
double m_angle
Definition: tgStairs.h:85
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 onVisit(tgModelVisitor &r)
Definition: tgStairs.cpp:112
void addRotation(const btVector3 &fixedPoint, const btVector3 &axis, double angle)
size_t m_nBlocks
Definition: tgStairs.h:73
Definition of class tgNode.
Definition: tgNode.h:45
Definition of class tgStructure.
double m_length
Definition: tgStairs.h:76
Definition of class tgStructureInfo.
double m_height
Definition: tgStairs.h:82
btScalar m_friction
Definition: tgStairs.h:67
virtual void step(double dt)
Definition: tgStairs.cpp:102
Definition of class tgBuildSpec.
virtual void setup(tgWorld &world)
Definition: tgStairs.cpp:80
void buildInto(tgModel &model, tgWorld &world)
void addNode(double x, double y, double z, std::string tags="")
Definition: tgStructure.cpp:70