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