NTRT Simulator  Version: Master
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
FlemonsSpineModelMixed.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 "FlemonsSpineModelMixed.h"
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 FlemonsSpineModelMixed::FlemonsSpineModelMixed(int segments) :
52  BaseSpineModelLearning(segments)
53 {
54 }
55 
56 FlemonsSpineModelMixed::~FlemonsSpineModelMixed()
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 = true;
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)); //y=15.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  if (i == 1)
138  {
139  snake.addPair(n0[1], n1[1],
140  tgString("starting outer front muscle seg", i - 1) + tgString(" seg", i));
141  snake.addPair(n0[2], n1[2],
142  tgString("starting outer right muscle seg", i - 1) + tgString(" seg", i));
143  snake.addPair(n0[3], n1[3],
144  tgString("starting outer back muscle seg", i - 1) + tgString(" seg", i));
145  snake.addPair(n0[4], n1[4],
146  tgString("starting outer top muscle seg", i - 1) + tgString(" seg", i));
147 
148  snake.addPair(n0[2], n1[1],
149  tgString("starting inner front muscle seg", i - 1) + tgString(" seg", i));
150  snake.addPair(n0[2], n1[4],
151  tgString("starting inner right muscle seg", i - 1) + tgString(" seg", i));
152  snake.addPair(n0[3], n1[1],
153  tgString("starting inner left muscle seg", i - 1) + tgString(" seg", i));
154  snake.addPair(n0[3], n1[4],
155  tgString("starting inner back muscle seg", i - 1) + tgString(" seg", i));
156 
157  }
158  else if(i == children.size() - 1)
159  {
160  snake.addPair(n0[1], n1[1],
161  tgString("ending outer front muscle seg", i - 1) + tgString(" seg", i));
162  snake.addPair(n0[2], n1[2],
163  tgString("ending outer right muscle seg", i - 1) + tgString(" seg", i));
164  snake.addPair(n0[3], n1[3],
165  tgString("ending outer back muscle seg", i - 1) + tgString(" seg", i));
166  snake.addPair(n0[4], n1[4],
167  tgString("ending outer top muscle seg", i - 1) + tgString(" seg", i));
168 
169  snake.addPair(n0[2], n1[1],
170  tgString("ending inner front muscle seg", i - 1) + tgString(" seg", i));
171  snake.addPair(n0[2], n1[4],
172  tgString("ending inner right muscle seg", i - 1) + tgString(" seg", i));
173  snake.addPair(n0[3], n1[1],
174  tgString("ending inner left muscle seg", i - 1) + tgString(" seg", i));
175  snake.addPair(n0[3], n1[4],
176  tgString("ending inner back muscle seg", i - 1) + tgString(" seg", i));
177 
178  }
179  else
180  {
181  snake.addPair(n0[1], n1[1],
182  tgString("middle outer front muscle seg", i - 1) + tgString(" seg", i));
183  snake.addPair(n0[2], n1[2],
184  tgString("middle outer right muscle seg", i - 1) + tgString(" seg", i));
185  snake.addPair(n0[3], n1[3],
186  tgString("middle outer back muscle seg", i - 1) + tgString(" seg", i));
187  snake.addPair(n0[4], n1[4],
188  tgString("middle outer top muscle seg", i - 1) + tgString(" seg", i));
189 
190  snake.addPair(n0[2], n1[1],
191  tgString("middle inner front muscle seg", i - 1) + tgString(" seg", i));
192  snake.addPair(n0[2], n1[4],
193  tgString("middle inner right muscle seg", i - 1) + tgString(" seg", i));
194  snake.addPair(n0[3], n1[1],
195  tgString("middle inner left muscle seg", i - 1) + tgString(" seg", i));
196  snake.addPair(n0[3], n1[4],
197  tgString("middle inner back muscle seg", i - 1) + tgString(" seg", i));
198  }
199  }
200 #endif
201  // Create the build spec that uses tags to turn the structure into a real model
202  tgBuildSpec spec;
203  spec.addBuilder("rod", new tgRodInfo(rodConfig));
204 
205 #if (0)
206  spec.addBuilder("muscle", new tgKinematicContactCableInfo(motorConfig));
207 #else
208  spec.addBuilder("muscle", new tgBasicContactCableInfo(motorConfig));
209 #endif
210 
211  // Create your structureInfo
212  tgStructureInfo structureInfo(snake, spec);
213 
214  // Use the structureInfo to build ourselves
215  structureInfo.buildInto(*this, world);
216 
217  // Setup vectors for control
218  m_allMuscles = tgCast::filter<tgModel, tgSpringCableActuator> (getDescendants());
219 
220  m_allSegments = this->find<tgModel> ("segment");
221 
222 #if (0)
223  // Debug printing
224  std::cout << "StructureInfo:" << std::endl;
225  std::cout << structureInfo << std::endl;
226 
227  std::cout << "Model: " << std::endl;
228  std::cout << *this << std::endl;
229 #endif
230  children.clear();
231 
232  // Actually setup the children, notify controller
234 }
235 
237 {
238 
240 
241 }
242 
244 {
245  /* CPG update occurs in the controller so that we can decouple it
246  * from the physics update
247  */
248 
249  BaseSpineModelLearning::step(dt); // Step any children
250 }
const std::vector< tgStructure * > & getChildren() const
Definition: tgStructure.h:184
void addChild(tgStructure *child)
Definition of class tgRodInfo.
virtual void step(double dt)
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)
Definition of class tgBasicActuatorInfo.
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...
Definition of class tgBasicContactCableInfo.
std::string tgString(std::string s, int i)
Definition: tgString.h:33
Implementing the tetrahedral complex spine inspired by Tom Flemons.
Definition of class tgStructure.
Definition of class tgStructureInfo.
Definition of class tgKinematicActuatorInfo.
Rand seeding simular to the evolution and terrain classes.
virtual void setup(tgWorld &world)
Definition of class tgBuildSpec.
virtual void step(double dt)
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