Main MRPT website > C++ reference for MRPT 1.9.9
test.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 
11 #include <mrpt/comms/CSerialPort.h>
16 #include <mrpt/system/os.h>
17 #include <mrpt/system/CTicTac.h>
18 #include <iostream>
19 
20 using namespace mrpt;
21 using namespace mrpt::hwdrivers;
22 using namespace mrpt::obs;
23 using namespace mrpt::maps;
24 using namespace mrpt::gui;
25 using namespace mrpt::poses;
26 using namespace mrpt::system;
27 using namespace std;
28 
29 string SERIAL_NAME; // Name of the serial port to open
30 
31 // ------------------------------------------------------
32 // Test_HOKUYO
33 // ------------------------------------------------------
34 void Test_HOKUYO()
35 {
36  CHokuyoURG laser;
37 
38  string serName, type;
39 
40  string ip;
41 
42  unsigned int port;
43 
44  cout << "Specify the type of the Hokuyo connection, usb or ethernet: ";
45  getline(cin, type);
46 
47  while ((mrpt::system::lowerCase(type) != "usb") &&
48  (mrpt::system::lowerCase(type) != "ethernet"))
49  {
50  cout << "Incorrect type" << endl;
51  cout << "Specify the type of the Hokuyo connection, usb or ethernet: ";
52  getline(cin, type);
53  }
54 
55  cout << endl
56  << endl
57  << "HOKUYO laser range finder test application." << endl
58  << endl;
59 
60  if (mrpt::system::lowerCase(type) == "usb")
61  {
62  if (SERIAL_NAME.empty())
63  {
64  cout << "Enter the serial port name (e.g. COM1, ttyS0, ttyUSB0, "
65  "ttyACM0): ";
66  getline(cin, serName);
67  }
68  else
69  {
70  cout << "Using serial port: " << SERIAL_NAME << endl;
71  serName = SERIAL_NAME;
72  }
73 
74  // Set the laser serial port:
75  laser.setSerialPort(serName);
76  }
77  else
78  {
79  cout << "Enter the ip direction: ";
80  getline(cin, ip);
81 
82  cout << "Enter the port number: ";
83  cin >> port;
84 
85  // Set the laser serial port:
86  laser.setIPandPort(ip, port);
87  }
88  string intensity;
89  cout << endl << endl << "Enable intensity [y/n]:";
90  getline(cin, intensity);
91  laser.setIntensityMode(mrpt::system::lowerCase(intensity) == "y");
92 
93  // Config: Use defaults + selected port ( serial or ethernet )
94 
95  printf("[TEST] Turning laser ON...\n");
96  if (laser.turnOn())
97  printf("[TEST] Initialization OK!\n");
98  else
99  {
100  printf("[TEST] Initialization failed!\n");
101  return;
102  }
103 
104 #if MRPT_HAS_WXWIDGETS
105  CDisplayWindowPlots win("Laser scans");
106 #endif
107 
108  cout << "Press any key to stop capturing..." << endl;
109 
110  CTicTac tictac;
111  tictac.Tic();
112 
113  while (!mrpt::system::os::kbhit())
114  {
115  bool thereIsObservation, hardError;
117 
118  laser.doProcessSimple(thereIsObservation, obs, hardError);
119 
120  if (hardError) printf("[TEST] Hardware error=true!!\n");
121 
122  if (thereIsObservation)
123  {
124  double FPS = 1.0 / tictac.Tac();
125 
126  printf(
127  "Scan received: %u ranges, FOV: %.02fdeg, %.03fHz: mid "
128  "rang=%fm\n",
129  (unsigned int)obs.scan.size(), RAD2DEG(obs.aperture), FPS,
130  obs.scan[obs.scan.size() / 2]);
131 
132  if (obs.hasIntensity())
133  {
134  size_t i;
135  std::cout << "[ ";
136  for (i = 0; i < obs.intensity.size() - 1; i++)
137  {
138  std::cout << obs.intensity[i] << ",\t";
139  if (i % 10 == 9) std::cout << std::endl;
140  }
141  std::cout << obs.intensity[i] << " ]" << std::endl;
142  }
143 
144  obs.sensorPose = CPose3D(0, 0, 0);
145 
148  theMap.insertObservation(&obs);
149 // map.save2D_to_text_file("_out_scan.txt");
150 
151 /*
152 COpenGLScene scene3D;
153 opengl::CPointCloud::Ptr points =
154 mrpt::make_aligned_shared<opengl::CPointCloud>();
155 points->loadFromPointsMap(&map);
156 scene3D.insert(points);
157 CFileStream("_out_point_cloud.3Dscene",fomWrite) << scene3D;
158 */
159 
160 #if MRPT_HAS_WXWIDGETS
161  std::vector<float> xs, ys, zs;
162  theMap.getAllPoints(xs, ys, zs);
163  win.plot(xs, ys, ".b3");
164  win.axis_equal();
165 #endif
166 
167  tictac.Tic();
168  }
169 
170  std::this_thread::sleep_for(15ms);
171  };
172 
173  laser.turnOff();
174 }
175 
176 int main(int argc, char** argv)
177 {
178  try
179  {
180  if (argc > 1)
181  {
182  SERIAL_NAME = string(argv[1]);
183  }
184 
185  Test_HOKUYO();
186  return 0;
187  }
188  catch (std::exception& e)
189  {
190  std::cout << "EXCEPCION: " << e.what() << std::endl;
191  return -1;
192  }
193  catch (...)
194  {
195  printf("Another exception!!");
196  return -1;
197  }
198 }
mrpt::system::os::kbhit
bool kbhit() noexcept
An OS-independent version of kbhit, which returns true if a key has been pushed.
Definition: os.cpp:390
os.h
mrpt::maps::CMetricMap::insertObservation
bool insertObservation(const mrpt::obs::CObservation *obs, const mrpt::poses::CPose3D *robotPose=NULL)
Insert the observation information into this map.
Definition: CMetricMap.cpp:95
SERIAL_NAME
string SERIAL_NAME
Definition: vision_stereo_rectify/test.cpp:25
string_utils.h
mrpt::system::CTicTac
A high-performance stopwatch, with typical resolution of nanoseconds.
Definition: system/CTicTac.h:19
mrpt::obs::CObservation2DRangeScan
A "CObservation"-derived class that represents a 2D range scan measurement (typically from a laser sc...
Definition: CObservation2DRangeScan.h:56
mrpt::hwdrivers::CHokuyoURG::turnOff
bool turnOff()
Disables the scanning mode (this can be used to turn the device in low energy mode,...
Definition: CHokuyoURG.cpp:382
mrpt::maps::CPointsMap::TInsertionOptions::minDistBetweenLaserPoints
float minDistBetweenLaserPoints
The minimum distance between points (in 3D): If two points are too close, one of them is not inserted...
Definition: CPointsMap.h:218
CSerialPort.h
mrpt::hwdrivers
Contains classes for various device interfaces.
Definition: C2DRangeFinderAbstract.h:22
CHokuyoURG.h
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::obs::CObservation2DRangeScan::hasIntensity
bool hasIntensity() const
Return true if scan has intensity.
Definition: CObservation2DRangeScan.cpp:259
mrpt::maps::CPointsMap::getAllPoints
void getAllPoints(VECTOR &xs, VECTOR &ys, VECTOR &zs, size_t decimation=1) const
Returns a copy of the 2D/3D points as a std::vector of float coordinates.
Definition: CPointsMap.h:536
type
GLuint GLuint GLsizei GLenum type
Definition: glext.h:3528
mrpt::poses
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CHierarchicalMapMHPartition.h:25
mrpt::RAD2DEG
double RAD2DEG(const double x)
Radians to degrees.
Definition: core/include/mrpt/core/bits_math.h:48
mrpt::obs
This namespace contains representation of robot actions and observations.
Definition: CParticleFilter.h:17
mrpt::system::lowerCase
std::string lowerCase(const std::string &str)
Returns an lower-case version of a string.
Definition: string_utils.cpp:26
mrpt::system::CTicTac::Tac
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:90
main
int main()
Definition: vision_stereo_rectify/test.cpp:78
mrpt::containers::ContainerReadOnlyProxyAccessor::size
size_t size() const
Definition: ContainerReadOnlyProxyAccessor.h:42
mrpt::obs::CObservation2DRangeScan::aperture
float aperture
The "aperture" or field-of-view of the range finder, in radians (typically M_PI = 180 degrees).
Definition: CObservation2DRangeScan.h:125
mrpt::obs::CObservation2DRangeScan::intensity
mrpt::containers::ContainerReadOnlyProxyAccessor< mrpt::aligned_std_vector< int32_t > > intensity
The intensity values of the scan.
Definition: CObservation2DRangeScan.h:111
mrpt::hwdrivers::CHokuyoURG::setSerialPort
void setSerialPort(const std::string &port_name)
If set to non-empty, the serial port will be attempted to be opened automatically when this class is ...
Definition: CHokuyoURG.h:216
win
mrpt::gui::CDisplayWindow3D::Ptr win
Definition: vision_stereo_rectify/test.cpp:31
mrpt::poses::CPose3D
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
mrpt::hwdrivers::CHokuyoURG::setIntensityMode
bool setIntensityMode(bool enabled)
If true scans will capture intensity.
Definition: CHokuyoURG.cpp:727
mrpt::maps::CSimplePointsMap
A cloud of points in 2D or 3D, which can be built from a sequence of laser scans.
Definition: CSimplePointsMap.h:31
mrpt::system::CTicTac::Tic
void Tic() noexcept
Starts the stopwatch.
Definition: CTicTac.cpp:79
mrpt::gui
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:16
mrpt::gui::CDisplayWindowPlots
Create a GUI window and display plots with MATLAB-like interfaces and commands.
Definition: CDisplayWindowPlots.h:31
Test_HOKUYO
void Test_HOKUYO()
Definition: vision_stereo_rectify/test.cpp:34
mrpt::obs::CObservation2DRangeScan::scan
mrpt::containers::ContainerReadOnlyProxyAccessor< mrpt::aligned_std_vector< float > > scan
The range values of the scan, in meters.
Definition: CObservation2DRangeScan.h:103
mrpt::maps::CPointsMap::insertionOptions
TInsertionOptions insertionOptions
The options used when inserting observations in the map.
Definition: CPointsMap.h:257
CTicTac.h
mrpt::hwdrivers::CHokuyoURG
This software driver implements the protocol SCIP-2.0 for interfacing HOKUYO URG/UTM/UXM/UST laser sc...
Definition: CHokuyoURG.h:77
string
GLsizei const GLchar ** string
Definition: glext.h:4101
mrpt::hwdrivers::CHokuyoURG::setIPandPort
void setIPandPort(const std::string &ip, const unsigned int &port)
Set the ip direction and port to connect using Ethernet communication.
Definition: CHokuyoURG.h:218
mrpt::maps
Definition: CBeacon.h:24
CDisplayWindowPlots.h
CSimplePointsMap.h
mrpt::obs::CObservation2DRangeScan::sensorPose
mrpt::poses::CPose3D sensorPose
The 6D pose of the sensor on the robot at the moment of starting the scan.
Definition: CObservation2DRangeScan.h:133
mrpt::hwdrivers::CHokuyoURG::doProcessSimple
void doProcessSimple(bool &outThereIsObservation, mrpt::obs::CObservation2DRangeScan &outObservation, bool &hardwareError)
Specific laser scanner "software drivers" must process here new data from the I/O stream,...
Definition: CHokuyoURG.cpp:78
mrpt::hwdrivers::CHokuyoURG::turnOn
bool turnOn()
Enables the scanning mode (which may depend on the specific laser device); this must be called before...
Definition: CHokuyoURG.cpp:271
CObservation2DRangeScan.h
mrpt::system
This namespace provides a OS-independent interface to many useful functions: filenames manipulation,...
Definition: math_frwds.h:25



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