NTRT Simulator  Version: Master
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
simpleMuscleNP.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 "simpleMuscleNP.h"
20 #include "core/tgModelVisitor.h"
21 #include "core/tgBulletUtil.h"
22 #include "core/tgWorld.h"
25 #include "core/tgRod.h"
26 #include "core/tgBox.h"
27 #include "tgcreator/tgBuildSpec.h"
28 #include "tgcreator/tgRodInfo.h"
29 #include "tgcreator/tgBoxInfo.h"
30 #include "tgcreator/tgStructure.h"
34 {
35 }
36 
38 {
39 }
40 
42 {
43 
44 
45  const double rodDensity = 1; // Note: This needs to be high enough or things fly apart...
46  const double rodRadius = 0.25;
47  const tgRod::Config rodConfig(rodRadius, rodDensity);
48  const tgRod::Config rodConfig2(rodRadius, 0.0);
49  const tgBox::Config boxConfig(rodRadius, rodRadius, 0.0);
50 
51  tgStructure s;
52 
53 #if (0)
54  s.addNode(-2, 2.1, 0);
55  s.addNode(0, 2.1, 0);
56  s.addNode(10, 2.1, 0);
57  s.addNode(12, 2.1, 0);
58  s.addNode(22, 2.1, 0);
59  s.addNode(24, 2.1, 0);
60 
61  s.addPair(0, 1, "rod2");
62  s.addPair(2, 3, "rod2");
63  s.addPair(4, 5, "rod2");
64 
65  s.addPair(1, 2, "muscle");
66  s.addPair(3, 4, "muscle");
67  s.move(btVector3(0, 0, 0));
68 #else
69  s.addNode(0, 5, 2);
70  s.addNode(0, 5, 0);
71  s.addNode(10, 5, 2);
72  s.addNode(10, 5, 0);
73  s.addNode(5, 2, -5);
74  s.addNode(5, 2, 5);
75 
76  s.addPair(0, 1, "rod");
77  s.addPair(2, 3, "rod");
78  s.addPair(4, 5, "box");
79 
80  s.addPair(0, 2, "muscle");
81  s.addPair(1, 3, "muscle");
82  //s.addPair(3, 4, "muscle");
83  s.move(btVector3(0, 0, 0));
84 #endif
85 
86 
87  // Move the structure so it doesn't start in the ground
88  s.move(btVector3(0, 0, 0));
89  //s.addRotation(btVector3(0.0, 0.0, 0.0), btVector3(0.0, 1.0, 0.0), M_PI_2);
90  tgSpringCableActuator::Config muscleConfig(1000, 10, 0.0, false, 600000000);
91 
92  // Create the build spec that uses tags to turn the structure into a real model
93  tgBuildSpec spec;
94  spec.addBuilder("rod", new tgRodInfo(rodConfig));
95  spec.addBuilder("rod2", new tgRodInfo(rodConfig2));
96  spec.addBuilder("box", new tgBoxInfo(boxConfig));
97  spec.addBuilder("muscle", new tgBasicContactCableInfo(muscleConfig));
98 
99  // Create your structureInfo
100  tgStructureInfo structureInfo(s, spec);
101  // Use the structureInfo to build ourselves
102  structureInfo.buildInto(*this, world);
103  // We could now use tgCast::filter or similar to pull out the
104  // models (e.g. muscles) that we want to control.
105  allMuscles = tgCast::filter<tgModel, tgSpringCableActuator> (getDescendants());
106  allRods = tgCast::filter<tgModel, tgRod> (getDescendants());
107 
108  notifySetup();
109  totalTime = 0.0;
110  tgModel::setup(world);
111 }
112 
114 {
116 }
117 
118 void simpleMuscleNP::step(double dt)
119 {
120  totalTime += dt;
121 
122  allMuscles[0]->setControlInput(10, dt);
123 // allMuscles[1]->setRestLength(11, dt);
124 
125  btVector3 com(0, 0, 0);
126  btScalar mass = 0;
127  btScalar energy = 0;
128  for (std::size_t i = 0; i < allRods.size(); i++)
129  {
130  tgRod& ri = *(allRods[i]);
131  com += ri.centerOfMass() * ri.mass();
132  mass += ri.mass();
133  }
134 
135  //std::cout << allMuscles[0]->getTension() << std::endl;
136  tgModel::step(dt);
137 }
138 
144 {
145  r.render(*this);
146  tgModel::onVisit(r);
147 }
Create a box shape as an obstacle or add it to your tensegrity.
virtual void teardown()
Definition: tgModel.cpp:68
virtual void setup(tgWorld &world)
Definition: tgModel.cpp:57
Definition of class tgRodInfo.
For testing MuscleNP contacts.
virtual void setup(tgWorld &world)
virtual void onVisit(const tgModelVisitor &r) const
virtual void step(double dt)
Definition: tgModel.cpp:84
virtual btVector3 centerOfMass() const
Definition: tgBaseRigid.cpp:74
virtual void onVisit(const tgModelVisitor &r) const
Definition: tgModel.cpp:107
Class that interfaces with Bullet to build the boxes.
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...
virtual void render(const tgRod &rod) const
Contains the definition of interface class tgModelVisitor.
Definition of class tgBasicContactCableInfo.
virtual void teardown()
Contains the definition of class tgWorld $Id$.
Contains the definition of class tgBulletUtil.
Definition of class tgStructure.
virtual ~simpleMuscleNP()
Definition of class tgStructureInfo.
virtual double mass() const
Definition: tgBaseRigid.h:65
virtual void step(double dt)
Contains the definition of class tgRod.
Definition of class tgBuildSpec.
Definition: tgRod.h:43
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