g2o
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>
36 using namespace std;
37 
38 namespace g2o {
39 
40  RobotLaser::RobotLaser() :
41  RawLaser(),
42  _laserTv(0.), _laserRv(0.), _forwardSafetyDist(0.), _sideSaftyDist(0.), _turnAxis(0.)
43  {
44  }
45 
47  {
48  }
49 
50  bool RobotLaser::read(std::istream& is)
51  {
52  int type;
53  double angle, fov, res, maxrange, acc;
54  int remission_mode;
55  is >> type >> angle >> fov >> res >> maxrange >> acc >> remission_mode;
56 
57  int beams;
58  is >> beams;
59  _laserParams = LaserParameters(type, beams, angle, res, maxrange, acc, remission_mode);
60  _ranges.resize(beams);
61  for (int i=0; i<beams; i++)
62  is >> _ranges[i];
63 
64  is >> beams;
65  _remissions.resize(beams);
66  for (int i = 0; i < beams; i++)
67  is >> _remissions[i];
68 
69  // special robot laser stuff
70  double x,y,theta;
71  is >> x >> y >> theta;
72  SE2 lp(x,y,theta);
73  //cerr << "x: " << x << " y:" << y << " th:" << theta << " ";
74  is >> x >> y >> theta;
75  //cerr << "x: " << x << " y:" << y << " th:" << theta;
76  _odomPose = SE2(x,y,theta);
79 
80  // timestamp + host
81  is >> _timestamp;
82  is >> _hostname;
83  is >> _loggerTimestamp;
84  return true;
85  }
86 
87  bool RobotLaser::write(std::ostream& os) const
88  {
89  os << _laserParams.type << " " << _laserParams.firstBeamAngle << " " << _laserParams.fov << " "
91  << _laserParams.remissionMode << " ";
92  os << ranges().size();
93  for (size_t i = 0; i < ranges().size(); ++i)
94  os << " " << ranges()[i];
95  os << " " << _remissions.size();
96  for (size_t i = 0; i < _remissions.size(); ++i)
97  os << " " << _remissions[i];
98 
99  // odometry pose
100  Vector3D p = (_odomPose * _laserParams.laserPose).toVector();
101  os << " " << p.x() << " " << p.y() << " " << p.z();
102  p = _odomPose.toVector();
103  os << " " << p.x() << " " << p.y() << " " << p.z();
104 
105  // crap values
106  os << FIXED(" " << _laserTv << " " << _laserRv << " " << _forwardSafetyDist << " "
107  << _sideSaftyDist << " " << _turnAxis);
108  os << FIXED(" " << timestamp() << " " << hostname() << " " << loggerTimestamp());
109 
110  return os.good();
111  }
112 
114  {
116  }
117 
118 
119 
120 
121 #ifdef G2O_HAVE_OPENGL
122  RobotLaserDrawAction::RobotLaserDrawAction(): DrawAction(typeid(RobotLaser).name()){
123  }
124 
125  bool RobotLaserDrawAction::refreshPropertyPtrs(HyperGraphElementAction::Parameters* params_){
126  if (!DrawAction::refreshPropertyPtrs(params_))
127  return false;
128  if (_previousParams){
129  _beamsDownsampling = _previousParams->makeProperty<IntProperty>(_typeName + "::BEAMS_DOWNSAMPLING", 1);
130  _pointSize = _previousParams->makeProperty<FloatProperty>(_typeName + "::POINT_SIZE", 1.0f);
131  _maxRange = _previousParams->makeProperty<FloatProperty>(_typeName + "::MAX_RANGE", -1.);
132  } else {
133  _beamsDownsampling = 0;
134  _pointSize= 0;
135  _maxRange = 0;
136  }
137  return true;
138  }
139 
140  HyperGraphElementAction* RobotLaserDrawAction::operator()(HyperGraph::HyperGraphElement* element,
142  if (typeid(*element).name()!=_typeName)
143  return 0;
144 
145  refreshPropertyPtrs(params_);
146  if (! _previousParams){
147  return this;
148  }
149  if (_show && !_show->value())
150  return this;
151  RobotLaser* that = static_cast<RobotLaser*>(element);
152 
153  RawLaser::Point2DVector points=that->cartesian();
154  if (_maxRange && _maxRange->value() >=0 ) {
155  // prune the cartesian points;
156  RawLaser::Point2DVector npoints(points.size());
157  int k = 0;
158  float r2=_maxRange->value();
159  r2 *= r2;
160  for (size_t i=0; i<points.size(); i++){
161  double x = points[i].x();
162  double y = points[i].y();
163  if (x*x + y*y < r2)
164  npoints[k++] = points[i];
165  }
166  points = npoints;
167  npoints.resize(k);
168  }
169 
170  glPushMatrix();
171  const SE2& laserPose = that->laserParams().laserPose;
172  glTranslatef((float)laserPose.translation().x(), (float)laserPose.translation().y(), 0.f);
173  glRotatef((float)RAD2DEG(laserPose.rotation().angle()),0.f,0.f,1.f);
174  glColor4f(1.f,0.f,0.f,0.5f);
175  int step = 1;
176  if (_beamsDownsampling )
177  step = _beamsDownsampling->value();
178  if (_pointSize) {
179  glPointSize(_pointSize->value());
180  }
181 
182  glBegin(GL_POINTS);
183  for (size_t i=0; i<points.size(); i+=step){
184  glVertex3f((float)points[i].x(), (float)points[i].y(), 0.f);
185  }
186  glEnd();
187  glPopMatrix();
188 
189  return this;
190  }
191 #endif
192 
193 }
std::string _hostname
name of the computer/robot generating the data
Definition: robot_data.h:63
const SE2 & odomPose() const
Definition: robot_laser.h:53
std::vector< Vector2D, Eigen::aligned_allocator< Vector2D > > Point2DVector
Definition: raw_laser.h:48
Vector3D toVector() const
convert to a 3D vector (x, y, theta)
Definition: se2.h:108
Eigen::Matrix< double, 3, 1, Eigen::ColMajor > Vector3D
Definition: eigen_types.h:46
Abstract action that operates on a graph entity.
double _timestamp
timestamp when the measurement was generated
Definition: robot_data.h:60
laser measurement obtained by a robot
Definition: robot_laser.h:42
void setOdomPose(const SE2 &odomPose)
represent SE2
Definition: se2.h:40
virtual bool read(std::istream &is)
read the data from a stream
Definition: robot_laser.cpp:50
double _loggerTimestamp
timestamp when the measurement was recorded
Definition: robot_data.h:61
double timestamp() const
Definition: robot_data.h:47
SE2 laserPose() const
Definition: robot_laser.h:52
std::vector< double > _ranges
Definition: raw_laser.h:76
const Eigen::Rotation2Dd & rotation() const
rotational component
Definition: se2.h:58
virtual bool write(std::ostream &os) const
write the data to a stream
Definition: robot_laser.cpp:87
double _turnAxis
Definition: robot_laser.h:59
double loggerTimestamp() const
Definition: robot_data.h:50
const std::vector< double > & ranges() const
the range measurements by the laser
Definition: raw_laser.h:64
double _laserTv
velocities and safety distances of the robot.
Definition: robot_laser.h:59
double _sideSaftyDist
Definition: robot_laser.h:59
const LaserParameters & laserParams() const
the parameters of the laser
Definition: raw_laser.h:72
const std::string & hostname() const
Definition: robot_data.h:56
LaserParameters _laserParams
Definition: raw_laser.h:78
virtual bool refreshPropertyPtrs(HyperGraphElementAction::Parameters *params_)
SE2 inverse() const
invert :-)
Definition: se2.h:82
const Vector2D & translation() const
translational component
Definition: se2.h:54
#define RAD2DEG(x)
Definition: macros.h:35
double _forwardSafetyDist
Definition: robot_laser.h:59
std::vector< double > _remissions
Definition: raw_laser.h:77
yylloc step()
Point2DVector cartesian() const
Definition: raw_laser.cpp:93
parameters for a 2D range finder
Raw laser measuerement.
Definition: raw_laser.h:46