NTRT Simulator  Version: Master
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
BigPuppy.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 
29 //This application
30 #include "BigPuppy.h"
31 
32 // This library
33 #include "core/tgModel.h"
34 #include "core/tgSimView.h"
35 #include "core/tgSimViewGraphics.h"
36 #include "core/tgSimulation.h"
37 #include "core/tgWorld.h"
38 #include "core/tgBasicActuator.h"
39 #include "core/tgRod.h"
41 #include "core/tgString.h"
42 #include "tgcreator/tgBuildSpec.h"
44 #include "tgcreator/tgRodInfo.h"
45 #include "tgcreator/tgStructure.h"
47 // The Bullet Physics library
48 #include "LinearMath/btVector3.h"
49 // The C++ Standard Library
50 #include <iostream>
51 #include <stdexcept>
52 
53 BigPuppy::BigPuppy() :
54 tgModel()
55 {
56 }
57 
58 BigPuppy::~BigPuppy()
59 {
60 }
61 
63 {
64  //Rod and Muscle configuration
65 
66  const double density = 4.2/300.0; //Note: this needs to be high enough or things fly apart...
67  const double radius = 0.5;
68  const double friction = 0.5;
69  const double rollFriction = 0.0;
70  const double restitution = 0.0;
71  const tgRod::Config rodConfig(radius, density, friction, rollFriction, restitution);
72 
73  const double density2 = 0; // This will be used for the "anchor" segments.
74  const tgRod::Config rodConfig2(radius, density2, friction, rollFriction, restitution);
75 
76  const double stiffness = 1000.0;
77  const double damping = .01*stiffness;
78  const double pretension = 0.0;
79 
80  const tgSpringCableActuator::Config stringConfig(stiffness, damping, pretension, false, 7000, 24);
81 
82 
83  const double passivePretension = 700; // 5 N
84  tgSpringCableActuator::Config muscleConfig(2000, 20, passivePretension);
85 
86  // Calculations for the flemons spine model
87  double v_size = 10.0;
88 
89  //"Massless" right leg
90  tgStructure rightLeg;
91 
92  //Right Leg nodes:
93  rightLeg.addNode(0,0,0); //0: Bottom Center of lower leg segment
94  rightLeg.addNode(0,10,0); //1: Center of lower leg segment
95  rightLeg.addNode(10,10,0); //2: Right of lower leg segment
96  rightLeg.addNode(-10,10,0); //3: Left of lower leg segment
97  rightLeg.addNode(0,20,0); //4: Top of lower leg segment
98  rightLeg.addNode(12,2,3); //5: Big toe; was y=0, z=5
99  rightLeg.addNode(12,1.5,-3.5); //6: Little toe; was y=0, z=-5
100  rightLeg.addNode(0,10,5); //7: Outer ankle
101  rightLeg.addNode(0,10,-5); //8: Inner ankle
102 
103  //Adding some extra nodes to stabilize base of leg:
104  rightLeg.addNode(4,0,0); //9
105  rightLeg.addNode(-4,0,0); //10
106  rightLeg.addNode(0,0,4); //11
107  rightLeg.addNode(0,0,-4); //12
108 
109  //Adding a toe extension
110  rightLeg.addNode(15,0,5); //13: Big toe
111  rightLeg.addNode(14,0,-5); //14: Little toe
112 
113  //Add rods for right leg: making this massless, except the two cross rods and toes
114  rightLeg.addPair(0,1,"rod2");
115  rightLeg.addPair(1,2,"rod2");
116  rightLeg.addPair(1,3,"rod2");
117  rightLeg.addPair(1,4,"rod2");
118  rightLeg.addPair(5,8,"rod");
119  rightLeg.addPair(6,7,"rod");
120  //Toe extension rods
121  rightLeg.addPair(5,13,"rod");
122  rightLeg.addPair(6,14,"rod");
123 
124  //Base rods: making these massless
125  rightLeg.addPair(0,9,"rod2");
126  rightLeg.addPair(0,10,"rod2");
127  rightLeg.addPair(0,11,"rod2");
128  rightLeg.addPair(0,12,"rod2");
129 
130  //Right leg with mass:
131  tgStructure rightLeg2;
132 
133  //Right Leg nodes:
134  rightLeg2.addNode(0,0,0); //0: Bottom Center of lower leg segment
135  rightLeg2.addNode(0,10,0); //1: Center of lower leg segment
136  rightLeg2.addNode(10,10,0); //2: Right of lower leg segment
137  rightLeg2.addNode(-10,10,0); //3: Left of lower leg segment
138  rightLeg2.addNode(0,20,0); //4: Top of lower leg segment
139  rightLeg2.addNode(12,2,3); //5: Big toe; was y=0, z=5
140  rightLeg2.addNode(12,1.5,-3.5); //6: Little toe; was y=0, z=-5
141  rightLeg2.addNode(0,10,5); //7: Outer ankle
142  rightLeg2.addNode(0,10,-5); //8: Inner ankle
143 
144  //Adding some extra nodes to stabilize base of leg:
145  rightLeg2.addNode(4,0,0); //9
146  rightLeg2.addNode(-4,0,0); //10
147  rightLeg2.addNode(0,0,4); //11
148  rightLeg2.addNode(0,0,-4); //12
149 
150  //Adding a toe extension
151  rightLeg2.addNode(15,0,5); //13: Big toe
152  rightLeg2.addNode(14,0,-5); //14: Little toe
153 
154  //Add rods for right leg:
155  rightLeg2.addPair(0,1,"rod");
156  rightLeg2.addPair(1,2,"rod");
157  rightLeg2.addPair(1,3,"rod");
158  rightLeg2.addPair(1,4,"rod");
159  rightLeg2.addPair(5,8,"rod");
160  rightLeg2.addPair(6,7,"rod");
161  //Toe extension rods
162  rightLeg2.addPair(5,13,"rod");
163  rightLeg2.addPair(6,14,"rod");
164 
165  //Base rods:
166  rightLeg2.addPair(0,9,"rod");
167  rightLeg2.addPair(0,10,"rod");
168  rightLeg2.addPair(0,11,"rod");
169  rightLeg2.addPair(0,12,"rod");
170 
171 
172  //"Massless" left leg
173  tgStructure leftLeg;
174 
175  //Left Leg nodes:
176  leftLeg.addNode(0,0,0); //0: Bottom Center of lower leg segment
177  leftLeg.addNode(0,10,0); //1: Center of lower leg segment
178  leftLeg.addNode(10,10,0); //2: Right of lower leg segment
179  leftLeg.addNode(-10,10,0); //3: Left of lower leg segment
180  leftLeg.addNode(0,20,0); //4: Top of lower leg segment
181  leftLeg.addNode(12,2,3); //5: Little toe; was y=0, z=5
182  leftLeg.addNode(12,1.5,-3.5); //6: Big toe; was y=0, z=-5
183  leftLeg.addNode(0,10,5); //7: Inner ankle
184  leftLeg.addNode(0,10,-5); //8: Outer ankle
185 
186  //Adding some extra nodes to stabilize base of leg:
187  leftLeg.addNode(4,0,0); //9
188  leftLeg.addNode(-4,0,0); //10
189  leftLeg.addNode(0,0,4); //11
190  leftLeg.addNode(0,0,-4); //12
191 
192  //Adding a toe extension
193  leftLeg.addNode(15,0,5); //13: Little toe
194  leftLeg.addNode(14,0,-5); //14: Big toe
195 
196  //Add rods for left leg:
197  leftLeg.addPair(0,1,"rod2");
198  leftLeg.addPair(1,2,"rod2");
199  leftLeg.addPair(1,3,"rod2");
200  leftLeg.addPair(1,4,"rod2");
201  leftLeg.addPair(5,8,"rod");
202  leftLeg.addPair(6,7,"rod");
203  //Toe extension rod
204  leftLeg.addPair(5,13,"rod");
205  leftLeg.addPair(6,14,"rod");
206 
207  //Base rods:
208  leftLeg.addPair(0,9,"rod2");
209  leftLeg.addPair(0,10,"rod2");
210  leftLeg.addPair(0,11,"rod2");
211  leftLeg.addPair(0,12,"rod2");
212 
213  //Left leg with mass:
214  tgStructure leftLeg2;
215 
216  //Left Leg nodes:
217  leftLeg2.addNode(0,0,0); //0: Bottom Center of lower leg segment
218  leftLeg2.addNode(0,10,0); //1: Center of lower leg segment
219  leftLeg2.addNode(10,10,0); //2: Right of lower leg segment
220  leftLeg2.addNode(-10,10,0); //3: Left of lower leg segment
221  leftLeg2.addNode(0,20,0); //4: Top of lower leg segment
222  leftLeg2.addNode(12,2,3); //5: Little toe; was y=0, z=5
223  leftLeg2.addNode(12,1.5,-3.5); //6: Big toe; was y=0, z=-5
224  leftLeg2.addNode(0,10,5); //7: Inner ankle
225  leftLeg2.addNode(0,10,-5); //8: Outer ankle
226 
227  //Adding some extra nodes to stabilize base of leg:
228  leftLeg2.addNode(4,0,0); //9
229  leftLeg2.addNode(-4,0,0); //10
230  leftLeg2.addNode(0,0,4); //11
231  leftLeg2.addNode(0,0,-4); //12
232 
233  //Adding a toe extension
234  leftLeg2.addNode(15,0,5); //13: Little toe
235  leftLeg2.addNode(14,0,-5); //14: Big toe
236 
237  //Add rods for left leg:
238  leftLeg2.addPair(0,1,"rod");
239  leftLeg2.addPair(1,2,"rod");
240  leftLeg2.addPair(1,3,"rod");
241  leftLeg2.addPair(1,4,"rod");
242  leftLeg2.addPair(5,8,"rod");
243  leftLeg2.addPair(6,7,"rod");
244  //Toe extension rod
245  leftLeg2.addPair(5,13,"rod");
246  leftLeg2.addPair(6,14,"rod");
247 
248  //Base rods:
249  leftLeg2.addPair(0,9,"rod");
250  leftLeg2.addPair(0,10,"rod");
251  leftLeg2.addPair(0,11,"rod");
252  leftLeg2.addPair(0,12,"rod");
253 
254  //Create the basic unit of the spine
255  tgStructure tetra;
256 
257  //Add the nodes
258  tetra.addNode(0,0,0); //Node 0
259  tetra.addNode(v_size, 0, v_size); //Node 1
260  tetra.addNode(v_size, 0, -v_size); //Node 2
261  tetra.addNode(-v_size, 0, -v_size); //Node 3
262  tetra.addNode(-v_size, 0, v_size); //Node 4
263 
264  tetra.addPair(0,1,"rod");
265  tetra.addPair(0,2,"rod");
266  tetra.addPair(0,3,"rod");
267  tetra.addPair(0,4,"rod");
268 
269  //Create the basic unit for the hips/shoulders:
270  tgStructure lHip;
271 
272  lHip.addNode(0,0,0); //Node 0
273  lHip.addNode(0, v_size, v_size); //Node 1
274  lHip.addNode(0, -v_size, -v_size); //Node 2
275  lHip.addNode(0, -v_size, v_size); //Node 3
276 
277  lHip.addPair(0,1,"rod");
278  lHip.addPair(0,2,"rod");
279  lHip.addPair(0,3,"rod");
280 
281  tgStructure rHip;
282 
283  rHip.addNode(0,0,0); //Node 0
284  rHip.addNode(0, v_size, -v_size); //Node 1
285  rHip.addNode(0, -v_size, -v_size); //Node 2
286  rHip.addNode(0, -v_size, v_size); //Node 3
287 
288  rHip.addPair(0,1,"rod");
289  rHip.addPair(0,2,"rod");
290  rHip.addPair(0,3,"rod");
291 
292  //Build the spine
293  tgStructure spine;
294  const double offsetDist = v_size + 1; //So rod ends don't touch, may need to adjust
295  const double offsetDist2 = v_size*5 + 5 + 3.3;
296  const double offsetDist3 = v_size*6;
297  std::size_t m_segments = 6;
298  std::size_t m_hips = 4;
299  std::size_t m_legs = 4;
300  //std::size_t m_anchors = 2; //Will need to change to three, if anchor all but one leg.
301  btVector3 offset(offsetDist,0.0,0);
302  btVector3 offset1(offsetDist*2,0.0,offsetDist);
303  btVector3 offset2(offsetDist2,0.0,offsetDist);
304  btVector3 offset3(offsetDist*2,0.0,-offsetDist);
305  btVector3 offset4(offsetDist2,0.0,-offsetDist);
306  btVector3 offset5(offsetDist3,-21.0,offsetDist);
307  btVector3 offset6(offsetDist3,-21.0,-offsetDist);
308  btVector3 offset7(v_size*2,-21.0,offsetDist);
309  btVector3 offset8(v_size*2,-21.0,-offsetDist);
310  //btVector3 offset9(offsetDist3,-20.5,offsetDist);
311  //btVector3 offset10(offsetDist3,-20.5,-offsetDist);
312 
313  for(std::size_t i = 0; i < m_segments; i++) { //Connect segments for spine
314  tgStructure* t = new tgStructure (tetra);
315  t->addTags(tgString("segment num", i + 1));
316  t->move((i + 1)*offset);
317 
318  if (i % 2 == 1){
319 
320  t->addRotation(btVector3((i + 1) * offsetDist, 0.0, 0.0), btVector3(1, 0, 0), 0.0);
321 
322  }
323  else{
324 
325  t->addRotation(btVector3((i + 1) * offsetDist, 0.0, 0.0), btVector3(1, 0, 0), M_PI/2.0);
326 
327  }
328 
329  spine.addChild(t); //Add a segment to the spine
330  }
331 
332  for(std::size_t i = m_segments; i < (m_segments + 2); i++) {//deal with right hip and shoulder first
333  tgStructure* t = new tgStructure (rHip);
334  t->addTags(tgString("segment num", i + 1));
335 
336  if(i % 2 == 0){
337  t->move(offset1);
338  t->addRotation(btVector3(offsetDist*2, 0.0, offsetDist), btVector3(1, 0, 0), 0.0);
339  }
340  else{
341  t->move(offset2);
342  t->addRotation(btVector3(offsetDist2, 0.0, offsetDist), btVector3(0, 0, 1), M_PI*1/8);
343  }
344 
345  spine.addChild(t); //Add a segment to the spine
346  }
347 
348  for(std::size_t i = (m_segments + 2); i < (m_segments + m_hips); i++) {//deal with left hip and shoulder now
349  tgStructure* t = new tgStructure (lHip);
350  t->addTags(tgString("segment num", i + 1));
351 
352  if(i % 2 == 0){
353  t->move(offset3);
354  t->addRotation(btVector3(offsetDist*2, 0.0, -offsetDist), btVector3(1, 0, 0), 0.0);
355  }
356  else{
357  t->move(offset4);
358  t->addRotation(btVector3(offsetDist2, 0.0, -offsetDist), btVector3(0, 0, 1), M_PI*1/8);
359  }
360 
361  spine.addChild(t); //Add a segment to the spine
362 
363  }
364 
365  for(std::size_t i = (m_segments + m_hips); i < (m_segments + m_hips + 2); i++) {//right front and back legs
366  tgStructure* t = new tgStructure (rightLeg);
367  t->addTags(tgString("segment num", i + 1));
368 
369  if(i % 2 == 0){//Front leg
370  tgStructure* t = new tgStructure (rightLeg2);
371  t->addTags(tgString("segment num", i + 1));
372  t->move(offset7);
373  t->addRotation(btVector3(v_size*2, -21.0, offsetDist), btVector3(0, 1, 0), M_PI);
374  spine.addChild(t); //Add a segment to the spine
375  }
376  else{ //Hind leg
377  tgStructure* t = new tgStructure (rightLeg);
378  t->addTags(tgString("segment num", i + 1));
379  t->move(offset5);
380  t->addRotation(btVector3(offsetDist3, -21.0, offsetDist), btVector3(0, 1, 0), M_PI);
381  spine.addChild(t); //Add a segment to the spine
382  }
383  }
384 
385  for(std::size_t i = (m_segments + m_hips + 2); i < (m_segments + m_hips + m_legs); i++) {//left front and back legs
386  tgStructure* t = new tgStructure (leftLeg);
387  t->addTags(tgString("segment num", i + 1));
388 
389  if(i % 2 == 0){//Front leg
390  tgStructure* t = new tgStructure (leftLeg2);
391  t->addTags(tgString("segment num", i + 1));
392  t->move(offset8);
393  t->addRotation(btVector3(v_size*2, -21.0, -offsetDist), btVector3(0, 1, 0), M_PI);
394  spine.addChild(t); //Add a segment to the spine
395  }
396  else{//Hind leg
397  tgStructure* t = new tgStructure (leftLeg);
398  t->addTags(tgString("segment num", i + 1));
399  t->move(offset6);
400  t->addRotation(btVector3(offsetDist3, -21.0, -offsetDist), btVector3(0, 1, 0), M_PI);
401  spine.addChild(t); //Add a segment to the spine
402  }
403  }
404 
405  spine.move(btVector3(0.0,21.0,0.0));
406 
407 
408  std::vector<tgStructure*> children = spine.getChildren();
409  for(std::size_t i = 2; i < (children.size() - (m_hips + m_legs)); i++) {
410 
411  tgNodes n0 = children[i-2]->getNodes();
412  tgNodes n1 = children[i-1]->getNodes();
413  tgNodes n2 = children[i]->getNodes();
414 
415  if(i==2){
416  //Extra muscles, to keep front vertebra from swinging. Need to fold this into loop above, for consistency
417  spine.addPair(n0[3], n1[3], tgString("spine front upper right muscle seg", i-2) + tgString(" seg", i-1));
418  spine.addPair(n0[3], n1[4], tgString("spine front upper left muscle seg", i-2) + tgString(" seg", i-1));
419  }
420 
421  //Add muscles to the spine
422  if(i < 3){
423  if(i % 2 == 0){ //front
424  spine.addPair(n0[1], n1[3], tgString("spine front lower right muscle seg", i-2) + tgString(" seg", i-1));
425  spine.addPair(n0[1], n1[4], tgString("spine front lower left muscle seg", i-2) + tgString(" seg", i-1));
426  spine.addPair(n0[2], n1[3], tgString("spine front upper right muscle seg", i-2) + tgString(" seg", i-1));
427  spine.addPair(n0[2], n1[4], tgString("spine front upper left muscle seg", i-2) + tgString(" seg", i-1));
428  }
429  else{ //rear
430  spine.addPair(n0[1], n1[3], tgString("spine rear upper left muscle seg", i-2) + tgString(" seg", i-1));
431  spine.addPair(n0[1], n1[4], tgString("spine rear lower left muscle seg", i-2) + tgString(" seg", i-1));
432  spine.addPair(n0[2], n1[3], tgString("spine rear upper right muscle seg", i-2) + tgString(" seg", i-1));
433  spine.addPair(n0[2], n1[4], tgString("spine rear lower right muscle seg", i-2) + tgString(" seg", i-1));
434  }
435  }
436  if(i < 6){
437  if(i % 2 == 0){
438  spine.addPair(n0[1], n2[4], tgString("spine bottom muscle seg", i-2) + tgString(" seg", i-1));
439  spine.addPair(n0[2], n2[3], tgString("spine top muscle seg", i-2) + tgString(" seg", i-1));
440  }
441  else{
442  spine.addPair(n0[1], n2[4], tgString("spine lateral left muscle seg", i-2) + tgString(" seg", i-1));
443  spine.addPair(n0[2], n2[3], tgString("spine lateral right muscle seg", i-2) + tgString(" seg", i-1));
444 
445  }
446  }
447  if(i > 0 && i < 5){
448  if(i % 2 == 0){//rear
449  spine.addPair(n1[1], n2[3], tgString("spine rear upper left muscle seg", i-1) + tgString(" seg", i));
450  spine.addPair(n1[1], n2[4], tgString("spine rear lower left muscle seg", i-1) + tgString(" seg", i));
451  spine.addPair(n1[2], n2[3], tgString("spine rear upper right muscle seg", i-1) + tgString(" seg", i));
452  spine.addPair(n1[2], n2[4], tgString("spine rear lower right muscle seg", i-1) + tgString(" seg", i));
453  }
454  else{//front
455 
456  spine.addPair(n1[1], n2[3], tgString("spine front lower right muscle seg", i-1) + tgString(" seg", i));
457  spine.addPair(n1[1], n2[4], tgString("spine front lower left muscle seg", i-1) + tgString(" seg", i));
458  spine.addPair(n1[2], n2[3], tgString("spine front upper right muscle seg", i-1) + tgString(" seg", i));
459  spine.addPair(n1[2], n2[4], tgString("spine front upper left muscle seg", i-1) + tgString(" seg", i));
460  }
461  }
462  if(i == 5){
463  //rear
464  spine.addPair(n1[1], n2[1], tgString("spine rear lower left muscle seg", i-1) + tgString(" seg", i));
465  spine.addPair(n1[1], n2[2], tgString("spine rear lower right muscle seg", i-1) + tgString(" seg", i));
466  spine.addPair(n1[2], n2[1], tgString("spine rear upper left muscle seg", i-1) + tgString(" seg", i));
467  spine.addPair(n1[2], n2[2], tgString("spine rear upper right muscle seg", i-1) + tgString(" seg", i));
468  //front
469  spine.addPair(n1[1], n2[3], tgString("spine front lower right muscle seg", i-1) + tgString(" seg", i));
470  spine.addPair(n1[1], n2[4], tgString("spine front lower left muscle seg", i-1) + tgString(" seg", i));
471  spine.addPair(n1[2], n2[3], tgString("spine front upper right muscle seg", i-1) + tgString(" seg", i));
472  spine.addPair(n1[2], n2[4], tgString("spine front upper left muscle seg", i-1) + tgString(" seg", i));
473 
474  }
475 
476  }
477 
478 
479  //Now add muscles to hips....
480  tgNodes n0 = children[0]->getNodes();
481  tgNodes n1 = children[1]->getNodes();
482  tgNodes n2 = children[2]->getNodes();
483  tgNodes n3 = children[3]->getNodes();
484  tgNodes n4 = children[4]->getNodes();
485  tgNodes n5 = children[5]->getNodes();
486  tgNodes n6 = children[6]->getNodes();
487  tgNodes n7 = children[7]->getNodes();
488  tgNodes n8 = children[8]->getNodes();
489  tgNodes n9 = children[9]->getNodes();
490  tgNodes n10 = children[10]->getNodes();
491  tgNodes n11 = children[11]->getNodes();
492  tgNodes n12 = children[12]->getNodes();
493  tgNodes n13 = children[13]->getNodes();
494 
495  //Left shoulder muscles
496  spine.addPair(n6[1], n1[1], tgString("left shoulder rear upper muscle seg", 6) + tgString(" seg", 1));
497  spine.addPair(n6[1], n1[4], tgString("left shoulder front upper muscle seg", 6) + tgString(" seg", 1));
498  spine.addPair(n6[1], n0[2], tgString("left shoulder front top muscle seg", 6) + tgString(" seg", 0));
499  spine.addPair(n6[1], n2[3], tgString("left shoulder rear top muscle seg", 6) + tgString(" seg", 2));
500 
501  spine.addPair(n6[2], n1[1], tgString("left shoulder rear lower muscle seg", 6) + tgString(" seg", 1));
502  spine.addPair(n6[2], n1[4], tgString("left shoulder front lower muscle seg", 6) + tgString(" seg", 1));
503  spine.addPair(n6[2], n0[1], tgString("left shoulder front bottom muscle seg", 6) + tgString(" seg", 0));
504  spine.addPair(n6[2], n2[4], tgString("left shoulder rear bottom muscle seg", 6) + tgString(" seg", 2));
505 
506  //Extra muscles, to move left shoulder forward and back:
507  spine.addPair(n6[0], n1[1], tgString("left shoulder rear mid muscle seg", 6) + tgString(" seg", 1));
508  spine.addPair(n6[0], n1[4], tgString("left shoulder front mid muscle seg", 6) + tgString(" seg", 1));
509 
510  //Left hip muscles
511  spine.addPair(n7[1], n5[1], tgString("left hip rear upper muscle seg", 7) + tgString(" seg", 5));
512  spine.addPair(n7[1], n5[4], tgString("left hip front upper muscle seg", 7) + tgString(" seg", 5));
513  spine.addPair(n7[1], n4[2], tgString("left hip rear top muscle seg", 7) + tgString(" seg", 4));
514  spine.addPair(n7[1], n4[3], tgString("left hip front top muscle seg", 7) + tgString(" seg", 4));
515 
516  spine.addPair(n7[2], n5[1], tgString("left hip rear lower muscle seg", 7) + tgString(" seg", 5));
517  spine.addPair(n7[2], n5[4], tgString("left hip front lower muscle seg", 7) + tgString(" seg", 5));
518  spine.addPair(n7[2], n4[1], tgString("left hip bottom muscle seg", 7) + tgString(" seg", 4));
519 
520  //Extra muscles, to move left hip forward and back:
521  spine.addPair(n7[0], n3[1], tgString("left hip rear mid muscle seg", 7) + tgString(" seg", 3)); //could also be n3[3]
522  spine.addPair(n7[0], n5[4], tgString("left hip front mid muscle seg", 7) + tgString(" seg", 5));
523 
524  //Inter-hip connector muscle
525  spine.addPair(n7[2], n9[3], tgString("inter-hip bottom muscle seg", 7) + tgString(" seg", 9)); //inter-hip bottom muscle
526 
527  //Right shoulder muscles
528  spine.addPair(n8[1], n1[2], tgString("right shoulder rear upper muscle seg", 8) + tgString(" seg", 1));
529  spine.addPair(n8[1], n1[3], tgString("right shoulder front upper muscle seg", 8) + tgString(" seg", 1));
530  spine.addPair(n8[1], n0[2], tgString("right shoulder front top muscle seg", 8) + tgString(" seg", 0));
531  spine.addPair(n8[1], n2[3], tgString("right shoulder rear top muscle seg", 8) + tgString(" seg", 2));
532 
533  spine.addPair(n8[3], n1[2], tgString("right shoulder rear lower muscle seg", 8) + tgString(" seg", 1));
534  spine.addPair(n8[3], n1[3], tgString("right shoulder front lower muscle seg", 8) + tgString(" seg", 1));
535  spine.addPair(n8[3], n0[1], tgString("right shoulder front bottom muscle seg", 8) + tgString(" seg", 0));
536  spine.addPair(n8[3], n2[4], tgString("right shoulder rear bottom muscle seg", 8) + tgString(" seg", 2));
537 
538  //Extra muscles, to move right shoulder forward and back:
539  spine.addPair(n8[0], n1[2], tgString("right shoulder rear mid muscle seg", 8) + tgString(" seg", 1));
540  spine.addPair(n8[0], n1[3], tgString("right shoulder front mid muscle seg", 8) + tgString(" seg", 1));
541 
542  //Right hip muscles
543  spine.addPair(n9[1], n5[2], tgString("right hip rear upper muscle seg", 9) + tgString(" seg", 5));
544  spine.addPair(n9[1], n5[3], tgString("right hip front upper muscle seg", 9) + tgString(" seg", 5));
545  spine.addPair(n9[1], n4[2], tgString("right hip rear top muscle seg", 9) + tgString(" seg", 4));
546  spine.addPair(n9[1], n4[3], tgString("right hip front top muscle seg", 9) + tgString(" seg", 4));
547 
548  spine.addPair(n9[3], n5[2], tgString("right hip rear lower muscle seg", 9) + tgString(" seg", 5));
549  spine.addPair(n9[3], n5[3], tgString("right hip front lower muscle seg", 9) + tgString(" seg", 5));
550  spine.addPair(n9[3], n4[1], tgString("right hip bottom muscle seg", 9) + tgString(" seg", 4));
551 
552  //Extra muscles, to move right hip forward and back:
553  spine.addPair(n9[0], n3[2], tgString("right hip rear mid muscle seg", 9) + tgString(" seg", 3)); //could also be n3[3]
554  spine.addPair(n9[0], n5[3], tgString("right hip front mid muscle seg", 9) + tgString(" seg", 5));
555 
556  //Leg/hip connections:
557 
558  //Right front leg/shoulder
559  spine.addPair(n10[4], n6[2], tgString("right outer bicep muscle seg", 10) + tgString(" seg", 6));
560  spine.addPair(n10[4], n6[3], tgString("right inner bicep muscle seg", 10) + tgString(" seg", 6));
561  //spine.addPair(n10[4], n1[1], tgString("right front abdomen connection muscle seg", 10) + tgString(" seg", 1));
562 
563  spine.addPair(n10[3], n6[2], tgString("right outer tricep muscle seg", 10) + tgString(" seg", 6));
564  spine.addPair(n10[3], n6[3], tgString("right inner tricep muscle seg", 10) + tgString(" seg", 6));
565 
566  spine.addPair(n10[2], n6[2], tgString("right outer front tricep muscle seg", 10) + tgString(" seg", 6));
567  spine.addPair(n10[2], n6[3], tgString("right inner front tricep muscle seg", 10) + tgString(" seg", 6));
568 
569  //Adding muscle to pull up on right front leg:
570  spine.addPair(n10[4], n6[1], tgString("right mid bicep muscle seg", 10) + tgString(" seg", 6));
571 
572  //Left front leg/shoulder
573  spine.addPair(n12[4], n8[2], tgString("left inner bicep muscle seg", 12) + tgString(" seg", 8));
574  spine.addPair(n12[4], n8[3], tgString("left outer bicep muscle seg", 12) + tgString(" seg", 8));
575  //spine.addPair(n12[4], n1[2], tgString("left front abdomen connection muscle seg", 12) + tgString(" seg", 1)); //Was n1[2]
576 
577  spine.addPair(n12[3], n8[2], tgString("left inner tricep muscle seg", 12) + tgString(" seg", 8));
578  spine.addPair(n12[3], n8[3], tgString("left outer tricep muscle seg", 12) + tgString(" seg", 8));
579 
580  spine.addPair(n12[2], n8[2], tgString("left inner front tricep muscle seg", 12) + tgString(" seg", 8));
581  spine.addPair(n12[2], n8[3], tgString("left outer front tricep muscle seg", 12) + tgString(" seg", 8));
582 
583  //Adding muscle to pull up on left front leg:
584  spine.addPair(n12[4], n8[1], tgString("left mid bicep muscle seg", 12) + tgString(" seg", 8));
585 
586  //Right rear leg/hip
587  spine.addPair(n11[4], n7[2], tgString("right outer thigh muscle seg", 11) + tgString(" seg", 7));
588  spine.addPair(n11[4], n7[3], tgString("right inner thigh muscle seg", 11) + tgString(" seg", 7));
589  //spine.addPair(n11[4], n3[1],tgString("right rear abdomen connection muscle seg", 11) + tgString(" seg", 4));
590 
591  spine.addPair(n11[3], n7[2], tgString("right outer calf muscle seg", 11) + tgString(" seg", 7));
592  spine.addPair(n11[3], n7[3], tgString("right inner calf muscle seg", 11) + tgString(" seg", 7));
593 
594  spine.addPair(n11[2], n7[2], tgString("right outer front calf muscle seg", 11) + tgString(" seg", 7));
595  spine.addPair(n11[2], n7[3], tgString("right inner front calf muscle seg", 11) + tgString(" seg", 7));
596 
597  //Adding muscle to pull rear right leg up:
598  spine.addPair(n11[4], n7[1], tgString("right central thigh muscle seg", 11) + tgString(" seg", 7));
599 
600  //Left rear leg/hip
601  spine.addPair(n13[4], n9[2], tgString("left inner thigh muscle seg", 13) + tgString(" seg", 9));
602  spine.addPair(n13[4], n9[3], tgString("left outer thigh muscle seg", 13) + tgString(" seg", 9));
603  //spine.addPair(n13[4], n3[2], tgString("left rear abdomen connection muscle seg", 13) + tgString(" seg", 4));
604 
605  spine.addPair(n13[3], n9[2], tgString("left inner calf muscle seg", 13) + tgString(" seg", 9));
606  spine.addPair(n13[3], n9[3], tgString("left outer calf muscle seg", 13) + tgString(" seg", 9));
607 
608  spine.addPair(n13[2], n9[2], tgString("left inner front calf muscle seg", 13) + tgString(" seg", 9));
609  spine.addPair(n13[2], n9[3], tgString("left outer front calf muscle seg", 13) + tgString(" seg", 9));
610 
611  //Adding muscle to pull rear left leg up:
612  spine.addPair(n13[4], n9[1], tgString("left central thigh muscle seg", 13) + tgString(" seg", 9));
613 
614  //Populate Legs with muscles
615  for(std::size_t i = (m_segments + m_hips); i < children.size(); i++) {
616 
617  tgNodes ni = children[i]->getNodes();
618 
619  if(i < (m_segments + m_legs + 2)){
620 
621  spine.addPair(ni[5], ni[7], tgString("right leg big outer muscle seg", i));
622  spine.addPair(ni[6], ni[8], tgString("right leg little inner muscle seg", i));
623  spine.addPair(ni[5], ni[6], tgString("right leg big little muscle seg", i));
624  spine.addPair(ni[0], ni[7], tgString("right leg lower outer muscle seg", i));
625  spine.addPair(ni[0], ni[8], tgString("right leg lower inner muscle seg", i));
626  spine.addPair(ni[2], ni[7], tgString("right leg front outer muscle seg", i));
627  spine.addPair(ni[2], ni[8], tgString("right leg front inner muscle seg", i));
628  spine.addPair(ni[3], ni[7], tgString("right leg heel outer muscle seg", i));
629  spine.addPair(ni[3], ni[8], tgString("right leg heel inner muscle seg", i));
630  spine.addPair(ni[4], ni[7], tgString("right leg upper outer muscle seg", i));
631  spine.addPair(ni[4], ni[8], tgString("right leg upper inner muscle seg", i));
632 
633  }
634  else{
635 
636  spine.addPair(ni[5], ni[7], tgString("left leg litte inner muscle seg", i));
637  spine.addPair(ni[6], ni[8], tgString("left leg big outer muscle seg", i));
638  spine.addPair(ni[5], ni[6], tgString("left leg big little muscle seg", i));
639  spine.addPair(ni[0], ni[7], tgString("left leg lower inner muscle seg", i));
640  spine.addPair(ni[0], ni[8], tgString("left leg lower outer muscle seg", i));
641  spine.addPair(ni[2], ni[7], tgString("left leg front inner muscle seg", i));
642  spine.addPair(ni[2], ni[8], tgString("left leg front outer muscle seg", i));
643  spine.addPair(ni[3], ni[7], tgString("left leg heel inner muscle seg", i));
644  spine.addPair(ni[3], ni[8], tgString("left leg heel outer muscle seg", i));
645  spine.addPair(ni[4], ni[7], tgString("left leg upper inner muscle seg", i));
646  spine.addPair(ni[4], ni[8], tgString("left leg upper outer muscle seg", i));
647 
648  }
649 
650  }
651 
652  // Create the build spec that uses tags to turn the structure into a real model
653  tgBuildSpec spec;
654  spec.addBuilder("rod", new tgRodInfo(rodConfig));
655  spec.addBuilder("rod2", new tgRodInfo(rodConfig2));
656  spec.addBuilder("muscle", new tgBasicActuatorInfo(muscleConfig));
657 
658  // Create your structureInfo
659  tgStructureInfo structureInfo(spine, spec);
660 
661  // Use the structureInfo to build ourselves
662  structureInfo.buildInto(*this, world);
663 
664  // We could now use tgCast::filter or similar to pull out the
665  // models (e.g. muscles) that we want to control.
666  allActuators = tgCast::filter<tgModel, tgSpringCableActuator> (getDescendants());
667 
668  // Notify controllers that setup has finished.
669  notifySetup();
670 
671  // Actually setup the children
672  tgModel::setup(world);
673 
674  children.clear();
675 }
676 
677 void BigPuppy::step(double dt)
678 {
679  // Precondition
680  if (dt <= 0.0)
681  {
682  throw std::invalid_argument("dt is not positive");
683  }
684  else
685  {
686  // Notify observers (controllers) of the step so that they can take action
687  notifyStep(dt);
688  tgModel::step(dt); // Step any children
689  }
690 }
691 
692 const std::vector<tgSpringCableActuator*>& BigPuppy::getAllActuators() const
693 {
694  return allActuators;
695 }
696 
698 {
699  notifyTeardown();
701 }
const std::vector< tgStructure * > & getChildren() const
Definition: tgStructure.h:184
virtual void teardown()
Definition: tgModel.cpp:68
virtual void setup(tgWorld &world)
Definition: tgModel.cpp:57
void addChild(tgStructure *child)
Definition of class tgRodInfo.
virtual void step(double dt)
Definition: BigPuppy.cpp:677
const std::vector< tgSpringCableActuator * > & getAllActuators() const
Definition: BigPuppy.cpp:692
Convenience function for combining strings with ints, mostly for naming structures.
virtual void step(double dt)
Definition: tgModel.cpp:84
virtual void teardown()
Definition: BigPuppy.cpp:697
Definition of class tgBasicActuatorInfo.
Contains the definition of class tgSimulation.
Contains the definition of class tgModel.
void addPair(int fromNodeIdx, int toNodeIdx, std::string tags="")
Definition: tgStructure.cpp:80
Contains the definition of class tgSimViewGraphics.
Contains the definition of abstract base class tgSpringCableActuator. Assumes that the string is line...
void addRotation(const btVector3 &fixedPoint, const btVector3 &axis, double angle)
Contains the definition of class tgBasicActuator.
std::string tgString(std::string s, int i)
Definition: tgString.h:33
Contains the definition of class tgWorld $Id$.
Definition of class tgStructure.
Definition of class tgStructureInfo.
Contains the definition of class tgSimView.
Contains the definition of class tgRod.
Definition of class tgBuildSpec.
void notifyStep(double dt)
virtual void setup(tgWorld &world)
Definition: BigPuppy.cpp:62
std::vector< tgModel * > getDescendants() const
Definition: tgModel.cpp:170
void buildInto(tgModel &model, tgWorld &world)
void addNode(double x, double y, double z, std::string tags="")
Definition: tgStructure.cpp:70