Carma-platform v4.2.0
CARMA Platform is built on robot operating system (ROS) and utilizes open source software (OSS) that enables Cooperative Driving Automation (CDA) features to allow Automated Driving Systems to interact and cooperate with infrastructure and other vehicles through communication.
route_generator_worker.hpp
Go to the documentation of this file.
1#pragma once
2
3/*
4 * Copyright (C) 2020-2022 LEIDOS.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7 * use this file except in compliance with the License. You may obtain a copy of
8 * the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15 * License for the specific language governing permissions and limitations under
16 * the License.
17 */
18
19#include <rclcpp/rclcpp.hpp>
20
21#include <carma_planning_msgs/msg/route.hpp>
22#include <carma_planning_msgs/msg/route_state.hpp>
23#include <carma_planning_msgs/msg/route_event.hpp>
24#include <geometry_msgs/msg/pose_stamped.hpp>
25#include <geometry_msgs/msg/twist_stamped.hpp>
26#include <visualization_msgs/msg/marker.hpp>
27#include <visualization_msgs/msg/marker_array.hpp>
28#include <std_msgs/msg/string.hpp>
29#include <carma_planning_msgs/srv/get_available_routes.hpp>
30#include <carma_planning_msgs/srv/set_active_route.hpp>
31#include <carma_planning_msgs/srv/abort_active_route.hpp>
32
35#include <carma_wm/Geometry.hpp>
36
37#include <lanelet2_core/geometry/Lanelet.h>
38#include <lanelet2_core/primitives/Lanelet.h>
39#include <lanelet2_extension/projection/local_frame_projector.h>
40#include <lanelet2_extension/io/autoware_osm_parser.h>
41#include <lanelet2_core/utility/Utilities.h>
42
43#include <wgs84_utils/wgs84_utils.h>
44#include <boost/filesystem.hpp>
45#include <boost/optional.hpp>
46#include <math.h>
47#include <unordered_set>
48#include <functional>
49#include <tf2_ros/transform_listener.h>
50#include <tf2_ros/buffer.h>
51#include <tf2/LinearMath/Transform.h>
52#include <tf2_geometry_msgs/tf2_geometry_msgs.hpp>
53
55
56namespace route {
57
59 {
60
61 public:
65 RouteGeneratorWorker(tf2_ros::Buffer& tf2_buffer);
66
70 std::function<bool()> reroutingChecker;
71
75 void setReroutingChecker(const std::function<bool()> inputFunction);
76
82
87 void setLoggerInterface(rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr logger);
88
93 void setClock(rclcpp::Clock::SharedPtr clock);
94
103 lanelet::Optional<lanelet::routing::Route> routing(const lanelet::BasicPoint2d start,
104 const std::vector<lanelet::BasicPoint2d>& via,
105 const lanelet::BasicPoint2d end,
106 const lanelet::LaneletMapConstPtr map_pointer,
107 const carma_wm::LaneletRoutingGraphConstPtr graph_pointer) const;
108
114 bool getAvailableRouteCb(const std::shared_ptr<rmw_request_id_t>,
115 const std::shared_ptr<carma_planning_msgs::srv::GetAvailableRoutes::Request>,
116 std::shared_ptr<carma_planning_msgs::srv::GetAvailableRoutes::Response> resp);
117
124 bool setActiveRouteCb(const std::shared_ptr<rmw_request_id_t>,
125 const std::shared_ptr<carma_planning_msgs::srv::SetActiveRoute::Request> req,
126 std::shared_ptr<carma_planning_msgs::srv::SetActiveRoute::Response> resp);
127
133 bool abortActiveRouteCb(const std::shared_ptr<rmw_request_id_t>,
134 const std::shared_ptr<carma_planning_msgs::srv::AbortActiveRoute::Request>,
135 std::shared_ptr<carma_planning_msgs::srv::AbortActiveRoute::Response> resp);
136
140 void bumperPoseCb();
141
146 void twistCb(geometry_msgs::msg::TwistStamped::UniquePtr msg);
147
152 void georeferenceCb(std_msgs::msg::String::UniquePtr msg);
153
158 void setRouteFilePath(const std::string& path);
159
164 void setDowntrackDestinationRange(double dt_dest_range);
165
173 void setPublishers(const carma_ros2_utils::PubPtr<carma_planning_msgs::msg::RouteEvent>& route_event_pub,
174 const carma_ros2_utils::PubPtr<carma_planning_msgs::msg::RouteState>& route_state_pub,
175 const carma_ros2_utils::PubPtr<carma_planning_msgs::msg::Route>& route_pub,
176 const carma_ros2_utils::PubPtr<visualization_msgs::msg::Marker>& route_marker_pub);
177
183 bool checkForDuplicateLaneletsInShortestPath(const lanelet::routing::Route& route) const;
184
189 std::vector<lanelet::BasicPoint3d> loadRouteDestinationsInMapFrame(const std::vector<carma_v2x_msgs::msg::Position3D>& destinations) const;
190
195 std::vector<carma_v2x_msgs::msg::Position3D> loadRouteDestinationGpsPointsFromRouteId(const std::string& route_id) const;
196
201 carma_planning_msgs::msg::Route composeRouteMsg(const lanelet::Optional<lanelet::routing::Route>& route) const;
202
206 bool spinCallback();
207
212 visualization_msgs::msg::Marker composeRouteMarkerMsg(const lanelet::Optional<lanelet::routing::Route>& route);
213
221 bool crosstrackErrorCheck(const std::shared_ptr<geometry_msgs::msg::PoseStamped>& msg, lanelet::ConstLanelet current_llt);
222
228 void setCrosstrackErrorCountMax(int cte_max);
229
235 void setCrosstrackErrorDistance(double cte_dist);
236
243 lanelet::ConstLanelet getClosestLaneletFromRouteLanelets(lanelet::BasicPoint2d position) const;
244
245 // Added for Unit Testing
246 void addLanelet(lanelet::ConstLanelet llt);
247
253 lanelet::Optional<lanelet::routing::Route> rerouteAfterRouteInvalidation(const std::vector<lanelet::BasicPoint2d>& destination_points_in_map);
254
259
260 // Current vehicle pose if it has been recieved
261 boost::optional<geometry_msgs::msg::PoseStamped> vehicle_pose_;
262
263 private:
264
265 const double DEG_TO_RAD = 0.0174533;
266
267 // route state worker
269
270 // directory of route files
271 std::string route_file_path_;
272
273 // const pointer to world model object
275
276 // route messages waiting to be updated and published
277 carma_planning_msgs::msg::Route route_msg_;
278 carma_planning_msgs::msg::RouteEvent route_event_msg_;
279 carma_planning_msgs::msg::RouteState route_state_msg_;
280 visualization_msgs::msg::Marker route_marker_msg_;
281
282 std::vector<lanelet::ConstPoint3d> points_;
283
284 //List of lanelets in the route
285 lanelet::ConstLanelets route_llts;
286
287 // minimum down track error which can trigger route complete event
289
290 // current cross track distance
292
293 // current down track distance relevant to the route
295
296 // current pose
297 lanelet::BasicPoint2d current_loc_;
298
299 // current lanelet cross track distance
301
302 // current lanelet down track
304
305 lanelet::Id ll_id_;
306
307 // current speed limit on current lanelet
308 double speed_limit_ = 0;
309 // Current vehicle forward speed
310 double current_speed_ = 0;
311 //A small static value for comparing doubles
312 static constexpr double epsilon_ = 0.001;
313
314 // local copy of Route publihsers
315 carma_ros2_utils::PubPtr<carma_planning_msgs::msg::RouteEvent> route_event_pub_;
316 carma_ros2_utils::PubPtr<carma_planning_msgs::msg::RouteState> route_state_pub_;
317 carma_ros2_utils::PubPtr<carma_planning_msgs::msg::Route> route_pub_;
318 carma_ros2_utils::PubPtr<visualization_msgs::msg::Marker> route_marker_pub_;
319
320 // a bool flag indicates a new route has been generated such that a local copy of Route message should be published again
323
324 // a queue of route event. All events in the queue will be published in order on each spin.
325 std::queue<uint8_t> route_event_queue;
326
327 // private helper function to add a new route event into event queue
328 void publishRouteEvent(uint8_t event_type);
329
330 // maximum cross track error which can trigger left route event
332
333 // counter to record how many times vehicle's position exceeds crosstrack distance
334 int cte_count_ = 0;
335
337
338 // destination points in map
339 std::vector<lanelet::BasicPoint2d> destination_points_in_map_;
340
341 // The current map projection for lat/lon to map frame conversion
342 boost::optional<std::string> map_proj_;
343
344 geometry_msgs::msg::TransformStamped tf_;
345 tf2::Stamped<tf2::Transform> frontbumper_transform_;
346
347 // TF listenser
348 tf2_ros::Buffer& tf2_buffer_;
349 std::unique_ptr<tf2_ros::TransformListener> tf2_listener_;
350
351 // Logger interface
352 rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr logger_;
353
354 rclcpp::Clock::SharedPtr clock_;
355 };
356
357} // namespace route
tf2::Stamped< tf2::Transform > frontbumper_transform_
bool setActiveRouteCb(const std::shared_ptr< rmw_request_id_t >, const std::shared_ptr< carma_planning_msgs::srv::SetActiveRoute::Request > req, std::shared_ptr< carma_planning_msgs::srv::SetActiveRoute::Response > resp)
Set_active_route service callback. User can select a route to start following by calling this service...
void setCrosstrackErrorDistance(double cte_dist)
set the maximum crosstrack error distance
bool getAvailableRouteCb(const std::shared_ptr< rmw_request_id_t >, const std::shared_ptr< carma_planning_msgs::srv::GetAvailableRoutes::Request >, std::shared_ptr< carma_planning_msgs::srv::GetAvailableRoutes::Response > resp)
Get_available_route service callback. Calls to this service will respond with a list of route names f...
void addLanelet(lanelet::ConstLanelet llt)
void setLoggerInterface(rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr logger)
Dependency injection for logger interface.
void setWorldModelPtr(carma_wm::WorldModelConstPtr wm)
Dependency injection for world model pointer.
std::vector< lanelet::BasicPoint2d > destination_points_in_map_
void setRouteFilePath(const std::string &path)
Set method for configurable parameter.
void initializeBumperTransformLookup()
Initialize transform lookup from front bumper to map.
void setClock(rclcpp::Clock::SharedPtr clock)
Dependency injection for clock object.
void bumperPoseCb()
Callback for the front bumper pose transform.
void setReroutingChecker(const std::function< bool()> inputFunction)
setReroutingChecker function to set the rerouting flag
lanelet::Optional< lanelet::routing::Route > rerouteAfterRouteInvalidation(const std::vector< lanelet::BasicPoint2d > &destination_points_in_map)
After route is invalidated, this function returns a new route based on the destinations points.
void setCrosstrackErrorCountMax(int cte_max)
set the crosstrack error counter maximum limit
void setDowntrackDestinationRange(double dt_dest_range)
Set method for configurable parameter.
lanelet::ConstLanelet getClosestLaneletFromRouteLanelets(lanelet::BasicPoint2d position) const
"Get the closest lanelet on the route relative to the vehicle's current position. If the input list d...
rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr logger_
boost::optional< std::string > map_proj_
visualization_msgs::msg::Marker composeRouteMarkerMsg(const lanelet::Optional< lanelet::routing::Route > &route)
composeRouteMarkerMsg is a function to generate route rviz markers
carma_ros2_utils::PubPtr< carma_planning_msgs::msg::Route > route_pub_
visualization_msgs::msg::Marker route_marker_msg_
bool spinCallback()
Spin callback which will be called frequently based on spin rate.
std::vector< lanelet::ConstPoint3d > points_
geometry_msgs::msg::TransformStamped tf_
void twistCb(geometry_msgs::msg::TwistStamped::UniquePtr msg)
Callback for the twist subscriber, which will store latest twist locally.
RouteGeneratorWorker(tf2_ros::Buffer &tf2_buffer)
Constructor for RouteGeneratorWorker class taking in dependencies via dependency injection.
carma_ros2_utils::PubPtr< carma_planning_msgs::msg::RouteEvent > route_event_pub_
std::vector< lanelet::BasicPoint3d > loadRouteDestinationsInMapFrame(const std::vector< carma_v2x_msgs::msg::Position3D > &destinations) const
Function to take route destination points from a vector of 3D points and convert them from lat/lon va...
carma_ros2_utils::PubPtr< carma_planning_msgs::msg::RouteState > route_state_pub_
std::function< bool()> reroutingChecker
reroutingChecker function to set the rerouting flag locally
carma_wm::WorldModelConstPtr world_model_
carma_planning_msgs::msg::RouteEvent route_event_msg_
lanelet::Optional< lanelet::routing::Route > routing(const lanelet::BasicPoint2d start, const std::vector< lanelet::BasicPoint2d > &via, const lanelet::BasicPoint2d end, const lanelet::LaneletMapConstPtr map_pointer, const carma_wm::LaneletRoutingGraphConstPtr graph_pointer) const
Generate Lanelet2 route based on input destinations.
carma_planning_msgs::msg::Route route_msg_
std::vector< carma_v2x_msgs::msg::Position3D > loadRouteDestinationGpsPointsFromRouteId(const std::string &route_id) const
Function to load route destination points from a route file and store them in a vector of 3D points.
carma_planning_msgs::msg::Route composeRouteMsg(const lanelet::Optional< lanelet::routing::Route > &route) const
Helper function to generate a CARMA route message based on planned lanelet route.
void setPublishers(const carma_ros2_utils::PubPtr< carma_planning_msgs::msg::RouteEvent > &route_event_pub, const carma_ros2_utils::PubPtr< carma_planning_msgs::msg::RouteState > &route_state_pub, const carma_ros2_utils::PubPtr< carma_planning_msgs::msg::Route > &route_pub, const carma_ros2_utils::PubPtr< visualization_msgs::msg::Marker > &route_marker_pub)
Method to pass publishers into worker class.
carma_planning_msgs::msg::RouteState route_state_msg_
std::unique_ptr< tf2_ros::TransformListener > tf2_listener_
bool crosstrackErrorCheck(const std::shared_ptr< geometry_msgs::msg::PoseStamped > &msg, lanelet::ConstLanelet current_llt)
crosstrackErrorCheck is a function that determines when the vehicle has left the route and reports wh...
void georeferenceCb(std_msgs::msg::String::UniquePtr msg)
Callback for the georeference subscriber used to set the map projection.
carma_ros2_utils::PubPtr< visualization_msgs::msg::Marker > route_marker_pub_
bool abortActiveRouteCb(const std::shared_ptr< rmw_request_id_t >, const std::shared_ptr< carma_planning_msgs::srv::AbortActiveRoute::Request >, std::shared_ptr< carma_planning_msgs::srv::AbortActiveRoute::Response > resp)
Abort_active_route service callback. User can call this service to abort a current following route an...
bool checkForDuplicateLaneletsInShortestPath(const lanelet::routing::Route &route) const
Helper function to check whether a route's shortest path contains any duplicate Lanelet IDs....
boost::optional< geometry_msgs::msg::PoseStamped > vehicle_pose_
std::queue< uint8_t > route_event_queue
void publishRouteEvent(uint8_t event_type)
std::shared_ptr< const WorldModel > WorldModelConstPtr
Definition: WorldModel.hpp:454
std::shared_ptr< const lanelet::routing::RoutingGraph > LaneletRoutingGraphConstPtr
Definition: WorldModel.hpp:53