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-2018, 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::obs;
19 using namespace mrpt::io;
20 using namespace mrpt::hwdrivers;
21 
22 /*-------------------------------------------------------------
23  Constructor
24 -------------------------------------------------------------*/
25 C2DRangeFinderAbstract::C2DRangeFinderAbstract()
26  : mrpt::system::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::config::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  CObservation2DRangeScan::TListExclusionAreasWithRanges::value_type
126  dat;
127 
128  dat.first.setAllVertices(x, y);
129  if (z_range.empty())
130  {
131  dat.second.first = -std::numeric_limits<double>::max();
132  dat.second.second = std::numeric_limits<double>::max();
133  }
134  else
135  {
136  ASSERTMSG_(
137  z_range.size() == 2,
138  "exclusionZone%u_z must be a range [z_min z_max]");
139  ASSERT_(z_range[0] <= z_range[1]);
140 
141  dat.second.first = z_range[0];
142  dat.second.second = z_range[1];
143  }
144 
145  m_lstExclusionPolys.push_back(dat);
146  }
147  else
148  break;
149  }
150 
151  // Load forbiden angles;
152  N = 1;
153 
154  for (;;)
155  {
156  const double ini = DEG2RAD(
157  configSource.read_double(
158  iniSection, format("exclusionAngles%u_ini", N), -1000));
159  const double end = DEG2RAD(
160  configSource.read_double(
161  iniSection, format("exclusionAngles%u_end", N++), -1000));
162 
163  if (ini > -M_PI && end > -M_PI)
164  m_lstExclusionAngles.push_back(make_pair(ini, end));
165  else
166  break;
167  }
168 }
169 
170 /*-------------------------------------------------------------
171  filterByExclusionAreas
172 -------------------------------------------------------------*/
175 {
177 }
178 
179 /*-------------------------------------------------------------
180  filterByExclusionAngles
181 -------------------------------------------------------------*/
184 {
186 }
187 
190 {
191  using namespace mrpt::opengl;
192 
193  // show laser scan
194  if (m_showPreview)
195  {
196  if (!m_win)
197  {
198  string caption = string("Preview of ") + m_sensorLabel;
199  m_win = mrpt::make_aligned_shared<mrpt::gui::CDisplayWindow3D>(
200  caption, 640, 480);
201  m_win->setCameraAzimuthDeg(180);
202  m_win->setCameraElevationDeg(90);
203  COpenGLScene::Ptr& theScene = m_win->get3DSceneAndLock();
204  theScene->insert(
205  CAxis::Ptr(
206  mrpt::make_aligned_shared<CAxis>(
207  -300, -300, -50, 300, 300, 50, 1.0, 3, true)));
208  m_win->unlockAccess3DScene();
209  }
210 
211  if (m_win && m_win->isOpen())
212  {
213  COpenGLScene::Ptr& theScene = m_win->get3DSceneAndLock();
215  CRenderizable::Ptr obj = theScene->getByName("laser");
216  if (!obj)
217  {
218  laser = mrpt::make_aligned_shared<opengl::CPlanarLaserScan>();
219  laser->setName("laser");
220  laser->setScan(obs);
221  theScene->insert(laser);
222  }
223  else
224  {
225  laser = std::dynamic_pointer_cast<CPlanarLaserScan>(obj);
226  laser->setScan(obs);
227  }
228  m_win->unlockAccess3DScene();
229  m_win->forceRepaint();
230  } // end if
231  } // end if
232 }
os.h
format
GLenum GLsizei GLenum format
Definition: glext.h:3531
mrpt::hwdrivers::C2DRangeFinderAbstract::bindIO
void bindIO(mrpt::io::CStream *streamIO)
Binds the object to a given I/O channel.
Definition: C2DRangeFinderAbstract.cpp:43
mrpt::hwdrivers::C2DRangeFinderAbstract::m_win
mrpt::gui::CDisplayWindow3D::Ptr m_win
Definition: C2DRangeFinderAbstract.h:68
mrpt::io
Definition: img/CImage.h:22
mrpt::obs::CObservation2DRangeScan
A "CObservation"-derived class that represents a 2D range scan measurement (typically from a laser sc...
Definition: CObservation2DRangeScan.h:56
mrpt::config::CConfigFileBase::read_double
double read_double(const std::string &section, const std::string &name, double defaultValue, bool failIfNotFound=false) const
Definition: CConfigFileBase.cpp:101
end
GLuint GLuint end
Definition: glext.h:3528
mrpt::hwdrivers::C2DRangeFinderAbstract::doProcessSimple
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,...
obj
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
mrpt::hwdrivers::C2DRangeFinderAbstract::filterByExclusionAngles
void filterByExclusionAngles(mrpt::obs::CObservation2DRangeScan &obs) const
Mark as invalid those ranges in a set of forbiden angle ranges.
Definition: C2DRangeFinderAbstract.cpp:182
mrpt::hwdrivers::CGenericSensor::ssWorking
@ ssWorking
Definition: CGenericSensor.h:87
mrpt::config::CConfigFileBase::read_bool
bool read_bool(const std::string &section, const std::string &name, bool defaultValue, bool failIfNotFound=false) const
Definition: CConfigFileBase.cpp:150
mrpt::hwdrivers
Contains classes for various device interfaces.
Definition: C2DRangeFinderAbstract.h:22
mrpt::hwdrivers::CGenericSensor::ssError
@ ssError
Definition: CGenericSensor.h:88
mrpt::opengl::CRenderizable::Ptr
std::shared_ptr< CRenderizable > Ptr
Definition: CRenderizable.h:45
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::utils::COutputLogger
mrpt::system::COutputLogger COutputLogger
Definition: utils/COutputLogger.h:7
THROW_EXCEPTION
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
ASSERT_
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:113
mrpt::hwdrivers::CGenericSensor::m_state
TSensorState m_state
Definition: CGenericSensor.h:147
mrpt::hwdrivers::C2DRangeFinderAbstract::m_hardwareError
bool m_hardwareError
Definition: C2DRangeFinderAbstract.h:49
mrpt::obs
This namespace contains representation of robot actions and observations.
Definition: CParticleFilter.h:17
mrpt::hwdrivers::C2DRangeFinderAbstract::getObservation
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 ...
Definition: C2DRangeFinderAbstract.cpp:53
M_PI
#define M_PI
Definition: core/include/mrpt/core/bits_math.h:38
mrpt::obs::CObservation2DRangeScan::filterByExclusionAngles
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...
Definition: CObservation2DRangeScan.cpp:351
mrpt::hwdrivers::C2DRangeFinderAbstract::~C2DRangeFinderAbstract
virtual ~C2DRangeFinderAbstract()
Destructor.
Definition: C2DRangeFinderAbstract.cpp:39
mrpt::hwdrivers::C2DRangeFinderAbstract::m_lstExclusionPolys
mrpt::obs::CObservation2DRangeScan::TListExclusionAreasWithRanges m_lstExclusionPolys
A list of optional exclusion polygons, in coordinates relative to the vehicle, that is,...
Definition: C2DRangeFinderAbstract.h:61
mrpt::config::CConfigFileBase
This class allows loading and storing values and vectors of different types from a configuration text...
Definition: config/CConfigFileBase.h:44
mrpt::format
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
mrpt::hwdrivers::C2DRangeFinderAbstract::m_stream
mrpt::io::CStream * m_stream
The I/O channel (will be nullptr if not bound).
Definition: C2DRangeFinderAbstract.h:72
mrpt::hwdrivers::C2DRangeFinderAbstract::loadCommonParams
void loadCommonParams(const mrpt::config::CConfigFileBase &configSource, const std::string &iniSection)
Should be call by derived classes at "loadConfig" (loads exclusion areas AND exclusion angles).
Definition: C2DRangeFinderAbstract.cpp:98
mrpt::hwdrivers::C2DRangeFinderAbstract::m_nextObservation
mrpt::obs::CObservation2DRangeScan::Ptr m_nextObservation
A dynamic object used as buffer in doProcess.
Definition: C2DRangeFinderAbstract.h:56
mrpt::hwdrivers::C2DRangeFinderAbstract::m_csChangeStream
std::mutex m_csChangeStream
For being thread-safe.
Definition: C2DRangeFinderAbstract.h:53
mrpt::hwdrivers::C2DRangeFinderAbstract::m_lastObservationIsNew
bool m_lastObservationIsNew
Definition: C2DRangeFinderAbstract.h:48
mrpt::hwdrivers::CGenericSensor::appendObservation
void appendObservation(const mrpt::serialization::CSerializable::Ptr &obj)
Like appendObservations() but for just one observation.
Definition: CGenericSensor.h:179
mrpt::hwdrivers::CGenericSensor::m_sensorLabel
std::string m_sensorLabel
See CGenericSensor.
Definition: CGenericSensor.h:140
mrpt::opengl::CPlanarLaserScan::Ptr
std::shared_ptr< CPlanarLaserScan > Ptr
Definition: CPlanarLaserScan.h:59
mrpt::opengl::CAxis::Ptr
std::shared_ptr< CAxis > Ptr
Definition: CAxis.h:33
mrpt::hwdrivers::C2DRangeFinderAbstract::m_showPreview
bool m_showPreview
If true, shows a 3D window with a preview of the grabber data.
Definition: C2DRangeFinderAbstract.h:67
ASSERTMSG_
#define ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
Definition: exceptions.h:101
mrpt::hwdrivers::C2DRangeFinderAbstract::processPreview
void processPreview(const mrpt::obs::CObservation2DRangeScan &obs)
Must be called inside the capture method to allow optional GUI preview of scans.
Definition: C2DRangeFinderAbstract.cpp:188
mrpt::hwdrivers::C2DRangeFinderAbstract::m_lastObservation
mrpt::obs::CObservation2DRangeScan m_lastObservation
Definition: C2DRangeFinderAbstract.h:47
mrpt::hwdrivers::C2DRangeFinderAbstract::m_lstExclusionAngles
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 ...
Definition: C2DRangeFinderAbstract.h:64
CAxis.h
mrpt::hwdrivers::C2DRangeFinderAbstract::m_csLastObservation
std::mutex m_csLastObservation
Definition: C2DRangeFinderAbstract.h:53
string
GLsizei const GLchar ** string
Definition: glext.h:4101
mrpt::opengl::COpenGLScene::Ptr
std::shared_ptr< COpenGLScene > Ptr
Definition: COpenGLScene.h:61
CPlanarLaserScan.h
mrpt::obs::CObservation2DRangeScan::filterByExclusionAreas
void filterByExclusionAreas(const TListExclusionAreas &areas)
Mark as invalid sensed points that fall within any of a set of "exclusion areas", given in coordinate...
Definition: CObservation2DRangeScan.cpp:330
mrpt::opengl
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
mrpt::config::CConfigFileBase::read_vector
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 ....
Definition: config/CConfigFileBase.h:171
y
GLenum GLint GLint y
Definition: glext.h:3538
mrpt::hwdrivers::C2DRangeFinderAbstract::filterByExclusionAreas
void filterByExclusionAreas(mrpt::obs::CObservation2DRangeScan &obs) const
Mark as invalid those points which (x,y) coordinates fall within the exclusion polygons.
Definition: C2DRangeFinderAbstract.cpp:173
mrpt::hwdrivers::C2DRangeFinderAbstract::doProcess
void doProcess()
Main method for a CGenericSensor.
Definition: C2DRangeFinderAbstract.cpp:70
x
GLenum GLint x
Definition: glext.h:3538
hwdrivers-precomp.h
C2DRangeFinderAbstract.h
mrpt::io::CStream
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: io/CStream.h:30
mrpt::DEG2RAD
double DEG2RAD(const double x)
Degrees to radians.
Definition: core/include/mrpt/core/bits_math.h:42



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST