NTRT Simulator  v1.1
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
BaseSpineModelLearning.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 
27 // This module
28 #include "BaseSpineModelLearning.h"
29 // This library
30 #include "core/tgCast.h"
32 #include "core/tgBaseRigid.h"
33 #include "core/tgRod.h"
34 #include "core/tgString.h"
35 // The C++ Standard Library
36 #include <algorithm> // std::fill
37 #include <iostream>
38 #include <stdexcept>
39 
40 BaseSpineModelLearning::BaseSpineModelLearning(int segments) :
41  m_segments(segments),
42  tgModel()
43 {
44 }
45 
46 BaseSpineModelLearning::~BaseSpineModelLearning()
47 {
48 }
49 
51 {
52 
53  notifySetup();
54 
55  // Actually setup the children
56  tgModel::setup(world);
57 }
58 
60 {
62 
64 
65  // These pointers will be freed by tgModel::teardown
66  m_allMuscles.clear();
67  m_allSegments.clear();
68  m_muscleMap.clear();
69 }
70 
72 {
73  /* CPG update occurs in the controller so that we can decouple it
74  * from the physics update
75  */
76  notifyStep(dt);
77 
78  tgModel::step(dt); // Step any children
79 }
80 
81 const std::vector<tgSpringCableActuator*>&
82 BaseSpineModelLearning::getMuscles (const std::string& key) const
83 {
84  const MuscleMap::const_iterator it = m_muscleMap.find(key);
85  if (it == m_muscleMap.end())
86  {
87  throw std::invalid_argument("Key '" + key + "' not found in muscle map");
88  }
89  else
90  {
91  return it->second;
92  }
93 }
94 
95 const std::vector<tgSpringCableActuator*>& BaseSpineModelLearning::getAllMuscles()
96 {
97  return m_allMuscles;
98 }
99 
100 const std::vector<tgBaseRigid*> BaseSpineModelLearning::getAllRigids()
101 {
102  if (m_allSegments.size() != m_segments)
103  {
104  throw std::runtime_error("Not initialized");
105  }
106 
107  std::vector<tgBaseRigid*> p_rods;
108 
109  for (std::size_t i = 0; i < m_allSegments.size(); i++)
110  {
111  std::vector<tgBaseRigid*> temp = tgCast::filter<tgModel, tgBaseRigid> (m_allSegments[i]->getDescendants());
112  p_rods.insert(p_rods.end(), temp.begin(), temp.end());
113  }
114 
115  return p_rods;
116 }
117 
118 const int BaseSpineModelLearning::getSegments()
119 {
120  return m_segments;
121 }
122 
123 std::vector<double> BaseSpineModelLearning::getSegmentCOM(const int n) const
124 {
125  if (m_allSegments.size() != m_segments)
126  {
127  throw std::runtime_error("Not initialized");
128  }
129  else if (n < 0)
130  {
131  throw std::range_error("Negative segment number");
132  }
133  else if (n >= m_segments)
134  {
135  throw std::range_error(tgString("Segment number > ", m_segments));
136  }
137 
138  std::vector<tgRod*> p_rods =
139  tgCast::filter<tgModel, tgRod> (m_allSegments[n]->getDescendants());
140 
141  // Ensure our segments are being populated correctly
142  assert(!p_rods.empty());
143 
144  btVector3 segmentCenterOfMass(0, 0, 0);
145  double segmentMass = 0.0;
146  for (std::size_t i = 0; i < p_rods.size(); i++)
147  {
148  const tgRod* const pRod = p_rods[i];
149  assert(pRod != NULL);
150  const double rodMass = pRod->mass();
151  //std::cout << "mass " << rodMass;
152  const btVector3 rodCenterOfMass = pRod->centerOfMass();
153  segmentCenterOfMass += rodCenterOfMass * rodMass;
154  segmentMass += rodMass;
155  }
156 
157  // Check to make sure the rods actually had mass
158  assert(segmentMass > 0.0);
159 
160  segmentCenterOfMass /= segmentMass;
161 
162  // Copy to the result std::vector
163  std::vector<double> result(3);
164  for (size_t i = 0; i < 3; ++i) { result[i] = segmentCenterOfMass[i]; }
165 
166  return result;
167 }
virtual void teardown()
Definition: tgModel.cpp:68
virtual void setup(tgWorld &world)
Definition: tgModel.cpp:57
Create a box shape as an obstacle or add it to your tensegrity.
Convenience function for combining strings with ints, mostly for naming structures.
virtual void step(double dt)
Definition: tgModel.cpp:84
Utility class for class casting and filtering collections by type.
virtual void setup(tgWorld &world)
virtual btVector3 centerOfMass() const
Definition: tgBaseRigid.cpp:74
A template base class for a tensegrity spine.
Contains the definition of abstract base class tgSpringCableActuator. Assumes that the string is line...
std::string tgString(std::string s, int i)
Definition: tgString.h:33
virtual double mass() const
Definition: tgBaseRigid.h:65
Contains the definition of class tgRod.
virtual void step(double dt)
Definition: tgRod.h:43