NTRT Simulator  v1.1
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
MuscleNPCons.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 "MuscleNPCons.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 "core/tgBaseRigid.h"
28 #include "tgcreator/tgBuildSpec.h"
29 #include "tgcreator/tgRodInfo.h"
30 #include "tgcreator/tgBoxInfo.h"
31 #include "tgcreator/tgStructure.h"
34 
37 
38 // The Bullet Physics Library
39 #include "BulletDynamics/Dynamics/btRigidBody.h"
40 
42 {
43 }
44 
46 {
47 }
48 
50 {
51 
52 
53  const double rodDensity = 1; // Note: This needs to be high enough or things fly apart...
54  const double rodRadius = 0.25;
55  const tgRod::Config rodConfig(rodRadius, rodDensity);
56  const tgRod::Config rodConfig2(rodRadius, 0.0);
57  const tgBox::Config boxConfig(rodRadius, rodRadius, rodDensity);
58 
59  tgStructure s;
60 
61  s.addNode(-2, 2, 0);
62  s.addNode(0, 2, 0);
63  s.addNode(0, 2, 0);
64  s.addNode(0, 4, 0);
65  s.addNode(0, 12, 0);
66  s.addNode(0, 14, 0);
67 
68 
69  // Free rod to which we will apply motion
70  s.addNode(-1, 6, 5);
71  s.addNode(1, 6, 5);
72 
73  //s.addPair(0, 1, "rod");
74  s.addPair(2, 3, "box");
75  s.addPair(4, 5, "box");
76 
77  s.addPair(6, 7, "rod");
78 
79  //s.addPair(1, 2, "muscle");
80  s.addPair(3, 4, "muscle");
81  s.move(btVector3(0, 0, 0));
82 
83 
84  // Move the structure so it doesn't start in the ground
85  s.move(btVector3(0, 0, 0));
86  s.addRotation(btVector3(0.0, 0.0, 0.0), btVector3(0.0, 1.0, 0.0), M_PI/4);
87  tgSpringCableActuator::Config muscleConfig(1000, 0, 0.0, false, 600000000);
88 
89  // Create the build spec that uses tags to turn the structure into a real model
90  tgBuildSpec spec;
91 
92  spec.addBuilder("rod2", new tgRodInfo(rodConfig2));
93  spec.addBuilder("box", new tgBoxInfo(boxConfig));
94  spec.addBuilder("rod", new tgRodInfo(rodConfig));
95  spec.addBuilder("muscle", new tgBasicContactCableInfo(muscleConfig));
96 
97  // Create your structureInfo
98  tgStructureInfo structureInfo(s, spec);
99  // Use the structureInfo to build ourselves
100  structureInfo.buildInto(*this, world);
101  // We could now use tgCast::filter or similar to pull out the
102  // models (e.g. muscles) that we want to control.
103  allRods.clear();
104  allMuscles.clear();
105  allMuscles = tgCast::filter<tgModel, tgSpringCableActuator> (getDescendants());
106  allRods = tgCast::filter<tgModel, tgBaseRigid> (getDescendants());
107 
108  btRigidBody* body = allRods[0]->getPRigidBody();
109  btRigidBody* body2 = allRods[1]->getPRigidBody();
110 
111  // Apply initial impulse
112  btVector3 impulse(0.5, 0.0, 0.5);
113  body->applyCentralImpulse(impulse);
114  body2->applyCentralImpulse(impulse);
115 
116  btRigidBody* body3 = allRods[2]->getPRigidBody();
117 
118  std::cout << body3->getCenterOfMassTransform () << std::endl;
119 
120  notifySetup();
121  totalTime = 0.0;
122  tgModel::setup(world);
123 }
124 
126 {
128 }
129 
130 void MuscleNPCons::step(double dt)
131 {
132  totalTime += dt;
133 
134  tgModel::step(dt);
135 
136  btVector3 vCom(0, 0, 0);
137  btScalar mass = 0;
138  btScalar energy = 0;
139  for (std::size_t i = 0; i < allRods.size(); i++)
140  {
141  tgBaseRigid& ri = *(allRods[i]);
142  btRigidBody* body = ri.getPRigidBody();
143  btVector3 localVel = body->getLinearVelocity();
144  vCom += localVel * ri.mass();
145  energy += localVel.length2() * ri.mass();// + body->getAngularVelocity().length2() ;
146  mass += ri.mass();
147  }
148 
149  btVector3 forceSum(0.0, 0.0, 0.0);
150 
151  const std::vector<const tgSpringCableAnchor*>& anchorList = allMuscles[0]->getSpringCable()->getAnchors();
152  int n = anchorList.size();
153  for (std::size_t i = 0; i < n; i++)
154  {
155  forceSum += anchorList[i]->getForce();
156  }
157 
158 #if (1)
159  std::cout << "Time " << totalTime << std::endl;
160  std::cout << "Momentum " << vCom << std::endl;
161  std::cout << "Energy " << energy << std::endl;
162  std::cout << "Other Momentum " << getMomentum() << std::endl;
163  std::cout << "Force sum " << forceSum << std::endl;
164  std::cout << "Length " << allMuscles[0]->getCurrentLength();
165  std::cout << " Dist " << (anchorList[0]->getWorldPosition() - anchorList[n-1]->getWorldPosition()).length() << std::endl;
166  std::cout << "Anchors: " << n << std::endl;
167 
168  if (energy > 20){
169  std::cout << "Here!" << std::endl;
170  }
171 #endif
172 }
173 
179 {
180  r.render(*this);
181  tgModel::onVisit(r);
182 }
183 
185 {
186  double energy = 0;
187  btScalar mass = 0;
188  for (std::size_t i = 0; i < allRods.size(); i++)
189  {
190  tgBaseRigid* ri = allRods[i];
191  btRigidBody* body = ri->getPRigidBody();
192  btVector3 localVel = body->getLinearVelocity();
193  energy += localVel.length2() * ri->mass();// + body->getAngularVelocity().length2() ;
194  mass += ri->mass();
195  }
196 
197  return energy;
198 }
199 
200 btVector3 MuscleNPCons::getMomentum() const
201 {
202  btVector3 vCom(0, 0, 0);
203  btScalar mass = 0;
204 
205  for (std::size_t i = 0; i < allRods.size(); i++)
206  {
207  tgBaseRigid* ri = allRods[i];
208  btRigidBody* body = ri->getPRigidBody();
209  btVector3 localVel = body->getLinearVelocity();
210  vCom += localVel * ri->mass();
211  mass += ri->mass();
212  }
213 
214  return vCom;
215 }
216 
217 btVector3 MuscleNPCons::getVelocityOfBody(int body_num) const
218 {
219  assert(body_num < allRods.size() && body_num >= 0);
220  tgBaseRigid* ri = allRods[body_num];
221  btRigidBody* body = ri->getPRigidBody();
222 
223  return body->getLinearVelocity();
224 }
virtual btVector3 getMomentum() const
virtual double getEnergy() const
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.
Create a box shape as an obstacle or add it to your tensegrity.
virtual void setup(tgWorld &world)
virtual void step(double dt)
Definition: tgModel.cpp:84
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:66
virtual void teardown()
Contains the definition of abstract base class tgSpringCableActuator. Assumes that the string is line...
void addRotation(const btVector3 &fixedPoint, const btVector3 &axis, double angle)
Definition: tgStructure.cpp:99
virtual void render(const tgRod &rod) const
Contains the definition of interface class tgModelVisitor.
Definition of class tgBasicContactCableInfo.
Contains the definition of class tgWorld $Id$.
Contains the definition of class tgBulletUtil.
Definition of class tgStructure.
virtual btRigidBody * getPRigidBody()
Definition: tgBaseRigid.h:76
Definition of class tgStructureInfo.
virtual double mass() const
Definition: tgBaseRigid.h:65
virtual void step(double dt)
Definitions of class tgSpringCableAnchor.
Definitions of classes tgBulletSpringCable $Id$.
virtual void onVisit(const tgModelVisitor &r) const
Contains the definition of class tgRod.
Definition of class tgBuildSpec.
std::vector< tgModel * > getDescendants() const
Definition: tgModel.cpp:172
For testing MuscleNP contacts.
void addNode(double x, double y, double z, std::string tags="")
Definition: tgStructure.cpp:56
virtual ~MuscleNPCons()