NTRT Simulator  Version: Master
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
FlemonsSpineModelGoal.cpp
Go to the documentation of this file.
1 /*
2  * Copyright © 2012, 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 
28 // This module
29 #include "FlemonsSpineModelGoal.h"
30 // This library
31 #include "core/tgCast.h"
33 #include "core/tgString.h"
34 #include "core/abstractMarker.h"
37 #include "tgcreator/tgBuildSpec.h"
39 #include "tgcreator/tgBoxInfo.h"
41 #include "tgcreator/tgRodInfo.h"
42 #include "tgcreator/tgStructure.h"
44 #include "tgcreator/tgUtil.h"
45 // The Bullet Physics library
46 #include "LinearMath/btVector3.h"
47 // The C++ Standard Library
48 #include <algorithm> // std::fill
49 #include <iostream>
50 #include <map>
51 #include <set>
52 
53 namespace
54 {
55  void addMarkers(tgStructure& structure, FlemonsSpineModelGoal& model, int segments)
56  {
57 
58  const std::vector<tgStructure*> children = structure.getChildren();
59 
60 
61  for(int i = 0; i < segments; i++)
62  {
63  std::cout << "Rigid size " << model.getAllRigids().size() << std::endl;
64  btRigidBody* firstBody = model.getAllRigids()[4 * i]->getPRigidBody();
65  tgNodes n0 = children[i]->getNodes();
66 
67  abstractMarker marker1(firstBody, n0[2] - firstBody->getCenterOfMassPosition (), btVector3(1, 0, 0), 0);
68 
69  model.addMarker(marker1);
70 
71  abstractMarker marker2(firstBody, n0[4] - firstBody->getCenterOfMassPosition (), btVector3(1, 0, 0), 0);
72 
73  model.addMarker(marker2);
74  }
75 
76  }
77 
78 }
79 
80 FlemonsSpineModelGoal::FlemonsSpineModelGoal(int segments, double goalAngle, double startAngle) :
81  BaseSpineModelGoal(segments, goalAngle),
82  m_startAngle(startAngle)
83 {
84 }
85 
86 FlemonsSpineModelGoal::~FlemonsSpineModelGoal()
87 {
88 }
89 
91 {
92  // This is basically a manual setup of a model.
93  // There are things that do this for us
95 
96  // Rod and Muscle configuration
97  // Note: This needs to be high enough or things fly apart...
98  const double density = 4.2/300.0;
99  const double radius = 0.5;
100  const double friction = 0.5;
101  const double rollFriction = 0.0;
102  const double restitution = 0.0;
103  const tgRod::Config rodConfig(radius, density, friction, rollFriction, restitution);
104 
105  const double elasticity = 1000.0;
106  const double damping = 10.0;
107  const double pretension = 0.0;
108  const bool history = true;
109  const double maxTens = 7000.0;
110  const double maxSpeed = 12.0;
111 
112  const double mRad = 1.0;
113  const double motorFriction = 10.0;
114  const double motorInertia = 1.0;
115  const bool backDrivable = false;
116  tgKinematicActuator::Config motorConfig(elasticity, damping, pretension,
117  mRad, motorFriction, motorInertia, backDrivable,
118  history, maxTens, maxSpeed);
119 
120  // Calculations for the flemons spine model
121  double v_size = 10.0;
122 
123  // Create the tetrahedra
124  tgStructure tetra;
125 
126  tetra.addNode(0.0, 0.0, 0.0); // center
127  tetra.addNode( v_size, v_size, v_size); // front
128  tetra.addNode( v_size, -v_size, -v_size); // right
129  tetra.addNode(-v_size, v_size, -v_size); // back
130  tetra.addNode(-v_size, -v_size, v_size); // left
131 
132  tetra.addPair(0, 1, "front rod");
133  tetra.addPair(0, 2, "right rod");
134  tetra.addPair(0, 3, "back rod");
135  tetra.addPair(0, 4, "left rod");
136 
137  // Move the first one so we can create a longer snake.
138  // Or you could move the snake at the end, up to you.
139  tetra.move(btVector3(0.0,15.0,50.0));
140 
141  // Create our snake segments
142  tgStructure snake;
145  btVector3 offset(0.0, 0.0, -v_size * 1.15);
146  for (std::size_t i = 0; i < m_segments; i++)
147  {
151  tgStructure* const p = new tgStructure(tetra);
152  p->addTags(tgString("segment num", i + 1));
153  p->move((i + 1.0) * offset);
154  snake.addChild(p); // Add a child to the snake
155  }
156  //conditionally compile for debugging
157 #if (1)
158  // Add muscles that connect the segments
159  // Tag the muscles with their segment numbers so CPGs can find
160  // them.
161  std::vector<tgStructure*> children = snake.getChildren();
162  for (std::size_t i = 1; i < children.size(); i++)
163  {
164  tgNodes n0 = children[i - 1]->getNodes();
165  tgNodes n1 = children[i]->getNodes();
166 
167  snake.addPair(n0[1], n1[1],
168  tgString("outer front muscle seg", i));
169  snake.addPair(n0[2], n1[2],
170  tgString("outer right muscle seg", i));
171  snake.addPair(n0[3], n1[3],
172  tgString("outer back muscle seg", i));
173  snake.addPair(n0[4], n1[4],
174  tgString("outer top muscle seg", i));
175 
176  snake.addPair(n0[2], n1[1],
177  tgString("inner front muscle seg", i));
178  snake.addPair(n0[2], n1[4],
179  tgString("inner right muscle seg", i));
180  snake.addPair(n0[3], n1[1],
181  tgString("inner left muscle seg", i));
182  snake.addPair(n0[3], n1[4],
183  tgString("inner back muscle seg", i));
184 
185  }
186 #endif
187 
188  btVector3 fixedPoint(0.0, 0.0, 0.0);
189  btVector3 axis(0.0, 1.0, 0.0);
190 
191  snake.addRotation(fixedPoint, axis, m_startAngle);
192 
193  // Create the build spec that uses tags to turn the structure into a real model
194  tgBuildSpec spec;
195  spec.addBuilder("rod", new tgRodInfo(rodConfig));
196 
197 #if (1)
198  spec.addBuilder("muscle", new tgKinematicContactCableInfo(motorConfig));
199 #else
200  spec.addBuilder("muscle", new tgBasicContactCableInfo(motorConfig));
201 #endif
202 
203  // Create your structureInfo
204  tgStructureInfo structureInfo(snake, spec);
205 
206  // Use the structureInfo to build ourselves
207  structureInfo.buildInto(*this, world);
208 
209  // Setup vectors for control
210  m_allMuscles = tgCast::filter<tgModel, tgSpringCableActuator> (getDescendants());
211 
212  m_allSegments = this->find<tgModel> ("segment");
213 
214  //addMarkers(snake, *this, m_segments);
215 
216 #if (0)
217  // Debug printing
218  std::cout << "StructureInfo:" << std::endl;
219  std::cout << structureInfo << std::endl;
220 
221  std::cout << "Model: " << std::endl;
222  std::cout << *this << std::endl;
223 
224  std::cout << "Spine Length: " << getSpineLength() << std::endl;
225 #endif
226 
227  std::vector<tgBaseRigid*> myRigids = this->getAllRigids();
228 #if (1)
229  for (int i =0; i < myRigids.size(); i++)
230  {
231  std::cout << myRigids[i]->mass() << " " <<myRigids[i]->getPRigidBody() << std::endl;
232  }
233 #endif
234 
235  children.clear();
236 
237  // Actually setup the children, notify controller
239 }
240 
242 {
243 
245 
246 }
247 
249 {
250  /* CPG update occurs in the controller so that we can decouple it
251  * from the physics update
252  */
253 
254  BaseSpineModelGoal::step(dt); // Step any children
255 }
virtual void setup(tgWorld &world)
const std::vector< tgStructure * > & getChildren() const
Definition: tgStructure.h:184
void addChild(tgStructure *child)
Definition of class tgRodInfo.
virtual void setup(tgWorld &world)
Convenience function for combining strings with ints, mostly for naming structures.
Utility class for class casting and filtering collections by type.
Implementing the tetrahedral complex spine inspired by Tom Flemons.
Markers for specific places on a tensegrity.
virtual void step(double dt)
Definition of class tgBasicActuatorInfo.
Class that interfaces with Bullet to build the boxes.
void addPair(int fromNodeIdx, int toNodeIdx, std::string tags="")
Definition: tgStructure.cpp:80
Contains the definition of abstract base class tgSpringCableActuator. Assumes that the string is line...
virtual void step(double dt)
void addRotation(const btVector3 &fixedPoint, const btVector3 &axis, double angle)
Definition of class tgBasicContactCableInfo.
std::string tgString(std::string s, int i)
Definition: tgString.h:33
Definition of class tgStructure.
Definition of class tgStructureInfo.
Definition of class tgKinematicActuatorInfo.
Rand seeding simular to the evolution and terrain classes.
Definition of class tgBuildSpec.
Definition of class tgKinematicContactCableInfo.
std::vector< tgModel * > getDescendants() const
Definition: tgModel.cpp:170
void buildInto(tgModel &model, tgWorld &world)
void addNode(double x, double y, double z, std::string tags="")
Definition: tgStructure.cpp:70