Skip to content

Commit 240e52c

Browse files
committed
feat(behavior_path_planner): replace polling subscribers
Signed-off-by: Mamoru Sobue <mamoru.sobue@tier4.jp>
1 parent f87cf32 commit 240e52c

File tree

2 files changed

+131
-152
lines changed

2 files changed

+131
-152
lines changed

planning/autoware_behavior_path_planner/include/autoware_behavior_path_planner/behavior_path_planner_node.hpp

+35-21
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "autoware_behavior_path_planner_common/interface/steering_factor_interface.hpp"
2222
#include "tier4_autoware_utils/ros/logger_level_configure.hpp"
2323

24+
#include <tier4_autoware_utils/ros/polling_subscriber.hpp>
2425
#include <tier4_autoware_utils/ros/published_time_publisher.hpp>
2526

2627
#include <autoware_adapi_v1_msgs/msg/operation_mode_state.hpp>
@@ -86,49 +87,62 @@ class BehaviorPathPlannerNode : public rclcpp::Node
8687
std::vector<std::string> getRunningModules();
8788

8889
private:
89-
rclcpp::Subscription<LaneletRoute>::SharedPtr route_subscriber_;
90-
rclcpp::Subscription<LaneletMapBin>::SharedPtr vector_map_subscriber_;
91-
rclcpp::Subscription<Odometry>::SharedPtr velocity_subscriber_;
92-
rclcpp::Subscription<AccelWithCovarianceStamped>::SharedPtr acceleration_subscriber_;
93-
rclcpp::Subscription<Scenario>::SharedPtr scenario_subscriber_;
94-
rclcpp::Subscription<PredictedObjects>::SharedPtr perception_subscriber_;
95-
rclcpp::Subscription<OccupancyGrid>::SharedPtr occupancy_grid_subscriber_;
96-
rclcpp::Subscription<OccupancyGrid>::SharedPtr costmap_subscriber_;
97-
rclcpp::Subscription<TrafficLightGroupArray>::SharedPtr traffic_signals_subscriber_;
98-
rclcpp::Subscription<LateralOffset>::SharedPtr lateral_offset_subscriber_;
99-
rclcpp::Subscription<OperationModeState>::SharedPtr operation_mode_subscriber_;
90+
// subscriber
91+
tier4_autoware_utils::InterProcessPollingSubscriber<LaneletRoute> route_subscriber_{
92+
this, "~/input/route", rclcpp::QoS{1}.transient_local()};
93+
tier4_autoware_utils::InterProcessPollingSubscriber<LaneletMapBin> vector_map_subscriber_{
94+
this, "~/input/vector_map", rclcpp::QoS{1}.transient_local()};
95+
tier4_autoware_utils::InterProcessPollingSubscriber<Odometry> velocity_subscriber_{
96+
this, "~/input/odometry"};
97+
tier4_autoware_utils::InterProcessPollingSubscriber<AccelWithCovarianceStamped>
98+
acceleration_subscriber_{this, "~/input/accel"};
99+
tier4_autoware_utils::InterProcessPollingSubscriber<Scenario> scenario_subscriber_{
100+
this, "~/input/scenario"};
101+
tier4_autoware_utils::InterProcessPollingSubscriber<PredictedObjects> perception_subscriber_{
102+
this, "~/input/perception"};
103+
tier4_autoware_utils::InterProcessPollingSubscriber<OccupancyGrid> occupancy_grid_subscriber_{
104+
this, "~/input/occupancy_grid_map"};
105+
tier4_autoware_utils::InterProcessPollingSubscriber<OccupancyGrid> costmap_subscriber_{
106+
this, "~/input/costmap"};
107+
tier4_autoware_utils::InterProcessPollingSubscriber<TrafficLightGroupArray>
108+
traffic_signals_subscriber_{this, "~/input/traffic_signals"};
109+
tier4_autoware_utils::InterProcessPollingSubscriber<LateralOffset> lateral_offset_subscriber_{
110+
this, "~/input/lateral_offset"};
111+
tier4_autoware_utils::InterProcessPollingSubscriber<OperationModeState>
112+
operation_mode_subscriber_{
113+
this, "/system/operation_mode/state", rclcpp::QoS{1}.transient_local()};
114+
tier4_autoware_utils::InterProcessPollingSubscriber<tier4_planning_msgs::msg::VelocityLimit>
115+
external_limit_max_velocity_subscriber_{this, "/planning/scenario_planning/max_velocity"};
116+
117+
// publisher
100118
rclcpp::Publisher<PathWithLaneId>::SharedPtr path_publisher_;
101119
rclcpp::Publisher<TurnIndicatorsCommand>::SharedPtr turn_signal_publisher_;
102120
rclcpp::Publisher<HazardLightsCommand>::SharedPtr hazard_signal_publisher_;
103121
rclcpp::Publisher<MarkerArray>::SharedPtr bound_publisher_;
104122
rclcpp::Publisher<PoseWithUuidStamped>::SharedPtr modified_goal_publisher_;
105123
rclcpp::Publisher<StopReasonArray>::SharedPtr stop_reason_publisher_;
106124
rclcpp::Publisher<RerouteAvailability>::SharedPtr reroute_availability_publisher_;
107-
rclcpp::Subscription<tier4_planning_msgs::msg::VelocityLimit>::SharedPtr
108-
external_limit_max_velocity_subscriber_;
109125
rclcpp::TimerBase::SharedPtr timer_;
110126

