Skip to content

Commit 6f370c3

Browse files
authored
Merge pull request #1602 from tier4/refactor/behavior-tree-1
Refactor/behavior_tree-1
2 parents e8c2fc5 + cb39567 commit 6f370c3

File tree

23 files changed

+314
-312
lines changed

23 files changed

+314
-312
lines changed

simulation/behavior_tree_plugin/include/behavior_tree_plugin/action_node.hpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ class ActionNode : public BT::ActionNodeBase
4646
public:
4747
ActionNode(const std::string & name, const BT::NodeConfiguration & config);
4848
~ActionNode() override = default;
49-
auto foundConflictingEntity(const lanelet::Ids & following_lanelets) const -> bool;
5049
auto getDistanceToConflictingEntity(
5150
const lanelet::Ids & route_lanelets,
5251
const math::geometry::CatmullRomSplineInterface & spline) const -> std::optional<double>;
@@ -63,8 +62,6 @@ class ActionNode : public BT::ActionNodeBase
6362
auto getRightOfWayEntities(const lanelet::Ids & following_lanelets) const
6463
-> std::vector<traffic_simulator::CanonicalizedEntityStatus>;
6564
auto getYieldStopDistance(const lanelet::Ids & following_lanelets) const -> std::optional<double>;
66-
auto getOtherEntityStatus(lanelet::Id lanelet_id) const
67-
-> std::vector<traffic_simulator::CanonicalizedEntityStatus>;
6865
auto stopEntity() const -> void;
6966
auto getHorizon() const -> double;
7067

@@ -109,24 +106,24 @@ class ActionNode : public BT::ActionNodeBase
109106
-> traffic_simulator::EntityStatus;
110107

111108
protected:
112-
traffic_simulator::behavior::Request request;
113-
std::shared_ptr<hdmap_utils::HdMapUtils> hdmap_utils;
114-
std::shared_ptr<traffic_simulator::TrafficLightsBase> traffic_lights;
115-
std::shared_ptr<traffic_simulator::CanonicalizedEntityStatus> canonicalized_entity_status;
116-
double current_time;
117-
double step_time;
118-
double default_matching_distance_for_lanelet_pose_calculation;
119-
std::optional<double> target_speed;
120-
EntityStatusDict other_entity_status;
121-
lanelet::Ids route_lanelets;
122-
std::shared_ptr<EuclideanDistancesMap> euclidean_distances_map;
123-
traffic_simulator_msgs::msg::BehaviorParameter behavior_parameter;
109+
traffic_simulator::behavior::Request request_;
110+
std::shared_ptr<hdmap_utils::HdMapUtils> hdmap_utils_;
111+
std::shared_ptr<traffic_simulator::TrafficLightsBase> traffic_lights_;
112+
std::shared_ptr<traffic_simulator::CanonicalizedEntityStatus> canonicalized_entity_status_;
113+
double current_time_;
114+
double step_time_;
115+
double default_matching_distance_for_lanelet_pose_calculation_;
116+
std::optional<double> target_speed_;
117+
EntityStatusDict other_entity_status_;
118+
lanelet::Ids route_lanelets_;
119+
traffic_simulator_msgs::msg::BehaviorParameter behavior_parameter_;
124120

125121
auto getDistanceToTargetEntity(
126122
const math::geometry::CatmullRomSplineInterface & spline,
127123
const traffic_simulator::CanonicalizedEntityStatus & status) const -> std::optional<double>;
128124

129125
private:
126+
auto foundConflictingEntity(const lanelet::Ids & following_lanelets) const -> bool;
130127
auto getDistanceToTargetEntityOnCrosswalk(
131128
const math::geometry::CatmullRomSplineInterface & spline,
132129
const traffic_simulator::CanonicalizedEntityStatus & status) const -> std::optional<double>;
@@ -136,8 +133,12 @@ class ActionNode : public BT::ActionNodeBase
136133
-> std::vector<traffic_simulator::CanonicalizedEntityStatus>;
137134
auto getConflictingEntityStatusOnLane(const lanelet::Ids & route_lanelets) const
138135
-> std::vector<traffic_simulator::CanonicalizedEntityStatus>;
136+
auto getOtherEntityStatus(lanelet::Id lanelet_id) const
137+
-> std::vector<traffic_simulator::CanonicalizedEntityStatus>;
139138
auto isOtherEntityAtConsideredAltitude(
140139
const traffic_simulator::CanonicalizedEntityStatus & entity_status) const -> bool;
140+
141+
std::shared_ptr<EuclideanDistancesMap> euclidean_distances_map_;
141142
};
142143
} // namespace entity_behavior
143144

