NTRT Simulator  Version: Master
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
tgRodSensor.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 class:
28 #include "tgRodSensor.h"
29 
30 // Includes from NTRT:
31 #include "core/tgSenseable.h"
32 #include "core/tgCast.h"
33 #include "core/tgTags.h"
34 
35 // Includes from the c++ standard library:
36 //#include <iostream>
37 #include <sstream>
38 #include <stdexcept>
39 #include <cassert>
40 #include <string> // for std::to_string(float)
41 
42 // Includes from Bullet Physics:
43 #include "LinearMath/btVector3.h"
44 
50 {
51  // Note that this pointer may be 0 (equivalent to NULL) if the cast in
52  // the calling function from tgSenseable to tgRod fails.
53  if (pRod == NULL) {
54  throw std::invalid_argument("Pointer to pRod is NULL inside tgRodSensor.");
55  }
56 }
57 
64 {
65 }
66 
73 std::vector<std::string> tgRodSensor::getSensorDataHeadings() {
74  // Note that this class has access to the parent's pointer, m_pSens.
75  // Let's cast that to a pointer to a tgRod right now.
76  // Here, "m_pRod" stands for "my pointer to a tgRod."
77  tgRod* m_pRod = tgCast::cast<tgSenseable, tgRod>(m_pSens);
78  // Check: if the cast failed, this will return 0.
79  // In that case, this tgRodSensor does not point to a tgRod!!!
80  assert( m_pRod != 0);
81 
82  // The list to which we'll append all the sensor headings:
83  std::vector<std::string> headings;
84 
85  // Pull out the tags for this rod, so we only have to call the accessor once.
86  tgTags m_tags = m_pRod->getTags();
87 
88  // Copied from tgSensor.h:
98  // The string 'prefix' will be added to each heading.
99  std::string prefix = "rod(";
100 
101  // Note that the orientation is a btVector3 object of Euler angles,
102  // which I believe are overloaded as strings...
103  // Also, the XYZ positions are of the center of mass.
104  // TO-DO: check which euler angles are which!!!
105 
106  headings.push_back( prefix + m_tags + ").X" );
107  headings.push_back( prefix + m_tags + ").Y" );
108  headings.push_back( prefix + m_tags + ").Z" );
109  headings.push_back( prefix + m_tags + ").Euler1" );
110  headings.push_back( prefix + m_tags + ").Euler2" );
111  headings.push_back( prefix + m_tags + ").Euler3" );
112  headings.push_back( prefix + m_tags + ").mass" );
113 
114  // Return the resulting vector.
115  return headings;
116 }
117 
121 std::vector<std::string> tgRodSensor::getSensorData() {
122  // Similar to getSensorDataHeading, cast the a pointer to a tgRod right now.
123  tgRod* m_pRod = tgCast::cast<tgSenseable, tgRod>(m_pSens);
124  // Check: if the cast failed, this will return 0.
125  // In that case, this tgRodSensor does not point to a tgRod!!!
126  assert( m_pRod != 0);
127  // Pick out the XYZ position of the center of mass of this rod.
128  btVector3 com = m_pRod->centerOfMass();
129  btVector3 orient = m_pRod->orientation();
130  // Note that the 'orientation' method also returns a btVector3.
131 
132  // The list of sensor data that will be returned:
133  std::vector<std::string> sensordata;
134 
148  // The floats (btScalars?) need to be converted to strings
149  // via a stringstream.
150  std::stringstream ss;
151 
152  // com[0]
153  ss << com[0];
154  sensordata.push_back( ss.str() );
155  // Reset the stream.
156  ss.str("");
157 
158  // com[1]
159  ss << com[1];
160  sensordata.push_back( ss.str() );
161  ss.str("");
162  // com[2]
163  ss << com[2];
164  sensordata.push_back( ss.str() );
165  ss.str("");
166  // orient[0]
167  ss << orient[0];
168  sensordata.push_back( ss.str() );
169  ss.str("");
170  // orient[1]
171  ss << orient[1];
172  sensordata.push_back( ss.str() );
173  ss.str("");
174  // orient[2]
175  ss << orient[2];
176  sensordata.push_back( ss.str() );
177  ss.str("");
178  // mass
179  ss << m_pRod->mass();
180  sensordata.push_back( ss.str() );
181  ss.str("");
182 
183  return sensordata;
184 }
185 
186 //end.
virtual ~tgRodSensor()
Definition: tgRodSensor.cpp:63
virtual std::vector< std::string > getSensorDataHeadings()
Definition: tgRodSensor.cpp:73
Utility class for class casting and filtering collections by type.
virtual btVector3 centerOfMass() const
Definition: tgBaseRigid.cpp:74
Contains the definition of class tgTags.
tgSenseable * m_pSens
Definition: tgSensor.h:102
Constains definition of concrete class tgRodSensor.
virtual btVector3 orientation() const
Definition: tgBaseRigid.cpp:87
tgRodSensor(tgRod *pRod)
Definition: tgRodSensor.cpp:49
virtual double mass() const
Definition: tgBaseRigid.h:65
virtual std::vector< std::string > getSensorData()
Constains the implementation of mixin class tgSenseable.
Definition: tgRod.h:43
Definition: tgTags.h:44