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.
carma_wm::WMListener Class Reference

Class which provies automated subscription and threading support for the world model. More...

#include <WMListener.hpp>

Collaboration diagram for carma_wm::WMListener:
Collaboration graph

Public Member Functions

 WMListener (rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_base, rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr node_logging, rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr node_topics, rclcpp::node_interfaces::NodeParametersInterface::SharedPtr node_params_, bool multi_thread=false)
 Constructor which can be used to specify threading behavior of this class. More...
 
 ~WMListener ()
 Destructor. More...
 
WorldModelConstPtr getWorldModel ()
 Returns a pointer to an intialized world model instance. More...
 
void setMapCallback (std::function< void()> callback)
 Allows user to set a callback to be triggered when a map update is received NOTE: If operating in multi-threaded mode the world model will remain locked until the user function completes. More...
 
void setRouteCallback (std::function< void()> callback)
 Allows user to set a callback to be triggered when a route update is received NOTE: If operating in multi-threaded mode the world model will remain locked until the user function completes. More...
 
std::unique_lock< std::mutex > getLock (bool pre_locked=true)
 Returns a unique_lock which can be used to lock world model updates until the user finishes a desired operation. This function should be used when multiple queries are needed and this object is operating in multi-threaded mode. More...
 
void setConfigSpeedLimit (double config_lim) const
 Allows user to set a callback to be triggered when a route update is received NOTE: If operating in multi-threaded mode the world model will remain locked until the user function completes. More...
 
void enableUpdatesWithoutRouteWL ()
 Use to allow updates to occur even if they invalidate the current route. This is only meant to be used by components which generate the route. More...
 
bool checkIfReRoutingNeededWL ()
 Returns true if a map update has been processed which requires rerouting. This method is meant to be used by routing components. More...
 

Private Member Functions

void mapUpdateCallback (autoware_lanelet2_msgs::msg::MapBin::SharedPtr geofence_msg)
 

Private Attributes

carma_ros2_utils::SubPtr< carma_perception_msgs::msg::RoadwayObstacleList > roadway_objects_sub_
 
carma_ros2_utils::SubPtr< autoware_lanelet2_msgs::msg::MapBin > map_update_sub_
 
rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_base_
 
rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr node_logging_
 
rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr node_topics_
 
rclcpp::node_interfaces::NodeParametersInterface::SharedPtr node_params_
 
std::unique_ptr< WMListenerWorkerworker_
 
carma_ros2_utils::SubPtr< autoware_lanelet2_msgs::msg::MapBin > map_sub_
 
carma_ros2_utils::SubPtr< carma_planning_msgs::msg::Route > route_sub_
 
carma_ros2_utils::SubPtr< carma_v2x_msgs::msg::SPAT > traffic_spat_sub_
 
carma_ros2_utils::SubPtr< rosgraph_msgs::msg::Clock > sim_clock_sub_
 
carma_ros2_utils::SubPtr< rosgraph_msgs::msg::Clock > ros1_clock_sub_
 
const bool multi_threaded_
 
std::mutex mw_mutex_
 

Detailed Description

Class which provies automated subscription and threading support for the world model.

Users should generally create and cache a single instance of this class within a node. They can then retrieve a pointer to an initialized WorldModel object for doing queries. By default this class follows the threading model of the host node, but it can operate in the background if specified in the constructor. When used in a multi-threading case users can ensure threadsafe operation though usage of the getLock function

Definition at line 46 of file WMListener.hpp.

Constructor & Destructor Documentation

◆ WMListener()

carma_wm::WMListener::WMListener ( rclcpp::node_interfaces::NodeBaseInterface::SharedPtr  node_base,
rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr  node_logging,
rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr  node_topics,
rclcpp::node_interfaces::NodeParametersInterface::SharedPtr  node_params_,
bool  multi_thread = false 
)

Constructor which can be used to specify threading behavior of this class.

By default this object follows node threading behavior (ie. waiting for ros::spin()) If the object is operating in multi-threaded mode a ros::AsyncSpinner is used to implement a background thread.

Parameters
multi_threadIf true this object will subscribe using background threads. Defaults to false

Definition at line 25 of file WMListener.cpp.

