Skip to content

Commit

Permalink
Added tgBox functionality to the YAML builder. Also added a demo of t…
Browse files Browse the repository at this point in the history
…gBox in src/dev/ultra-spine/HorizontalSpine/BoxDemo.yaml.
  • Loading branch information
apsabelhaus committed Aug 5, 2016
1 parent 178ea17 commit e5147a9
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/core/tgBox.h
Expand Up @@ -51,15 +51,15 @@ class tgBox : public tgBaseRigid
*/
struct Config
{
/**
/**
* Initialize with radius and density, which may default.
* @param[in] radius the rod's radius; must be non-negative
* @param[in] density the rod's density; must be non-negative
*/
Config(double w = 1.0,
double h = 1.0,
double d = 1.0,
double f = 1.0,
double f = 1.0,
double rf = 0.0,
double res = 0.2);

Expand All @@ -70,7 +70,7 @@ class tgBox : public tgBaseRigid
/** The rod's height; must be nonnegative. */
const double height;

/** The rod's density; must be nonnegative. */
/** The rod's density; must be nonnegative. */
const double density;

/** The rod's friction;
Expand Down
17 changes: 17 additions & 0 deletions src/dev/ultra-spine/HorizontalSpine/BoxDemo.yaml
@@ -0,0 +1,17 @@
# YAML file showing how to use a tgBox

nodes:
bottom: [0, 2, 0]
top: [0, 10, 0]

pair_groups:
box:
- [bottom, top]

builders:
box:
class: tgBoxInfo
parameters:
width: 0.5
height: 5
density: 0.014
70 changes: 68 additions & 2 deletions src/yamlbuilder/TensegrityModel.cpp
Expand Up @@ -19,7 +19,7 @@
/**
* @file TensegrityModel.cpp
* @brief Contains the definition of the members of the class TensegrityModel.
* @author Simon Kotwicz & Jonah Eisen
* @author Simon Kotwicz, Jonah Eisen, Drew Sabelhaus
* $Id$
*/

Expand All @@ -31,11 +31,13 @@
#include "core/tgBasicActuator.h"
#include "core/tgKinematicActuator.h"
#include "core/tgRod.h"
#include "core/tgBox.h"
#include "tgcreator/tgBasicActuatorInfo.h"
#include "tgcreator/tgBasicContactCableInfo.h"
#include "tgcreator/tgKinematicActuatorInfo.h"
#include "tgcreator/tgKinematicContactCableInfo.h"
#include "tgcreator/tgRodInfo.h"
#include "tgcreator/tgBoxInfo.h"
#include "tgcreator/tgStructureInfo.h"

TensegrityModel::TensegrityModel(const std::string& structurePath) : tgModel() {
Expand All @@ -48,11 +50,12 @@ void TensegrityModel::setup(tgWorld& world) {
// create the build spec that uses tags to turn the structure into a model
tgBuildSpec spec;

// add default rod and string builders that match the tags rods & strings
// add default builders (rods, strings, boxes) that match the tags (rods, strings, boxes)
// (these will be overwritten if a different builder is specified for those tags)
Yam emptyYam = Yam();
addRodBuilder("tgRodInfo", "rod", emptyYam, spec);
addBasicActuatorBuilder("tgBasicActuatorInfo", "string", emptyYam, spec);
addBoxBuilder("tgBoxInfo", "box", emptyYam, spec);

tgStructure structure;
buildStructure(structure, topLvlStructurePath, spec);
Expand Down Expand Up @@ -494,6 +497,9 @@ void TensegrityModel::addBuilders(tgBuildSpec& spec, const Yam& builders) {
else if (builderClass == "tgKinematicContactCableInfo" || builderClass == "tgKinematicActuatorInfo") {
addKinematicActuatorBuilder(builderClass, tagMatch, parameters, spec);
}
if (builderClass == "tgBoxInfo") {
addBoxBuilder(builderClass, tagMatch, parameters, spec);
}
// add more builders here if they use a different Config
else {
throw std::invalid_argument("Unsupported builder class: " + builderClass);
Expand Down Expand Up @@ -611,6 +617,66 @@ void TensegrityModel::addKinematicActuatorBuilder(const std::string& builderClas
// add more builders that use tgKinematicActuator::Config here
}

void TensegrityModel::addBoxBuilder(const std::string& builderClass, const std::string& tagMatch, const Yam& parameters, tgBuildSpec& spec) {
/**
* Builder procedure:
* (1) create a list (a map, really) of all the possible parameters, and initialize
* them to the defaults in TensegrityModel.h
* (2) substitute these defaults with any parameters from the YAMl file
* (3) create the tgBoxInfo object and add it to the list of builders
* @TO-DO: this seems to be called multiple times per "builder" block in a YAML file.
* That's probably not correct. Why?
*/

// Debugging
#if (0)
std::cout << "addBoxBuilder" << std::endl;
#endif

// (1)
// Parameters to be used in tgBox::Config. See core/tgBox.h
// Boxes are treated like rods with a rectangular cross-section: the box length
// is specified by the distance between nodes, and the width and height come from
// the Config struct.
// Create a map of strings to doubles that will hold the parameters
std::map<std::string, double> bp;
bp["width"] = boxWidth;
bp["height"] = boxHeight;
bp["density"] = boxDensity;
bp["friction"] = boxFriction;
bp["roll_friction"] = boxRollFriction;
bp["restitution"] = boxRestitution;

// (2)
// Debugging
#if (0)
std::cout << "Box Builder Parameters: " << std::endl << std::flush;
#endif

if (parameters) {
for (YAML::const_iterator parameter = parameters.begin(); parameter != parameters.end(); ++parameter) {
std::string parameterName = parameter->first.as<std::string>();
if (bp.find(parameterName) == bp.end()) {
throw std::invalid_argument("Unsupported " + builderClass + " parameter: " + parameterName);
}
// if defined overwrite default parameter value
bp[parameterName] = parameter->second.as<double>();
}
}

// (3)
// this usage is the same as in NTRT v1.0 models.
const tgBox::Config boxConfig = tgBox::Config(bp["width"], bp["height"],
bp["density"], bp["friction"], bp["roll_friction"], bp["restitution"]);
if (builderClass == "tgBoxInfo") {
// tgBuildSpec takes ownership of the tgBoxInfo object
spec.addBuilder(tagMatch, new tgBoxInfo(boxConfig));
#if (0)
std::cout << "Box Builder Added. " << std::endl << std::flush;
#endif
}
}

void TensegrityModel::yamlNoDuplicates(const Yam& yam, const std::string structurePath) {
std::set<std::string> keys;
for (YAML::const_iterator iter = yam.begin(); iter != yam.end(); ++iter) {
Expand Down
44 changes: 43 additions & 1 deletion src/yamlbuilder/TensegrityModel.h
Expand Up @@ -22,7 +22,8 @@
/**
* @file TensegrityModel.cpp
* @brief Contains the definition of the members of the class TensegrityModel.
* @author Simon Kotwicz & Jonah Eisen
* @author Simon Kotwicz, Jonah Eisen, Drew Sabelhaus
* @copyright Copyright (C) 2016 NASA Ames Research Center
* $Id$
*/

Expand Down Expand Up @@ -54,6 +55,13 @@ class TensegrityModel : public tgSubject<TensegrityModel>, public tgModel
{
public:

/**
* List of all the default config parameters for all the
* types of objects that wil be supported.
*/

// Rod parameters:

/*
* Default rod radius.
*/
Expand All @@ -75,6 +83,35 @@ class TensegrityModel : public tgSubject<TensegrityModel>, public tgModel
*/
const static double rodRestitution = 0.2;

// Box parameters:

/*
* Default box width. (see tgBox.h)
*/
const static double boxWidth = 1.0;
/*
* Default box height. (see tgBox.h)
*/
const static double boxHeight = 1.0;
/*
* Default box density. (see tgBox.h)
*/
const static double boxDensity = 1.0;
/*
* Default box friction. (see tgBox.h)
*/
const static double boxFriction = 1.0;
/*
* Default box rolling friction. (see tgBox.h)
*/
const static double boxRollFriction = 0.0;
/*
* Default box restitution. (see tgBox.h)
*/
const static double boxRestitution = 0.2;

// String parameters:

/*
* Default string stiffness.
*/
Expand Down Expand Up @@ -323,6 +360,11 @@ class TensegrityModel : public tgSubject<TensegrityModel>, public tgModel
*/
void addKinematicActuatorBuilder(const std::string& builderClass, const std::string& tagMatch, const Yam& parameters, tgBuildSpec& spec);

/*
* Responsible for adding a builder that uses the tgBox config
*/
void addBoxBuilder(const std::string& builderClass, const std::string& tagMatch, const Yam& parameters, tgBuildSpec& spec);

/*
* Ensures YAML node contains only keys from the supplied vector
*/
Expand Down

0 comments on commit e5147a9

Please sign in to comment.