Main MRPT website > C++ reference for MRPT 1.9.9
C2DRangeFinderAbstract.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "hwdrivers-precomp.h" // Precompiled headers
11 
13 #include <mrpt/opengl/CAxis.h>
14 #include <mrpt/opengl/CPlanarLaserScan.h> // in library mrpt-maps
15 #include <mrpt/system/os.h>
16 
17 using namespace std;
18 using namespace mrpt::utils;
19 using namespace mrpt::obs;
20 using namespace mrpt::hwdrivers;
21 
22 /*-------------------------------------------------------------
23  Constructor
24 -------------------------------------------------------------*/
25 C2DRangeFinderAbstract::C2DRangeFinderAbstract()
26  : mrpt::utils::COutputLogger("C2DRangeFinderAbstract"),
27  m_lastObservation(),
28  m_lastObservationIsNew(false),
29  m_hardwareError(false),
30  m_nextObservation(),
31  m_showPreview(false),
32  m_stream(nullptr)
33 {
34 }
35 
36 /*-------------------------------------------------------------
37  Destructor
38 -------------------------------------------------------------*/
40 /*-------------------------------------------------------------
41  bindIO
42 -------------------------------------------------------------*/
44 {
45  m_csChangeStream.lock();
46  m_stream = streamIO;
47  m_csChangeStream.unlock();
48 }
49 
50 /*-------------------------------------------------------------
51  getObservation
52 -------------------------------------------------------------*/
54  bool& outThereIsObservation,
55  mrpt::obs::CObservation2DRangeScan& outObservation, bool& hardwareError)
56 {
57  m_csLastObservation.lock();
58 
59  hardwareError = m_hardwareError;
60  outThereIsObservation = m_lastObservationIsNew;
61 
62  if (outThereIsObservation) outObservation = m_lastObservation;
63 
64  m_csLastObservation.unlock();
65 }
66 
67 /*-------------------------------------------------------------
68  doProcess
69 -------------------------------------------------------------*/
71 {
72  bool thereIs, hwError;
73 
74  if (!m_nextObservation)
76  mrpt::make_aligned_shared<CObservation2DRangeScan>();
77 
78  doProcessSimple(thereIs, *m_nextObservation, hwError);
79 
80  if (hwError)
81  {
82  m_state = ssError;
83  THROW_EXCEPTION("Couldn't communicate to the USB board!");
84  }
85 
86  if (thereIs)
87  {
89 
91  m_nextObservation.reset(); // Create a new object in the next call
92  }
93 }
94 
95 /*-------------------------------------------------------------
96  loadExclusionAreas
97 -------------------------------------------------------------*/
99  const mrpt::utils::CConfigFileBase& configSource,
100  const std::string& iniSection)
101 {
102  // Params:
103  m_showPreview = configSource.read_bool(iniSection, "preview", false);
104 
105  // Load exclusion areas:
106  m_lstExclusionPolys.clear();
107  m_lstExclusionAngles.clear();
108 
109  unsigned int N = 1;
110 
111  for (;;)
112  {
113  vector<double> x, y, z_range;
114  configSource.read_vector(
115  iniSection, format("exclusionZone%u_x", N), vector<double>(0), x);
116  configSource.read_vector(
117  iniSection, format("exclusionZone%u_y", N), vector<double>(0), y);
118  configSource.read_vector(
119  iniSection, format("exclusionZone%u_z", N++), vector<double>(0),
120  z_range);
121 
122  if (!x.empty() && !y.empty())
123  {
124  ASSERT_(x.size() == y.size())
125 
126  CObservation2DRangeScan::TListExclusionAreasWithRanges::value_type
127  dat;
128 
129  dat.first.setAllVertices(x, y);
130  if (z_range.empty())
131  {
132  dat.second.first = -std::numeric_limits<double>::max();
133  dat.second.second = std::numeric_limits<double>::max();
134  }
135  else
136  {
137  ASSERTMSG_(
138  z_range.size() == 2,
139  "exclusionZone%u_z must be a range [z_min z_max]");
140  ASSERT_(z_range[0] <= z_range[1]);
141 
142  dat.second.first = z_range[0];
143  dat.second.second = z_range[1];
144  }
145 
146  m_lstExclusionPolys.push_back(dat);
147  }
148  else
149  break;
150  }
151 
152  // Load forbiden angles;
153  N = 1;
154 
155  for (;;)
156  {
157  const double ini = DEG2RAD(
158  configSource.read_double(
159  iniSection, format("exclusionAngles%u_ini", N), -1000));
160  const double end = DEG2RAD(
161  configSource.read_double(
162  iniSection, format("exclusionAngles%u_end", N++), -1000));
163 
164  if (ini > -M_PI && end > -M_PI)
165  m_lstExclusionAngles.push_back(make_pair(ini, end));
166  else
167  break;
168  }
169 }
170 
171 /*-------------------------------------------------------------
172  filterByExclusionAreas
173 -------------------------------------------------------------*/
176 {
178 }
179 
180 /*-------------------------------------------------------------
181  filterByExclusionAngles
182 -------------------------------------------------------------*/
185 {
187 }
188 
191 {
192  using namespace mrpt::opengl;
193 
194  // show laser scan
195  if (m_showPreview)
196  {
197  if (!m_win)
198  {
199  string caption = string("Preview of ") + m_sensorLabel;
200  m_win = mrpt::make_aligned_shared<mrpt::gui::CDisplayWindow3D>(
201  caption, 640, 480);
202  m_win->setCameraAzimuthDeg(180);
203  m_win->setCameraElevationDeg(90);
204  COpenGLScene::Ptr& theScene = m_win->get3DSceneAndLock();
205  theScene->insert(
206  CAxis::Ptr(
207  mrpt::make_aligned_shared<CAxis>(
208  -300, -300, -50, 300, 300, 50, 1.0, 3, true)));
209  m_win->unlockAccess3DScene();
210  }
211 
212  if (m_win && m_win->isOpen())
213  {
214  COpenGLScene::Ptr& theScene = m_win->get3DSceneAndLock();
216  CRenderizable::Ptr obj = theScene->getByName("laser");
217  if (!obj)
218  {
219  laser = mrpt::make_aligned_shared<opengl::CPlanarLaserScan>();
220  laser->setName("laser");
221  laser->setScan(obs);
222  theScene->insert(laser);
223  }
224  else
225  {
226  laser = std::dynamic_pointer_cast<CPlanarLaserScan>(obj);
227  laser->setScan(obs);
228  }
229  m_win->unlockAccess3DScene();
230  m_win->forceRepaint();
231  } // end if
232  } // end if
233 }
virtual void doProcessSimple(bool &outThereIsObservation, mrpt::obs::CObservation2DRangeScan &outObservation, bool &hardwareError)=0
Specific laser scanner "software drivers" must process here new data from the I/O stream...
mrpt::obs::CObservation2DRangeScan m_lastObservation
bool read_bool(const std::string &section, const std::string &name, bool defaultValue, bool failIfNotFound=false) const
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
void filterByExclusionAngles(const std::vector< std::pair< double, double >> &angles)
Mark as invalid the ranges in any of a given set of "forbiden angle ranges", given as pairs<min_angle...
utils::CStream * m_stream
The I/O channel (will be nullptr if not bound).
mrpt::gui::CDisplayWindow3D::Ptr m_win
std::string m_sensorLabel
See CGenericSensor.
#define THROW_EXCEPTION(msg)
std::mutex m_csChangeStream
For being thread-safe.
Contains classes for various device interfaces.
std::shared_ptr< CRenderizable > Ptr
Definition: CRenderizable.h:45
STL namespace.
#define M_PI
Definition: bits.h:92
void filterByExclusionAreas(mrpt::obs::CObservation2DRangeScan &obs) const
Mark as invalid those points which (x,y) coordinates fall within the exclusion polygons.
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
void filterByExclusionAngles(mrpt::obs::CObservation2DRangeScan &obs) const
Mark as invalid those ranges in a set of forbiden angle ranges.
This class allows loading and storing values and vectors of different types from a configuration text...
void getObservation(bool &outThereIsObservation, mrpt::obs::CObservation2DRangeScan &outObservation, bool &hardwareError)
Get the last observation from the sensor, if available, and unmarks it as being "the last one" (thus ...
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
std::vector< std::pair< double, double > > m_lstExclusionAngles
A list of pairs of angles <init,end> such as all sensor ranges falling in those forbiden angles will ...
void read_vector(const std::string &section, const std::string &name, const VECTOR_TYPE &defaultValue, VECTOR_TYPE &outValues, bool failIfNotFound=false) const
Reads a configuration parameter of type vector, stored in the file as a string: "[v1 v2 v3 ...
GLuint GLuint end
Definition: glext.h:3528
std::shared_ptr< CPlanarLaserScan > Ptr
This namespace contains representation of robot actions and observations.
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:19
This object renders a 2D laser scan by means of three elements: the points, the line along end-points...
std::shared_ptr< CAxis > Ptr
Definition: CAxis.h:33
void bindIO(mrpt::utils::CStream *streamIO)
Binds the object to a given I/O channel.
#define DEG2RAD
void loadCommonParams(const mrpt::utils::CConfigFileBase &configSource, const std::string &iniSection)
Should be call by derived classes at "loadConfig" (loads exclusion areas AND exclusion angles)...
GLsizei const GLchar ** string
Definition: glext.h:4101
mrpt::obs::CObservation2DRangeScan::TListExclusionAreasWithRanges m_lstExclusionPolys
A list of optional exclusion polygons, in coordinates relative to the vehicle, that is...
mrpt::obs::CObservation2DRangeScan::Ptr m_nextObservation
A dynamic object used as buffer in doProcess.
void appendObservation(const mrpt::utils::CSerializable::Ptr &obj)
Like appendObservations() but for just one observation.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
A "CObservation"-derived class that represents a 2D range scan measurement (typically from a laser sc...
double read_double(const std::string &section, const std::string &name, double defaultValue, bool failIfNotFound=false) const
bool m_showPreview
If true, shows a 3D window with a preview of the grabber data.
std::shared_ptr< COpenGLScene > Ptr
Definition: COpenGLScene.h:62
void doProcess()
Main method for a CGenericSensor.
void processPreview(const mrpt::obs::CObservation2DRangeScan &obs)
Must be called inside the capture method to allow optional GUI preview of scans.
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
#define ASSERT_(f)
GLenum GLint GLint y
Definition: glext.h:3538
GLenum GLsizei GLenum format
Definition: glext.h:3531
GLenum GLint x
Definition: glext.h:3538
void filterByExclusionAreas(const TListExclusionAreas &areas)
Mark as invalid sensed points that fall within any of a set of "exclusion areas", given in coordinate...
#define ASSERTMSG_(f, __ERROR_MSG)
void setScan(const mrpt::obs::CObservation2DRangeScan &scan)



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019