NTRT Simulator  v1.1
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
T12SuperBallPayload.cpp
Go to the documentation of this file.
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 
25 // This module
26 #include "T12SuperBallPayload.h"
27 // This library
28 #include "core/tgBasicActuator.h"
29 #include "core/tgRod.h"
30 #include "core/abstractMarker.h"
31 #include "tgcreator/tgBuildSpec.h"
33 #include "tgcreator/tgRodInfo.h"
34 #include "tgcreator/tgStructure.h"
36 // The Bullet Physics library
37 #include "LinearMath/btVector3.h"
38 // The C++ Standard Library
39 #include <stdexcept>
40 
41 namespace
42 {
43  // see tgBaseString.h for a descripton of some of these rod parameters
44  // (specifically, those related to the motor moving the strings.)
45  // NOTE that any parameter that depends on units of length will scale
46  // with the current gravity scaling. E.g., with gravity as 98.1,
47  // the length units below are in decimeters.
48 
49  // Note: This current model of the SUPERball rod is 1.5m long by 3 cm radius,
50  // which is 0.00424 m^3.
51  // For SUPERball v1.5, mass = 3.5kg per strut, which comes out to
52  // 0.825 kg / (decimeter^3).
53 
54  // similarly, frictional parameters are for the tgRod objects.
55  const struct Config
56  {
57  double density;
58  double radius;
59  double stiffness;
60  double damping;
61  double rod_length;
62  double rod_space;
63  double friction;
64  double rollFriction;
65  double restitution;
66  double pretension;
67  bool history;
68  double maxTens;
69  double targetVelocity;
70  } c =
71  {
72  0.825, // density (kg / length^3)
73  0.31, // radius (length)
74  3000.0, // stiffness (kg / sec^2)
75  200.0, // damping (kg / sec)
76  15.0, // rod_length (length)
77  7.5, // rod_space (length)
78  1.0, // friction (unitless)
79  0.01, // rollFriction (unitless)
80  0.2, // restitution (?)
81  0, // pretension (force)
82  false, // history (boolean)
83  100000, // maxTens
84  10000 // targetVelocity
85 #if (0)
86  20000 // maxAcc
87 #endif
88  // Use the below values for earlier versions of simulation.
89  // 1.006,
90  // 0.31,
91  // 300000.0,
92  // 3000.0,
93  // 15.0,
94  // 7.5,
95  };
96 } // namespace
97 
98 /*
99  * helper arrays for node and rod numbering schema
100  */
101 /*returns the number of the rod for a given node */
102 const int rodNumbersPerNode[13]={0,1,2,3,3,4,0,1,2,5,5,4,6};
103 
104 /*returns the node that is at the other end of the given node */
105 const int otherEndOfTheRod[13]={6,7,8,4,3,11,0,1,2,10,9,5,12};
106 
107 /*returns the node that is at the parallel rod
108  * and at the same end of the given node
109  */
110 const int parallelNode[13]={1,0,5,9,10,2,7,6,11,3,4,8,12};
111 
113 {
114 }
115 
117 {
118 }
119 
120 //Node numbers seen from Front
121 // -----0-------1------
122 // ---------2----------
123 // 3------------------4
124 // ---------5----------
125 // -----6-------7------
126 //
127 //Node numbers seen from Back
128 // -----0-------1------
129 // ---------8----------
130 // 9-----------------10
131 // ---------11---------
132 // -----6-------7------
133 //
134 
135 
136 void T12SuperBallPayload::addNodes(tgStructure& s)
137 {
138  const double half_length = c.rod_length / 2;
139 
140  nodePositions.push_back(btVector3(-half_length, c.rod_space, 0)); // 0
141  nodePositions.push_back(btVector3( half_length, c.rod_space, 0)); // 1
142  nodePositions.push_back(btVector3(0, half_length, -c.rod_space)); // 2
143  nodePositions.push_back(btVector3(-c.rod_space, 0, -half_length)); // 3
144  nodePositions.push_back(btVector3( c.rod_space, 0, -half_length)); // 4
145  nodePositions.push_back(btVector3(0, -half_length, -c.rod_space)); // 5
146  nodePositions.push_back(btVector3(-half_length, -c.rod_space, 0)); // 6
147  nodePositions.push_back(btVector3( half_length, -c.rod_space, 0)); // 7
148  nodePositions.push_back(btVector3(0, half_length, c.rod_space)); // 8
149  nodePositions.push_back(btVector3(-c.rod_space, 0, half_length)); // 9
150  nodePositions.push_back(btVector3( c.rod_space, 0, half_length)); // 10
151  nodePositions.push_back(btVector3(0, -half_length, c.rod_space)); // 11
152 
153  for(int i=0;i<12;i++)
154  {
155  s.addNode(nodePositions[i][0],nodePositions[i][1],nodePositions[i][2]);
156  }
157 }
158 
159 void T12SuperBallPayload::addRods(tgStructure& s)
160 {
161  s.addPair( 0, 6, "r1 rod");
162  s.addPair( 1, 7, "r2 rod");
163  s.addPair( 2, 8, "r3 rod");
164  s.addPair( 3, 4, "r4 rod");
165  s.addPair( 5, 11, "r5 rod");
166  s.addPair( 9, 10, "r6 rod");
167 
168 }
169 
170 
171 void T12SuperBallPayload::addMarkers(tgStructure &s)
172 {
173  std::vector<tgRod *> rods=find<tgRod>("rod");
174 
175  for(int i=0;i<12;i++)
176  {
177  const btRigidBody* bt = rods[rodNumbersPerNode[i]]->getPRigidBody();
178  btTransform inverseTransform = bt->getWorldTransform().inverse();
179  btVector3 pos = inverseTransform * (nodePositions[i]);
180  abstractMarker tmp=abstractMarker(bt,pos,btVector3(0.08*i,1.0 - 0.08*i,.0),i);
181  this->addMarker(tmp);
182  }
183 }
184 
185 void T12SuperBallPayload::addMuscles(tgStructure& s)
186 {
187 
188  int muscleConnections[13][13];
189  musclesPerNodes.resize(13);
190  for(int i=0;i<13;i++)
191  {
192  musclesPerNodes[i].resize(13);
193  for(int j=0;j<13;j++)
194  musclesPerNodes[i][j]=NULL;
195  }
196  for(int i=0;i<13;i++)
197  for(int j=0;j<13;j++)
198  muscleConnections[i][j]=-1;
199 
200  muscleConnections[0][3]=0;
201  muscleConnections[3][2]=0;
202  muscleConnections[2][0]=0;
203  muscleConnections[4][5]=0;
204  muscleConnections[5][7]=0;
205  muscleConnections[7][4]=0;
206  muscleConnections[1][8]=0;
207  muscleConnections[8][10]=0;
208  muscleConnections[10][1]=0;
209  muscleConnections[9][11]=0;
210  muscleConnections[11][6]=0;
211  muscleConnections[6][9]=0;
212  muscleConnections[1][2]=1;
213  muscleConnections[2][4]=1;
214  muscleConnections[4][1]=1;
215  muscleConnections[3][5]=1;
216  muscleConnections[5][6]=1;
217  muscleConnections[6][3]=1;
218  muscleConnections[0][8]=1;
219  muscleConnections[8][9]=1;
220  muscleConnections[9][0]=1;
221  muscleConnections[11][7]=1;
222  muscleConnections[7][10]=1;
223  muscleConnections[10][11]=1;
224 
225  for(int i=0;i<13;i++)
226  {
227  for(int j=0;j<13;j++)
228  {
229  if(muscleConnections[i][j]>=0)
230  {
231  std::stringstream tag;
232  tag<<"muscle-"<<i<<"-"<<j;
233  s.addPair(i, j, "muscle");
234  //musclesPerNodes[i][j]=s.addPair(i, j, tag);
235  //musclesPerNodes[j][i]=musclesPerNodes[i][j];
236  }
237  }
238  }
239 }
240 
242 {
243 
244  const tgRod::Config rodConfig(c.radius, c.density, c.friction,
245  c.rollFriction, c.restitution);
247  tgBasicActuator::Config muscleConfig(c.stiffness, c.damping, c.pretension,
248  c.history, c.maxTens, c.targetVelocity);
249 
250  // Start creating the structure
251  tgStructure s;
252  addNodes(s);
253  addRods(s);
254  addMuscles(s);
255 
256 // // Add a rotation. This is needed if the ground slopes too much,
257 // // otherwise glitches put a rod below the ground.
258 // btVector3 rotationPoint = btVector3(0, 0, 0); // origin
259 // btVector3 rotationAxis = btVector3(0, 1, 0); // y-axis
260 // double rotationAngle = M_PI/2;
261 // s.addRotation(rotationPoint, rotationAxis, rotationAngle);
262 
263  //s.move(btVector3(0,30,0));
264 
265  // Create the build spec that uses tags to turn the structure into a real model
266  tgBuildSpec spec;
267  spec.addBuilder("rod", new tgRodInfo(rodConfig));
268  spec.addBuilder("muscle", new tgBasicActuatorInfo(muscleConfig));
269 
270  // Create your structureInfo
271  tgStructureInfo structureInfo(s, spec);
272 
273  // Use the structureInfo to build ourselves
274  structureInfo.buildInto(*this, world);
275 
276  // We could now use tgCast::filter or similar to pull out the
277  // models (e.g. muscles) that we want to control.
278  allMuscles = tgCast::filter<tgModel, tgBasicActuator> (getDescendants());
279 
280  // call the onSetup methods of all observed things e.g. controllers
281  notifySetup();
282 
283  // Actually setup the children
284  tgModel::setup(world);
285 
286  //map the rods and add the markers to them
287  addMarkers(s);
288 
289  btVector3 location(0,10.0,0);
290  btVector3 rotation(0.0,0.6,0.8);
291  btVector3 speed(0,20,100);
292  this->moveModel(location,rotation,speed);
293 }
294 
296 {
297  // Precondition
298  if (dt <= 0.0)
299  {
300  throw std::invalid_argument("dt is not positive");
301  }
302  else
303  {
304  // Notify observers (controllers) of the step so that they can take action
305  notifyStep(dt);
306  tgModel::step(dt); // Step any children
307  }
308 }
309 
311 {
312  tgModel::onVisit(r);
313 }
314 
315 const std::vector<tgBasicActuator*>& T12SuperBallPayload::getAllMuscles() const
316 {
317  return allMuscles;
318 }
319 
321 {
323 }
324 
325 void T12SuperBallPayload::moveModel(btVector3 positionVector,btVector3 rotationVector,btVector3 speedVector)
326 {
327  std::vector<tgRod *> rods=find<tgRod>("rod");
328 
329  btQuaternion initialRotationQuat;
330  initialRotationQuat.setEuler(rotationVector[0],rotationVector[1],rotationVector[2]);
331  btTransform initialTransform;
332  initialTransform.setIdentity();
333  initialTransform.setRotation(initialRotationQuat);
334  initialTransform.setOrigin(positionVector);
335  for(int i=0;i<rods.size();i++)
336  {
337  rods[i]->getPRigidBody()->setLinearVelocity(speedVector);
338  rods[i]->getPRigidBody()->setWorldTransform(initialTransform * rods[i]->getPRigidBody()->getWorldTransform());
339  }
340 }
virtual void teardown()
Definition: tgModel.cpp:68
virtual void setup(tgWorld &world)
Definition: tgModel.cpp:57
Definition of class tgRodInfo.
Contains the definition of class T12SuperBallPayload. $Id$.
virtual void step(double dt)
Definition: tgModel.cpp:84
Markers for specific places on a tensegrity.
Definition of class tgBasicActuatorInfo.
virtual void onVisit(const tgModelVisitor &r) const
Definition: tgModel.cpp:107
void addPair(int fromNodeIdx, int toNodeIdx, std::string tags="")
Definition: tgStructure.cpp:66
Contains the definition of class tgBasicActuator.
Definition of class tgStructure.
Definition of class tgStructureInfo.
virtual void onVisit(tgModelVisitor &r)
virtual void step(double dt)
virtual void setup(tgWorld &world)
const std::vector< tgBasicActuator * > & getAllMuscles() const
Contains the definition of class tgRod.
Definition of class tgBuildSpec.
std::vector< tgModel * > getDescendants() const
Definition: tgModel.cpp:172
void addNode(double x, double y, double z, std::string tags="")
Definition: tgStructure.cpp:56