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