NTRT Simulator  Version: Master
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
TensegrityModel.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 TENSEGRITY_MODEL_H
20 #define TENSEGRITY_MODEL_H
21 
30 // C++ Standard Library
31 #include <map>
32 #include <string>
33 #include <vector>
34 // NTRT Core and tgCreator Libraries
35 #include "core/tgModel.h"
36 #include "core/tgSubject.h"
37 #include "tgcreator/tgBuildSpec.h"
38 #include "tgcreator/tgStructure.h"
39 // Bullet Physics library
40 #include "LinearMath/btVector3.h"
41 // Helper libraries
42 #include <yaml-cpp/yaml.h>
43 
44 // Forward declarations
46 class tgModelVisitor;
47 class tgWorld;
48 class tgStructureInfo;
49 
50 typedef YAML::Node Yam; // to avoid confusion with structure nodes
51 
55 class TensegrityModel : public tgSubject<TensegrityModel>, public tgModel
56 {
57 public:
58 
64  // Rod parameters:
65 
66  /*
67  * Default rod radius.
68  */
69  const static double rodRadius = 0.5;
70  /*
71  * Default rod density.
72  */
73  const static double rodDensity = 1.0;
74  /*
75  * Default rod friction.
76  */
77  const static double rodFriction = 1.0;
78  /*
79  * Default rod roll friction.
80  */
81  const static double rodRollFriction = 0.0;
82  /*
83  * Default rod restitution.
84  */
85  const static double rodRestitution = 0.2;
86 
87  // Box parameters:
88 
89  /*
90  * Default box width. (see tgBox.h)
91  */
92  const static double boxWidth = 1.0;
93  /*
94  * Default box height. (see tgBox.h)
95  */
96  const static double boxHeight = 1.0;
97  /*
98  * Default box density. (see tgBox.h)
99  */
100  const static double boxDensity = 1.0;
101  /*
102  * Default box friction. (see tgBox.h)
103  */
104  const static double boxFriction = 1.0;
105  /*
106  * Default box rolling friction. (see tgBox.h)
107  */
108  const static double boxRollFriction = 0.0;
109  /*
110  * Default box restitution. (see tgBox.h)
111  */
112  const static double boxRestitution = 0.2;
113 
114  // Sphere parameters:
115 
116  /*
117  * Default sphere radius, density, friction, rolling friction, density, etc.
118  * See tgSphere.h.
119  */
120  const static double sphereRadius = 0.5;
121  const static double sphereDensity = 1.0;
122  const static double sphereFriction = 1.0;
123  const static double sphereRollFriction = 0.0;
124  const static double sphereRestitution = 0.2;
125 
126  // String parameters:
127 
128  /*
129  * Default string stiffness.
130  */
131  const static double stringStiffness = 1000.0;
132  /*
133  * Default string damping.
134  */
135  const static double stringDamping = 10.0;
136  /*
137  * Default string pretension.
138  */
139  const static double stringPretension = 0.0;
140  /*
141  * Default string radius.
142  */
143  const static double stringRadius = 1.0;
144  /*
145  * Default string motor friction.
146  */
147  const static double stringMotorFriction = 0.0;
148  /*
149  * Default string motor intertia.
150  */
151  const static double stringMotorInertia = 1.0;
152  /*
153  * Default string back drivable (boolean).
154  */
155  const static double stringBackDrivable = 0;
156  /*
157  * Default string history (boolean).
158  */
159  const static double stringHistory = 0;
160  /*
161  * Default string max tension.
162  */
163  const static double stringMaxTension = 1000.0;
164  /*
165  * Default string target velocity.
166  */
167  const static double stringTargetVelocity = 100.0;
168  /*
169  * Default string min actual length.
170  */
171  const static double stringMinActualLength = 0.1;
172  /*
173  * Default string min rest length.
174  */
175  const static double stringMinRestLength = 0.1;
176  /*
177  * Default string rotation.
178  */
179  const static double stringRotation = 0;
180  /*
181  * Default flags for automatic movement of cable anchor points.
182  * See the config struct in tgBasicActuator.h for more information.
183  * NTRTsim defaults to moving cable anchor points to the edges of
184  * a rigid body.
185  */
186  const static bool stringMoveCablePointAToEdge = true;
187  const static bool stringMoveCablePointBToEdge = true;
188 
189  /*
190  * YAML-encoded structure path.
191  */
192  std::string topLvlStructurePath;
193 
199  bool debugging_on = false;
200 
206  TensegrityModel(const std::string& structurePath);
207 
213  TensegrityModel(const std::string& structurePath, bool debugging);
214 
219  virtual ~TensegrityModel();
220 
229  virtual void setup(tgWorld& world);
230 
235  virtual void teardown();
236 
241  virtual void step(double timeStep);
242 
250  virtual void onVisit(tgModelVisitor& visitor);
251 
256  const std::vector<tgSpringCableActuator*>& getAllActuators() const;
257 
258 private:
262  std::vector<tgSpringCableActuator*> allActuators;
263 
264  /*
265  * Responsible for adding all the children defined in a structure file, and apply their
266  * rotation, scale, offset and translation attributes.
267  */
268  void addChildren(tgStructure& structure, const std::string& structurePath, tgBuildSpec& spec, const Yam& substructures);
269 
270  /*
271  * Responsible for adding the child structure defined in the file childStructurePath.
272  */
273  void addChild(tgStructure& structure, const std::string& parentPath,
274  const std::string& childName, const Yam& childStructurePath, tgBuildSpec& spec);
275 
276  /*
277  * Responsible for applying any rotation attributes for a child structure.
278  */
279  void addChildRotation(tgStructure& childStructure, const Yam& rotation);
280 
281  /*
282  * Responsible for applying any scale attributes for a child structure.
283  */
284  void addChildScale(tgStructure& childStructure, const Yam& scale);
285 
286  /*
287  * Responsible for applying any offset attributes for a child structure.
288  * An offset is a translation defined by an offset multiplied by an offset index.
289  */
290  void addChildOffset(tgStructure& childStructure, int offsetIndex, const Yam& offset);
291 
292  /*
293  * Responsible for applying any translation attributes for a child structure.
294  */
295  void addChildTranslation(tgStructure& childStructure, const Yam& translation);
296 
297  /*
298  * Responsible for building a structure. This includes adding children, builders, nodes, pairs and bonds.
299  */
300  void buildStructure(tgStructure& structure, const std::string& structurePath, tgBuildSpec& spec);
301 
302  /*
303  * Responsible for adding nodes to the structure.
304  */
305  void addNodes(tgStructure& structure, const Yam& nodes);
306 
307  /*
308  * Responsible for adding pairs groups to the structure. A pair group is defined by a tag which is used to match
309  * pairs with builders that have the same tag.
310  */
311  void addPairGroups(tgStructure& structure, const Yam& pair_groups);
312 
313  /*
314  * Responsible for adding bond groups to the structure. Multiple bonds can be nested within a tag or multiple
315  * tags can be nested within a bond.
316  */
317  void addBondGroups(tgStructure& structure, const Yam& bond_groups, tgBuildSpec& spec);
318 
319  /*
320  * Responsible for adding bonds to the structure.
321  * A bond is defined as: 'structureName1/structureName2/.../bond_type'. More than two structure can be bonded
322  * together this way.
323  */
324  void addBonds(tgStructure& structure, const std::string& bonds, const std::string& tags, const Yam& pairs, tgBuildSpec& spec);
325 
326  /*
327  * Responsible for adding node_node pairs to the structure.
328  * A node_node pair is just a pair between two nodes.
329  */
330  void addNodeNodePairs(tgStructure& structure, const std::string& tags, const Yam& pairs,
331  const std::string* childStructure1Name = NULL, const std::string* childStructure2Name = NULL);
332 
333  /*
334  * Responsible for adding node_edge pairs to the structure.
335  * A node_edge pair is a bond between nodes and edges. At least three node to edge pairs must be defined
336  * for a valid node_edge bond.
337  */
338  void addNodeEdgePairs(tgStructure& structure, const std::string& tags, const Yam& pairs,
339  const std::string* childStructure1Name, const std::string* childStructure2Name, tgBuildSpec& spec);
340 
341  /*
342  * Responsible for parsing node edge pairs defined in YAML-encoded structure and populating reference node arrays
343  * as well as ligand and receptor arrays (ligand = node, receptor = pair)
344  */
345  void parseNodeEdgePairs(tgStructure& childStructure1, tgStructure& childStructure2,
346  std::vector<btVector3>& structure1RefNodes, std::vector<btVector3>& structure2RefNodes,
347  std::vector<tgNode*>& ligands, std::vector< std::pair<tgNode*, tgNode*> >& receptors, const Yam& links);
348 
349  /*
350  * Responsible for parsing an attachment point, which is either a node or pair and populating the reference node array
351  * and the ligand or receptor array.
352  */
353  void parseAttachmentPoint(tgStructure& structure, const std::string& attachment, std::vector<btVector3>& refNodes,
354  std::vector<tgNode*>& ligands, std::vector< std::pair<tgNode*, tgNode*> >& receptors);
355 
356  /*
357  * Responsible for applying transformations to the second structure bonded in a node_edge bond so that it is positioned
358  * properly to be connected to the first structure.
359  */
360  void rotateAndTranslate(tgStructure& childStructure2,
361  std::vector<btVector3>& structure1RefNodes, std::vector<btVector3>& structure2RefNodes);
362 
363  /*
364  * Responsible for removing a pair from a structure. Makes sure only pairs that are strings can be removed.
365  */
366  void removePair(tgStructure& structure, const tgNode* from, const tgNode* to, bool isEdgePair,
367  const std::vector<tgBuildSpec::RigidAgent*>& rigidAgents, tgBuildSpec& spec);
368 
369  /*
370  * Returns a node defined by a path of the form: 'parentStructure.childStructure.nodeName'
371  */
372  tgNode& getNode(tgStructure& structure, const std::string& nodePath);
373 
374  /*
375  * Returns a structure defined by a path of the form: 'parentStructure.childStructure'
376  */
377  tgStructure& getStructure(tgStructure& parentStructure, const std::string& structurePath);
378 
379  /*
380  * Responsible for adding any builders to the build spec
381  */
382  void addBuilders(tgBuildSpec& spec, const Yam& builders);
383 
384  /*
385  * Responsible for adding a builder that uses the tgRod config
386  */
387  void addRodBuilder(const std::string& builderClass, const std::string& tagMatch, const Yam& parameters, tgBuildSpec& spec);
388 
389  /*
390  * Responsible for adding a builder that uses the tgBasicActuator config
391  */
392  void addBasicActuatorBuilder(const std::string& builderClass, const std::string& tagMatch, const Yam& parameters, tgBuildSpec& spec);
393 
394  /*
395  * Responsible for adding a builder that uses the tgKinematicActuator config
396  */
397  void addKinematicActuatorBuilder(const std::string& builderClass, const std::string& tagMatch, const Yam& parameters, tgBuildSpec& spec);
398 
399  /*
400  * Responsible for adding a builder that uses the tgBox config
401  */
402  void addBoxBuilder(const std::string& builderClass, const std::string& tagMatch, const Yam& parameters, tgBuildSpec& spec);
403 
404  /*
405  * Responsible for adding a builder that uses the tgSphere config
406  */
407  void addSphereBuilder(const std::string& builderClass, const std::string& tagMatch, const Yam& parameters, tgBuildSpec& spec);
408 
409  /*
410  * Ensures YAML node contains only keys from the supplied vector
411  */
412  void yamlContainsOnly(const Yam& yam, const std::string structurePath, const std::vector<std::string> keys);
413 
414  /*
415  * Ensures that YAML has all unique keys within each map
416  */
417  void yamlNoDuplicates(const Yam& yam, const std::string structurePath);
418 
422  void trace(const tgStructure& structure,
423  const tgStructureInfo& structureInfo, tgModel& model);
424 
425 };
426 
427 #endif // TENSEGRITY_MODEL_H
TensegrityModel(const std::string &structurePath)
static const double rodRadius
Definition of tgSubject class.
virtual void onVisit(tgModelVisitor &visitor)
virtual void step(double timeStep)
const std::vector< tgSpringCableActuator * > & getAllActuators() const
Contains the definition of class tgModel.
virtual void setup(tgWorld &world)
Definition: tgNode.h:45
Definition of class tgStructure.
virtual void teardown()
virtual ~TensegrityModel()
Definition of class tgBuildSpec.