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