31 :node_base_(node_base),node_logging_(node_logging),node_topics_(node_topics),worker_(std::unique_ptr<WMListenerWorker>(new WMListenerWorker)), multi_threaded_(multi_thread)
32{
33
34 RCLCPP_DEBUG_STREAM(node_logging_->get_logger(), "WMListener: Creating world model listener");
35
36 //Declare parameter if it doesn't exist
37 rclcpp::Parameter config_speed_limit_param("config_speed_limit");
38 if(!node_params_->get_parameter("config_speed_limit", config_speed_limit_param)){
39 rclcpp::ParameterValue config_speed_limit_param_value;
40 config_speed_limit_param_value = node_params_->declare_parameter("config_speed_limit", rclcpp::ParameterValue (0.0));
41 }
42
43 rclcpp::Parameter participant_param("vehicle_participant_type");
44 if(!node_params_->get_parameter("vehicle_participant_type", participant_param)){
45 rclcpp::ParameterValue participant_param_value;
46 participant_param_value = node_params_->declare_parameter("vehicle_participant_type", rclcpp::ParameterValue(""));
47 }
48
49 //Declare parameter if it doesn't exist
50 rclcpp::Parameter use_sim_time_param("use_sim_time");
51 if(!node_params_->get_parameter("use_sim_time", use_sim_time_param)){
52 rclcpp::ParameterValue use_sim_time_param_value;
53 use_sim_time_param_value = node_params_->declare_parameter("use_sim_time", rclcpp::ParameterValue (false));
54 }
55
56 rclcpp::Parameter is_spat_wall_time_param("is_spat_wall_time");
57 if(!node_params_->get_parameter("is_spat_wall_time", is_spat_wall_time_param)){
58 rclcpp::ParameterValue is_spat_wall_time_param_value;
59 is_spat_wall_time_param_value = node_params_->declare_parameter("is_spat_wall_time", rclcpp::ParameterValue (true));
60 }
61
62 // Get params
63 config_speed_limit_param = node_params_->get_parameter("config_speed_limit");
64 participant_param = node_params_->get_parameter("vehicle_participant_type");
65 use_sim_time_param = node_params_->get_parameter("use_sim_time");
66 is_spat_wall_time_param = node_params_->get_parameter("is_spat_wall_time");
67
68 RCLCPP_INFO_STREAM(node_logging->get_logger(), "Loaded config speed limit: " << config_speed_limit_param.as_double());
69 RCLCPP_INFO_STREAM(node_logging->get_logger(), "Loaded vehicle participant type: " << participant_param.as_string());
70 RCLCPP_INFO_STREAM(node_logging->get_logger(), "Is using simulation time? : " << use_sim_time_param.as_bool());
71 RCLCPP_INFO_STREAM(node_logging->get_logger(), "Is SPaT using wall time? : " << is_spat_wall_time_param.as_bool());
72
73
74 setConfigSpeedLimit(config_speed_limit_param.as_double());
75 worker_->setVehicleParticipationType(participant_param.as_string());
76 worker_->isUsingSimTime(use_sim_time_param.as_bool());
77 worker_->isSpatWallTime(is_spat_wall_time_param.as_bool());
78
79 rclcpp::SubscriptionOptions map_update_options;
80 rclcpp::SubscriptionOptions map_options;
81 rclcpp::SubscriptionOptions route_options;
82 rclcpp::SubscriptionOptions roadway_objects_options;
83 rclcpp::SubscriptionOptions traffic_spat_options;
84 rclcpp::SubscriptionOptions ros1_clock_options;
85 rclcpp::SubscriptionOptions sim_clock_options;
86
88 {
89 RCLCPP_DEBUG_STREAM(node_logging_->get_logger(), "WMListener: Using a multi-threaded subscription");
90
91 map_update_options.callback_group = node_base_->create_callback_group(rclcpp::CallbackGroupType::MutuallyExclusive);
92
93 map_options.callback_group = node_base_->create_callback_group(rclcpp::CallbackGroupType::MutuallyExclusive);
94
95 route_options.callback_group = node_base_->create_callback_group(rclcpp::CallbackGroupType::MutuallyExclusive);
96
97 roadway_objects_options.callback_group = node_base_->create_callback_group(rclcpp::CallbackGroupType::MutuallyExclusive);
98
99 traffic_spat_options.callback_group = node_base_->create_callback_group(rclcpp::CallbackGroupType::MutuallyExclusive);
100
101 ros1_clock_options.callback_group = node_base_->create_callback_group(rclcpp::CallbackGroupType::MutuallyExclusive);
102
103 sim_clock_options.callback_group = node_base_->create_callback_group(rclcpp::CallbackGroupType::MutuallyExclusive);
104 }
105
106 // Setup subscribers
107 route_sub_ = rclcpp::create_subscription<carma_planning_msgs::msg::Route>(node_topics_, "route", 1,
108 [this](const carma_planning_msgs::msg::Route::SharedPtr msg)
109 {
110 this->worker_->routeCallback(msg);
111 }
112 , route_options);
113
114 ros1_clock_sub_ = rclcpp::create_subscription<rosgraph_msgs::msg::Clock>(node_topics_, "/clock", 1,
115 [this](const rosgraph_msgs::msg::Clock::SharedPtr msg)
116 {
117 this->worker_->ros1ClockCallback(msg);
118 }
119 , ros1_clock_options);
120
121 sim_clock_sub_ = rclcpp::create_subscription<rosgraph_msgs::msg::Clock>(node_topics_, "/sim_clock", 1,
122 [this](const rosgraph_msgs::msg::Clock::SharedPtr msg)
123 {
124 this->worker_->simClockCallback(msg);
125 }
126 , sim_clock_options);
127
128 roadway_objects_sub_ = rclcpp::create_subscription<carma_perception_msgs::msg::RoadwayObstacleList>(node_topics_, "roadway_objects", 1,
129 [this](const carma_perception_msgs::msg::RoadwayObstacleList::SharedPtr msg)
130 {
131 this->worker_->roadwayObjectListCallback(msg);
132 }
133 , roadway_objects_options);
134
135 traffic_spat_sub_ = rclcpp::create_subscription<carma_v2x_msgs::msg::SPAT>(node_topics_, "incoming_spat", 1,
136 [this](const carma_v2x_msgs::msg::SPAT::SharedPtr msg)
137 {
138 this->worker_->incomingSpatCallback(msg);
139 }
140 , traffic_spat_options);
141
142 // NOTE: Currently, intra-process comms must be disabled for subscribers that are transient_local: https://github.com/ros2/rclcpp/issues/1753
143 map_update_options.use_intra_process_comm = rclcpp::IntraProcessSetting::Disable; // Disable intra-process comms for the map update subscriber
144 auto map_update_sub_qos = rclcpp::QoS(rclcpp::KeepLast(10)); // Set the queue size for the map update subscriber
145 map_update_sub_qos.transient_local(); // If it is possible that this node is a late-joiner to its topic, it must be set to transient_local to receive earlier messages that were missed.
146 // NOTE: The publisher's QoS must be set to transisent_local() as well for earlier messages to be resent to this later-joiner.
147
148 // Create map update subscriber that will receive earlier messages that were missed ONLY if the publisher is transient_local too
149 map_update_sub_ = rclcpp::create_subscription<autoware_lanelet2_msgs::msg::MapBin>(node_topics_, "map_update", map_update_sub_qos,
150 [this](const autoware_lanelet2_msgs::msg::MapBin::SharedPtr msg)
151 {
152 this->mapUpdateCallback(msg);
153 }
154 , map_update_options);
155
156 map_options.use_intra_process_comm = rclcpp::IntraProcessSetting::Disable; // Disable intra-process comms for the semantic map subscriber
157 auto map_sub_qos = rclcpp::QoS(rclcpp::KeepLast(2)); // Set the queue size for the semantic map subscriber
158 map_sub_qos.transient_local(); // If it is possible that this node is a late-joiner to its topic, it must be set to transient_local to receive earlier messages that were missed.
159 // NOTE: The publisher's QoS must be set to transisent_local() as well for earlier messages to be resent to this later-joiner.
160
161 // Create semantic mab subscriber that will receive earlier messages that were missed ONLY if the publisher is transient_local too
162 map_sub_ = rclcpp::create_subscription<autoware_lanelet2_msgs::msg::MapBin>(node_topics_, "semantic_map", map_sub_qos,
163 [this](const autoware_lanelet2_msgs::msg::MapBin::SharedPtr msg)
164 {
165 this->worker_->mapCallback(msg);
166 }
167 , map_options);
168}
rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr node_logging_
Definition: WMListener.hpp:136
void mapUpdateCallback(autoware_lanelet2_msgs::msg::MapBin::SharedPtr geofence_msg)
Definition: WMListener.cpp:190
void setConfigSpeedLimit(double config_lim) const
Allows user to set a callback to be triggered when a route update is received NOTE: If operating in m...
Definition: WMListener.cpp:222
carma_ros2_utils::SubPtr< autoware_lanelet2_msgs::msg::MapBin > map_update_sub_
Definition: WMListener.hpp:133
carma_ros2_utils::SubPtr< rosgraph_msgs::msg::Clock > sim_clock_sub_
Definition: WMListener.hpp:145
carma_ros2_utils::SubPtr< rosgraph_msgs::msg::Clock > ros1_clock_sub_
Definition: WMListener.hpp:146
rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr node_topics_
Definition: WMListener.hpp:137
carma_ros2_utils::SubPtr< autoware_lanelet2_msgs::msg::MapBin > map_sub_
Definition: WMListener.hpp:142
std::unique_ptr< WMListenerWorker > worker_
Definition: WMListener.hpp:140
rclcpp::node_interfaces::NodeParametersInterface::SharedPtr node_params_
Definition: WMListener.hpp:138
carma_ros2_utils::SubPtr< carma_perception_msgs::msg::RoadwayObstacleList > roadway_objects_sub_
Definition: WMListener.hpp:132
rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_base_
Definition: WMListener.hpp:135
carma_ros2_utils::SubPtr< carma_planning_msgs::msg::Route > route_sub_
Definition: WMListener.hpp:143
const bool multi_threaded_
Definition: WMListener.hpp:147
carma_ros2_utils::SubPtr< carma_v2x_msgs::msg::SPAT > traffic_spat_sub_
Definition: WMListener.hpp:144