simulation/behavior_tree_plugin/include/behavior_tree_plugin/pedestrian/behavior_tree.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class PedestrianBehaviorTree : public BehaviorPluginBase
4040
public:
4141
void configure(const rclcpp::Logger & logger) override;
4242
auto update(const double current_time, const double step_time) -> void override;
43-
const std::string & getCurrentAction() const override;
43+
auto getCurrentAction() -> const std::string & override;
4444

4545
#define DEFINE_GETTER_SETTER(NAME, TYPE) \
4646
TYPE get##NAME() override { return tree_.rootBlackboard()->get<TYPE>(get##NAME##Key()); } \

simulation/behavior_tree_plugin/include/behavior_tree_plugin/vehicle/behavior_tree.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class VehicleBehaviorTree : public BehaviorPluginBase
4040
public:
4141
auto update(const double current_time, const double step_time) -> void override;
4242
void configure(const rclcpp::Logger & logger) override;
43-
const std::string & getCurrentAction() const override;
43+
auto getCurrentAction() -> const std::string & override;
4444

4545
auto getBehaviorParameter() -> traffic_simulator_msgs::msg::BehaviorParameter override;
4646

simulation/behavior_tree_plugin/src/action_node.cpp

Lines changed: 95 additions & 95 deletions
Large diffs are not rendered by default.

simulation/behavior_tree_plugin/src/pedestrian/behavior_tree.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ auto PedestrianBehaviorTree::createBehaviorTree(const std::string & format_path)
7878
return factory_.createTreeFromText(xml_str.str());
7979
}
8080

81-
const std::string & PedestrianBehaviorTree::getCurrentAction() const
81+
auto PedestrianBehaviorTree::getCurrentAction() -> const std::string &
8282
{
8383
return logging_event_ptr_->getCurrentAction();
8484
}

