MRPT  1.9.9
test.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include <mrpt/comms/CSerialPort.h>
15 #include <mrpt/system/CTicTac.h>
16 #include <mrpt/system/os.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  opengl::CPointCloud::Create();
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 (const std::exception& e)
189  {
190  std::cerr << "MRPT error: " << mrpt::exception_to_str(e) << std::endl;
191  return -1;
192  }
193  catch (...)
194  {
195  printf("Another exception!!");
196  return -1;
197  }
198 }
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:86
double RAD2DEG(const double x)
Radians to degrees.
Create a GUI window and display plots with MATLAB-like interfaces and commands.
mrpt::containers::ContainerReadOnlyProxyAccessor< mrpt::aligned_std_vector< int32_t > > intensity
The intensity values of the scan.
A high-performance stopwatch, with typical resolution of nanoseconds.
Contains classes for various device interfaces.
A cloud of points in 2D or 3D, which can be built from a sequence of laser scans. ...
bool turnOn() override
Enables the scanning mode (which may depend on the specific laser device); this must be called before...
Definition: CHokuyoURG.cpp:273
STL namespace.
void doProcessSimple(bool &outThereIsObservation, mrpt::obs::CObservation2DRangeScan &outObservation, bool &hardwareError) override
Specific laser scanner "software drivers" must process here new data from the I/O stream...
Definition: CHokuyoURG.cpp:64
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:213
std::string lowerCase(const std::string &str)
Returns an lower-case version of a string.
mrpt::containers::ContainerReadOnlyProxyAccessor< mrpt::aligned_std_vector< float > > scan
The range values of the scan, in meters.
bool turnOff() override
Disables the scanning mode (this can be used to turn the device in low energy mode, if available)
Definition: CHokuyoURG.cpp:384
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:557
bool setIntensityMode(bool enabled)
If true scans will capture intensity.
Definition: CHokuyoURG.cpp:728
This namespace contains representation of robot actions and observations.
mrpt::gui::CDisplayWindow3D::Ptr win
GLsizei const GLchar ** string
Definition: glext.h:4116
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
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...
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:84
void setIPandPort(const std::string &ip, const unsigned int &port)
Set the ip direction and port to connect using Ethernet communication.
Definition: CHokuyoURG.h:215
void Test_HOKUYO()
This software driver implements the protocol SCIP-2.0 for interfacing HOKUYO URG/UTM/UXM/UST laser sc...
Definition: CHokuyoURG.h:74
bool kbhit() noexcept
An OS-independent version of kbhit, which returns true if a key has been pushed.
Definition: os.cpp:394
float aperture
The "aperture" or field-of-view of the range finder, in radians (typically M_PI = 180 degrees)...
std::string exception_to_str(const std::exception &e)
Builds a nice textual representation of a nested exception, which if generated using MRPT macros (THR...
Definition: exceptions.cpp:59
TInsertionOptions insertionOptions
The options used when inserting observations in the map.
Definition: CPointsMap.h:271
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:14
bool hasIntensity() const
Return true if scan has intensity.
void Tic() noexcept
Starts the stopwatch.
Definition: CTicTac.cpp:75
mrpt::poses::CPose3D sensorPose
The 6D pose of the sensor on the robot at the moment of starting the scan.
GLuint GLuint GLsizei GLenum type
Definition: glext.h:3532
bool insertObservation(const mrpt::obs::CObservation &obs, const mrpt::poses::CPose3D *robotPose=nullptr)
Insert the observation information into this map.
Definition: CMetricMap.cpp:93
float minDistBetweenLaserPoints
The minimum distance between points (in 3D): If two points are too close, one of them is not inserted...
Definition: CPointsMap.h:232
string SERIAL_NAME



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019