37 #include "LinearMath/btVector3.h"
40 #include "BulletDynamics/Dynamics/btRigidBody.h"
41 #include "BulletDynamics/Dynamics/btDynamicsWorld.h"
71 double targetVelocity;
87 #if (0) // acceleration constrant removed 12/10/14
104 const int rodNumbersPerNode[13]={0,1,2,3,3,4,0,1,2,5,5,4,6};
107 const int otherEndOfTheRod[13]={6,7,8,4,3,11,0,1,2,10,9,5,12};
112 const int parallelNode[13]={1,0,5,9,10,2,7,6,11,3,4,8,12};
141 const double half_length = c.rod_length / 2;
143 nodePositions.push_back(btVector3(-half_length, c.rod_space, 0));
144 nodePositions.push_back(btVector3( half_length, c.rod_space, 0));
145 nodePositions.push_back(btVector3(0, half_length, -c.rod_space));
146 nodePositions.push_back(btVector3(-c.rod_space, 0, -half_length));
147 nodePositions.push_back(btVector3( c.rod_space, 0, -half_length));
148 nodePositions.push_back(btVector3(0, -half_length, -c.rod_space));
149 nodePositions.push_back(btVector3(-half_length, -c.rod_space, 0));
150 nodePositions.push_back(btVector3( half_length, -c.rod_space, 0));
151 nodePositions.push_back(btVector3(0, half_length, c.rod_space));
152 nodePositions.push_back(btVector3(-c.rod_space, 0, half_length));
153 nodePositions.push_back(btVector3( c.rod_space, 0, half_length));
154 nodePositions.push_back(btVector3(0, -half_length, c.rod_space));
156 for(
int i=0;i<12;i++)
158 s.
addNode(nodePositions[i][0],nodePositions[i][1],nodePositions[i][2]);
174 void SuperBallModel::addMarkers()
176 std::vector<tgRod *> rods=find<tgRod>(
"rod");
178 for(
int i=0;i<12;i++)
180 const btRigidBody* bt = rods[rodNumbersPerNode[i]]->getPRigidBody();
181 btTransform inverseTransform = bt->getWorldTransform().inverse();
182 btVector3 pos = inverseTransform * (nodePositions[i]);
184 this->addMarker(tmp);
188 void SuperBallModel::addSensors()
190 std::cout<<
"Adding sensors"<<std::endl;
191 std::vector<tgRod *> rods=find<tgRod>(
"rod");
193 for(
int i=0;i<12;i++)
195 const btRigidBody* bt = rods[rodNumbersPerNode[i]]->getPRigidBody();
196 btTransform inverseTransform = bt->getWorldTransform().inverse();
197 btVector3 pos = inverseTransform * (nodePositions[i]);
199 this->heightSensors.push_back(tmp);
205 std::vector<double> sensorInfo;
206 for(
int i=0;i<heightSensors.size();i++)
208 sensorInfo.push_back(heightSensors.at(i).getHeight());
216 musclesPerNodes.resize(13);
217 for(
int i=0;i<13;i++)
219 musclesPerNodes[i].resize(13);
220 for(
int j=0;j<13;j++)
221 musclesPerNodes[i][j]=NULL;
223 for(
int i=0;i<13;i++)
224 for(
int j=0;j<13;j++)
225 muscleConnections[i][j]=-1;
227 muscleConnections[0][3]=0;
228 muscleConnections[3][2]=0;
229 muscleConnections[2][0]=0;
230 muscleConnections[4][5]=0;
231 muscleConnections[5][7]=0;
232 muscleConnections[7][4]=0;
233 muscleConnections[1][8]=0;
234 muscleConnections[8][10]=0;
235 muscleConnections[10][1]=0;
236 muscleConnections[9][11]=0;
237 muscleConnections[11][6]=0;
238 muscleConnections[6][9]=0;
239 muscleConnections[1][2]=1;
240 muscleConnections[2][4]=1;
241 muscleConnections[4][1]=1;
242 muscleConnections[3][5]=1;
243 muscleConnections[5][6]=1;
244 muscleConnections[6][3]=1;
245 muscleConnections[0][8]=1;
246 muscleConnections[8][9]=1;
247 muscleConnections[9][0]=1;
248 muscleConnections[11][7]=1;
249 muscleConnections[7][10]=1;
250 muscleConnections[10][11]=1;
252 for(
int i=0;i<13;i++)
254 for(
int j=0;j<13;j++)
256 if(muscleConnections[i][j]>=0)
258 std::stringstream tag;
259 tag<<
"muscle "<<i<<
"-"<<j;
270 const tgRod::Config rodConfig(c.radius, c.density, c.friction,
271 c.rollFriction, c.restitution);
274 c.history, c.maxTens, c.targetVelocity);
293 spec.addBuilder(
"rod",
new tgRodInfo(rodConfig));
304 allMuscles = tgCast::filter<tgModel, tgBasicActuator> (
getDescendants());
312 this->m_world = world;
318 fillNodeNumberingSchema(6,11,9);
319 fillMusclesPerNode();
322 btVector3 location(0,13.0,0);
323 btVector3 rotation(0.0,0.6,0.8);
324 btVector3 speed(0,10,0);
325 this->moveModel(location,rotation,speed);
334 throw std::invalid_argument(
"dt is not positive");
358 std::cout<<
"Tearing down model"<<std::endl;
359 this->nodeNumberingSchema.clear();
360 this->heightSensors.clear();
361 this->nodePositions.clear();
366 void SuperBallModel::moveModel(btVector3 positionVector,btVector3 rotationVector,btVector3 speedVector)
368 std::vector<tgRod *> rods=find<tgRod>(
"rod");
370 btQuaternion initialRotationQuat;
371 initialRotationQuat.setEuler(rotationVector[0],rotationVector[1],rotationVector[2]);
372 btTransform initialTransform;
373 initialTransform.setIdentity();
374 initialTransform.setRotation(initialRotationQuat);
375 initialTransform.setOrigin(positionVector);
376 for(
int i=0;i<rods.size();i++)
378 rods[i]->getPRigidBody()->setLinearVelocity(speedVector);
379 rods[i]->getPRigidBody()->setWorldTransform(initialTransform * rods[i]->getPRigidBody()->getWorldTransform());
385 std::vector<btVector3 > positions;
386 for(
int i=0;i<heightSensors.size();i++)
388 positions.push_back(heightSensors[i].getWorldPosition());
396 std::vector<btVector3 > directions;
397 for(
int i=0;i<12;i++)
399 temp = heightSensors[i].getWorldPosition() - heightSensors[otherEndOfTheRod[i]].getWorldPosition();
400 temp /= temp.length();
401 directions.push_back(temp);
407 btVector3 SuperBallModel::getCenter()
409 btVector3 pos(0.0,0.0,0.0);
410 for(
int i=0;i< heightSensors.size();i++)
412 pos += heightSensors[i].getWorldPosition();
414 pos /= heightSensors.size();
419 btDynamicsWorld *SuperBallModel::getWorld()
424 void SuperBallModel::fillNodeNumberingSchema(
int base1,
int base2,
int base3)
426 nodeNumberingSchema.resize(3);
427 for (
int i = 0; i < 3; ++i)
429 nodeNumberingSchema[i].resize(2);
430 for (
int j = 0; j < 2; ++j)
432 nodeNumberingSchema[i][j].resize(2);
433 for (
int k=0;k<2;k++)
435 nodeNumberingSchema[i][j][k]=-1;
440 nodeNumberingSchema[0][0][0]=base1;
441 nodeNumberingSchema[1][0][0]=base2;
442 nodeNumberingSchema[2][0][0]=base3;
447 nodeNumberingSchema[i][0][1]=otherEndOfTheRod[nodeNumberingSchema[i][0][0]];
449 nodeNumberingSchema[i][1][0]=parallelNode[nodeNumberingSchema[i][0][0]];
451 nodeNumberingSchema[i][1][1]=otherEndOfTheRod[nodeNumberingSchema[i][1][0]];
455 void SuperBallModel::fillNodeMappingFromBasePoints(
int a,
int b,
int c)
457 for(
int i=0;i<13;i++)
460 nodeMappingReverse[i]=-1;
464 nodeMapping[a]=nodeNumberingSchema[0][0][0];
465 nodeMapping[b]=nodeNumberingSchema[1][0][0];
466 nodeMapping[c]=nodeNumberingSchema[2][0][0];
468 nodeMapping[otherEndOfTheRod[a]]=nodeNumberingSchema[0][0][1];
469 nodeMapping[otherEndOfTheRod[b]]=nodeNumberingSchema[1][0][1];
470 nodeMapping[otherEndOfTheRod[c]]=nodeNumberingSchema[2][0][1];
472 nodeMapping[parallelNode[a]]=nodeNumberingSchema[0][1][0];
473 nodeMapping[parallelNode[b]]=nodeNumberingSchema[1][1][0];
474 nodeMapping[parallelNode[c]]=nodeNumberingSchema[2][1][0];
476 nodeMapping[otherEndOfTheRod[parallelNode[a]]]=nodeNumberingSchema[0][1][1];
477 nodeMapping[otherEndOfTheRod[parallelNode[b]]]=nodeNumberingSchema[1][1][1];
478 nodeMapping[otherEndOfTheRod[parallelNode[c]]]=nodeNumberingSchema[2][1][1];
480 for(
int i=0;i<13;i++)
483 nodeMappingReverse[nodeMapping[i]]=i;
488 int SuperBallModel::getOtherEndOfTheRod(
int i)
491 return otherEndOfTheRod[i];
496 void SuperBallModel::fillMusclesPerNode()
498 for(
int i=0;i<13;i++)
500 for(
int j=0;j<13;j++)
502 if(muscleConnections[i][j]>=0)
504 std::stringstream tag;
505 tag<<
"muscle "<<i<<
"-"<<j;
506 std::vector<tgBasicActuator*> foundStr = this->find<tgBasicActuator>(tag.str());
507 if(!foundStr.empty())
510 musclesPerNodes[i][j]=foundStr[0];
511 musclesPerNodes[j][i]=musclesPerNodes[i][j];
std::vector< double > getSensorInfo()
virtual void setup(tgWorld &world)
Definition of class tgRodInfo.
std::vector< btVector3 > getSensorOrientations()
static btDynamicsWorld & worldToDynamicsWorld(const tgWorld &world)
virtual void step(double dt)
Contains the definition of class SuperBallModel. $Id$.
const std::vector< tgBasicActuator * > & getAllMuscles() const
Markers for specific places on a tensegrity.
Definition of class tgBasicActuatorInfo.
virtual void onVisit(const tgModelVisitor &r) const
void addPair(int fromNodeIdx, int toNodeIdx, std::string tags="")
virtual void step(double dt)
Contains the definition of class tgBasicActuator.
Definition of class tgStructure.
virtual ~SuperBallModel()
Definition of class tgStructureInfo.
virtual void setup(tgWorld &world)
std::vector< btVector3 > getSensorPositions()
virtual void onVisit(tgModelVisitor &r)
Contains the definition of class tgRod.
Definition of class tgBuildSpec.
SuperBallModel(tgWorld &world)
void notifyStep(double dt)
std::vector< tgModel * > getDescendants() const
void buildInto(tgModel &model, tgWorld &world)
void addNode(double x, double y, double z, std::string tags="")