NTRT Simulator  Version: Master
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
FlemonsSpineModelLearning.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"
34 #include "tgcreator/tgBuildSpec.h"
37 #include "tgcreator/tgRodInfo.h"
38 #include "tgcreator/tgStructure.h"
40 #include "tgcreator/tgUtil.h"
41 // The Bullet Physics library
42 #include "LinearMath/btVector3.h"
43 // The C++ Standard Library
44 #include <algorithm> // std::fill
45 #include <iostream>
46 #include <map>
47 #include <set>
48 
49 #define USE_KINEMATIC
50 
51 FlemonsSpineModelLearning::FlemonsSpineModelLearning(int segments) :
52  BaseSpineModelLearning(segments)
53 {
54 }
55 
56 FlemonsSpineModelLearning::~FlemonsSpineModelLearning()
57 {
58 }
59 
61 {
62  // Rod and Muscle configuration
63  // Note: This needs to be high enough or things fly apart...
64  const double density = 4.2/300.0;
65  const double radius = 0.5;
66  const double friction = 0.5;
67  const double rollFriction = 0.0;
68  const double restitution = 0.0;
69  const tgRod::Config rodConfig(radius, density, friction, rollFriction, restitution);
70 
71  const double elasticity = 1000.0;
72  const double damping = 10.0;
73  const double pretension = 0.0;
74  const bool history = false;
75  const double maxTens = 7000.0;
76  const double maxSpeed = 12.0;
77 #ifdef USE_KINEMATIC
78 
79  const double mRad = 1.0;
80  const double motorFriction = 10.0;
81  const double motorInertia = 1.0;
82  const bool backDrivable = false;
83  tgKinematicActuator::Config motorConfig(elasticity, damping, pretension,
84  mRad, motorFriction, motorInertia, backDrivable,
85  history, maxTens, maxSpeed);
86 
87 #else
88  tgSpringCableActuator::Config muscleConfig(elasticity, damping, pretension, history, maxTens, maxSpeed);
89 #endif
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 #ifdef USE_KINEMATIC
162  spec.addBuilder("muscle", new tgKinematicActuatorInfo(motorConfig));
163 #else
164  spec.addBuilder("muscle", new tgBasicActuatorInfo(muscleConfig));
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:184
virtual void setup(tgWorld &world)
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.
Implementing the tetrahedral complex spine inspired by Tom Flemons.
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...
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.
virtual void step(double dt)
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