NTRT Simulator  Version: Master
 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() const
96 {
97  return m_allMuscles;
98 }
99 
100 const std::vector<tgBaseRigid*> BaseSpineModelLearning::getAllRigids() const
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() const
119 {
120  return m_segments;
121 }
122 
123 std::vector<double> BaseSpineModelLearning::getSegmentCOM(const int n) const
124 {
125 
126  btVector3 segmentCenterOfMass = getSegmentCOMVector(n);
127 
128  // Copy to the result std::vector
129  std::vector<double> result(3);
130  for (size_t i = 0; i < 3; ++i) { result[i] = segmentCenterOfMass[i]; }
131 
132  return result;
133 }
134 
135 btVector3 BaseSpineModelLearning::getSegmentCOMVector(const int n) const
136 {
137  if (m_allSegments.size() != m_segments)
138  {
139  throw std::runtime_error("Not initialized");
140  }
141  else if (n < 0)
142  {
143  throw std::range_error("Negative segment number");
144  }
145  else if (n >= m_segments)
146  {
147  throw std::range_error(tgString("Segment number > ", m_segments));
148  }
149 
150  std::vector<tgRod*> p_rods =
151  tgCast::filter<tgModel, tgRod> (m_allSegments[n]->getDescendants());
152 
153  // Ensure our segments are being populated correctly
154  assert(!p_rods.empty());
155 
156  btVector3 segmentCenterOfMass(0, 0, 0);
157  double segmentMass = 0.0;
158  for (std::size_t i = 0; i < p_rods.size(); i++)
159  {
160  const tgRod* const pRod = p_rods[i];
161  assert(pRod != NULL);
162  const double rodMass = pRod->mass();
163  //std::cout << "mass " << rodMass;
164  const btVector3 rodCenterOfMass = pRod->centerOfMass();
165  segmentCenterOfMass += rodCenterOfMass * rodMass;
166  segmentMass += rodMass;
167  }
168 
169  // Check to make sure the rods actually had mass
170  assert(segmentMass > 0.0);
171 
172  segmentCenterOfMass /= segmentMass;
173 
174  return segmentCenterOfMass;
175 }
176 
177 double BaseSpineModelLearning::getSpineLength() const
178 {
179  const btVector3 start = getSegmentCOMVector(0);
180  const btVector3 end = getSegmentCOMVector(m_segments - 1);
181 
182  return (start - end).length();
183 }
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