NTRT Simulator
 All Classes Files Functions Variables Typedefs Friends Pages
RBStringTest.cpp
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 
19 #include "RBStringTest.h"
20 
21 #include "btBulletDynamicsCommon.h"
22 #include <iostream>
23 
24 #include "tgcreator/tgRodInfo.h"
28 #include "tgcreator/tgBuildSpec.h"
29 
30 #include "tgcreator/tgStructure.h"
32 #include "tgcreator/tgUtil.h"
33 #include "core/tgString.h"
34 #include "tgcreator/tgNode.h"
35 
36 #include "core/tgCast.h"
37 
38 RBStringTest::Config::Config( int segments,
39  const tgRod::Config& rodConf,
40  const tgLinearString::Config& stringConf,
41  double minTotalLength) :
42 m_segments(segments),
43 m_rodConfig(rodConf),
44 m_stringConfig(stringConf),
45 m_minTotalLength(minTotalLength)
46 {
47 }
48 
49 RBStringTest::RBStringTest(tgNode* start, tgNode* end, const RBStringTest::Config& config) :
50  m_pStartNode(start),
51  m_pEndNode(end),
52  m_config(config),
53  tgModel()
54 {
55 }
56 
58 {
59 
60  // todo - figure out a way to change only the one parameter with less code
61  const double stiffness = m_config.m_stringConfig.stiffness;
62  const double damping = m_config.m_stringConfig.damping;
63  tgLinearString::Config muscleConfig1(stiffness, damping, false, -M_PI / 2.0);
64  tgLinearString::Config muscleConfig2(stiffness, damping, false, M_PI / 2.0);
65  tgLinearString::Config muscleConfig3(stiffness, damping, false, M_PI);
66  tgLinearString::Config muscleConfig4(stiffness, damping, false, 0);
67 
68  // Calculations for the flemons spine model
69  double v_size = m_config.m_minTotalLength / (double) m_config.m_segments;
70 
71  // tgNode is a subclass, so this should be fine
72  btVector3 buildVec = (*m_pEndNode - *m_pStartNode);
73 
74  double spacing = buildVec.length() / (double) m_config.m_segments;
75  // After this buildVec stays normalized.
76  btVector3 offset = buildVec.normalize() * spacing;
77 
78  // Create the tetrahedra
79  tgStructure tetra;
80 
81  tetra.addNode(0,0,0); // Bottom
82  tgNode endNode(v_size * buildVec); //Fancy upcast
83  tetra.addNode(endNode); // Top
84 
85  tetra.addPair(0,1, "a rod");
86 
87  // Move the first one into position
88  tetra.move(*m_pStartNode);
89 
90 
91  // Create our snake segments - these will be tgModels with
92  // tgRods as children
93  tgStructure snake;
94 
95  for(int i = 0; i < m_config.m_segments; i++) {
96  // @todo: the snake is a temporary variable -- will its destructor be called? If not, where do we delete its children?
97  tgStructure* t = new tgStructure(tetra);
98  t->addTags(tgString("segment num", i + 1));
99  t->move((i + 1)*offset);
100  snake.addChild(t); // Add a child to the snake
101  }
102 
103  // Add muscles that connect the segments
104  std::vector<tgStructure*> children = snake.getChildren();
105  for(int i = 1; i < children.size(); i++) {
106  tgNodes n0 = children[i-1]->getNodes();
107  tgNodes n1 = children[i]->getNodes();
108 
109  snake.addPair(n0[1], n1[0], tgString("muscle seg", i-1) + tgString(" seg", i));
110  snake.addPair(n0[1], n1[0], tgString("muscle2 seg", i-1) + tgString(" seg", i));
111  snake.addPair(n0[1], n1[0], tgString("muscle3 seg", i-1) + tgString(" seg", i));
112  snake.addPair(n0[1], n1[0], tgString("muscle4 seg", i-1) + tgString(" seg", i));
113  }
114 
115  // Create the build spec that uses tags to turn the structure into a real model
116  tgBuildSpec spec;
117  spec.addBuilder("rod", new tgRodInfo(m_config.m_rodConfig));
118 
119  spec.addBuilder("muscle", new tgLinearStringInfo(muscleConfig1));
120  spec.addBuilder("muscle2", new tgLinearStringInfo(muscleConfig2));
121  spec.addBuilder("muscle3", new tgLinearStringInfo(muscleConfig3));
122  spec.addBuilder("muscle4", new tgLinearStringInfo(muscleConfig4));
123 
124  // Create your structureInfo
125  tgStructureInfo structureInfo(snake, spec);
126 
127  // Use the structureInfo to build ourselves
128  structureInfo.buildInto(*this, world);
129 
130  // We could now use tgCast::filter or similar to pull out the models (e.g. muscles) that we want to control.
131  allMuscles = tgCast::filter<tgModel, tgLinearString> (getDescendants());
132 
133  // Debug printing
134  std::cout << "StructureInfo:" << std::endl;
135  std::cout << structureInfo << std::endl;
136 
137  std::cout << "Model: " << std::endl;
138  std::cout << *this << std::endl;
139 
140 
141 }
142 
143 void RBStringTest::step(double dt)
144 {
145  // Notify observers (controllers) of the step so that they can take action
146  notifyStep(dt);
147 
148  tgModel::step(dt); // Step any children
149 
150 }
151 
152 void RBStringTest::changeMuscle (double length, double dt)
153 {
154  for( int i = 0; i < allMuscles.size(); i++){
155  allMuscles[i]->setRestLength(length, dt);
156  }
157 }
158 
const std::vector< tgStructure * > & getChildren() const
Definition: tgStructure.h:130
void addChild(tgStructure *child)
Definition of class tgRodInfo.
Convenience function for combining strings with ints, mostly for naming structures.
virtual void step(double dt)
Definition: tgModel.cpp:86
Utility class for class casting and filtering collections by type.
void addPair(int fromNodeIdx, int toNodeIdx, std::string tags="")
Definition: tgStructure.cpp:65
virtual void step(double dt)
Definition of class tgConnectorInfo.
Definition of class tgNode.
std::string tgString(std::string s, int i)
Definition: tgString.h:32
Definition: tgNode.h:43
Definition of class tgStructure.
Definition of class tgStructureInfo.
Definition of class tgLinearStringInfo.
virtual void setup(tgWorld &world)
Contains the definition of class tgUtil and overloaded operator<<() free functions.
Definition of class tgBuildSpec.
Definition of class tgRigidAutoCompound.
void notifyStep(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