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