References map_sub_, map_update_sub_, mapUpdateCallback(), multi_threaded_, node_base_, node_logging_, node_params_, node_topics_, roadway_objects_sub_, ros1_clock_sub_, route_sub_, setConfigSpeedLimit(), sim_clock_sub_, traffic_spat_sub_, and worker_.

Here is the call graph for this function:

◆ ~WMListener()

carma_wm::WMListener::~WMListener ( )

Destructor.

Definition at line 170 of file WMListener.cpp.

170{}

Member Function Documentation

◆ checkIfReRoutingNeededWL()

bool carma_wm::WMListener::checkIfReRoutingNeededWL ( )

Returns true if a map update has been processed which requires rerouting. This method is meant to be used by routing components.

Returns
True is rerouting is needed

Definition at line 178 of file WMListener.cpp.

179{
180 const std::lock_guard<std::mutex> lock(mw_mutex_);
181 return worker_->checkIfReRoutingNeeded();
182}

References mw_mutex_, and worker_.

Referenced by route::Route::handle_on_configure().

Here is the caller graph for this function:

◆ enableUpdatesWithoutRouteWL()

void carma_wm::WMListener::enableUpdatesWithoutRouteWL ( )

Use to allow updates to occur even if they invalidate the current route. This is only meant to be used by components which generate the route.

