MRPT  2.0.4
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-2020, 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 
14 #include <mrpt/poses/CPose2D.h>
16 #include <mrpt/system/CTicTac.h>
17 #include <mrpt/system/filesystem.h>
18 #include <iostream>
19 
20 using namespace mrpt;
21 using namespace mrpt::maps;
22 using namespace mrpt::nav;
23 using namespace mrpt::serialization;
24 using namespace mrpt::img;
25 using namespace mrpt::math;
26 using namespace mrpt::poses;
27 using namespace mrpt::io;
28 using namespace mrpt::system;
29 using namespace std;
30 
31 #include <mrpt/examples_config.h>
32 
33 string myGridMap(
34  MRPT_EXAMPLES_BASE_DIRECTORY +
35  string("../share/mrpt/datasets/2006-MalagaCampus.gridmap.gz"));
36 
37 // ------------------------------------------------------
38 // TestPathPlanning
39 // ------------------------------------------------------
40 void TestPathPlanning()
41 {
42  // Load the gridmap:
43  COccupancyGridMap2D gridmap;
44 
46  THROW_EXCEPTION_FMT("Map file '%s' not found", myGridMap.c_str());
47 
48  printf("Loading gridmap...");
49  {
51  auto arch = archiveFrom(f);
52  arch >> gridmap;
53  }
54  printf(
55  "Done! %f x %f m\n", gridmap.getXMax() - gridmap.getXMin(),
56  gridmap.getYMax() - gridmap.getYMin());
57 
58  // Find path:
59  PlannerSimple2D pathPlanning;
60  pathPlanning.robotRadius = 0.30f;
61 
62  std::deque<TPoint2D> thePath;
63  bool notFound;
64  CTicTac tictac;
65 
66  CPose2D origin(20, -110, 0);
67  CPose2D target(90, 40, 0);
68 
69  cout << "Origin: " << origin << endl;
70  cout << "Target: " << target << endl;
71 
72  cout << "Searching path...";
73  cout.flush();
74  tictac.Tic();
75 
76  pathPlanning.computePath(gridmap, origin, target, thePath, notFound);
77 
78  double t = tictac.Tac();
79  cout << "Done in " << t * 1000 << " ms" << endl;
80 
81  printf("Path found: %s\n", notFound ? "NO" : "YES");
82  printf("Path has %u steps\n", (unsigned)thePath.size());
83 
84  // Save result:
85  CImage img;
86  gridmap.getAsImage(img, false, true); // Force a RGB image
87 
88  // Draw the path:
89  // ---------------------
90  int R = round(pathPlanning.robotRadius / gridmap.getResolution());
91 
92  for (std::deque<TPoint2D>::const_iterator it = thePath.begin();
93  it != thePath.end(); ++it)
94  img.drawCircle(
95  gridmap.x2idx(it->x), gridmap.getSizeY() - 1 - gridmap.y2idx(it->y),
96  R, TColor(0, 0, 255));
97 
98  img.drawMark(
99  gridmap.x2idx(origin.x()),
100  gridmap.getSizeY() - 1 - gridmap.y2idx(origin.y()),
101  TColor(0x20, 0x20, 0x20), '+', 10);
102  img.drawMark(
103  gridmap.x2idx(target.x()),
104  gridmap.getSizeY() - 1 - gridmap.y2idx(target.y()),
105  TColor(0x50, 0x50, 0x50), 'x', 10);
106 
107  const std::string dest = "path_planning.png";
108  cout << "Saving output to: " << dest << endl;
109  img.saveToFile(dest);
110  printf("Done\n");
111 
112 #if MRPT_HAS_WXWIDGETS
113  mrpt::gui::CDisplayWindow3D win("Computed path");
114  win.setImageView(img);
115  win.repaint();
116 
117  win.waitForKey();
118 #endif
119 }
120 
121 int main(int argc, char** argv)
122 {
123  try
124  {
126  return 0;
127  }
128  catch (exception& e)
129  {
130  cout << "MRPT exception caught: " << e.what() << endl;
131  return -1;
132  }
133  catch (...)
134  {
135  printf("Another exception!!");
136  return -1;
137  }
138 }
float getXMax() const
Returns the "x" coordinate of right side of grid map.
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:87
float getYMin() const
Returns the "y" coordinate of top side of grid map.
float getResolution() const
Returns the resolution of the grid map.
void TestPathPlanning()
bool fileExists(const std::string &fileName)
Test if a given file (or directory) exists.
Definition: filesystem.cpp:128
A high-performance stopwatch, with typical resolution of nanoseconds.
STL namespace.
float robotRadius
The aproximate robot radius used in the planification.
Searches for collision-free path in 2D occupancy grids for holonomic circular robots.
CArchiveStreamBase< STREAM > archiveFrom(STREAM &s)
Helper function to create a templatized wrapper CArchive object for a: MRPT&#39;s CStream, std::istream, std::ostream, std::stringstream.
Definition: CArchive.h:592
This base provides a set of functions for maths stuff.
float getXMin() const
Returns the "x" coordinate of left side of grid map.
mrpt::gui::CDisplayWindow3D::Ptr win
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
A class for storing an occupancy grid map.
void getAsImage(mrpt::img::CImage &img, bool verticalFlip=false, bool forceRGB=false, bool tricolor=false) const
Returns the grid as a 8-bit graylevel image, where each pixel is a cell (output image is RGB only if ...
string myGridMap(MRPT_EXAMPLES_BASE_DIRECTORY+string("../share/mrpt/datasets/2006-MalagaCampus.gridmap.gz"))
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
const char * argv[]
A class used to store a 2D pose, including the 2D coordinate point and a heading (phi) angle...
Definition: CPose2D.h:39
const float R
float getYMax() const
Returns the "y" coordinate of bottom side of grid map.
Transparently opens a compressed "gz" file and reads uncompressed data from it.
const int argc
A RGB color - 8bit.
Definition: TColor.h:25
void Tic() noexcept
Starts the stopwatch.
Definition: CTicTac.cpp:76
unsigned int getSizeY() const
Returns the vertical size of grid map in cells count.
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:69
int x2idx(float x) const
Transform a coordinate value into a cell index.
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:148
A graphical user interface (GUI) for efficiently rendering 3D scenes in real-time.
int round(const T value)
Returns the closer integer (int) to x.
Definition: round.h:24



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020