111127
std::map<std::string, rclcpp::Publisher<Path>::SharedPtr> path_candidate_publishers_;
112128
std::map<std::string, rclcpp::Publisher<Path>::SharedPtr> path_reference_publishers_;
113129

114130
std::shared_ptr<PlannerData> planner_data_;
115-
116-
std::shared_ptr<PlannerManager> planner_manager_;
117-
118-
std::unique_ptr<SteeringFactorInterface> steering_factor_interface_ptr_;
119-
Scenario::SharedPtr current_scenario_{nullptr};
120-
131+
Scenario::ConstSharedPtr current_scenario_{nullptr};
121132
LaneletMapBin::ConstSharedPtr map_ptr_{nullptr};
122133
LaneletRoute::ConstSharedPtr route_ptr_{nullptr};
123134
bool has_received_map_{false};
124135
bool has_received_route_{false};
125136

137+
std::shared_ptr<PlannerManager> planner_manager_;
138+
139+
std::unique_ptr<SteeringFactorInterface> steering_factor_interface_ptr_;
140+
126141
std::mutex mutex_pd_; // mutex for planner_data_
127142
std::mutex mutex_manager_; // mutex for bt_manager_ or planner_manager_
128-
std::mutex mutex_map_; // mutex for has_received_map_ and map_ptr_
129-
std::mutex mutex_route_; // mutex for has_received_route_ and route_ptr_
130143

131144
// setup
145+
void takeData();
132146
bool isDataReady();
133147

134148
// parameters

planning/autoware_behavior_path_planner/src/behavior_path_planner_node.cpp

+96-131
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,6 @@
2828
#include <string>
2929
#include <vector>
3030

