NTRT Simulator  v1.1
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
BuildTestModel.h
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 
19 #ifndef BUILD_TEST_MODEL_H
20 #define BUILD_TEST_MODEL_H
21 
22 #include "core/tgModel.h"
23 #include "core/tgBasicActuator.h"
24 #include "tgcreator/tgNodes.h"
25 #include "core/tgSubject.h"
26 #include "core/tgModelVisitor.h"
27 
28 #include "btBulletDynamicsCommon.h"
29 #include <iostream>
30 #include <set>
31 #include <vector>
32 
33 #include "tgcreator/tgRodInfo.h"
35 //#include "tgcreator/tgRigidCreator.h"
36 
37 class BuildTestModel: public tgSubject<BuildTestModel>, public tgModel
38 {
39 public:
40 
41  BuildTestModel(std::string name) : tgModel(name)
42  {
43 
44  }
45 
47  {
48  }
49 
50  virtual void setup(tgWorld& world)
51  {
52  // This is basically a manual setup of a model. There are things that do this for us (@todo: reference the things that do this for us)
53 
54  const double density = 0.9;
55  const double radius = 0.3;
56  const tgRod::Config rodConfig(radius, density);
57 
58  tgNodes nodes;
59  nodes.addNode(0,0,0);
60  nodes.addNode(0,5,0);
61  nodes.addNode(5,8,0);
62  nodes.addNode(5,5,0);
63 
64  tgPairs pairs;
65  pairs.addPair(nodes.pair(0, 1), "rod");
66  pairs.addPair(nodes.pair(1, 2), "rod");
67  pairs.addPair(nodes.pair(2, 3), "rod");
68  pairs.addPair(nodes.pair(3, 0), "rod");
69 
70  m_rod = new tgRodInfo(rodConfig, pairs[0]);
71  m_rod2 = new tgRodInfo(rodConfig, pairs[1]);
72  // @todo: This doesn't compound correctly if rod4 isn't there?? rod4 does if rod3 isn't... probably an off-by-one error in compounding...
73  tgRodInfo* rod3 = new tgRodInfo(rodConfig, tgPair(nodes[2], nodes[3], "rod"));
74  //tgRodInfo* rod4 = new tgRodInfo(rodConfig, pairs[3]);
75 
76  // Start with a set of tgRigidInfo*. A tgStructureInfo will also make this available.
77  std::vector<tgRigidInfo*> rigids;
78  rigids.push_back(m_rod);
79  rigids.push_back(m_rod2);
80  rigids.push_back(rod3);
81  //rigids.insert(rod4);
82 
83  // Create compound rigids out of rigids that share nodes
84  tgRigidAutoCompound autoCompound(rigids);
85  autoCompound.execute(); // After this we have all rigids grouped (even if they're in their own group)
86 
87  // not using this right now...
88  //tgBuildSpec spec;
89  //spec.addBuilder("rod", new tgRodInfo(rodConfig)); // @todo: should probably have tgRigidBuilder be abstract and use something like tgRodBuilder
90 
91 
92 
93  std::vector<tgRigidInfo*>::iterator it;
94  for(it = rigids.begin(); it != rigids.end(); it++) {
95  (*it)->createModel(world);
96  }
97 
98  // Create a btRigidBody for each rigid and give ownership to the corresponding tgRigidInfo
99  //tgRigidCreator rigidCreator(world);
100  //rigidCreator.buildRigidBodies(rigids);
101 
102  /*
103  rigidCreator.createRigidBodies(m_rod);
104  rigidCreator.createRigidBodies(m_rod2);
105  rigidCreator.createRigidBodies(rod3);
106  */
107  /*
108  btenRigidBuilder rb = btenRigidBuilder(*(world->getDynamicsWorld()));
109 
110  btenMuscle2PBuilder mb = btenMuscle2PBuilder((world->getDynamicsWorld()));
111 
112  btenMuscle2PBuilder::Muscle2PConfig muscleConfig;
113  muscleConfig.elasticity = 1000;
114  muscleConfig.dampening = 10;
115 
116  rb.addRigid(*m_rod);
117  rb.addRigid(*m_rod2);
118 
119  rb.init();
120 
121  btVector3* cp1 = new btVector3(0, 20, 0);
122  btVector3* cp2 = new btVector3(10, 20, 0);
123 
124  newString = new tgBasicActuator(mb.createMuscle(*m_rod, *cp1, *m_rod2, *cp2, muscleConfig), "Muscle1");
125 
126  this->addChild(newString);
127 
128  // std::cout << "after initialize, rod is " << m_rod << std::endl;
129 
130  delete cp1;
131  delete cp2;
132 
133  tgModel::setup(world); // Setup any children we might have...
134  */
135 
136  }
137 
138  virtual void step(double dt)
139  {
140  //btTransform trans;
141  //m_rod->getRigidBody()->getMotionState()->getWorldTransform(trans);
142  //std::cout << "rod height: " << trans.getOrigin().getY() << std::endl;
143 
144  // Notify observers (controllers) of the step so that they can take action
145  notifyStep(dt);
146 
147  tgModel::step(dt); // Step any children
148  }
149 
150  virtual void onVisit(const tgModelVisitor& r) const
151  {
152  // Example: m_rod->getRigidBody()->dosomething()...
153 
154  //newString->onRender(r);
155 
156  //tgModel::onRender(r);
157  //std::cout << " BuildTestModel has special rendering requirements!" << std::endl;
158  }
159 
160  void changeMuscle (double length)
161  {
162  //Do we need error checking here too?
163  //newString->setRestLength(length); // commented out for testing
164  }
165 
166 private:
167  tgRodInfo* m_rod;
168  tgRodInfo* m_rod2;
169 
170  tgBasicActuator* newString;
171 };
172 
173 #endif
Definition of class tgRodInfo.
Definition of tgSubject class.
tgPair pair(int from, int to, std::string tags="")
Definition: tgNodes.cpp:30
virtual void step(double dt)
Definition: tgModel.cpp:84
virtual void onVisit(const tgModelVisitor &r) const
virtual void setup(tgWorld &world)
Contains the definition of class tgModel.
Definition of class tgNodes.
Contains the definition of interface class tgModelVisitor.
virtual void step(double dt)
tgModel()
Definition: tgModel.cpp:33
Contains the definition of class tgBasicActuator.
Definition: tgPair.h:48
int addNode(const btVector3 &node)
Definition: tgNodes.h:129
Definition of class tgRigidAutoCompound.
void notifyStep(double dt)