simulation/behavior_tree_plugin/src/pedestrian/follow_lane_action.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ bool FollowLaneAction::detectObstacleInLane(
5656
auto hasObstacleInPedestrianLanes =
5757
[this](const lanelet::Ids pedestrian_lanes_local, const double max_detect_length) {
5858
using math::geometry::operator-;
59-
const auto & pedestrian_position = canonicalized_entity_status->getMapPose().position;
59+
const auto & pedestrian_position = canonicalized_entity_status_->getMapPose().position;
6060
lanelet::Ids other_entity_lane_ids;
61-
for (const auto & [_, status] : other_entity_status) {
61+
for (const auto & [_, status] : other_entity_status_) {
6262
if (status.getType().type != traffic_simulator_msgs::msg::EntityType::EGO) {
6363
continue;
6464
}
@@ -84,9 +84,9 @@ bool FollowLaneAction::detectObstacleInLane(
8484
auto hasObstacleInFrontOfPedestrian = [this]() {
8585
using math::geometry::operator-;
8686

87-
const auto & pedestrian_position = canonicalized_entity_status->getMapPose().position;
87+
const auto & pedestrian_position = canonicalized_entity_status_->getMapPose().position;
8888

89-
for (const auto & [_, entity_status] : other_entity_status) {
89+
for (const auto & [_, entity_status] : other_entity_status_) {
9090
const auto & other_position = entity_status.getMapPose().position;
9191
const auto relative_position = other_position - pedestrian_position;
9292
const double relative_angle_rad = std::atan2(relative_position.y, relative_position.x);
@@ -108,25 +108,25 @@ BT::NodeStatus FollowLaneAction::tick()
108108
{
109109
getBlackBoardValues();
110110
if (
111-
request != traffic_simulator::behavior::Request::NONE &&
112-
request != traffic_simulator::behavior::Request::FOLLOW_LANE) {
111+
request_ != traffic_simulator::behavior::Request::NONE &&
112+
request_ != traffic_simulator::behavior::Request::FOLLOW_LANE) {
113113
return BT::NodeStatus::FAILURE;
114114
}
115-
if (!canonicalized_entity_status->isInLanelet()) {
115+
if (!canonicalized_entity_status_->isInLanelet()) {
116116
stopEntity();
117117
return BT::NodeStatus::RUNNING;
118118
}
119119
auto following_lanelets =
120-
hdmap_utils->getFollowingLanelets(canonicalized_entity_status->getLaneletId());
121-
if (!target_speed) {
122-
target_speed = hdmap_utils->getSpeedLimit(following_lanelets);
120+
hdmap_utils_->getFollowingLanelets(canonicalized_entity_status_->getLaneletId());
121+
if (!target_speed_) {
122+
target_speed_ = hdmap_utils_->getSpeedLimit(following_lanelets);
123123
}
124124

125125
const auto obstacle_detector_result =
126-
detectObstacleInLane(following_lanelets, behavior_parameter.see_around);
127-
target_speed = obstacle_detector_result ? 0.0 : target_speed;
126+
detectObstacleInLane(following_lanelets, behavior_parameter_.see_around);
127+
target_speed_ = obstacle_detector_result ? 0.0 : target_speed_;
128128

129-
setCanonicalizedEntityStatus(calculateUpdatedEntityStatus(target_speed.value()));
129+
setCanonicalizedEntityStatus(calculateUpdatedEntityStatus(target_speed_.value()));
130130
return BT::NodeStatus::RUNNING;
131131
}
132132
} // namespace pedestrian

simulation/behavior_tree_plugin/src/pedestrian/follow_trajectory_sequence/follow_polyline_trajectory_action.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ auto FollowPolylineTrajectoryAction::calculateWaypoints()
2222
-> const traffic_simulator_msgs::msg::WaypointsArray
2323
{
2424
auto waypoints = traffic_simulator_msgs::msg::WaypointsArray();
25-
waypoints.waypoints.push_back(canonicalized_entity_status->getMapPose().position);
25+
waypoints.waypoints.push_back(canonicalized_entity_status_->getMapPose().position);
2626
for (const auto & vertex : polyline_trajectory->shape.vertices) {
2727
waypoints.waypoints.push_back(vertex.position.position);
2828
}
@@ -50,35 +50,35 @@ auto FollowPolylineTrajectoryAction::providedPorts() -> BT::PortsList
5050
{
5151
auto ports = PedestrianActionNode::providedPorts();
5252
ports.emplace(BT::InputPort<decltype(polyline_trajectory)>("polyline_trajectory"));
53-
ports.emplace(BT::InputPort<decltype(target_speed)>("target_speed"));
53+
ports.emplace(BT::InputPort<decltype(target_speed_)>("target_speed"));
5454
return ports;
5555
}
5656

5757
auto FollowPolylineTrajectoryAction::tick() -> BT::NodeStatus
5858
{
5959
auto getTargetSpeed = [&]() -> double {
60-
if (target_speed.has_value()) {
61-
return target_speed.value();
60+
if (target_speed_.has_value()) {
61+
return target_speed_.value();
6262
} else {
63-
return canonicalized_entity_status->getTwist().linear.x;
63+
return canonicalized_entity_status_->getTwist().linear.x;
6464
}
6565
};
6666

6767
if (getBlackBoardValues();
68-
request != traffic_simulator::behavior::Request::FOLLOW_POLYLINE_TRAJECTORY or
68+
request_ != traffic_simulator::behavior::Request::FOLLOW_POLYLINE_TRAJECTORY or
6969
not getInput<decltype(polyline_trajectory)>("polyline_trajectory", polyline_trajectory) or
70-
not getInput<decltype(target_speed)>("target_speed", target_speed) or
70+
not getInput<decltype(target_speed_)>("target_speed", target_speed_) or
7171
not polyline_trajectory) {
7272
return BT::NodeStatus::FAILURE;
73-
} else if (std::isnan(canonicalized_entity_status->getTime())) {
73+
} else if (std::isnan(canonicalized_entity_status_->getTime())) {
7474
THROW_SIMULATION_ERROR(
7575
"Time in canonicalized_entity_status is NaN - FollowTrajectoryAction does not support such "
7676
"case.");
7777
} else if (
7878
const auto entity_status_updated = traffic_simulator::follow_trajectory::makeUpdatedStatus(
79-
static_cast<traffic_simulator::EntityStatus>(*canonicalized_entity_status),
80-
*polyline_trajectory, behavior_parameter, hdmap_utils, step_time,
81-
default_matching_distance_for_lanelet_pose_calculation, getTargetSpeed())) {
79+
static_cast<traffic_simulator::EntityStatus>(*canonicalized_entity_status_),
80+
*polyline_trajectory, behavior_parameter_, hdmap_utils_, step_time_,
81+
default_matching_distance_for_lanelet_pose_calculation_, getTargetSpeed())) {
8282
setCanonicalizedEntityStatus(entity_status_updated.value());
8383
setOutput("waypoints", calculateWaypoints());
8484
setOutput("obstacle", calculateObstacle(calculateWaypoints()));

simulation/behavior_tree_plugin/src/pedestrian/pedestrian_action_node.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,20 @@ auto PedestrianActionNode::calculateUpdatedEntityStatus(double target_speed) con
4040
-> traffic_simulator::EntityStatus
4141
{
4242
return ActionNode::calculateUpdatedEntityStatus(
43-
target_speed, behavior_parameter.dynamic_constraints);
43+
target_speed, behavior_parameter_.dynamic_constraints);
4444
}
4545

4646
auto PedestrianActionNode::calculateUpdatedEntityStatusInWorldFrame(double target_speed) const
4747
-> traffic_simulator::EntityStatus
4848
{
4949
auto entity_status_updated = ActionNode::calculateUpdatedEntityStatusInWorldFrame(
50-
target_speed, behavior_parameter.dynamic_constraints);
50+
target_speed, behavior_parameter_.dynamic_constraints);
5151
if (
5252
const auto canonicalized_lanelet_pose =
5353
traffic_simulator::pose::pedestrian::transformToCanonicalizedLaneletPose(
54-
entity_status_updated.pose, canonicalized_entity_status->getBoundingBox(),
55-
canonicalized_entity_status->getLaneletIds(), true,
56-
default_matching_distance_for_lanelet_pose_calculation)) {
54+
entity_status_updated.pose, canonicalized_entity_status_->getBoundingBox(),
55+
canonicalized_entity_status_->getLaneletIds(), true,
56+
default_matching_distance_for_lanelet_pose_calculation_)) {
5757
entity_status_updated.lanelet_pose_valid = true;
5858
entity_status_updated.lanelet_pose =
5959
static_cast<traffic_simulator::LaneletPose>(canonicalized_lanelet_pose.value());

simulation/behavior_tree_plugin/src/pedestrian/walk_straight_action.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ void WalkStraightAction::getBlackBoardValues() { PedestrianActionNode::getBlackB
4141
BT::NodeStatus WalkStraightAction::tick()
4242
{
4343
getBlackBoardValues();
44-
if (request != traffic_simulator::behavior::Request::WALK_STRAIGHT) {
44+
if (request_ != traffic_simulator::behavior::Request::WALK_STRAIGHT) {
4545
return BT::NodeStatus::FAILURE;
4646
}
47-
if (!target_speed) {
48-
target_speed = 1.111;
47+
if (!target_speed_) {
48+
target_speed_ = 1.111;
4949
}
50-
setCanonicalizedEntityStatus(calculateUpdatedEntityStatusInWorldFrame(target_speed.value()));
50+
setCanonicalizedEntityStatus(calculateUpdatedEntityStatusInWorldFrame(target_speed_.value()));
5151
return BT::NodeStatus::RUNNING;
5252
}
5353
} // namespace pedestrian

simulation/behavior_tree_plugin/src/vehicle/behavior_tree.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ auto VehicleBehaviorTree::setBehaviorParameter(
137137
getBehaviorParameterKey(), clamp(behavior_parameter));
138138
}
139139

140-
const std::string & VehicleBehaviorTree::getCurrentAction() const
140+
auto VehicleBehaviorTree::getCurrentAction() -> const std::string &
141141
{
142142
return logging_event_ptr_->getCurrentAction();
143143
}

simulation/behavior_tree_plugin/src/vehicle/follow_lane_sequence/follow_front_entity_action.cpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ FollowFrontEntityAction::calculateObstacle(const traffic_simulator_msgs::msg::Wa
5252

5353
const traffic_simulator_msgs::msg::WaypointsArray FollowFrontEntityAction::calculateWaypoints()
5454
{
55-
if (!canonicalized_entity_status->isInLanelet()) {
55+
if (!canonicalized_entity_status_->isInLanelet()) {
5656
THROW_SIMULATION_ERROR("failed to assign lane");
5757
}
58-
if (canonicalized_entity_status->getTwist().linear.x >= 0) {
58+
if (canonicalized_entity_status_->getTwist().linear.x >= 0) {
5959
traffic_simulator_msgs::msg::WaypointsArray waypoints;
6060
double horizon = getHorizon();
61-
const auto lanelet_pose = canonicalized_entity_status->getLaneletPose();
61+
const auto lanelet_pose = canonicalized_entity_status_->getLaneletPose();
6262
waypoints.waypoints = reference_trajectory->getTrajectory(
6363
lanelet_pose.s, lanelet_pose.s + horizon, 1.0, lanelet_pose.offset);
6464
trajectory = std::make_unique<math::geometry::CatmullRomSubspline>(
@@ -73,14 +73,14 @@ BT::NodeStatus FollowFrontEntityAction::tick()
7373
{
7474
getBlackBoardValues();
7575
if (
76-
request != traffic_simulator::behavior::Request::NONE &&
77-
request != traffic_simulator::behavior::Request::FOLLOW_LANE) {
76+
request_ != traffic_simulator::behavior::Request::NONE &&
77+
request_ != traffic_simulator::behavior::Request::FOLLOW_LANE) {
7878
return BT::NodeStatus::FAILURE;
7979
}
80-
if (getRightOfWayEntities(route_lanelets).size() != 0) {
80+
if (getRightOfWayEntities(route_lanelets_).size() != 0) {
8181
return BT::NodeStatus::FAILURE;
8282
}
83-
if (!behavior_parameter.see_around) {
83+
if (!behavior_parameter_.see_around) {
8484
return BT::NodeStatus::FAILURE;
8585
}
8686
const auto waypoints = calculateWaypoints();
@@ -91,8 +91,9 @@ BT::NodeStatus FollowFrontEntityAction::tick()
9191
return BT::NodeStatus::FAILURE;
9292
}
9393
auto distance_to_stopline =
94-
traffic_simulator::distance::distanceToStopLine(route_lanelets, *trajectory);
95-
auto distance_to_conflicting_entity = getDistanceToConflictingEntity(route_lanelets, *trajectory);
94+
traffic_simulator::distance::distanceToStopLine(route_lanelets_, *trajectory);
95+
auto distance_to_conflicting_entity =
96+
getDistanceToConflictingEntity(route_lanelets_, *trajectory);
9697
const auto front_entity_name = getFrontEntityName(*trajectory);
9798
if (!front_entity_name) {
9899
return BT::NodeStatus::FAILURE;
@@ -112,28 +113,28 @@ BT::NodeStatus FollowFrontEntityAction::tick()
112113
return BT::NodeStatus::FAILURE;
113114
}
114115
}
115-
if (!target_speed) {
116-
target_speed = hdmap_utils->getSpeedLimit(route_lanelets);
116+
if (!target_speed_) {
117+
target_speed_ = hdmap_utils_->getSpeedLimit(route_lanelets_);
117118
}
118119
const double front_entity_linear_velocity = front_entity_status.getTwist().linear.x;
119-
if (target_speed.value() <= front_entity_linear_velocity) {
120-
setCanonicalizedEntityStatus(calculateUpdatedEntityStatus(target_speed.value()));
120+
if (target_speed_.value() <= front_entity_linear_velocity) {
121+
setCanonicalizedEntityStatus(calculateUpdatedEntityStatus(target_speed_.value()));
121122
const auto obstacle = calculateObstacle(waypoints);
122123
setOutput("waypoints", waypoints);
123124
setOutput("obstacle", obstacle);
124125
return BT::NodeStatus::RUNNING;
125126
}
126127
if (
127128
distance_to_front_entity_.value() >=
128-
(calculateStopDistance(behavior_parameter.dynamic_constraints) +
129+
(calculateStopDistance(behavior_parameter_.dynamic_constraints) +
129130
vehicle_parameters.bounding_box.dimensions.x + 5)) {
130131
setCanonicalizedEntityStatus(calculateUpdatedEntityStatus(front_entity_linear_velocity + 2));
131132
setOutput("waypoints", waypoints);
132133
setOutput("obstacle", calculateObstacle(waypoints));
133134
return BT::NodeStatus::RUNNING;
134135
} else if (
135136
distance_to_front_entity_.value() <=
136-
calculateStopDistance(behavior_parameter.dynamic_constraints)) {
137+
calculateStopDistance(behavior_parameter_.dynamic_constraints)) {
137138
setCanonicalizedEntityStatus(calculateUpdatedEntityStatus(front_entity_linear_velocity - 2));
138139
setOutput("waypoints", waypoints);
139140
setOutput("obstacle", calculateObstacle(waypoints));

0 commit comments

Comments
 (0)