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"
42 #include "core/tgRod.h"
44 #include "core/tgString.h"
45 #include "tgcreator/tgBuildSpec.h"
47 #include "tgcreator/tgRodInfo.h"
48 #include "tgcreator/tgStructure.h"
50 // The Bullet Physics library
51 #include "LinearMath/btVector3.h"
52 // The C++ Standard Library
53 #include <iostream>
54 #include <stdexcept>
55 
56 //#define USE_KINEMATIC
57 #define PASSIVE_STRUCTURE
58 
59 BigPuppy::BigPuppy() :
60 tgModel()
61 {
62 }
63 
64 BigPuppy::~BigPuppy()
65 {
66 }
67 
68 void BigPuppy::addNodesFoot(tgStructure& s, double r1, double r2){
69  s.addNode(r2,0,r2);//0
70  s.addNode(r2,0,-r2);//1
71  s.addNode(-r2,0,-r2);//2
72  s.addNode(-r2,0,r2);//3
73  s.addNode(r2/2,r1/2,0);//4
74  s.addNode(0,r1/2,-r2/2);//5
75  s.addNode(-r2/2,r1/2,0);//6
76  s.addNode(0,r1/2,r2/2);//7
77 }
78 
79 void BigPuppy::addRodsFoot(tgStructure& s){
80  s.addPair(0,6,"rod");
81  s.addPair(1,7,"rod");
82  s.addPair(2,4,"rod");
83  s.addPair(3,5,"rod");
84 }
85 
86 void BigPuppy::addNodesLeg(tgStructure& s, double r){
87  s.addNode(0,0,0); //0: Bottom Center of lower leg segment
88  s.addNode(0,r,0); //1: Center of lower leg segment
89  s.addNode(r,r,0); //2: Right of lower leg segment
90  s.addNode(-r,r,0); //3: Left of lower leg segment
91  s.addNode(0,2*r,0); //4: Top of lower leg segment
92  s.addNode(0,-r/2,0); //5: Leg segment extension for connections to foot.
93 }
94 
95 void BigPuppy::addRodsLeg(tgStructure& s){
96  s.addPair(0,1,"rod");
97  s.addPair(1,2,"rod");
98  s.addPair(1,3,"rod");
99  s.addPair(1,4,"rod");
100  s.addPair(0,5,"rod");
101 }
102 
103 void BigPuppy::addNodesHip(tgStructure& s, double r){
104  s.addNode(0,0,0); //Node 0
105  s.addNode(0,r,r); //Node 1
106  s.addNode(0,-r,-r); //Node 2
107  s.addNode(0,-r,r); //Node 3
108 }
109 
110 void BigPuppy::addRodsHip(tgStructure& s){
111  s.addPair(0,1,"rod");
112  s.addPair(0,2,"rod");
113  s.addPair(0,3,"rod");
114 }
115 
116 void BigPuppy::addNodesVertebra(tgStructure& s, double r){
117  s.addNode(0,0,0); //Node 0
118  s.addNode(r,0,r); //Node 1
119  s.addNode(r,0,-r); //Node 2
120  s.addNode(-r,0,-r); //Node 3
121  s.addNode(-r,0,r); //Node 4
122 }
123 
124 void BigPuppy::addRodsVertebra(tgStructure& s){
125  s.addPair(0,1,"rod");
126  s.addPair(0,2,"rod");
127  s.addPair(0,3,"rod");
128  s.addPair(0,4,"rod");
129 }
130 
131 void BigPuppy::addSegments(tgStructure& puppy, tgStructure& vertebra, tgStructure& hip, tgStructure& leg, tgStructure& foot,
132  double r, std::size_t segments, std::size_t hips, std::size_t legs, std::size_t feet){
133  const double offsetDist = r+1;
134  const double offsetDist2 = r*5+8;
135  const double offsetDist3 = r*6;
136  const double yOffset_leg = -(2*r+1);
137  const double yOffset_foot = -(2*r+6);
138 
139  //Vertebrae
140  btVector3 offset(offsetDist,0.0,0);
141  //Hips
142  btVector3 offset1(offsetDist*2,0.0,offsetDist);
143  btVector3 offset2(offsetDist2,0.0,offsetDist);
144  btVector3 offset3(offsetDist*2,0.0,-offsetDist);
145  btVector3 offset4(offsetDist2,0.0,-offsetDist);
146  //Lower legs
147  btVector3 offset5(offsetDist3,yOffset_leg,offsetDist);
148  btVector3 offset6(offsetDist3,yOffset_leg,-offsetDist);
149  btVector3 offset7(r*2,yOffset_leg,offsetDist);
150  btVector3 offset8(r*2,yOffset_leg,-offsetDist);
151  //Feet
152  btVector3 offset9(offsetDist3+1,yOffset_foot,offsetDist);
153  btVector3 offset10(offsetDist3+1,yOffset_foot,-offsetDist);
154  btVector3 offset11(r*2+1,yOffset_foot,offsetDist);
155  btVector3 offset12(r*2+1,yOffset_foot,-offsetDist);
156 
157  for(std::size_t i = 0; i < segments; i++) { //Connect segments for spine of puppy
158  tgStructure* t = new tgStructure (vertebra);
159  t->addTags(tgString("segment num", i + 1));
160  t->move((i + 1)*offset);
161 
162  if (i % 2 == 1){
163 
164  t->addRotation(btVector3((i + 1) * offsetDist, 0.0, 0.0), btVector3(1, 0, 0), 0.0);
165 
166  }
167  else{
168 
169  t->addRotation(btVector3((i + 1) * offsetDist, 0.0, 0.0), btVector3(1, 0, 0), M_PI/2.0);
170 
171  }
172 
173  puppy.addChild(t); //Add a segment to the puppy
174  }
175 
176  for(std::size_t i = segments; i < (segments + 2); i++) {//deal with right hip and shoulder first
177  tgStructure* t = new tgStructure (hip);
178  t->addTags(tgString("segment num", i + 1));
179 
180  if(i % 2 == 0){
181  t->move(offset1);
182  t->addRotation(btVector3(offsetDist*2, 0.0, offsetDist), btVector3(0, 1, 0), M_PI);
183  }
184  else{
185  t->move(offset2);
186  t->addRotation(btVector3(offsetDist2, 0.0, offsetDist), btVector3(0, 1, 0), M_PI);
187  }
188 
189  puppy.addChild(t); //Add a segment to the puppy
190  }
191 
192  for(std::size_t i = (segments + 2); i < (segments + hips); i++) {//deal with left hip and shoulder now
193  tgStructure* t = new tgStructure (hip);
194  t->addTags(tgString("segment num", i + 1));
195 
196  if(i % 2 == 0){
197  t->move(offset3);
198  }
199  else{
200  t->move(offset4);
201  }
202 
203  puppy.addChild(t); //Add a segment to the puppy
204 
205  }
206 
207  for(std::size_t i = (segments + hips); i < (segments + hips + 2); i++) {//right front and back legs
208  tgStructure* t = new tgStructure (leg);
209  t->addTags(tgString("segment num", i + 1));
210 
211  if(i % 2 == 0){
212  t->move(offset7);
213  t->addRotation(btVector3(r*2, yOffset_leg, offsetDist), btVector3(0, 1, 0), M_PI);
214  //the rotations for the legs are a remnant of the earlier design. Removing them now
215  //would mean changing all my muscle attachments. I will do this someday.
216  }
217  else{
218  t->move(offset5);
219  t->addRotation(btVector3(offsetDist3, yOffset_leg, offsetDist), btVector3(0, 1, 0), M_PI);
220  }
221 
222  puppy.addChild(t); //Add a segment to the puppy
223  }
224 
225  for(std::size_t i = (segments + hips + 2); i < (segments + hips + legs); i++) {//left front and back legs
226  tgStructure* t = new tgStructure (leg);
227  t->addTags(tgString("segment num", i + 1));
228 
229  if(i % 2 == 0){
230  t->move(offset8);
231  t->addRotation(btVector3(r*2, yOffset_leg, -offsetDist), btVector3(0, 1, 0), M_PI);
232  }
233  else{
234  t->move(offset6);
235  t->addRotation(btVector3(offsetDist3, yOffset_leg, -offsetDist), btVector3(0, 1, 0), M_PI);
236  }
237 
238  puppy.addChild(t); //Add a segment to the puppy
239  }
240 
241  for(std::size_t i = (segments + hips + legs); i < (segments + hips + legs + 2); i++) {//right front and back feet
242  tgStructure* t = new tgStructure (foot);
243  t->addTags(tgString("segment num", i + 1));
244 
245  if(i % 2 == 0){
246  t->move(offset11);
247  t->addRotation(btVector3(r*2+1, yOffset_foot, offsetDist), btVector3(0, 1, 0), 0.0);
248  }
249  else{
250  t->move(offset9);
251  t->addRotation(btVector3(offsetDist3+1, yOffset_foot, offsetDist), btVector3(0, 1, 0), 0.0);
252  }
253 
254  puppy.addChild(t); //Add a segment to the puppy
255  }
256 
257  for(std::size_t i = (segments + hips + legs + 2); i < (segments + hips + legs + feet); i++) {//left front and back feet
258  tgStructure* t = new tgStructure (foot);
259  t->addTags(tgString("segment num", i + 1));
260 
261  if(i % 2 == 0){
262  t->move(offset12);
263  t->addRotation(btVector3(r*2+1, yOffset_foot, -offsetDist), btVector3(0, 1, 0), 0.0);
264  }
265  else{
266  t->move(offset10);
267  t->addRotation(btVector3(offsetDist3+1, yOffset_foot, -offsetDist), btVector3(0, 1, 0), 0.0);
268  }
269 
270  puppy.addChild(t); //Add a segment to the puppy
271  }
272 }
273 
274 void BigPuppy::addMuscles(tgStructure& puppy, std::size_t segments, std::size_t hips, std::size_t legs, std::size_t feet){
275  //Time to add the muscles to the structure. Todo: try to clean this up some more.
276  std::vector<tgStructure*> children = puppy.getChildren();
277  for(std::size_t i = 2; i < (children.size() - (hips + legs + feet)); i++) {
278 
279  tgNodes n0 = children[i-2]->getNodes();
280  tgNodes n1 = children[i-1]->getNodes();
281  tgNodes n2 = children[i]->getNodes();
282 
283 
284  if(i==2){
285  //Extra muscles, to keep front vertebra from swinging.
286  puppy.addPair(n0[3], n1[3], tgString("spine first upper right muscle seg", i-2) + tgString(" seg", i-1));
287  puppy.addPair(n0[3], n1[4], tgString("spine first upper left muscle seg", i-2) + tgString(" seg", i-1));
288 
289  }
290 
291  //Add muscles to the puppy
292  if(i < 3){
293  if(i % 2 == 0){ //front
294  puppy.addPair(n0[1], n1[3], tgString("spine front lower right muscle seg", i-2) + tgString(" seg", i-1));
295  puppy.addPair(n0[1], n1[4], tgString("spine front lower left muscle seg", i-2) + tgString(" seg", i-1));
296  puppy.addPair(n0[2], n1[3], tgString("spine front upper right muscle seg", i-2) + tgString(" seg", i-1));
297  puppy.addPair(n0[2], n1[4], tgString("spine front upper left muscle seg", i-2) + tgString(" seg", i-1));
298  }
299  else{ //rear
300  puppy.addPair(n0[1], n1[3], tgString("spine rear upper left muscle seg", i-2) + tgString(" seg", i-1));
301  puppy.addPair(n0[1], n1[4], tgString("spine rear lower left muscle seg", i-2) + tgString(" seg", i-1));
302  puppy.addPair(n0[2], n1[3], tgString("spine rear upper right muscle seg", i-2) + tgString(" seg", i-1));
303  puppy.addPair(n0[2], n1[4], tgString("spine rear lower right muscle seg", i-2) + tgString(" seg", i-1));
304  }
305  }
306  if(i < 6){
307  if(i % 2 == 0){
308  puppy.addPair(n0[1], n2[4], tgString("spine bottom muscle seg", i-2) + tgString(" seg", i-1));
309  puppy.addPair(n0[2], n2[3], tgString("spine top muscle seg", i-2) + tgString(" seg", i-1));
310  }
311  else{
312  puppy.addPair(n0[1], n2[4], tgString("spine lateral left muscle seg", i-2) + tgString(" seg", i-1));
313  puppy.addPair(n0[2], n2[3], tgString("spine lateral right muscle seg", i-2) + tgString(" seg", i-1));
314 
315  }
316  }
317  if(i > 0 && i < 5){
318  if(i % 2 == 0){//rear
319  puppy.addPair(n1[1], n2[3], tgString("spine rear upper left muscle seg", i-1) + tgString(" seg", i));
320  puppy.addPair(n1[1], n2[4], tgString("spine rear lower left muscle seg", i-1) + tgString(" seg", i));
321  puppy.addPair(n1[2], n2[3], tgString("spine rear upper right muscle seg", i-1) + tgString(" seg", i));
322  puppy.addPair(n1[2], n2[4], tgString("spine rear lower right muscle seg", i-1) + tgString(" seg", i));
323  }
324  else{//front
325 
326  puppy.addPair(n1[1], n2[3], tgString("spine front lower right muscle seg", i-1) + tgString(" seg", i));
327  puppy.addPair(n1[1], n2[4], tgString("spine front lower left muscle seg", i-1) + tgString(" seg", i));
328  puppy.addPair(n1[2], n2[3], tgString("spine front upper right muscle seg", i-1) + tgString(" seg", i));
329  puppy.addPair(n1[2], n2[4], tgString("spine front upper left muscle seg", i-1) + tgString(" seg", i));
330  }
331  }
332  if(i == 5){
333  //rear
334  puppy.addPair(n1[1], n2[1], tgString("spine last lower left muscle seg", i-1) + tgString(" seg", i));
335  puppy.addPair(n1[1], n2[2], tgString("spine last lower right muscle seg", i-1) + tgString(" seg", i));
336  puppy.addPair(n1[2], n2[1], tgString("spine last upper left muscle seg", i-1) + tgString(" seg", i));
337  puppy.addPair(n1[2], n2[2], tgString("spine last upper right muscle seg", i-1) + tgString(" seg", i));
338  //front
339  puppy.addPair(n1[1], n2[3], tgString("spine front lower right muscle seg", i-1) + tgString(" seg", i));
340  puppy.addPair(n1[1], n2[4], tgString("spine front lower left muscle seg", i-1) + tgString(" seg", i));
341  puppy.addPair(n1[2], n2[3], tgString("spine front upper right muscle seg", i-1) + tgString(" seg", i));
342  puppy.addPair(n1[2], n2[4], tgString("spine front upper left muscle seg", i-1) + tgString(" seg", i));
343 
344  }
345 
346  }
347 
348 
349  //Now add muscles to hips....
350  tgNodes n0 = children[0]->getNodes();
351  tgNodes n1 = children[1]->getNodes();
352  tgNodes n2 = children[2]->getNodes();
353  tgNodes n3 = children[3]->getNodes();
354  tgNodes n4 = children[4]->getNodes();
355  tgNodes n5 = children[5]->getNodes();
356  tgNodes n6 = children[6]->getNodes();
357  tgNodes n7 = children[7]->getNodes();
358  tgNodes n8 = children[8]->getNodes();
359  tgNodes n9 = children[9]->getNodes();
360  tgNodes n10 = children[10]->getNodes();
361  tgNodes n11 = children[11]->getNodes();
362  tgNodes n12 = children[12]->getNodes();
363  tgNodes n13 = children[13]->getNodes();
364 
365  //Left shoulder muscles
366  puppy.addPair(n6[1], n1[1], tgString("left shoulder rear upper muscle seg", 6) + tgString(" seg", 1));
367  puppy.addPair(n6[1], n1[4], tgString("left shoulder front upper muscle seg", 6) + tgString(" seg", 1));
368  puppy.addPair(n6[1], n0[2], tgString("left shoulder front top muscle seg", 6) + tgString(" seg", 0));
369  puppy.addPair(n6[1], n2[3], tgString("left shoulder rear top muscle seg", 6) + tgString(" seg", 2));
370 
371  puppy.addPair(n6[3], n1[1], tgString("left shoulder rear lower muscle seg", 6) + tgString(" seg", 1));
372  puppy.addPair(n6[3], n1[4], tgString("left shoulder front lower muscle seg", 6) + tgString(" seg", 1));
373  puppy.addPair(n6[3], n0[1], tgString("left shoulder front bottom muscle seg", 6) + tgString(" seg", 0));
374  puppy.addPair(n6[3], n2[4], tgString("left shoulder rear bottom muscle seg", 6) + tgString(" seg", 2));
375 
376  //Extra muscles, to move left shoulder forward and back:
377  puppy.addPair(n6[0], n1[1], tgString("left shoulder rear mid muscle seg", 6) + tgString(" seg", 1));
378  puppy.addPair(n6[0], n1[4], tgString("left shoulder front mid muscle seg", 6) + tgString(" seg", 1));
379 
380  //Left hip muscles
381  puppy.addPair(n7[1], n5[1], tgString("left hip rear upper muscle seg", 7) + tgString(" seg", 5));
382  puppy.addPair(n7[1], n5[4], tgString("left hip front upper muscle seg", 7) + tgString(" seg", 5));
383  puppy.addPair(n7[1], n4[2], tgString("left hip rear top muscle seg", 7) + tgString(" seg", 4));
384  puppy.addPair(n7[1], n4[3], tgString("left hip front top muscle seg", 7) + tgString(" seg", 4));
385 
386  puppy.addPair(n7[3], n5[1], tgString("left hip rear lower muscle seg", 7) + tgString(" seg", 5));
387  puppy.addPair(n7[3], n5[4], tgString("left hip front lower muscle seg", 7) + tgString(" seg", 5));
388  puppy.addPair(n7[3], n4[1], tgString("left hip bottom muscle seg", 7) + tgString(" seg", 4));
389 
390  //Extra muscles, to move left hip forward and back:
391  puppy.addPair(n7[0], n3[1], tgString("left hip rear mid muscle seg", 7) + tgString(" seg", 3)); //could also be n3[3]
392  puppy.addPair(n7[0], n5[4], tgString("left hip front mid muscle seg", 7) + tgString(" seg", 5));
393 
394  //Inter-hip connector muscle
395  puppy.addPair(n7[3], n9[3], tgString("inter-hip bottom muscle seg", 7) + tgString(" seg", 9)); //inter-hip bottom muscle
396 
397  //Right shoulder muscles
398  puppy.addPair(n8[1], n1[2], tgString("right shoulder rear upper muscle seg", 8) + tgString(" seg", 1));
399  puppy.addPair(n8[1], n1[3], tgString("right shoulder front upper muscle seg", 8) + tgString(" seg", 1));
400  puppy.addPair(n8[1], n0[2], tgString("right shoulder front top muscle seg", 8) + tgString(" seg", 0));
401  puppy.addPair(n8[1], n2[3], tgString("right shoulder rear top muscle seg", 8) + tgString(" seg", 2));
402 
403  puppy.addPair(n8[3], n1[2], tgString("right shoulder rear lower muscle seg", 8) + tgString(" seg", 1));
404  puppy.addPair(n8[3], n1[3], tgString("right shoulder front lower muscle seg", 8) + tgString(" seg", 1));
405  puppy.addPair(n8[3], n0[1], tgString("right shoulder front bottom muscle seg", 8) + tgString(" seg", 0));
406  puppy.addPair(n8[3], n2[4], tgString("right shoulder rear bottom muscle seg", 8) + tgString(" seg", 2));
407 
408  //Extra muscles, to move right shoulder forward and back:
409  puppy.addPair(n8[0], n1[2], tgString("right shoulder rear mid muscle seg", 8) + tgString(" seg", 1));
410  puppy.addPair(n8[0], n1[3], tgString("right shoulder front mid muscle seg", 8) + tgString(" seg", 1));
411 
412  //Right hip muscles
413  puppy.addPair(n9[1], n5[2], tgString("right hip rear upper muscle seg", 9) + tgString(" seg", 5));
414  puppy.addPair(n9[1], n5[3], tgString("right hip front upper muscle seg", 9) + tgString(" seg", 5));
415  puppy.addPair(n9[1], n4[2], tgString("right hip rear top muscle seg", 9) + tgString(" seg", 4));
416  puppy.addPair(n9[1], n4[3], tgString("right hip front top muscle seg", 9) + tgString(" seg", 4));
417 
418  puppy.addPair(n9[3], n5[2], tgString("right hip rear lower muscle seg", 9) + tgString(" seg", 5));
419  puppy.addPair(n9[3], n5[3], tgString("right hip front lower muscle seg", 9) + tgString(" seg", 5));
420  puppy.addPair(n9[3], n4[1], tgString("right hip bottom muscle seg", 9) + tgString(" seg", 4));
421 
422  //Extra muscles, to move right hip forward and back:
423  puppy.addPair(n9[0], n3[2], tgString("right hip rear mid muscle seg", 9) + tgString(" seg", 3)); //could also be n3[3]
424  puppy.addPair(n9[0], n5[3], tgString("right hip front mid muscle seg", 9) + tgString(" seg", 5));
425 
426  //Leg/hip connections:
427 
428  //Left front leg/shoulder
429  puppy.addPair(n10[4], n6[3], tgString("left outer bicep muscle seg", 10) + tgString(" seg", 6));
430  puppy.addPair(n10[4], n6[2], tgString("left inner bicep muscle seg", 10) + tgString(" seg", 6));
431  puppy.addPair(n10[4], n1[4], tgString("left front abdomen connection muscle seg", 10) + tgString(" seg", 1));
432 
433  puppy.addPair(n10[3], n6[3], tgString("left outer tricep muscle seg", 10) + tgString(" seg", 6));
434  puppy.addPair(n10[3], n6[2], tgString("left inner tricep muscle seg", 10) + tgString(" seg", 6));
435 
436  puppy.addPair(n10[2], n6[3], tgString("left outer front tricep muscle seg", 10) + tgString(" seg", 6));
437  puppy.addPair(n10[2], n6[2], tgString("left inner front tricep muscle seg", 10) + tgString(" seg", 6));
438 
439  //Adding muscle to pull up on left front leg:
440  puppy.addPair(n10[4], n6[1], tgString("left mid bicep muscle seg", 10) + tgString(" seg", 6));
441 
442  //Right front leg/shoulder
443  puppy.addPair(n12[4], n8[2], tgString("right inner bicep muscle seg", 12) + tgString(" seg", 8));
444  puppy.addPair(n12[4], n8[3], tgString("right outer bicep muscle seg", 12) + tgString(" seg", 8));
445  puppy.addPair(n12[4], n1[3], tgString("right front abdomen connection muscle seg", 12) + tgString(" seg", 1)); //Was n1[2]
446 
447  puppy.addPair(n12[3], n8[2], tgString("inner right tricep muscle seg", 12) + tgString(" seg", 8));
448  puppy.addPair(n12[3], n8[3], tgString("outer right tricep muscle seg", 12) + tgString(" seg", 8));
449 
450  puppy.addPair(n12[2], n8[2], tgString("inner right front tricep muscle seg", 12) + tgString(" seg", 8));
451  puppy.addPair(n12[2], n8[3], tgString("outer right front tricep muscle seg", 12) + tgString(" seg", 8));
452 
453  //Adding muscle to pull up on right front leg:
454  puppy.addPair(n12[4], n8[1], tgString("right mid bicep muscle seg", 12) + tgString(" seg", 8));
455 
456  //Left rear leg/hip
457  puppy.addPair(n11[4], n7[3], tgString("left outer thigh muscle seg", 11) + tgString(" seg", 7));
458  puppy.addPair(n11[4], n7[2], tgString("left inner thigh muscle seg", 11) + tgString(" seg", 7));
459 
460  puppy.addPair(n11[4], n3[4],tgString("left rear abdomen muscle seg", 11) + tgString(" seg", 3));
461  puppy.addPair(n11[3], n5[1],tgString("left rear abdomen muscle seg", 11) + tgString(" seg", 5));
462 
463  puppy.addPair(n11[3], n7[3], tgString("left outer calf muscle seg", 11) + tgString(" seg", 7));
464  puppy.addPair(n11[3], n7[2], tgString("left inner calf muscle seg", 11) + tgString(" seg", 7));
465 
466  puppy.addPair(n11[2], n7[3], tgString("left outer front calf muscle seg", 11) + tgString(" seg", 7));
467  puppy.addPair(n11[2], n7[2], tgString("left inner front calf muscle seg", 11) + tgString(" seg", 7));
468 
469  //Adding muscle to pull rear left leg up:
470  puppy.addPair(n11[4], n7[1], tgString("left central thigh muscle seg", 11) + tgString(" seg", 7));
471 
472  //Right rear leg/hip
473  puppy.addPair(n13[4], n9[2], tgString("right inner thigh muscle seg", 13) + tgString(" seg", 9));
474  puppy.addPair(n13[4], n9[3], tgString("right outer thigh muscle seg", 13) + tgString(" seg", 9));
475 
476  puppy.addPair(n13[4], n3[3], tgString("right rear abdomen connection muscle seg", 13) + tgString(" seg", 3));
477  puppy.addPair(n13[3], n5[2], tgString("right rear abdomen connection muscle seg", 13) + tgString(" seg", 5));
478 
479  puppy.addPair(n13[3], n9[2], tgString("right inner calf muscle seg", 13) + tgString(" seg", 9));
480  puppy.addPair(n13[3], n9[3], tgString("right outer calf muscle seg", 13) + tgString(" seg", 9));
481 
482  puppy.addPair(n13[2], n9[2], tgString("right inner front calf muscle seg", 13) + tgString(" seg", 9));
483  puppy.addPair(n13[2], n9[3], tgString("right outer front calf muscle seg", 13) + tgString(" seg", 9));
484 
485  //Adding muscle to pull rear right leg up:
486  puppy.addPair(n13[4], n9[1], tgString("right central thigh muscle seg", 13) + tgString(" seg", 9));
487 
488  //Populate feet with muscles. Todo: think up names to differentiate each!
489  for(std::size_t i = (segments + hips + legs); i < children.size(); i++) {
490  tgNodes ni = children[i]->getNodes();
491  tgNodes ni4 = children[i-4]->getNodes(); //Think of a nicer name for this!
492 
493  puppy.addPair(ni[0],ni[1],tgString("front lower foot muscle seg", i));
494  puppy.addPair(ni[0],ni[3],tgString("right lower foot muscle seg", i));
495  puppy.addPair(ni[1],ni[2],tgString("left lower foot muscle seg", i));
496  puppy.addPair(ni[2],ni[3],tgString("rear lower foot muscle seg", i));
497  puppy.addPair(ni[0],ni[7],tgString("right mid foot muscle seg", i));
498  puppy.addPair(ni[1],ni[4],tgString("front mid foot muscle seg", i));
499  puppy.addPair(ni[2],ni[5],tgString("left mid foot muscle seg", i));
500  puppy.addPair(ni[3],ni[6],tgString("rear mid foot muscle seg", i));
501  puppy.addPair(ni[4],ni[5],tgString("front left upper foot muscle seg", i));
502  puppy.addPair(ni[4],ni[7],tgString("front right upper foot muscle seg", i));
503  puppy.addPair(ni[5],ni[6],tgString("rear left upper foot muscle seg", i));
504  puppy.addPair(ni[6],ni[7],tgString("rear right upper foot muscle seg", i));
505 
506  //Connect feet to legs:
507  puppy.addPair(ni4[5],ni[0],tgString("right front plantar muscle seg", i) + tgString(" seg", i-4));
508  puppy.addPair(ni4[5],ni[1],tgString("left front plantar muscle seg", i) + tgString(" seg", i-4));
509  puppy.addPair(ni4[5],ni[2],tgString("left rear plantar muscle seg", i) + tgString(" seg", i-4));
510  puppy.addPair(ni4[5],ni[3],tgString("right rear plantar muscle seg", i) + tgString(" seg", i-4));
511 
512  puppy.addPair(ni4[0],ni[4],tgString("front ankle muscle seg", i) + tgString(" seg", i-4));
513  puppy.addPair(ni4[0],ni[5],tgString("left ankle muscle seg", i) + tgString(" seg", i-4));
514  puppy.addPair(ni4[0],ni[6],tgString("rear ankle muscle seg", i) + tgString(" seg", i-4));
515  puppy.addPair(ni4[0],ni[7],tgString("right ankle muscle seg", i) + tgString(" seg", i-4));
516 
517  }
518 
519 }
520 
521 void BigPuppy::setup(tgWorld& world)
522 {
523  //Rod and Muscle configuration.
524  const double density = 4.2/300.0; //Note: this needs to be high enough or things fly apart...
525  const double radius = 0.5;
526  const double rod_space = 10.0;
527  const double rod_space2 = 8.0;
528  const double friction = 0.5;
529  const double rollFriction = 0.0;
530  const double restitution = 0.0;
531 
532  const tgRod::Config rodConfig(radius, density, friction, rollFriction, restitution);
533 
534  const double stiffness = 1000.0;
535  const double damping = .01*stiffness;
536  const double pretension = 0.0;
537  const bool history = false;
538  const double maxTens = 7000.0;
539  const double maxSpeed = 12.0;
540 
541  const double passivePretension = 1000; // 5 N.
542 
543  const double passivePretension2 = 1000;
544 
545 #ifdef USE_KINEMATIC
546 
547  const double mRad = 1.0;
548  const double motorFriction = 10.0;
549  const double motorInertia = 1.0;
550  const bool backDrivable = false;
551  #ifdef PASSIVE_STRUCTURE
552  tgKinematicActuator::Config motorConfig(2000, 20, passivePretension,
553  mRad, motorFriction, motorInertia, backDrivable,
554  history, maxTens, maxSpeed);
555 
556  #else
557  tgKinematicActuator::Config motorConfig(stiffness, damping, pretension,
558  mRad, motorFriction, motorInertia, backDrivable,
559  history, maxTens, maxSpeed);
560  #endif
561 
562 #else
563 
564  #ifdef PASSIVE_STRUCTURE
565  tgSpringCableActuator::Config muscleConfig(2000, 20, passivePretension);
566 
567  #else
568  tgSpringCableActuator::Config muscleConfig(stiffness, damping, pretension, history, maxTens, 2*maxSpeed);
569  #endif
570 
571 #endif
572  // Calculations for the flemons puppy model
573 
574  //Todo: make separate functions for node, rod, and muscle placement! Do for each type of segment.
575 
576  //Foot:
577  tgStructure foot;
578  addNodesFoot(foot,rod_space,rod_space2);
579  addRodsFoot(foot);
580 
581  //Leg:
582  tgStructure leg;
583  addNodesLeg(leg,rod_space);
584  addRodsLeg(leg);
585 
586  //Create the basic unit of the puppy
587  tgStructure vertebra;
588  addNodesVertebra(vertebra,rod_space);
589  addRodsVertebra(vertebra);
590 
591  //Create the basic unit for the hips/shoulders.
592  tgStructure hip;
593  addNodesHip(hip,rod_space);
594  addRodsHip(hip);
595 
596  //Build the puppy
597  tgStructure puppy;
598 
599  std::size_t m_segments = 6;
600  std::size_t m_hips = 4;
601  std::size_t m_legs = 4;
602  std::size_t m_feet = 4;
603  const double yOffset_foot = -(2*rod_space+6);
604 
605  addSegments(puppy,vertebra,hip,leg,foot,rod_space,m_segments,m_hips,m_legs,m_feet);
606 
607  puppy.move(btVector3(0.0,-yOffset_foot,0.0));
608 
609  addMuscles(puppy,m_segments,m_hips,m_legs,m_feet);
610 
611  //Time to add the muscles to the structure. Todo: make this a function; also try to clean this up.
612  std::vector<tgStructure*> children = puppy.getChildren();
613 
614  // Create the build spec that uses tags to turn the structure into a real model
615  tgBuildSpec spec;
616  spec.addBuilder("rod", new tgRodInfo(rodConfig));
617 
618 #ifdef USE_KINEMATIC
619  spec.addBuilder("muscle", new tgKinematicContactCableInfo(motorConfig));
620 #else
621  spec.addBuilder("muscle", new tgBasicContactCableInfo(muscleConfig));
622 #endif
623 
624  // Create your structureInfo
625  tgStructureInfo structureInfo(puppy, spec);
626 
627  // Use the structureInfo to build ourselves
628  structureInfo.buildInto(*this, world);
629 
630  // We could now use tgCast::filter or similar to pull out the
631  // models (e.g. muscles) that we want to control.
632  allMuscles = tgCast::filter<tgModel, tgSpringCableActuator> (getDescendants());
633 
634  // Notify controllers that setup has finished.
635  notifySetup();
636 
637  // Actually setup the children
638  tgModel::setup(world);
639 
640  children.clear();
641 }
642 
643 void BigPuppy::step(double dt)
644 {
645  // Precondition
646  if (dt <= 0.0)
647  {
648  throw std::invalid_argument("dt is not positive");
649  }
650  else
651  {
652  // Notify observers (controllers) of the step so that they can take action
653  notifyStep(dt);
654  tgModel::step(dt); // Step any children
655  }
656 }
657 
658 const std::vector<tgSpringCableActuator*>& BigPuppy::getAllMuscles() const
659 {
660  return allMuscles;
661 }
662 
663 void BigPuppy::teardown()
664 {
665  notifyTeardown();
667 }
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
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)
Definition of class tgBasicContactCableInfo.
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.
Definition of class tgKinematicActuatorInfo.
const std::vector< tgSpringCableActuator * > & getAllMuscles() const
Definition: BigPuppy.cpp:658
Contains the definition of class tgRod.
Definition of class tgBuildSpec.
Definition of class tgKinematicContactCableInfo.
void notifyStep(double dt)
virtual void setup(tgWorld &world)
Definition: BigPuppy.cpp:62
std::vector< tgModel * > getDescendants() const
Definition: tgModel.cpp:170
void addNode(double x, double y, double z, std::string tags="")
Definition: tgStructure.cpp:70