Definition at line 172 of file WMListener.cpp.

173{
174 const std::lock_guard<std::mutex> lock(mw_mutex_);
175 worker_->enableUpdatesWithoutRoute();
176}

References mw_mutex_, and worker_.

Referenced by route::Route::handle_on_configure().

Here is the caller graph for this function:

◆ getLock()

std::unique_lock< std::mutex > carma_wm::WMListener::getLock ( bool  pre_locked = true)

Returns a unique_lock which can be used to lock world model updates until the user finishes a desired operation. This function should be used when multiple queries are needed and this object is operating in multi-threaded mode.

Parameters
pre_lockedIf true the returned lock will already be locked. If false the lock will be deferred with std::defer_lock Default is true
Returns
std::unique_lock for thread safe access to world model data

Definition at line 211 of file WMListener.cpp.

212{
213 if (pre_locked)
214 {
215 std::unique_lock<std::mutex> lock(mw_mutex_); // Create movable lock
216 return lock;
217 }
218 std::unique_lock<std::mutex> lock(mw_mutex_, std::defer_lock); // Create movable deferred lock
219 return lock;
220}

References mw_mutex_.

◆ getWorldModel()

WorldModelConstPtr carma_wm::WMListener::getWorldModel ( )

Returns a pointer to an intialized world model instance.

Returns
Const pointer to a world model object

Definition at line 184 of file WMListener.cpp.

185{
186 const std::lock_guard<std::mutex> lock(mw_mutex_);
187 return worker_->getWorldModel();
188}

References mw_mutex_, and worker_.

Referenced by plan_delegator::PlanDelegator::handle_on_configure(), and route::Route::handle_on_configure().

Here is the caller graph for this function:

◆ mapUpdateCallback()

void carma_wm::WMListener::mapUpdateCallback ( autoware_lanelet2_msgs::msg::MapBin::SharedPtr  geofence_msg)
private

Definition at line 190 of file WMListener.cpp.

191{
192 const std::lock_guard<std::mutex> lock(mw_mutex_);
193
194 RCLCPP_INFO_STREAM(node_logging_->get_logger(), "New Map Update Received. SeqNum: " << geofence_msg->seq_id);
195
196 worker_->mapUpdateCallback(geofence_msg);
197}

References mw_mutex_, node_logging_, and worker_.

Referenced by WMListener().

Here is the caller graph for this function:

◆ setConfigSpeedLimit()

void carma_wm::WMListener::setConfigSpeedLimit ( double  config_lim) const

