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