g2o
Loading...
Searching...
No Matches
robot_laser.cpp
Go to the documentation of this file.
1// g2o - General Graph Optimization
2// Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
9// * Redistributions of source code must retain the above copyright notice,
10// this list of conditions and the following disclaimer.
11// * Redistributions in binary form must reproduce the above copyright
12// notice, this list of conditions and the following disclaimer in the
13// documentation and/or other materials provided with the distribution.
14//
15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27#include "robot_laser.h"
28
29#include "g2o/stuff/macros.h"
30
31#ifdef G2O_HAVE_OPENGL
33#endif
34
35#include <iomanip>
36using namespace std;
37
38namespace g2o {
39
41 : RawLaser(),
42 _laserTv(0.),
43 _laserRv(0.),
44 _forwardSafetyDist(0.),
45 _sideSafetyDist(0.),
46 _turnAxis(0.) {}
47
49
50bool RobotLaser::read(std::istream& is) {
51 int type;
52 double angle, fov, res, maxrange, acc;
53 int remission_mode;
54 is >> type >> angle >> fov >> res >> maxrange >> acc >> remission_mode;
55
56 int beams;
57 is >> beams;
59 LaserParameters(type, beams, angle, res, maxrange, acc, remission_mode);
60 _ranges.resize(beams);
61 for (int i = 0; i < beams; i++) is >> _ranges[i];
62
63 is >> beams;
64 _remissions.resize(beams);
65 for (int i = 0; i < beams; i++) is >> _remissions[i];
66
67 // special robot laser stuff
68 double x, y, theta;
69 is >> x >> y >> theta;
70 SE2 lp(x, y, theta);
71 is >> x >> y >> theta;
72 _odomPose = SE2(x, y, theta);
76
77 // timestamp + host
78 is >> _timestamp;
79 is >> _hostname;
80 is >> _loggerTimestamp;
81 return true;
82}
83
84bool RobotLaser::write(std::ostream& os) const {
85 os << _laserParams.type << " " << _laserParams.firstBeamAngle << " "
86 << _laserParams.fov << " " << _laserParams.angularStep << " "
87 << _laserParams.maxRange << " " << _laserParams.accuracy << " "
89 os << ranges().size();
90 for (size_t i = 0; i < ranges().size(); ++i) os << " " << ranges()[i];
91 os << " " << _remissions.size();
92 for (size_t i = 0; i < _remissions.size(); ++i) os << " " << _remissions[i];
93
94 // odometry pose
95 Vector3 p = (_odomPose * _laserParams.laserPose).toVector();
96 os << " " << p.x() << " " << p.y() << " " << p.z();
97 p = _odomPose.toVector();
98 os << " " << p.x() << " " << p.y() << " " << p.z();
99
100 // crap values
101 os << FIXED(" " << _laserTv << " " << _laserRv << " " << _forwardSafetyDist
102 << " " << _sideSafetyDist << " " << _turnAxis);
103 os << FIXED(" " << timestamp() << " " << hostname() << " "
104 << loggerTimestamp());
105
106 return os.good();
107}
108
109void RobotLaser::setOdomPose(const SE2& odomPose) { _odomPose = odomPose; }
110
111#ifdef G2O_HAVE_OPENGL
112RobotLaserDrawAction::RobotLaserDrawAction()
113 : DrawAction(typeid(RobotLaser).name()),
114 _beamsDownsampling(nullptr),
115 _pointSize(nullptr),
116 _maxRange(nullptr) {}
117
118bool RobotLaserDrawAction::refreshPropertyPtrs(
119 HyperGraphElementAction::Parameters* params_) {
120 if (!DrawAction::refreshPropertyPtrs(params_)) return false;
121 if (_previousParams) {
122 _beamsDownsampling = _previousParams->makeProperty<IntProperty>(
123 _typeName + "::BEAMS_DOWNSAMPLING", 1);
124 _pointSize = _previousParams->makeProperty<FloatProperty>(
125 _typeName + "::POINT_SIZE", 1.0f);
126 _maxRange = _previousParams->makeProperty<FloatProperty>(
127 _typeName + "::MAX_RANGE", -1.);
128 } else {
129 _beamsDownsampling = 0;
130 _pointSize = 0;
131 _maxRange = 0;
132 }
133 return true;
134}
135
136HyperGraphElementAction* RobotLaserDrawAction::operator()(
137 HyperGraph::HyperGraphElement* element,
138 HyperGraphElementAction::Parameters* params_) {
139 if (typeid(*element).name() != _typeName) return nullptr;
140
141 refreshPropertyPtrs(params_);
142 if (!_previousParams) {
143 return this;
144 }
145 if (_show && !_show->value()) return this;
146 RobotLaser* that = static_cast<RobotLaser*>(element);
147
148 RawLaser::Point2DVector points = that->cartesian();
149 if (_maxRange && _maxRange->value() >= 0) {
150 // prune the cartesian points;
151 RawLaser::Point2DVector npoints(points.size());
152 int k = 0;
153 auto r2 = std::pow(_maxRange->value(), 2);
154 for (size_t i = 0; i < points.size(); i++) {
155 if (points[i].squaredNorm() < r2) npoints[k++] = points[i];
156 }
157 points = npoints;
158 npoints.resize(k);
159 }
160
161 glPushMatrix();
162 const SE2& laserPose = that->laserParams().laserPose;
163 glTranslatef((float)laserPose.translation().x(),
164 (float)laserPose.translation().y(), 0.f);
165 glRotatef((float)RAD2DEG(laserPose.rotation().angle()), 0.f, 0.f, 1.f);
166 glColor4f(1.f, 0.f, 0.f, 0.5f);
167 int step = 1;
168 if (_beamsDownsampling) step = _beamsDownsampling->value();
169 if (_pointSize) {
170 glPointSize(_pointSize->value());
171 }
172
173 glBegin(GL_POINTS);
174 for (size_t i = 0; i < points.size(); i += step) {
175 glVertex3f((float)points[i].x(), (float)points[i].y(), 0.f);
176 }
177 glEnd();
178 glPopMatrix();
179
180 return this;
181}
182#endif
183
184} // namespace g2o
virtual bool refreshPropertyPtrs(HyperGraphElementAction::Parameters *params_)
Raw laser measuerement.
Definition raw_laser.h:45
const std::vector< double > & ranges() const
the range measurements by the laser
Definition raw_laser.h:63
std::vector< double > _remissions
Definition raw_laser.h:76
std::vector< double > _ranges
Definition raw_laser.h:75
LaserParameters _laserParams
Definition raw_laser.h:77
std::vector< Vector2 > Point2DVector
Definition raw_laser.h:47
double timestamp() const
Definition robot_data.h:46
double _loggerTimestamp
timestamp when the measurement was recorded
Definition robot_data.h:60
double loggerTimestamp() const
Definition robot_data.h:49
const std::string & hostname() const
Definition robot_data.h:55
std::string _hostname
name of the computer/robot generating the data
Definition robot_data.h:63
double _timestamp
timestamp when the measurement was generated
Definition robot_data.h:59
laser measurement obtained by a robot
Definition robot_laser.h:43
virtual bool read(std::istream &is)
read the data from a stream
double _forwardSafetyDist
Definition robot_laser.h:59
virtual bool write(std::ostream &os) const
write the data to a stream
double _laserTv
velocities and safety distances of the robot.
Definition robot_laser.h:59
const SE2 & odomPose() const
Definition robot_laser.h:53
double _sideSafetyDist
Definition robot_laser.h:59
void setOdomPose(const SE2 &odomPose)
represent SE2
Definition se2.h:43
Vector3 toVector() const
convert to a 3D vector (x, y, theta)
Definition se2.h:105
SE2 inverse() const
invert :-)
Definition se2.h:83
#define RAD2DEG(x)
Definition macros.h:35
VectorN< 3 > Vector3
Definition eigen_types.h:51
Property< int > IntProperty
Definition property.h:148
Property< float > FloatProperty
Definition property.h:150
Definition jet.h:876
yylloc step()
parameters for a 2D range finder