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