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_node.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022 LEIDOS.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16#include "route/route_node.hpp"
17
18namespace route
19{
20 namespace std_ph = std::placeholders;
21
22 Route::Route(const rclcpp::NodeOptions &options)
23 : carma_ros2_utils::CarmaLifecycleNode(options),
24 tf2_buffer_(this->get_clock()),
25 wml_(this->get_node_base_interface(), this->get_node_logging_interface(),
26 this->get_node_topics_interface(), this->get_node_parameters_interface()),
27 rg_worker_(tf2_buffer_)
28 {
29 // Create initial config
30 config_ = Config();
31
32 // Declare parameters
33 config_.max_crosstrack_error = declare_parameter<double>("max_crosstrack_error", config_.max_crosstrack_error);
34 config_.destination_downtrack_range = declare_parameter<double>("destination_downtrack_range", config_.destination_downtrack_range);
35 config_.route_spin_rate = declare_parameter<double>("route_spin_rate", config_.route_spin_rate);
36 config_.cte_max_count = declare_parameter<int>("cte_max_count", config_.cte_max_count);
37 config_.route_file_path = declare_parameter<std::string>("route_file_path", config_.route_file_path);
38 }
39
40 rcl_interfaces::msg::SetParametersResult Route::parameter_update_callback(const std::vector<rclcpp::Parameter> &parameters)
41 {
42 auto error = update_params<double>({
43 {"max_crosstrack_error", config_.max_crosstrack_error},
44 {"destination_downtrack_range", config_.destination_downtrack_range},
45 {"route_spin_rate", config_.route_spin_rate}}, parameters);
46 auto error_2 = update_params<int>({{"cte_max_count", config_.cte_max_count}}, parameters);
47 auto error_3 = update_params<std::string>({{"route_file_path", config_.route_file_path}}, parameters);
48
49 rcl_interfaces::msg::SetParametersResult result;
50
51 result.successful = !error && !error_2 && !error_3;
52
53 return result;
54 }
55
56 carma_ros2_utils::CallbackReturn Route::handle_on_configure(const rclcpp_lifecycle::State &)
57 {
58 RCLCPP_INFO_STREAM(get_logger(), "Route trying to configure");
59
60 // Reset config
61 config_ = Config();
62
63 // Load parameters
64 get_parameter<double>("max_crosstrack_error", config_.max_crosstrack_error);
65 get_parameter<double>("destination_downtrack_range", config_.destination_downtrack_range);
66 get_parameter<double>("route_spin_rate", config_.route_spin_rate);
67 get_parameter<int>("cte_max_count", config_.cte_max_count);
68 get_parameter<std::string>("route_file_path", config_.route_file_path);
69
70 RCLCPP_INFO_STREAM(get_logger(), "Loaded params: " << config_);
71
72 // Register runtime parameter update callback
73 add_on_set_parameters_callback(std::bind(&Route::parameter_update_callback, this, std_ph::_1));
74
75 // Setup subscribers
76 twist_sub_ = create_subscription<geometry_msgs::msg::TwistStamped>("current_velocity", 1,
77 std::bind(&RouteGeneratorWorker::twistCb, &rg_worker_, std_ph::_1));
78 geo_sub_ = create_subscription<std_msgs::msg::String>("georeference", 1,
79 std::bind(&RouteGeneratorWorker::georeferenceCb, &rg_worker_, std_ph::_1));
80
81 // Setup publishers
82 route_pub_ = create_publisher<carma_planning_msgs::msg::Route>("route", 1);
83 route_state_pub_ = create_publisher<carma_planning_msgs::msg::RouteState>("route_state", 1);
84
85 // NOTE: Currently, intra-process comms must be disabled for the following two publishers that are transient_local: https://github.com/ros2/rclcpp/issues/1753
86 rclcpp::PublisherOptions intra_proc_disabled;
87 intra_proc_disabled.use_intra_process_comm = rclcpp::IntraProcessSetting::Disable; // Disable intra-process comms for this PublisherOptions object
88
89 // Create a publisher that will send all previously published messages to late-joining subscribers ONLY If the subscriber is transient_local too
90 auto pub_qos_transient_local = rclcpp::QoS(rclcpp::KeepAll()); // A publisher with this QoS will store all messages that it has sent on the topic
91 pub_qos_transient_local.transient_local(); // A publisher with this QoS will re-send all (when KeepAll is used) messages to all late-joining subscribers
92 // NOTE: The subscriber's QoS must be set to transient_local() as well for earlier messages to be resent to the later-joiner.
93
94 route_event_pub_ = create_publisher<carma_planning_msgs::msg::RouteEvent>("route_event", pub_qos_transient_local, intra_proc_disabled);
95 route_marker_pub_ = create_publisher<visualization_msgs::msg::Marker>("route_marker", 1);
96
97 // Setup service servers
98 get_available_route_srv_ = create_service<carma_planning_msgs::srv::GetAvailableRoutes>("get_available_routes",
99 std::bind(&RouteGeneratorWorker::getAvailableRouteCb, &rg_worker_, std_ph::_1, std_ph::_2, std_ph::_3));
100 set_active_route_srv_ = create_service<carma_planning_msgs::srv::SetActiveRoute>("set_active_route",
101 std::bind(&RouteGeneratorWorker::setActiveRouteCb, &rg_worker_, std_ph::_1, std_ph::_2, std_ph::_3));
102 abort_active_route_srv_ = create_service<carma_planning_msgs::srv::AbortActiveRoute>("abort_active_route",
103 std::bind(&RouteGeneratorWorker::abortActiveRouteCb, &rg_worker_, std_ph::_1, std_ph::_2, std_ph::_3));
104
105 // Set world model pointer from wm listener
108
109 // Configure route generator worker parameters
110 rg_worker_.setClock(get_clock());
111 rg_worker_.setLoggerInterface(get_node_logging_interface());
119
120 // Return success if everthing initialized successfully
121 return CallbackReturn::SUCCESS;
122 }
123
124 carma_ros2_utils::CallbackReturn Route::handle_on_activate(const rclcpp_lifecycle::State &prev_state)
125 {
127
128 // Timer for route generator worker's spin callback
129 auto rg_worker_spin_period_ms = int ((1 / config_.route_spin_rate) * 1000); // Conversion from frequency (Hz) to milliseconds time period
130 spin_timer_ = create_timer(get_clock(),
131 std::chrono::milliseconds(rg_worker_spin_period_ms),
133
134 return CallbackReturn::SUCCESS;
135 }
136
137} // route
138
139#include "rclcpp_components/register_node_macro.hpp"
140
141// Register the component with class_loader
142RCLCPP_COMPONENTS_REGISTER_NODE(route::Route)
WorldModelConstPtr getWorldModel()
Returns a pointer to an intialized world model instance.
Definition: WMListener.cpp:184
void enableUpdatesWithoutRouteWL()
Use to allow updates to occur even if they invalidate the current route. This is only meant to be use...
Definition: WMListener.cpp:172
bool checkIfReRoutingNeededWL()
Returns true if a map update has been processed which requires rerouting. This method is meant to be ...
Definition: WMListener.cpp:178
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 setLoggerInterface(rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr logger)
Dependency injection for logger interface.
void setWorldModelPtr(carma_wm::WorldModelConstPtr wm)
Dependency injection for world model pointer.
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 setReroutingChecker(const std::function< bool()> inputFunction)
setReroutingChecker function to set the rerouting flag
void setCrosstrackErrorCountMax(int cte_max)
set the crosstrack error counter maximum limit
void setDowntrackDestinationRange(double dt_dest_range)
Set method for configurable parameter.
bool spinCallback()
Spin callback which will be called frequently based on spin rate.
void twistCb(geometry_msgs::msg::TwistStamped::UniquePtr msg)
Callback for the twist subscriber, which will store latest twist locally.
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.
void georeferenceCb(std_msgs::msg::String::UniquePtr msg)
Callback for the georeference subscriber used to set the map projection.
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...
The route package provides the following functionality:
Definition: route_node.hpp:51
carma_wm::WorldModelConstPtr wm_
Definition: route_node.hpp:80
RouteGeneratorWorker rg_worker_
Definition: route_node.hpp:83
Route(const rclcpp::NodeOptions &)
Route constructor.
Definition: route_node.cpp:22
rcl_interfaces::msg::SetParametersResult parameter_update_callback(const std::vector< rclcpp::Parameter > &parameters)
Function callback for dynamic parameter updates.
Definition: route_node.cpp:40
carma_ros2_utils::SubPtr< geometry_msgs::msg::TwistStamped > twist_sub_
Definition: route_node.hpp:55
carma_ros2_utils::CallbackReturn handle_on_configure(const rclcpp_lifecycle::State &)
Definition: route_node.cpp:56
rclcpp::TimerBase::SharedPtr spin_timer_
Definition: route_node.hpp:70
carma_ros2_utils::CallbackReturn handle_on_activate(const rclcpp_lifecycle::State &)
Definition: route_node.cpp:124
carma_ros2_utils::ServicePtr< carma_planning_msgs::srv::GetAvailableRoutes > get_available_route_srv_
Definition: route_node.hpp:65
Config config_
Definition: route_node.hpp:73
carma_ros2_utils::SubPtr< std_msgs::msg::String > geo_sub_
Definition: route_node.hpp:56
carma_ros2_utils::PubPtr< carma_planning_msgs::msg::Route > route_pub_
Definition: route_node.hpp:59
carma_ros2_utils::ServicePtr< carma_planning_msgs::srv::SetActiveRoute > set_active_route_srv_
Definition: route_node.hpp:66
carma_ros2_utils::PubPtr< carma_planning_msgs::msg::RouteState > route_state_pub_
Definition: route_node.hpp:60
carma_ros2_utils::ServicePtr< carma_planning_msgs::srv::AbortActiveRoute > abort_active_route_srv_
Definition: route_node.hpp:67
carma_ros2_utils::PubPtr< visualization_msgs::msg::Marker > route_marker_pub_
Definition: route_node.hpp:62
carma_ros2_utils::PubPtr< carma_planning_msgs::msg::RouteEvent > route_event_pub_
Definition: route_node.hpp:61
carma_wm::WMListener wml_
Definition: route_node.hpp:79
Struct containing the algorithm configuration values for the route node.
std::string route_file_path
double destination_downtrack_range
double route_spin_rate
double max_crosstrack_error