/
ContactCableDemo.cpp
220 lines (179 loc) · 6.11 KB
/
ContactCableDemo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* Copyright © 2012, United States Government, as represented by the
* Administrator of the National Aeronautics and Space Administration.
* All rights reserved.
*
* The NASA Tensegrity Robotics Toolkit (NTRT) v1 platform is licensed
* under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
#include "ContactCableDemo.h"
#include "core/tgModelVisitor.h"
#include "core/tgBulletUtil.h"
#include "core/tgWorld.h"
#include "core/tgSpringCableActuator.h"
#include "core/tgSpringCableActuator.h"
#include "core/tgRod.h"
#include "core/tgBox.h"
#include "core/tgBaseRigid.h"
#include "tgcreator/tgBuildSpec.h"
#include "tgcreator/tgRodInfo.h"
#include "tgcreator/tgBoxInfo.h"
#include "tgcreator/tgStructure.h"
#include "tgcreator/tgStructureInfo.h"
#include "tgcreator/tgBasicContactCableInfo.h"
#include "core/tgBulletSpringCable.h"
#include "core/tgSpringCableAnchor.h"
// The Bullet Physics Library
#include "BulletDynamics/Dynamics/btRigidBody.h"
ContactCableDemo::ContactCableDemo()
{
}
ContactCableDemo::~ContactCableDemo()
{
}
void ContactCableDemo::setup(tgWorld& world)
{
const double rodDensity = 1; // Note: This needs to be high enough or things fly apart...
const double rodRadius = 0.25;
const tgRod::Config rodConfig(rodRadius, rodDensity);
const tgRod::Config rodConfig2(rodRadius, 0.0);
const tgBox::Config boxConfig(rodRadius, rodRadius, rodDensity);
tgStructure s;
s.addNode(0, 2, 0); // 0
s.addNode(0, 4, 0); // 1
s.addNode(0, 12, 0); // 2
s.addNode(0, 14, 0); // 3
// Static rod that the others will run into
s.addNode(-1, 8, 5); // 4
s.addNode(1, 8, 5); // 5
s.addPair(0, 1, "rod");
s.addPair(2, 3, "box");
s.addPair(4, 5, "rod");
s.addPair(1, 2, "muscle");
s.move(btVector3(0, 0, 0));
// Move the structure so it doesn't start in the ground
s.move(btVector3(0, 0, 0));
s.addRotation(btVector3(0.0, 0.0, 0.0), btVector3(0.0, 1.0, 0.0), M_PI/4);
tgSpringCableActuator::Config muscleConfig(1000, 0, 0.0, false, 600000000);
// Create the build spec that uses tags to turn the structure into a real model
tgBuildSpec spec;
spec.addBuilder("rod2", new tgRodInfo(rodConfig2));
spec.addBuilder("box", new tgBoxInfo(boxConfig));
spec.addBuilder("rod", new tgRodInfo(rodConfig));
spec.addBuilder("muscle", new tgBasicContactCableInfo(muscleConfig));
// Create your structureInfo
tgStructureInfo structureInfo(s, spec);
// Use the structureInfo to build ourselves
structureInfo.buildInto(*this, world);
// We could now use tgCast::filter or similar to pull out the
// models (e.g. muscles) that we want to control.
allRods.clear();
allMuscles.clear();
allMuscles = tgCast::filter<tgModel, tgSpringCableActuator> (getDescendants());
allRods = tgCast::filter<tgModel, tgBaseRigid> (getDescendants());
btRigidBody* body = allRods[0]->getPRigidBody();
btRigidBody* body2 = allRods[1]->getPRigidBody();
// Apply initial impulse
btVector3 impulse(0.4, 0.0, 0.4);
body->applyCentralImpulse(impulse);
body2->applyCentralImpulse(impulse);
btRigidBody* body3 = allRods[2]->getPRigidBody();
std::cout << body3->getCenterOfMassTransform () << std::endl;
notifySetup();
totalTime = 0.0;
tgModel::setup(world);
}
void ContactCableDemo::teardown()
{
tgModel::teardown();
}
void ContactCableDemo::step(double dt)
{
totalTime += dt;
tgModel::step(dt);
btVector3 vCom(0, 0, 0);
btScalar mass = 0;
btScalar energy = 0;
for (std::size_t i = 0; i < allRods.size(); i++)
{
tgBaseRigid& ri = *(allRods[i]);
btRigidBody* body = ri.getPRigidBody();
btVector3 localVel = body->getLinearVelocity();
vCom += localVel * ri.mass();
energy += localVel.length2() * ri.mass();// + body->getAngularVelocity().length2() ;
mass += ri.mass();
}
btVector3 forceSum(0.0, 0.0, 0.0);
const std::vector<const tgSpringCableAnchor*>& anchorList = allMuscles[0]->getSpringCable()->getAnchors();
int n = anchorList.size();
for (std::size_t i = 0; i < n; i++)
{
forceSum += anchorList[i]->getForce();
}
#if (0)
std::cout << "Time " << totalTime << std::endl;
std::cout << "Momentum " << vCom << std::endl;
std::cout << "Energy " << energy << std::endl;
std::cout << "Other Momentum " << getMomentum() << std::endl;
std::cout << "Force sum " << forceSum << std::endl;
std::cout << "Length " << allMuscles[0]->getCurrentLength();
std::cout << " Dist " << (anchorList[0]->getWorldPosition() - anchorList[n-1]->getWorldPosition()).length() << std::endl;
std::cout << "Anchors: " << n << std::endl;
if (energy > 20){
std::cout << "Here!" << std::endl;
}
#endif
}
/**
* Call tgModelVisitor::render() on self and all descendants.
* @param[in,out] r a reference to a tgModelVisitor
*/
void ContactCableDemo::onVisit(const tgModelVisitor& r) const
{
r.render(*this);
tgModel::onVisit(r);
}
double ContactCableDemo::getEnergy() const
{
double energy = 0;
btScalar mass = 0;
for (std::size_t i = 0; i < allRods.size(); i++)
{
tgBaseRigid* ri = allRods[i];
btRigidBody* body = ri->getPRigidBody();
btVector3 localVel = body->getLinearVelocity();
energy += localVel.length2() * ri->mass();// + body->getAngularVelocity().length2() ;
mass += ri->mass();
}
return energy;
}
btVector3 ContactCableDemo::getMomentum() const
{
btVector3 vCom(0, 0, 0);
btScalar mass = 0;
for (std::size_t i = 0; i < allRods.size(); i++)
{
tgBaseRigid* ri = allRods[i];
btRigidBody* body = ri->getPRigidBody();
btVector3 localVel = body->getLinearVelocity();
vCom += localVel * ri->mass();
mass += ri->mass();
}
return vCom;
}
btVector3 ContactCableDemo::getVelocityOfBody(int body_num) const
{
assert(body_num < allRods.size() && body_num >= 0);
tgBaseRigid* ri = allRods[body_num];
btRigidBody* body = ri->getPRigidBody();
return body->getLinearVelocity();
}