Allows user to set a callback to be triggered when a route update is received NOTE: If operating in multi-threaded mode the world model will remain locked until the user function completes.

Parameters
config_limA function that populate the configurable speed limit value after the world model is updated with a new route

Definition at line 222 of file WMListener.cpp.

223{
224 worker_->setConfigSpeedLimit(config_lim);
225}

References worker_.

Referenced by WMListener().

Here is the caller graph for this function:

◆ setMapCallback()

void carma_wm::WMListener::setMapCallback ( std::function< void()>  callback)

Allows user to set a callback to be triggered when a map update is received NOTE: If operating in multi-threaded mode the world model will remain locked until the user function completes.

Parameters
callbackA callback function that will be triggered after the world model receives a new map update

Definition at line 199 of file WMListener.cpp.

200{
201 const std::lock_guard<std::mutex> lock(mw_mutex_);
202 worker_->setMapCallback(callback);
203}
bool callback(cav_srvs::PlanTrajectory::Request &req, cav_srvs::PlanTrajectory::Response &res)
Definition: helper.cpp:21

References callback(), mw_mutex_, and worker_.

Here is the call graph for this function:

◆ setRouteCallback()

void carma_wm::WMListener::setRouteCallback ( std::function< void()>  callback)

Allows user to set a callback to be triggered when a route update is received NOTE: If operating in multi-threaded mode the world model will remain locked until the user function completes.

Parameters
callbackA callback function that will be triggered after the world model is updated with a new route

Definition at line 205 of file WMListener.cpp.

206{
207 const std::lock_guard<std::mutex> lock(mw_mutex_);
208 worker_->setRouteCallback(callback);
209}

References callback(), mw_mutex_, and worker_.

Here is the call graph for this function:

Member Data Documentation

◆ map_sub_

carma_ros2_utils::SubPtr<autoware_lanelet2_msgs::msg::MapBin> carma_wm::WMListener::map_sub_
private

Definition at line 142 of file WMListener.hpp.

Referenced by WMListener().

◆ map_update_sub_

carma_ros2_utils::SubPtr<autoware_lanelet2_msgs::msg::MapBin> carma_wm::WMListener::map_update_sub_
private

Definition at line 133 of file WMListener.hpp.

Referenced by WMListener().

◆ multi_threaded_

const bool carma_wm::WMListener::multi_threaded_
private

Definition at line 147 of file WMListener.hpp.

Referenced by WMListener().

◆ mw_mutex_

std::mutex carma_wm::WMListener::mw_mutex_
private

◆ node_base_

rclcpp::node_interfaces::NodeBaseInterface::SharedPtr carma_wm::WMListener::node_base_
private

Definition at line 135 of file WMListener.hpp.

Referenced by WMListener().

◆ node_logging_

rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr carma_wm::WMListener::node_logging_
private

Definition at line 136 of file WMListener.hpp.

Referenced by WMListener(), and mapUpdateCallback().

◆ node_params_

rclcpp::node_interfaces::NodeParametersInterface::SharedPtr carma_wm::WMListener::node_params_
private

Definition at line 138 of file WMListener.hpp.

Referenced by WMListener().

◆ node_topics_

rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr carma_wm::WMListener::node_topics_
private

Definition at line 137 of file WMListener.hpp.

Referenced by WMListener().

◆ roadway_objects_sub_

carma_ros2_utils::SubPtr<carma_perception_msgs::msg::RoadwayObstacleList> carma_wm::WMListener::roadway_objects_sub_
private

Definition at line 132 of file WMListener.hpp.

Referenced by WMListener().

◆ ros1_clock_sub_

carma_ros2_utils::SubPtr<rosgraph_msgs::msg::Clock> carma_wm::WMListener::ros1_clock_sub_
private

Definition at line 146 of file WMListener.hpp.

Referenced by WMListener().

◆ route_sub_

carma_ros2_utils::SubPtr<carma_planning_msgs::msg::Route> carma_wm::WMListener::route_sub_
private

Definition at line 143 of file WMListener.hpp.

Referenced by WMListener().

◆ sim_clock_sub_

carma_ros2_utils::SubPtr<rosgraph_msgs::msg::Clock> carma_wm::WMListener::sim_clock_sub_
private

Definition at line 145 of file WMListener.hpp.

Referenced by WMListener().

◆ traffic_spat_sub_

carma_ros2_utils::SubPtr<carma_v2x_msgs::msg::SPAT> carma_wm::WMListener::traffic_spat_sub_
private

Definition at line 144 of file WMListener.hpp.

Referenced by WMListener().

◆ worker_


The documentation for this class was generated from the following files: