selfdriving
Waypoints.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2021, 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 #pragma once
10 
11 #include <mrpt/containers/yaml.h>
12 #include <mrpt/img/TColor.h>
13 #include <mrpt/math/TPoint2D.h>
14 #include <mrpt/math/TPose2D.h>
15 #include <mrpt/opengl/opengl_frwds.h>
16 #include <mrpt/system/datetime.h>
17 
18 #include <any>
19 #include <optional>
20 #include <string>
21 #include <vector>
22 
23 namespace mpp
24 {
25 using waypoint_idx_t = std::size_t;
26 
27 /** A single waypoint within WaypointSequence. */
28 struct Waypoint
29 {
30  /** Ctor with default values */
31  Waypoint() = default;
32 
33  Waypoint(
34  double target_x, double target_y, double allowed_distance,
35  bool allow_skip = true,
36  std::optional<double> target_heading_ = std::nullopt,
37  double speed_ratio_ = 1.0);
38 
39  /** [Mandatory] Coordinates of desired target location
40  * (world/global coordinates).
41  * \sa target_heading, targetAsPose()
42  */
43  mrpt::math::TPoint2D target{INVALID_NUM, INVALID_NUM};
44 
45  /** Set to the optional desired orientation [radians] of the robot at this
46  * waypoint. (Default: none=any heading).
47  *
48  * Some navigator implementations may ignore this preferred heading,
49  * read the docs of each implementation to find it out.
50  *
51  * \sa targetAsPose()
52  */
53  std::optional<double> targetHeading;
54 
55  /** Returns the target as a SE(2) pose, with its correct heading if
56  * targetHeading is defined, or heading=0 otherwise.
57  */
58  mrpt::math::TPose2D targetAsPose() const;
59 
60  /** (Default="map") Frame ID in which target is given. Optional, use
61  * only for submapping applications. */
62  std::string targetFrameId = "map";
63 
64  /** [Mandatory] How close should the robot get to this waypoint for it to be
65  * considered reached. */
67 
68  /** (Default=1.0) Desired robot speed at the target, as a ratio of the full
69  * robot speed. That is: speed_ratio=1 means that the user wants the robot
70  * to navigate to the target and smoothly continue to the next one when
71  * reached. speed_ratio=0 on the other hand means that the robot should
72  * approach this waypoint slowing down and end up totally stopped.
73  */
74  double speedRatio = 1.0;
75 
76  /** [Default=true] Whether it is allowed to the navigator to proceed to a
77  * more advanced waypoint
78  * in the sequence if it determines that it is easier to skip this one
79  * (e.g. it seems blocked by dynamic obstacles).
80  * This value is ignored for the last waypoint in a sequence, since it is
81  * always considered to be the ultimate goal and hence not subject to be
82  * skipped.
83  *
84  * \sa preferNotToSkip
85  */
86  bool allowSkip = true;
87 
88  /**
89  * This modifies the behavior of TWaypoint::allowSkip according
90  * to:
91  *
92  * \verbatim
93  * +------------+-----------------+---------------------------+
94  * | allowSkip | preferNotToSkip | Waypoint obstructed |
95  * | | | with obstacles? |
96  * | | +------------+--------------+
97  * | | | Yes | No |
98  * +------------+-----------------+------------+--------------+
99  * | false | false | Trigger | Pass through |
100  * | (default) +-----------------+ rnav error | waypoint |
101  * | | true | | |
102  * +------------+-----------------+------------+--------------+
103  * | true | false(default) | Skipped | Skipped |
104  * +------------+-----------------+ +--------------+
105  * | true | true | | Not skipped |
106  * +------------+-----------------+------------+--------------+
107  * \endverbatim
108  *
109  */
110  bool preferNotToSkip = false;
111 
112  /** Check whether all the minimum mandatory fields have been filled by the
113  * user. */
114  bool isValid() const;
115 
116  /** get in human-readable format */
117  std::string getAsText() const;
118 
119  /** Save waypoint as YAML */
120  mrpt::containers::yaml asYAML() const;
121 
122  /** Load waypoint from YAML */
123  static Waypoint FromYAML(const mrpt::containers::yaml& d);
124 
125  /** The default value of fields (used to detect non-set values) */
126  static constexpr int INVALID_NUM{-100000};
127 };
128 
129 /** used in getAsOpenglVisualization() */
131 {
133 
134  double outter_radius{.3}, inner_radius{.2};
135  double outter_radius_non_skippable{.3}, inner_radius_non_skippable{.0};
136  double outter_radius_reached{.2}, inner_radius_reached{.1};
137  double heading_arrow_len{1.0};
138  mrpt::img::TColor color_regular, color_current_goal, color_reached;
139  bool show_labels{true};
140 };
141 
142 /** The struct for requesting navigation requests for a sequence of waypoints.
143  * Users can directly fill in the list of waypoints manipulating the public
144  * field `waypoints`.
145  */
147 {
148  std::vector<Waypoint> waypoints;
149 
150  /** Ctor with default values */
151  WaypointSequence() = default;
152 
153  void clear() { waypoints.clear(); }
154 
155  /** Gets navigation params as a human-readable format */
156  std::string getAsText() const;
157 
158  /** Renders the sequence of waypoints (previous contents of `obj` are
159  * cleared) */
160  void getAsOpenglVisualization(
161  mrpt::opengl::CSetOfObjects& obj,
162  const WaypointsRenderingParams& params = {}) const;
163 
164  /** Save waypoints as YAML */
165  mrpt::containers::yaml asYAML() const;
166 
167  /** Load waypoints from YAML */
168  static WaypointSequence FromYAML(const mrpt::containers::yaml& d);
169 };
170 
171 /** A waypoint with an execution status. \ingroup nav_reactive */
172 struct WaypointStatus : public Waypoint
173 {
174  WaypointStatus() = default;
175 
176  /** Whether this waypoint has been reached already (to within the allowed
177  * distance as per user specifications) or skipped. */
178  bool reached{false};
179 
180  /** If `reached==true` this boolean tells whether the waypoint was
181  * physically reached (false) or marked as reached because it was skipped
182  * (true). */
183  bool skipped{false};
184 
185  /** Timestamp of when this waypoint was reached. (Default=INVALID_TIMESTAMP
186  * means not reached so far) */
187  mrpt::system::TTimeStamp timestamp_reach = INVALID_TIMESTAMP;
188 
189  /** (Initialized to 0 automatically) How many times this waypoint has been
190  * seen as "reachable" before it being the current active waypoint. */
191  int counter_seen_reachable{0};
192 
193  /** Gets navigation params as a human-readable format */
194  std::string getAsText() const;
195 };
196 
197 /** The struct for querying the status of waypoints navigation.
198  */
200 {
201  WaypointStatusSequence() = default;
202 
203  /** Waypoints parameters and status (reached, skipped, etc.) */
204  std::vector<WaypointStatus> waypoints;
205 
206  /** Extracts a copy of the waypoints, without status information. */
208  {
209  WaypointSequence seq;
210  for (const auto& w : waypoints) seq.waypoints.emplace_back(w);
211  return seq;
212  }
213 
214  /** Timestamp of user navigation command. */
215  mrpt::system::TTimeStamp timestamp_nav_started = INVALID_TIMESTAMP;
216 
217  /** Whether the final waypoint has been reached successfuly. */
218  bool final_goal_reached = false;
219 
220  /** Index in `waypoints` of the waypoint the navigator is currently trying
221  * to reach.
222  * This will point to the last waypoint after navigation ends successfully.
223  * It has no value if navigation has not started yet */
224  std::optional<waypoint_idx_t> waypoint_index_current_goal;
225 
226  /** Robot pose at last time step (has INVALID_NUM fields upon
227  * initialization) */
228 
229  /** Ctor with default values */
230  /** Gets navigation params as a human-readable format */
231  std::string getAsText() const;
232 
233  /** Renders the sequence of waypoints (previous contents of `obj` are
234  * cleared) */
235  void getAsOpenglVisualization(
236  mrpt::opengl::CSetOfObjects& obj,
237  const WaypointsRenderingParams& params = {}) const;
238 };
239 } // namespace mpp
mrpt::containers::yaml asYAML() const
std::string targetFrameId
Definition: Waypoints.h:62
bool preferNotToSkip
Definition: Waypoints.h:110
Definition: bestTrajectory.h:15
Waypoint()=default
Definition: Waypoints.h:199
WaypointSequence withoutStatus() const
Definition: Waypoints.h:207
mrpt::img::TColor color_regular
Definition: Waypoints.h:138
void clear()
Definition: Waypoints.h:153
std::optional< double > targetHeading
Definition: Waypoints.h:53
static constexpr int INVALID_NUM
Definition: Waypoints.h:126
bool allowSkip
Definition: Waypoints.h:86
double allowedDistance
Definition: Waypoints.h:66
static Waypoint FromYAML(const mrpt::containers::yaml &d)
bool isValid() const
Definition: Waypoints.h:146
Definition: Waypoints.h:130
mrpt::math::TPoint2D target
Definition: Waypoints.h:43
Definition: Waypoints.h:28
std::vector< WaypointStatus > waypoints
Definition: Waypoints.h:204
std::vector< Waypoint > waypoints
Definition: Waypoints.h:148
std::string getAsText() const
mrpt::math::TPose2D targetAsPose() const
Definition: Waypoints.h:172
double speedRatio
Definition: Waypoints.h:74
std::optional< waypoint_idx_t > waypoint_index_current_goal
Definition: Waypoints.h:224
std::size_t waypoint_idx_t
Definition: Waypoints.h:25