NTRT Simulator
 All Classes Files Functions Variables Typedefs Friends Pages
ContactTestModel.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 CONTACT_TEST_MODEL_H
20 #define CONTACT_TEST_MODEL_H
21 
22 #include "core/tgModel.h"
23 #include "core/tgLinearString.h"
24 
25 #include "core/tgSubject.h"
26 
27 #include "btBulletDynamicsCommon.h"
28 #include <iostream>
29 #include <set>
30 
31 #include "tgcreator/tgRodInfo.h"
32 #include "core/tgLinearString.h"
33 #include "dev/btietz/tgRBString.h"
37 #include "tgcreator/tgBuildSpec.h"
38 
39 #include "tgcreator/tgStructure.h"
41 #include "tgcreator/tgUtil.h"
42 #include "core/tgString.h"
43 
44 //#include <tgmath.h>
45 
46 class ContactTestModel : public tgSubject<ContactTestModel>, public tgModel
47 {
48 public:
49 
51  {
52  }
53 
54  virtual ~ContactTestModel()
55  {}
56 
58  virtual void setup(tgWorld& world)
59  {
60 
61  // Config for rods that are a part of RB Strings
62  const double radius = 0.15;
63  const double density = 4; // Note: This needs to be high enough or things fly apart...
64  const tgRod::Config rodConfig(radius, density);
65 
66  const double stiffness = 10000.0;
67  const double damping = .01*stiffness;
68  const int segments = 16;
69  const tgLinearString::Config stringConfig(stiffness, damping);
70  const tgRBString::Config rbConfig(segments, rodConfig, stringConfig, 8.0);
71 
72  // Config for the other rods
73  const double bigRadius = 1.0;
74  const double massLess = 0;
75  const double massive = 1.0;
76 
77  const tgRod::Config masslessRod(bigRadius, massLess);
78  const tgRod::Config massiveRod(bigRadius, massive);
79 
80  double length = 10.0;
81  double height = 20.0;
82 
83  tgStructure s;
84 
85  s.addNode(0, height, 0); // 0
86  s.addNode(length, height, 0); // 1
87  s.addNode(0, height + 10.0, 0); // 2
88  s.addNode(length, height + 10.0, 0); // 3
89 
90  // Rods
91  s.addPair( 0, 1, "massive rod");
92  s.addPair( 2, 3, "massless rod");
93 
94 
95  // Muscles
96  s.addPair(0, 2, "muscle");
97  s.addPair(1, 3, "muscle");
98 
99  //s.move(btVector3(0, 10, 0));
100 
101  // Create the build spec that uses tags to turn the structure into a real model
102  tgBuildSpec spec;
103  spec.addBuilder("massive", new tgRodInfo(massiveRod));
104  spec.addBuilder("massless", new tgRodInfo(masslessRod));
105  spec.addBuilder("muscle", new tgRBStringInfo(rbConfig));
106 
107  // Create your structureInfo
108  tgStructureInfo structureInfo(s, spec);
109 
110  // Use the structureInfo to build ourselves
111  structureInfo.buildInto(*this, world);
112 
113  // We could now use tgCast::filter or similar to pull out the models (e.g. muscles) that we want to control.
114  allMuscles = tgCast::filter<tgModel, tgRBString> (getDescendants());
115 
116  // Debug printing
117  std::cout << "StructureInfo:" << std::endl;
118  std::cout << structureInfo << std::endl;
119 
120  std::cout << "Model: " << std::endl;
121  std::cout << *this << std::endl;
122 
123  // Actually setup the children
124  tgModel::setup(world);
125 
126  std::cout << "Finished Setup!" << std::endl;
127 
128  }
129 
130  // @todo: Is there a way that we can get rid of the need to override this function in subclasses of tgModel?
131  // comment_BRT: only if we can somehow make tgModel a template class,
132  // we need to know what class we're notifying
133  virtual void step(double dt)
134  {
135  // Notify observers (controllers) of the step so that they can take action
136  notifyStep(dt);
137 
138  tgModel::step(dt); // Step any children
139 
140  }
141 
142  void teardown()
143  {
145  }
146 
147  std::vector <tgRBString*>& getAllMuscles ()
148  {
149  return allMuscles;
150  }
151 
152 private:
153  std::vector<tgRBString*> allMuscles;
154 };
155 
156 #endif
virtual void teardown()
Definition: tgModel.cpp:73
virtual void setup(tgWorld &world)
Definition: tgModel.cpp:62
Definition of class tgRodInfo.
Definition of tgSubject class.
Convenience function for combining strings with ints, mostly for naming structures.
virtual void step(double dt)
Definition: tgModel.cpp:86
virtual void step(double dt)
Contains the definition of class tgModel. $Id$.
void addPair(int fromNodeIdx, int toNodeIdx, std::string tags="")
Definition: tgStructure.cpp:65
tgModel()
Definition: tgModel.cpp:32
Contains the definition of class tgLinearString.
virtual void setup(tgWorld &world)
Definition of class tgStructure.
Definition of class tgStructureInfo.
Contains the definition of class tgRBString. A string with small rigid bodies to create contact dynam...
Definition of class tgLinearStringInfo.
Contains the definition of class tgUtil and overloaded operator<<() free functions.
Definition of class tgBuildSpec.
Definition of class tgRigidAutoCompound.
std::vector< tgModel * > getDescendants() const
Definition: tgModel.cpp:174
Contains the definition of class tgRBStringInfo. A string with small rigid bodies to create contact d...
void addNode(double x, double y, double z, std::string tags="")
Definition: tgStructure.cpp:55