NTRT Simulator  Version: Master
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
tgBoxAnchorDebugModel.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 
25 // This module
26 #include "tgBoxAnchorDebugModel.h"
27 // This library
28 #include "core/tgBasicActuator.h"
29 #include "core/tgRod.h"
30 #include "tgcreator/tgBuildSpec.h"
32 #include "tgcreator/tgRodInfo.h"
33 #include "tgcreator/tgStructure.h"
35 // For sensing (DEBUGGING RIGHT NOW)
36 #include "sensors/tgRodSensor.h"
37 // The Bullet Physics library
38 #include "LinearMath/btVector3.h"
39 // The C++ Standard Library
40 #include <stdexcept>
41 
42 namespace
43 {
44  // see tgBasicActuator and tgRod for a descripton of these rod parameters
45  // (specifically, those related to the motor moving the strings.)
46  // NOTE that any parameter that depends on units of length will scale
47  // with the current gravity scaling. E.g., with gravity as 98.1,
48  // the length units below are in decimeters.
49 
50  // Note: This current model of the SUPERball rod is 1.5m long by 3 cm radius,
51  // which is 0.00424 m^3.
52  // For SUPERball v1.5, mass = 3.5kg per strut, which comes out to
53  // 0.825 kg / (decimeter^3).
54 
55  // similarly, frictional parameters are for the tgRod objects.
56  const struct Config
57  {
58  double density;
59  double radius;
60  double rodLength;
61  double stiffness;
62  double damping;
63  double friction;
64  double rollFriction;
65  double restitution;
66  double pretension;
67  bool hist;
68  double maxTens;
69  double targetVelocity;
70  bool moveCablePointAToEdge;
71  bool moveCablePointBToEdge;
72  } c =
73  {
74  0.688, // density (kg / length^3)
75  3.0, // radius (length)
76  2.0, // rodLength (length)
77  613.0, // stiffness (kg / sec^2) was 1500
78  200.0, // damping (kg / sec)
79  0.99, // friction (unitless)
80  0.01, // rollFriction (unitless)
81  0.0, // restitution (?)
82  200.0, // pretension -> set to 4 * 613, the previous value of the rest length controller
83  0, // History logging (boolean)
84  100000, // maxTens
85  10000, // targetVelocity
86  false, // moveCablePointAToEdge
87  false, // moveCablePointBToEdge
88 
89  // Use the below values for earlier versions of simulation.
90  // 1.006,
91  // 0.31,
92  // 300000.0,
93  // 3000.0,
94  // 15.0,
95  // 7.5,
96  };
97 } // namespace
98 
99 // Constructor does nothing. Everything happens in setup.
101 {
102 }
103 
104 // Destructor does nothing. Everythign happens in teardown.
106 {
107 }
108 
109 void tgBoxAnchorDebugModel::addNodes(tgStructure& s)
110 {
111  s.addNode(0, 0, 0); // 0
112  s.addNode(0, c.rodLength, 0); // 1
113  s.addNode(0, 2 * c.rodLength, 0); // 2
114  s.addNode(0, 3 * c.rodLength, 0); // 3
115 }
116 
117 void tgBoxAnchorDebugModel::addRods(tgStructure& s)
118 {
119  s.addPair( 0, 1, "rod");
120  s.addPair( 2, 3, "rod");
121 }
122 
123 void tgBoxAnchorDebugModel::addActuators(tgStructure& s)
124 {
125  s.addPair(1, 2, "muscle");
126 }
127 
129 {
130 
131  const tgRod::Config rodConfig(c.radius, c.density, c.friction,
132  c.rollFriction, c.restitution);
133 
135  tgBasicActuator::Config muscleConfig(c.stiffness, c.damping, c.pretension,
136  c.hist, c.maxTens, c.targetVelocity);
137  // Since the boolean flags for moving the anchor attachment points
138  // are not next in line in the Config constructor, assign them explicitly
139  // after the config struct has been constructed:
140  muscleConfig.moveCablePointAToEdge = c.moveCablePointAToEdge;
141  muscleConfig.moveCablePointBToEdge = c.moveCablePointBToEdge;
142 
143  // Start creating the structure
144  tgStructure s;
145  addNodes(s);
146 
147  /*
148  // DEBUGGING
149  // Check: did the nodes move?
150  std::cout << "Nodes in the structure after addNodes: " <<
151  s.getNodes() << std::endl;
152  */
153 
154  addRods(s);
155  addActuators(s);
156 
157  /*
158  // DEBUGGING
159  // Check: did the nodes move?
160  std::cout << "Nodes in the structure after addPairs: " <<
161  s.getNodes() << std::endl;
162  // Check: did the pairs move?
163  std::cout << "Pairs in the structure after addPairs: " << std::endl;
164  // need to iterate through the std::vector of tgPair objects
165  std::vector<tgPair> pairsAfterAdd = s.getPairs().getPairs();
166  for( size_t i = 0; i < pairsAfterAdd.size(); i++ ) {
167  // Print the pair.
168  std::cout << pairsAfterAdd[i] << ", from and to: " <<
169  pairsAfterAdd[i].getFrom() << " , " << pairsAfterAdd[i].getTo() << std::endl;
170  }
171  */
172 
173  // Move the whole structure up a bit.
174  s.move(btVector3(0, 5, 0));
175 
176  /*
177  // DEBUGGING
178  // Check: did the nodes move?
179  std::cout << "Nodes in the structure after move: " <<
180  s.getNodes() << std::endl;
181  // Check: did the pairs move?
182  std::cout << "Pairs in the structure after move: " << std::endl;
183  // need to iterate through the std::vector of tgPair objects
184  std::vector<tgPair> pairsAfterMove = s.getPairs().getPairs();
185  for( size_t i = 0; i < pairsAfterMove.size(); i++ ) {
186  // Print the pair.
187  std::cout << pairsAfterMove[i] << ", from and to: " <<
188  pairsAfterMove[i].getFrom() << " , " << pairsAfterMove[i].getTo() <<
189  std::endl;
190  }
191  */
192 
193  // Add a rotation. This is needed if the ground slopes too much,
194  // otherwise glitches put a rod below the ground.
195  /*btVector3 rotationPoint = btVector3(0, 0, 0); // origin
196  btVector3 rotationAxis = btVector3(0, 1, 0); // y-axis
197  double rotationAngle = M_PI/2;
198  s.addRotation(rotationPoint, rotationAxis, rotationAngle);
199  */
200 
201  // Create the build spec that uses tags to turn the structure into a real model
202  tgBuildSpec spec;
203  spec.addBuilder("rod", new tgRodInfo(rodConfig));
204  spec.addBuilder("muscle", new tgBasicActuatorInfo(muscleConfig));
205 
206  // Create your structureInfo
207  tgStructureInfo structureInfo(s, spec);
208 
209  // Use the structureInfo to build ourselves
210  structureInfo.buildInto(*this, world);
211 
212  // We could now use tgCast::filter or similar to pull out the
213  // models (e.g. muscles) that we want to control.
214  allActuators = tgCast::filter<tgModel, tgBasicActuator> (getDescendants());
215 
216  //Debugging: test out the new tgRodSensor on a basic level.
217  // All the descendants from this tgModel, now that they have been built:
218  std::vector<tgModel*> all_children = getDescendants();
219  // Pick out the tgRods:
220  std::vector<tgRod*> allRods = tgCast::filter<tgModel, tgRod>(all_children);
221  // Print out the tgRods
222  std::cout << "tgBoxAnchorDebugDemo tgRods: " << std::endl;
223  for (size_t i = 0; i < allRods.size(); i++)
224  {
225  std::cout << "rod number " << i << ": " << std::endl;
226  std::cout << "tags: " << allRods[i]->getTags() << std::endl;
227  std::cout << allRods[i]->toString() << std::endl;
228  }
229 
230  // Now, create some sensors and test them out.
231  for (size_t i = 0; i < allRods.size(); i++)
232  {
233  tgRodSensor* rodsensor_i = new tgRodSensor(allRods[i]);
234  allRodSensors.push_back(rodsensor_i);
235  }
236  /*
237  for (size_t i = 0; i < allRodSensors.size(); i++)
238  {
239  std::cout << allRodSensors[i]->getSensorDataHeading() << std::endl;
240  }
241  */
242 
243 
244 
245  // call the onSetup methods of all observed things e.g. controllers
246  notifySetup();
247 
248  // Actually setup the children
249  tgModel::setup(world);
250 }
251 
253 {
254  // Precondition
255  if (dt <= 0.0)
256  {
257  throw std::invalid_argument("dt is not positive");
258  }
259  else
260  {
261  //DEBUGGING: try out the new sensors!
262  /*
263  for (size_t i = 0; i < allRodSensors.size(); i++) {
264  std::cout << allRodSensors[i]->getSensorData() << std::endl;
265  }
266  */
267  // Notify observers (controllers) of the step so that they can take action
268  notifyStep(dt);
269  tgModel::step(dt); // Step any children
270  }
271 }
272 
274 {
275  tgModel::onVisit(r);
276 }
277 
278 const std::vector<tgBasicActuator*>& tgBoxAnchorDebugModel::getAllActuators() const
279 {
280  return allActuators;
281 }
282 
284 {
285  notifyTeardown();
287 }
virtual void setup(tgWorld &world)
virtual void teardown()
Definition: tgModel.cpp:68
virtual void setup(tgWorld &world)
Definition: tgModel.cpp:57
Definition of class tgRodInfo.
virtual void step(double dt)
virtual void step(double dt)
Definition: tgModel.cpp:84
const std::vector< tgBasicActuator * > & getAllActuators() const
Definition of class tgBasicActuatorInfo.
virtual void onVisit(const tgModelVisitor &r) const
Definition: tgModel.cpp:107
void addPair(int fromNodeIdx, int toNodeIdx, std::string tags="")
Definition: tgStructure.cpp:80
Constains definition of concrete class tgRodSensor.
Contains the definition of class tgBasicActuator.
Definition of class tgStructure.
Definition of class tgStructureInfo.
Contains the definition of class tgBoxAnchorDebugModel. $Id$.
Contains the definition of class tgRod.
Definition of class tgBuildSpec.
virtual void onVisit(tgModelVisitor &r)
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