NTRT Simulator
 All Classes 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"
32 #include "core/tgLinearString.h"
33 #include "core/tgString.h"
34 #include "tgcreator/tgBuildSpec.h"
36 #include "tgcreator/tgRodInfo.h"
37 #include "tgcreator/tgStructure.h"
39 #include "tgcreator/tgUtil.h"
40 // The Bullet Physics library
41 #include "LinearMath/btVector3.h"
42 // The C++ Standard Library
43 #include <algorithm> // std::fill
44 #include <iostream>
45 #include <map>
46 #include <set>
47 
48 FlemonsSpineModelLearning::FlemonsSpineModelLearning(int segments) :
49  BaseSpineModelLearning(segments)
50 {
51 }
52 
53 FlemonsSpineModelLearning::~FlemonsSpineModelLearning()
54 {
55 }
56 
58 {
59  // This is basically a manual setup of a model.
60  // There are things that do this for us
62 
63  // Rod and Muscle configuration
64  // Note: This needs to be high enough or things fly apart...
65  const double density = 4.2/300.0;
66  const double radius = 0.5;
67  const tgRod::Config rodConfig(radius, density);
68 
69  tgLinearString::Config muscleConfig(1000, 10, false, 0, 7000, 12, 4000);
70 
71  // Calculations for the flemons spine model
72  double v_size = 10.0;
73 
74  // Create the tetrahedra
75  tgStructure tetra;
76 
77  tetra.addNode(0.0, 0.0, 0.0); // center
78  tetra.addNode( v_size, v_size, v_size); // front
79  tetra.addNode( v_size, -v_size, -v_size); // right
80  tetra.addNode(-v_size, v_size, -v_size); // back
81  tetra.addNode(-v_size, -v_size, v_size); // left
82 
83  tetra.addPair(0, 1, "front rod");
84  tetra.addPair(0, 2, "right rod");
85  tetra.addPair(0, 3, "back rod");
86  tetra.addPair(0, 4, "left rod");
87 
88  // Move the first one so we can create a longer snake.
89  // Or you could move the snake at the end, up to you.
90  tetra.move(btVector3(0.0,15.0,100.0));
91 
92  // Create our snake segments
93  tgStructure snake;
96  btVector3 offset(0.0, 0.0, -v_size * 1.15);
97  for (std::size_t i = 0; i < m_segments; i++)
98  {
102  tgStructure* const p = new tgStructure(tetra);
103  p->addTags(tgString("segment num", i + 1));
104  p->move((i + 1.0) * offset);
105  snake.addChild(p); // Add a child to the snake
106  }
107  //conditionally compile for debugging
108 #if (1)
109  // Add muscles that connect the segments
110  // Tag the muscles with their segment numbers so CPGs can find
111  // them.
112  std::vector<tgStructure*> children = snake.getChildren();
113  for (std::size_t i = 1; i < children.size(); i++)
114  {
115  tgNodes n0 = children[i - 1]->getNodes();
116  tgNodes n1 = children[i]->getNodes();
117 
118  snake.addPair(n0[1], n1[1],
119  tgString("outer front muscle seg", i - 1) + tgString(" seg", i));
120  snake.addPair(n0[2], n1[2],
121  tgString("outer right muscle seg", i - 1) + tgString(" seg", i));
122  snake.addPair(n0[3], n1[3],
123  tgString("outer back muscle seg", i - 1) + tgString(" seg", i));
124  snake.addPair(n0[4], n1[4],
125  tgString("outer top muscle seg", i - 1) + tgString(" seg", i));
126 
127  snake.addPair(n0[2], n1[1],
128  tgString("inner front muscle seg", i - 1) + tgString(" seg", i));
129  snake.addPair(n0[2], n1[4],
130  tgString("inner right muscle seg", i - 1) + tgString(" seg", i));
131  snake.addPair(n0[3], n1[1],
132  tgString("inner left muscle seg", i - 1) + tgString(" seg", i));
133  snake.addPair(n0[3], n1[4],
134  tgString("inner back muscle seg", i - 1) + tgString(" seg", i));
135 
136  }
137 #endif
138  // Create the build spec that uses tags to turn the structure into a real model
139  tgBuildSpec spec;
140  spec.addBuilder("rod", new tgRodInfo(rodConfig));
141 
142 #if (1)
143  spec.addBuilder("muscle", new tgLinearStringInfo(muscleConfig));
144 #endif
145 
146  // Create your structureInfo
147  tgStructureInfo structureInfo(snake, spec);
148 
149  // Use the structureInfo to build ourselves
150  structureInfo.buildInto(*this, world);
151 
152  // Setup vectors for control
153  m_allMuscles = tgCast::filter<tgModel, tgLinearString> (getDescendants());
154 
155  m_allSegments = this->find<tgModel> ("segment");
156 
157 #if (0)
158  // Debug printing
159  std::cout << "StructureInfo:" << std::endl;
160  std::cout << structureInfo << std::endl;
161 
162  std::cout << "Model: " << std::endl;
163  std::cout << *this << std::endl;
164 #endif
165  children.clear();
166 
167  // Actually setup the children, notify controller
169 }
170 
172 {
173 
175 
176 }
177 
179 {
180  /* CPG update occurs in the controller so that we can decouple it
181  * from the physics update
182  */
183 
184  BaseSpineModelLearning::step(dt); // Step any children
185 }
const std::vector< tgStructure * > & getChildren() const
Definition: tgStructure.h:130
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)
void addPair(int fromNodeIdx, int toNodeIdx, std::string tags="")
Definition: tgStructure.cpp:65
Contains the definition of class tgLinearString.
std::string tgString(std::string s, int i)
Definition: tgString.h:32
Definition of class tgStructure.
Definition of class tgStructureInfo.
Definition of class tgLinearStringInfo.
Contains the definition of class tgUtil and overloaded operator<<() free functions.
Definition of class tgBuildSpec.
virtual void step(double dt)
std::vector< tgModel * > getDescendants() const
Definition: tgModel.cpp:174
void addNode(double x, double y, double z, std::string tags="")
Definition: tgStructure.cpp:55