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