NTRT Simulator  Version: Master
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
tgRBStringInfo.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 #include "tgRBStringInfo.h"
29 
30 #include "btBulletDynamicsCommon.h"
31 #include <iostream>
32 
33 #include "tgcreator/tgRodInfo.h"
35 #include "tgcreator/tgLinearStringInfo.h"
36 //#include "../tgRigidAutoCompound.h"
37 #include "tgcreator/tgBuildSpec.h"
38 
39 #include "tgcreator/tgStructure.h"
41 #include "tgcreator/tgNode.h"
42 #include "tgcreator/tgPair.h"
43 
44 #include "core/tgBaseString.h"
45 #include "core/tgRod.h"
46 #include "tgcreator/tgUtil.h"
47 #include "core/tgString.h"
48 
49 tgRBStringInfo::tgRBStringInfo(const tgRBString::Config& config) :
50  m_config(config),
52 {
53 }
54 
55 tgRBStringInfo::tgRBStringInfo(const tgRBString::Config& config, tgTags tags) :
56  m_config(config),
57  tgConnectorInfo(tags)
58 {}
59 
60 tgRBStringInfo::tgRBStringInfo(const tgRBString::Config& config, const tgPair& pair) :
61  m_config(config),
62  tgConnectorInfo(pair)
63 {}
64 
65 tgModel* tgRBStringInfo::createModel(tgWorld& world)
66 {
67  tgNode startNode = this->getFrom();
68  tgNode endNode = this->getTo();
69 
70  btVector3 buildVec = (endNode - startNode);
71  double startLength = buildVec.length();
72 
73  tgRBString* thisString = new tgRBString(getTags(), m_config, startLength);
74  buildModel(world, thisString);
75  return thisString;
76 }
77 
78 void tgRBStringInfo::buildModel(tgWorld& world, tgModel* thisString)
79 {
80  // get global from _ to - still need these is both places I think.
81  tgNode startNode = this->getFrom();
82  tgNode endNode = this->getTo();
83 
84  // FIRST create intermedate body
85 
86  // todo - figure out a way to change only the one parameter with less code
87  const double stiffness = m_config.m_stringConfig.stiffness;
88  const double damping = m_config.m_stringConfig.damping;
89  tgLinearString::Config muscleConfig1(stiffness, damping, false, -M_PI / 2.0);
90  tgLinearString::Config muscleConfig2(stiffness, damping, false, M_PI / 2.0);
91  tgLinearString::Config muscleConfig3(stiffness, damping, false, M_PI);
92  tgLinearString::Config muscleConfig4(stiffness, damping, false, 0);
93 
94  // Calculate the size of the rods
95  double v_size = m_config.m_minTotalLength / (double) m_config.m_segments;
96 
97  // tgNode is a subclass, so this should be fine
98  btVector3 buildVec = (endNode - startNode);
99  double startLength = buildVec.length();
100 
101  double spacing = (startLength - v_size / 2.0) / (double) m_config.m_segments;
102  // After this buildVec stays normalized.
103  btVector3 offset = buildVec.normalize() * spacing;
104 
105  // Create the sub rods
106  tgStructure strRod;
107 
108  strRod.addNode(0,0,0); // Bottom
109  tgNode secNode(v_size * buildVec); //Fancy upcast
110  strRod.addNode(secNode); // Top
111 
112  strRod.addPair(0,1, "a rod");
113 
114  // Move the first one into position
115  strRod.move(startNode);
116 
117  // Create our sub-structure string
118  tgStructure snake;
119 
120  for(int i = 0; i < m_config.m_segments; i++) {
121  // Destructor is called when tgStructure is destroyed (end of this method)
122  tgStructure* t = new tgStructure(strRod);
123  t->addTags(tgString("srod num", i + 1));
124  t->move((i + 1)*offset);
125  snake.addChild(t); // Add a child to the snake
126  }
127 
128  // Add muscles that connect the segments
129  std::vector<tgStructure*> children = snake.getChildren();
130  for(int i = 1; i < children.size(); i++) {
131  tgNodes n0 = children[i-1]->getNodes();
132  tgNodes n1 = children[i]->getNodes();
133 
134  snake.addPair(n0[1], n1[0], tgString("muscle v1 seg", i) + tgString(" seg", i));
135  snake.addPair(n0[1], n1[0], tgString("muscle v2 seg", i) + tgString(" seg", i));
136  snake.addPair(n0[1], n1[0], tgString("muscle v3 seg", i) + tgString(" seg", i));
137  snake.addPair(n0[1], n1[0], tgString("muscle v4 seg", i) + tgString(" seg", i));
138  }
139 
140  // Create the build spec that uses tags to turn the structure into a real model
141  tgBuildSpec spec;
142  spec.addBuilder("rod", new tgRodInfo(m_config.m_rodConfig));
143 
144  spec.addBuilder("muscle v1", new tgLinearStringInfo(muscleConfig1));
145  spec.addBuilder("muscle v2", new tgLinearStringInfo(muscleConfig2));
146  spec.addBuilder("muscle v3", new tgLinearStringInfo(muscleConfig3));
147  spec.addBuilder("muscle v4", new tgLinearStringInfo(muscleConfig4));
148 
149  // Create your structureInfo
150  tgStructureInfo structureInfo(snake, spec);
151 
152 
153 
154  // Use the structureInfo to build our model
155  structureInfo.buildInto(*thisString, world);
156 
157  /* Now that the strings sub-structure has been created, attach it
158  * to the super-structure, again using four strings
159  * Very manual from here, since this is the only place we have all
160  * of the necessary pointers
161  */
162 
163  // We have to delete these
164  std::vector<tgPair*> linkerPairs;
165 
166  // "From" side:
167  tgNodes n0 = children[0]->getNodes();
168  // Saves code to only have one - but might mess up the tags a little
169  linkerPairs.push_back(new tgPair(startNode, n0[0], tgString("anchor seg", 0) + tgString(" seg", 1)));
170 
171  // "To" side:
172  std::size_t cldend = children.size() - 1;
173  tgNodes n1 = children[cldend]->getNodes();
174  linkerPairs.push_back(new tgPair(n1[1], endNode, tgString("anchor seg", cldend - 1) + tgString(" seg", cldend)));
175 
176  // Perhaps overkill...
177  // These pointers will be deleted by tgStructure
178  std::vector <tgRigidInfo*> allRigids(structureInfo.getAllRigids());
179  allRigids.push_back(getToRigidInfo());
180  allRigids.push_back(getFromRigidInfo());
181 
182  // We have to delete these
183  std::vector <tgConnectorInfo* > linkerInfo;
184 
185  // Can this part be done in a loop easily?
186  linkerInfo.push_back(new tgLinearStringInfo(muscleConfig1, *linkerPairs[0]));
187  linkerInfo.push_back(new tgLinearStringInfo(muscleConfig1, *linkerPairs[1]));
188 
189  linkerInfo.push_back(new tgLinearStringInfo(muscleConfig2, *linkerPairs[0]));
190  linkerInfo.push_back(new tgLinearStringInfo(muscleConfig2, *linkerPairs[1]));
191 
192  linkerInfo.push_back(new tgLinearStringInfo(muscleConfig3, *linkerPairs[0]));
193  linkerInfo.push_back(new tgLinearStringInfo(muscleConfig3, *linkerPairs[1]));
194 
195  linkerInfo.push_back(new tgLinearStringInfo(muscleConfig4, *linkerPairs[0]));
196  linkerInfo.push_back(new tgLinearStringInfo(muscleConfig4, *linkerPairs[1]));
197 
198  for (std::size_t i = 0; i < linkerInfo.size(); i++)
199  {
200  linkerInfo[i]->chooseRigids(allRigids);
201  linkerInfo[i]->initConnector(world);
202 
203  tgModel* m = linkerInfo[i]->createModel(world);
204  m->setTags(linkerInfo[i]->getTags());
205  if(m != 0) {
206  thisString->addChild(m);
207  }
208  }
209 
210  // Clean up - see if there's a way to do this with references instead?
211  for (std::size_t i = 0; i < linkerInfo.size(); i++)
212  {
213  delete linkerInfo[i];
214  }
215  linkerInfo.clear();
216 
217  for (std::size_t i = 0; i < linkerPairs.size(); i++)
218  {
219  delete linkerPairs[i];
220  }
221  linkerPairs.clear();
222 
223 #if (0)
224  // Debug printing
225  std::cout << "StructureInfo:" << std::endl;
226  std::cout << structureInfo << std::endl;
227 
228  std::cout << "Model: " << std::endl;
229  std::cout << *thisString << std::endl;
230 #endif
231 
232 }
const std::vector< tgStructure * > & getChildren() const
Definition: tgStructure.h:184
void addChild(tgStructure *child)
Definition of class tgRodInfo.
void addChild(tgModel *pChild)
Definition: tgModel.cpp:122
Convenience function for combining strings with ints, mostly for naming structures.
Definition of class tgPair.
void addPair(int fromNodeIdx, int toNodeIdx, std::string tags="")
Definition: tgStructure.cpp:80
Definition of class tgConnectorInfo.
Definition of class tgNode.
Definition: tgPair.h:48
std::string tgString(std::string s, int i)
Definition: tgString.h:33
Definition: tgNode.h:45
Definition of class tgStructure.
Definition of class tgStructureInfo.
Rand seeding simular to the evolution and terrain classes.
Contains the definition of class tgRod.
Definition of class tgBuildSpec.
Definition: tgTags.h:44
Contains the definition of class tgRBStringInfo. A string with small rigid bodies to create contact d...
void addNode(double x, double y, double z, std::string tags="")
Definition: tgStructure.cpp:70