NTRT Simulator  v1.1
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
FlemonsSpineModelContact.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
30 // This library
31 #include "core/tgCast.h"
33 #include "core/tgString.h"
36 #include "tgcreator/tgBuildSpec.h"
39 #include "tgcreator/tgRodInfo.h"
40 #include "tgcreator/tgStructure.h"
42 #include "tgcreator/tgUtil.h"
43 // The Bullet Physics library
44 #include "LinearMath/btVector3.h"
45 // The C++ Standard Library
46 #include <algorithm> // std::fill
47 #include <iostream>
48 #include <map>
49 #include <set>
50 
51 FlemonsSpineModelContact::FlemonsSpineModelContact(int segments) :
52  BaseSpineModelLearning(segments)
53 {
54 }
55 
56 FlemonsSpineModelContact::~FlemonsSpineModelContact()
57 {
58 }
59 
61 {
62  // This is basically a manual setup of a model.
63  // There are things that do this for us
65 
66  // Rod and Muscle configuration
67  // Note: This needs to be high enough or things fly apart...
68  const double density = 4.2/300.0;
69  const double radius = 0.5;
70  const double friction = 0.5;
71  const double rollFriction = 0.0;
72  const double restitution = 0.0;
73  const tgRod::Config rodConfig(radius, density, friction, rollFriction, restitution);
74 
75  const double elasticity = 1000.0;
76  const double damping = 10.0;
77  const double pretension = 0.0;
78  const bool history = false;
79  const double maxTens = 7000.0;
80  const double maxSpeed = 12.0;
81 
82  const double mRad = 1.0;
83  const double motorFriction = 10.0;
84  const double motorInertia = 1.0;
85  const bool backDrivable = false;
86  tgKinematicActuator::Config motorConfig(elasticity, damping, pretension,
87  mRad, motorFriction, motorInertia, backDrivable,
88  history, maxTens, maxSpeed);
89 
90  // Calculations for the flemons spine model
91  double v_size = 10.0;
92 
93  // Create the tetrahedra
94  tgStructure tetra;
95 
96  tetra.addNode(0.0, 0.0, 0.0); // center
97  tetra.addNode( v_size, v_size, v_size); // front
98  tetra.addNode( v_size, -v_size, -v_size); // right
99  tetra.addNode(-v_size, v_size, -v_size); // back
100  tetra.addNode(-v_size, -v_size, v_size); // left
101 
102  tetra.addPair(0, 1, "front rod");
103  tetra.addPair(0, 2, "right rod");
104  tetra.addPair(0, 3, "back rod");
105  tetra.addPair(0, 4, "left rod");
106 
107  // Move the first one so we can create a longer snake.
108  // Or you could move the snake at the end, up to you.
109  tetra.move(btVector3(0.0,15.0,100.0));
110 
111  // Create our snake segments
112  tgStructure snake;
115  btVector3 offset(0.0, 0.0, -v_size * 1.15);
116  for (std::size_t i = 0; i < m_segments; i++)
117  {
121  tgStructure* const p = new tgStructure(tetra);
122  p->addTags(tgString("segment num", i + 1));
123  p->move((i + 1.0) * offset);
124  snake.addChild(p); // Add a child to the snake
125  }
126  //conditionally compile for debugging
127 #if (1)
128  // Add muscles that connect the segments
129  // Tag the muscles with their segment numbers so CPGs can find
130  // them.
131  std::vector<tgStructure*> children = snake.getChildren();
132  for (std::size_t i = 1; i < children.size(); i++)
133  {
134  tgNodes n0 = children[i - 1]->getNodes();
135  tgNodes n1 = children[i]->getNodes();
136 
137  snake.addPair(n0[1], n1[1],
138  tgString("outer front muscle seg", i - 1) + tgString(" seg", i));
139  snake.addPair(n0[2], n1[2],
140  tgString("outer right muscle seg", i - 1) + tgString(" seg", i));
141  snake.addPair(n0[3], n1[3],
142  tgString("outer back muscle seg", i - 1) + tgString(" seg", i));
143  snake.addPair(n0[4], n1[4],
144  tgString("outer top muscle seg", i - 1) + tgString(" seg", i));
145 
146  snake.addPair(n0[2], n1[1],
147  tgString("inner front muscle seg", i - 1) + tgString(" seg", i));
148  snake.addPair(n0[2], n1[4],
149  tgString("inner right muscle seg", i - 1) + tgString(" seg", i));
150  snake.addPair(n0[3], n1[1],
151  tgString("inner left muscle seg", i - 1) + tgString(" seg", i));
152  snake.addPair(n0[3], n1[4],
153  tgString("inner back muscle seg", i - 1) + tgString(" seg", i));
154 
155  }
156 #endif
157  // Create the build spec that uses tags to turn the structure into a real model
158  tgBuildSpec spec;
159  spec.addBuilder("rod", new tgRodInfo(rodConfig));
160 
161 #if (1)
162  spec.addBuilder("muscle", new tgKinematicContactCableInfo(motorConfig));
163 #else
164  spec.addBuilder("muscle", new tgBasicContactCableInfo(motorConfig));
165 #endif
166 
167  // Create your structureInfo
168  tgStructureInfo structureInfo(snake, spec);
169 
170  // Use the structureInfo to build ourselves
171  structureInfo.buildInto(*this, world);
172 
173  // Setup vectors for control
174  m_allMuscles = tgCast::filter<tgModel, tgSpringCableActuator> (getDescendants());
175 
176  m_allSegments = this->find<tgModel> ("segment");
177 
178 #if (0)
179  // Debug printing
180  std::cout << "StructureInfo:" << std::endl;
181  std::cout << structureInfo << std::endl;
182 
183  std::cout << "Model: " << std::endl;
184  std::cout << *this << std::endl;
185 #endif
186  children.clear();
187 
188  // Actually setup the children, notify controller
190 }
191 
193 {
194 
196 
197 }
198 
200 {
201  /* CPG update occurs in the controller so that we can decouple it
202  * from the physics update
203  */
204 
205  BaseSpineModelLearning::step(dt); // Step any children
206 }
const std::vector< tgStructure * > & getChildren() const
Definition: tgStructure.h:131
void addChild(tgStructure *child)
Definition of class tgRodInfo.
Convenience function for combining strings with ints, mostly for naming structures.
Utility class for class casting and filtering collections by type.
virtual void setup(tgWorld &world)
virtual void setup(tgWorld &world)
Implementing the tetrahedral complex spine inspired by Tom Flemons.
Definition of class tgBasicActuatorInfo.
void addPair(int fromNodeIdx, int toNodeIdx, std::string tags="")
Definition: tgStructure.cpp:66
Contains the definition of abstract base class tgSpringCableActuator. Assumes that the string is line...
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.
Contains the definition of class tgUtil and overloaded operator<<() free functions.
Definition of class tgBuildSpec.
virtual void step(double dt)
Definition of class tgKinematicContactCableInfo.
std::vector< tgModel * > getDescendants() const
Definition: tgModel.cpp:172
void addNode(double x, double y, double z, std::string tags="")
Definition: tgStructure.cpp:56