31-
namespace
32-
{
33-
rclcpp::SubscriptionOptions createSubscriptionOptions(rclcpp::Node * node_ptr)
34-
{
35-
rclcpp::CallbackGroup::SharedPtr callback_group =
36-
node_ptr->create_callback_group(rclcpp::CallbackGroupType::MutuallyExclusive);
37-
38-
auto sub_opt = rclcpp::SubscriptionOptions();
39-
sub_opt.callback_group = callback_group;
40-
41-
return sub_opt;
42-
}
43-
} // namespace
44-
4531
namespace autoware::behavior_path_planner
4632
{
4733
using autoware::vehicle_info_utils::VehicleInfoUtils;
@@ -78,54 +64,6 @@ BehaviorPathPlannerNode::BehaviorPathPlannerNode(const rclcpp::NodeOptions & nod
7864

7965
bound_publisher_ = create_publisher<MarkerArray>("~/debug/bound", 1);
8066

81-
const auto qos_transient_local = rclcpp::QoS{1}.transient_local();
82-
// subscriber
83-
velocity_subscriber_ = create_subscription<Odometry>(
84-
"~/input/odometry", 1, std::bind(&BehaviorPathPlannerNode::onOdometry, this, _1),
85-
createSubscriptionOptions(this));
86-
acceleration_subscriber_ = create_subscription<AccelWithCovarianceStamped>(
87-
"~/input/accel", 1, std::bind(&BehaviorPathPlannerNode::onAcceleration, this, _1),
88-
createSubscriptionOptions(this));
89-
perception_subscriber_ = create_subscription<PredictedObjects>(
90-
"~/input/perception", 1, std::bind(&BehaviorPathPlannerNode::onPerception, this, _1),
91-
createSubscriptionOptions(this));
92-
occupancy_grid_subscriber_ = create_subscription<OccupancyGrid>(
93-
"~/input/occupancy_grid_map", 1, std::bind(&BehaviorPathPlannerNode::onOccupancyGrid, this, _1),
94-
createSubscriptionOptions(this));
95-
costmap_subscriber_ = create_subscription<OccupancyGrid>(
96-
"~/input/costmap", 1, std::bind(&BehaviorPathPlannerNode::onCostMap, this, _1),
97-
createSubscriptionOptions(this));
98-
traffic_signals_subscriber_ =
99-
this->create_subscription<autoware_perception_msgs::msg::TrafficLightGroupArray>(
100-
"~/input/traffic_signals", 1, std::bind(&BehaviorPathPlannerNode::onTrafficSignals, this, _1),
101-
createSubscriptionOptions(this));
102-
lateral_offset_subscriber_ = this->create_subscription<LateralOffset>(
103-
"~/input/lateral_offset", 1, std::bind(&BehaviorPathPlannerNode::onLateralOffset, this, _1),
104-
createSubscriptionOptions(this));
105-
operation_mode_subscriber_ = create_subscription<OperationModeState>(
106-
"/system/operation_mode/state", qos_transient_local,
107-
std::bind(&BehaviorPathPlannerNode::onOperationMode, this, _1),
108-
createSubscriptionOptions(this));
109-
scenario_subscriber_ = create_subscription<Scenario>(
110-
"~/input/scenario", 1,
111-
[this](const Scenario::ConstSharedPtr msg) {
112-
current_scenario_ = std::make_shared<Scenario>(*msg);
113-
},
114-
createSubscriptionOptions(this));
115-
external_limit_max_velocity_subscriber_ =
116-
create_subscription<tier4_planning_msgs::msg::VelocityLimit>(
117-
"/planning/scenario_planning/max_velocity", 1,
118-
std::bind(&BehaviorPathPlannerNode::on_external_velocity_limiter, this, _1),
119-
createSubscriptionOptions(this));
120-
121-
// route_handler
122-
vector_map_subscriber_ = create_subscription<LaneletMapBin>(
123-
"~/input/vector_map", qos_transient_local, std::bind(&BehaviorPathPlannerNode::onMap, this, _1),
124-
createSubscriptionOptions(this));
125-
route_subscriber_ = create_subscription<LaneletRoute>(
126-
"~/input/route", qos_transient_local, std::bind(&BehaviorPathPlannerNode::onRoute, this, _1),
127-
createSubscriptionOptions(this));
128-
12967
{
13068
const std::string path_candidate_name_space = "/planning/path_candidate/";
13169
const std::string path_reference_name_space = "/planning/path_reference/";
@@ -274,6 +212,100 @@ BehaviorPathPlannerParameters BehaviorPathPlannerNode::getCommonParam()
274212
return p;
275213
}
276214

215+
void BehaviorPathPlannerNode::takeData()
216+
{
217+
// route
218+
{
219+
const auto msg = route_subscriber_.takeNewData();
220+
if (msg) {
221+
if (msg->segments.empty()) {
222+
RCLCPP_ERROR(get_logger(), "input route is empty. ignored");
223+
} else {
224+
route_ptr_ = msg;
225+
has_received_route_ = true;
226+
}
227+
}
228+
}
229+
// map
230+
{
231+
const auto msg = vector_map_subscriber_.takeNewData();
232+
if (msg) {
233+
map_ptr_ = msg;
234+
has_received_map_ = true;
235+
}
236+
}
237+
// velocity
238+
{
239+
const auto msg = velocity_subscriber_.takeData();
240+
if (msg) {
241+
planner_data_->self_odometry = msg;
242+
}
243+
}
244+
// acceleration
245+
{
246+
const auto msg = acceleration_subscriber_.takeData();
247+
if (msg) {
248+
planner_data_->self_acceleration = msg;
249+
}
250+
}
251+
// scenario
252+
{
253+
const auto msg = scenario_subscriber_.takeData();
254+
if (msg) {
255+
current_scenario_ = msg;
256+
}
257+
}
258+
// perception
259+
{
260+
const auto msg = perception_subscriber_.takeData();
261+
if (msg) {
262+
planner_data_->dynamic_object = msg;
263+
}
264+
}
265+
// occupancy_grid
266+
{
267+
const auto msg = occupancy_grid_subscriber_.takeData();
268+
if (msg) {
269+
planner_data_->occupancy_grid = msg;
270+
}
271+
}
272+
// costmap
273+
{
274+
const auto msg = costmap_subscriber_.takeData();
275+
if (msg) {
276+
planner_data_->costmap = msg;
277+
}
278+
}
279+
// traffic_signal
280+
{
281+
const auto msg = traffic_signals_subscriber_.takeData();
282+
if (msg) {
283+
onTrafficSignals(msg);
284+
}
285+
}
286+
// lateral_offset
287+
{
288+
const auto msg = lateral_offset_subscriber_.takeData();
289+
if (msg) {
290+
onLateralOffset(msg);
291+
}
292+
}
293+
// operation_mode
294+
{
295+
const auto msg = operation_mode_subscriber_.takeData();
296+
if (msg) {
297+
planner_data_->operation_mode = msg;
298+
}
299+
}
300+
// external_velocity_limiter
301+
{
302+
const auto msg = external_limit_max_velocity_subscriber_.takeData();
303+
if (msg) {
304+
planner_data_->external_limit_max_velocity = msg;
305+
}
306+
}
307+
}
308+
277309
// wait until mandatory data is ready
278310
bool BehaviorPathPlannerNode::isDataReady()
279311
{
@@ -287,21 +319,17 @@ bool BehaviorPathPlannerNode::isDataReady()
287319
}
288320

289321
{
290-
std::lock_guard<std::mutex> lk_route(mutex_route_);
291322
if (!route_ptr_) {
292323
return missing("route");
293324
}
294325
}
295326

296327
{
297-
std::lock_guard<std::mutex> lk_map(mutex_map_);
298328
if (!map_ptr_) {
299329
return missing("map");
300330
}
301331
}
302332

303-
const std::lock_guard<std::mutex> lock(mutex_pd_); // for planner_data_
304-
305333
if (!planner_data_->dynamic_object) {
306334
return missing("dynamic_object");
307335
}
@@ -327,6 +355,8 @@ bool BehaviorPathPlannerNode::isDataReady()
327355

328356
void BehaviorPathPlannerNode::run()
329357
{
358+
takeData();
359+
330360
if (!isDataReady()) {
331361
return;
332362
}
@@ -341,7 +371,6 @@ void BehaviorPathPlannerNode::run()
341371
// check for map update
342372
LaneletMapBin::ConstSharedPtr map_ptr{nullptr};
343373
{
344-
std::lock_guard<std::mutex> lk_map(mutex_map_); // for has_received_map_ and map_ptr_
345374
if (has_received_map_) {
346375
// Note: duplicating the shared_ptr prevents the data from being deleted by another thread!
347376
map_ptr = map_ptr_;
@@ -352,7 +381,6 @@ void BehaviorPathPlannerNode::run()
352381
// check for route update
353382
LaneletRoute::ConstSharedPtr route_ptr{nullptr};
354383
{
355-
std::lock_guard<std::mutex> lk_route(mutex_route_); // for has_received_route_ and route_ptr_
356384
if (has_received_route_) {
357385
// Note: duplicating the shared_ptr prevents the data from being deleted by another thread!
358386
route_ptr = route_ptr_;
@@ -765,35 +793,8 @@ bool BehaviorPathPlannerNode::keepInputPoints(
765793
return false;
766794
}
767795

768-
void BehaviorPathPlannerNode::onOdometry(const Odometry::ConstSharedPtr msg)
769-
{
770-
const std::lock_guard<std::mutex> lock(mutex_pd_);
771-
planner_data_->self_odometry = msg;
772-
}
773-
void BehaviorPathPlannerNode::onAcceleration(const AccelWithCovarianceStamped::ConstSharedPtr msg)
774-
{
775-
const std::lock_guard<std::mutex> lock(mutex_pd_);
776-
planner_data_->self_acceleration = msg;
777-
}
778-
void BehaviorPathPlannerNode::onPerception(const PredictedObjects::ConstSharedPtr msg)
779-
{
780-
const std::lock_guard<std::mutex> lock(mutex_pd_);
781-
planner_data_->dynamic_object = msg;
782-
}
783-
void BehaviorPathPlannerNode::onOccupancyGrid(const OccupancyGrid::ConstSharedPtr msg)
784-
{
785-
const std::lock_guard<std::mutex> lock(mutex_pd_);
786-
planner_data_->occupancy_grid = msg;
787-
}
788-
void BehaviorPathPlannerNode::onCostMap(const OccupancyGrid::ConstSharedPtr msg)
789-
{
790-
const std::lock_guard<std::mutex> lock(mutex_pd_);
791-
planner_data_->costmap = msg;
792-
}
793796
void BehaviorPathPlannerNode::onTrafficSignals(const TrafficLightGroupArray::ConstSharedPtr msg)
794797
{
795-
std::lock_guard<std::mutex> lock(mutex_pd_);
796-
797798
planner_data_->traffic_light_id_map.clear();
798799
for (const auto & signal : msg->traffic_light_groups) {
799800
TrafficSignalStamped traffic_signal;
@@ -802,45 +803,9 @@ void BehaviorPathPlannerNode::onTrafficSignals(const TrafficLightGroupArray::Con
802803
planner_data_->traffic_light_id_map[signal.traffic_light_group_id] = traffic_signal;
803804
}
804805
}
805-
void BehaviorPathPlannerNode::onMap(const LaneletMapBin::ConstSharedPtr msg)
806-
{
807-
const std::lock_guard<std::mutex> lock(mutex_map_);
808-
map_ptr_ = msg;
809-
has_received_map_ = true;
810-
}
811-
void BehaviorPathPlannerNode::onRoute(const LaneletRoute::ConstSharedPtr msg)
812-
{
813-
if (msg->segments.empty()) {
814-
RCLCPP_ERROR(get_logger(), "input route is empty. ignored");
815-
return;
816-
}
817-
818-
const std::lock_guard<std::mutex> lock(mutex_route_);
819-
route_ptr_ = msg;
820-
has_received_route_ = true;
821-
}
822-
void BehaviorPathPlannerNode::onOperationMode(const OperationModeState::ConstSharedPtr msg)
823-
{
824-
const std::lock_guard<std::mutex> lock(mutex_pd_);
825-
planner_data_->operation_mode = msg;
826-
}
827806

828-
void BehaviorPathPlannerNode::on_external_velocity_limiter(
829-
const tier4_planning_msgs::msg::VelocityLimit::ConstSharedPtr msg)
830-
{
831-
// Note: Using this parameter during path planning might cause unexpected deceleration or jerk.
832-
// Therefore, do not use it for anything other than safety checks.
833-
if (!msg) {
834-
return;
835-
}
836-
837-
const std::lock_guard<std::mutex> lock(mutex_pd_);
838-
planner_data_->external_limit_max_velocity = msg;
839-
}
840807
void BehaviorPathPlannerNode::onLateralOffset(const LateralOffset::ConstSharedPtr msg)
841808
{
842-
std::lock_guard<std::mutex> lock(mutex_pd_);
843-
844809
if (!planner_data_->lateral_offset) {
845810
planner_data_->lateral_offset = msg;
846811
return;

0 commit comments

Comments
 (0)