diff --git a/simulation/behavior_tree_plugin/include/behavior_tree_plugin/action_node.hpp b/simulation/behavior_tree_plugin/include/behavior_tree_plugin/action_node.hpp index 7257c7b6ab5..5fc46fd05a0 100644 --- a/simulation/behavior_tree_plugin/include/behavior_tree_plugin/action_node.hpp +++ b/simulation/behavior_tree_plugin/include/behavior_tree_plugin/action_node.hpp @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -86,6 +87,8 @@ class ActionNode : public BT::ActionNodeBase BT::InputPort>("canonicalized_entity_status"), BT::InputPort>("traffic_lights"), BT::InputPort("request"), + BT::InputPort>("euclidean_distances_map"), + BT::InputPort("behavior_parameter"), BT::OutputPort>("obstacle"), BT::OutputPort("waypoints"), BT::OutputPort("request"), @@ -116,6 +119,8 @@ class ActionNode : public BT::ActionNodeBase std::optional target_speed; EntityStatusDict other_entity_status; lanelet::Ids route_lanelets; + std::shared_ptr euclidean_distances_map; + traffic_simulator_msgs::msg::BehaviorParameter behavior_parameter; auto getDistanceToTargetEntity( const math::geometry::CatmullRomSplineInterface & spline, diff --git a/simulation/behavior_tree_plugin/include/behavior_tree_plugin/pedestrian/behavior_tree.hpp b/simulation/behavior_tree_plugin/include/behavior_tree_plugin/pedestrian/behavior_tree.hpp index 86d925e8a49..c726d6df948 100644 --- a/simulation/behavior_tree_plugin/include/behavior_tree_plugin/pedestrian/behavior_tree.hpp +++ b/simulation/behavior_tree_plugin/include/behavior_tree_plugin/pedestrian/behavior_tree.hpp @@ -70,6 +70,7 @@ class PedestrianBehaviorTree : public BehaviorPluginBase DEFINE_GETTER_SETTER(TrafficLights, std::shared_ptr) DEFINE_GETTER_SETTER(VehicleParameters, traffic_simulator_msgs::msg::VehicleParameters) DEFINE_GETTER_SETTER(Waypoints, traffic_simulator_msgs::msg::WaypointsArray) + DEFINE_GETTER_SETTER(EuclideanDistancesMap, std::shared_ptr) // clang-format on #undef DEFINE_GETTER_SETTER diff --git a/simulation/behavior_tree_plugin/include/behavior_tree_plugin/pedestrian/pedestrian_action_node.hpp b/simulation/behavior_tree_plugin/include/behavior_tree_plugin/pedestrian/pedestrian_action_node.hpp index b7de2b568b7..a1804be6047 100644 --- a/simulation/behavior_tree_plugin/include/behavior_tree_plugin/pedestrian/pedestrian_action_node.hpp +++ b/simulation/behavior_tree_plugin/include/behavior_tree_plugin/pedestrian/pedestrian_action_node.hpp @@ -33,7 +33,6 @@ class PedestrianActionNode : public ActionNode { // clang-format off return BT::PortsList({ - BT::InputPort("behavior_parameter"), BT::InputPort("pedestrian_parameters"), }) + entity_behavior::ActionNode::providedPorts(); // clang-format on @@ -42,9 +41,6 @@ class PedestrianActionNode : public ActionNode auto calculateUpdatedEntityStatusInWorldFrame(double target_speed) const -> traffic_simulator::EntityStatus; auto calculateUpdatedEntityStatus(double target_speed) const -> traffic_simulator::EntityStatus; - -protected: - traffic_simulator_msgs::msg::BehaviorParameter behavior_parameter; }; } // namespace entity_behavior diff --git a/simulation/behavior_tree_plugin/include/behavior_tree_plugin/vehicle/behavior_tree.hpp b/simulation/behavior_tree_plugin/include/behavior_tree_plugin/vehicle/behavior_tree.hpp index 8333e5ae027..611a08715d6 100644 --- a/simulation/behavior_tree_plugin/include/behavior_tree_plugin/vehicle/behavior_tree.hpp +++ b/simulation/behavior_tree_plugin/include/behavior_tree_plugin/vehicle/behavior_tree.hpp @@ -74,6 +74,7 @@ class VehicleBehaviorTree : public BehaviorPluginBase DEFINE_GETTER_SETTER(TrafficLights, std::shared_ptr) DEFINE_GETTER_SETTER(VehicleParameters, traffic_simulator_msgs::msg::VehicleParameters) DEFINE_GETTER_SETTER(Waypoints, traffic_simulator_msgs::msg::WaypointsArray) + DEFINE_GETTER_SETTER(EuclideanDistancesMap, std::shared_ptr) // clang-format on #undef DEFINE_GETTER_SETTER diff --git a/simulation/behavior_tree_plugin/include/behavior_tree_plugin/vehicle/vehicle_action_node.hpp b/simulation/behavior_tree_plugin/include/behavior_tree_plugin/vehicle/vehicle_action_node.hpp index 43403ba3c65..8f575f69189 100644 --- a/simulation/behavior_tree_plugin/include/behavior_tree_plugin/vehicle/vehicle_action_node.hpp +++ b/simulation/behavior_tree_plugin/include/behavior_tree_plugin/vehicle/vehicle_action_node.hpp @@ -43,7 +43,6 @@ class VehicleActionNode : public ActionNode // clang-format off return BT::PortsList({ BT::InputPort>("reference_trajectory"), - BT::InputPort("behavior_parameter"), BT::InputPort("vehicle_parameters")}) + entity_behavior::ActionNode::providedPorts(); // clang-format on @@ -56,7 +55,6 @@ class VehicleActionNode : public ActionNode const traffic_simulator_msgs::msg::WaypointsArray & waypoints) = 0; protected: - traffic_simulator_msgs::msg::BehaviorParameter behavior_parameter; traffic_simulator_msgs::msg::VehicleParameters vehicle_parameters; std::shared_ptr reference_trajectory; std::unique_ptr trajectory; diff --git a/simulation/behavior_tree_plugin/src/action_node.cpp b/simulation/behavior_tree_plugin/src/action_node.cpp index c970daf30d2..adc90ebaf7f 100644 --- a/simulation/behavior_tree_plugin/src/action_node.cpp +++ b/simulation/behavior_tree_plugin/src/action_node.cpp @@ -91,6 +91,14 @@ auto ActionNode::getBlackBoardValues() -> void if (!getInput("route_lanelets", route_lanelets)) { THROW_SIMULATION_ERROR("failed to get input route_lanelets in ActionNode"); } + if (!getInput>( + "euclidean_distances_map", euclidean_distances_map)) { + THROW_SIMULATION_ERROR("failed to get input euclidean_distances_map in ActionNode"); + } + if (!getInput( + "behavior_parameter", behavior_parameter)) { + behavior_parameter = traffic_simulator_msgs::msg::BehaviorParameter(); + } } auto ActionNode::getHorizon() const -> double @@ -237,34 +245,40 @@ auto ActionNode::getDistanceToFrontEntity( auto ActionNode::getFrontEntityName(const math::geometry::CatmullRomSplineInterface & spline) const -> std::optional { - std::vector distances; - std::vector entities; - for (const auto & [name, status] : other_entity_status) { - const auto distance = getDistanceToTargetEntity(spline, status); - const auto quat = math::geometry::getRotation( - canonicalized_entity_status->getMapPose().orientation, - other_entity_status.at(name).getMapPose().orientation); - /** - * @note hard-coded parameter, if the Yaw value of RPY is in ~1.5708 -> 1.5708, entity is a candidate of front entity. - */ - if ( - std::fabs(math::geometry::convertQuaternionToEulerAngle(quat).z) <= - boost::math::constants::half_pi()) { - if (distance && distance.value() < 40) { - entities.emplace_back(name); - distances.emplace_back(distance.value()); + if (euclidean_distances_map != nullptr) { + std::map local_euclidean_distances_map; + const double stop_distance = calculateStopDistance(behavior_parameter.dynamic_constraints); + const double horizon = spline.getLength() > stop_distance ? spline.getLength() : stop_distance; + for (const auto & [name_pair, euclidean_distance] : *euclidean_distances_map) { + if (euclidean_distance < horizon) { + if (name_pair.first == canonicalized_entity_status->getName()) { + local_euclidean_distances_map.emplace(euclidean_distance, name_pair.second); + } else if (name_pair.second == canonicalized_entity_status->getName()) { + local_euclidean_distances_map.emplace(euclidean_distance, name_pair.first); + } + } + } + + for (const auto & [euclidean_distance, name] : local_euclidean_distances_map) { + const auto quaternion = math::geometry::getRotation( + canonicalized_entity_status->getMapPose().orientation, + other_entity_status.at(name).getMapPose().orientation); + /** + * @note hard-coded parameter, if the Yaw value of RPY is in ~1.5708 -> 1.5708, entity is a candidate of front entity. + */ + if ( + std::fabs(math::geometry::convertQuaternionToEulerAngle(quaternion).z) <= + boost::math::constants::half_pi()) { + const auto longitudinal_distance = + getDistanceToTargetEntity(spline, other_entity_status.at(name)); + + if (longitudinal_distance && longitudinal_distance.value() < horizon) { + return name; + } } } } - if (entities.size() != distances.size()) { - THROW_SIMULATION_ERROR("size of entities and distances vector does not match."); - } - if (distances.empty()) { - return std::nullopt; - } - std::vector::iterator iter = std::min_element(distances.begin(), distances.end()); - size_t index = std::distance(distances.begin(), iter); - return entities[index]; + return std::nullopt; } auto ActionNode::getDistanceToTargetEntityOnCrosswalk( diff --git a/simulation/behavior_tree_plugin/src/pedestrian/pedestrian_action_node.cpp b/simulation/behavior_tree_plugin/src/pedestrian/pedestrian_action_node.cpp index 1443370e8a0..6330e3df1b1 100644 --- a/simulation/behavior_tree_plugin/src/pedestrian/pedestrian_action_node.cpp +++ b/simulation/behavior_tree_plugin/src/pedestrian/pedestrian_action_node.cpp @@ -30,10 +30,6 @@ PedestrianActionNode::PedestrianActionNode( void PedestrianActionNode::getBlackBoardValues() { ActionNode::getBlackBoardValues(); - if (!getInput( - "behavior_parameter", behavior_parameter)) { - behavior_parameter = traffic_simulator_msgs::msg::BehaviorParameter(); - } if (!getInput( "pedestrian_parameters", pedestrian_parameters)) { THROW_SIMULATION_ERROR("failed to get input pedestrian_parameters in PedestrianActionNode"); diff --git a/simulation/behavior_tree_plugin/src/vehicle/vehicle_action_node.cpp b/simulation/behavior_tree_plugin/src/vehicle/vehicle_action_node.cpp index 549ba448d97..3406662ebce 100644 --- a/simulation/behavior_tree_plugin/src/vehicle/vehicle_action_node.cpp +++ b/simulation/behavior_tree_plugin/src/vehicle/vehicle_action_node.cpp @@ -29,10 +29,6 @@ VehicleActionNode::VehicleActionNode(const std::string & name, const BT::NodeCon void VehicleActionNode::getBlackBoardValues() { ActionNode::getBlackBoardValues(); - if (!getInput( - "behavior_parameter", behavior_parameter)) { - behavior_parameter = traffic_simulator_msgs::msg::BehaviorParameter(); - } if (!getInput( "vehicle_parameters", vehicle_parameters)) { THROW_SIMULATION_ERROR("failed to get input vehicle_parameters in VehicleActionNode"); diff --git a/simulation/do_nothing_plugin/include/do_nothing_plugin/plugin.hpp b/simulation/do_nothing_plugin/include/do_nothing_plugin/plugin.hpp index 1804932d741..2bd473f4f6e 100644 --- a/simulation/do_nothing_plugin/include/do_nothing_plugin/plugin.hpp +++ b/simulation/do_nothing_plugin/include/do_nothing_plugin/plugin.hpp @@ -58,6 +58,7 @@ public: \ DEFINE_GETTER_SETTER(TrafficLights, std::shared_ptr) DEFINE_GETTER_SETTER(VehicleParameters, traffic_simulator_msgs::msg::VehicleParameters) DEFINE_GETTER_SETTER(Waypoints, traffic_simulator_msgs::msg::WaypointsArray) + DEFINE_GETTER_SETTER(EuclideanDistancesMap, std::shared_ptr) // clang-format on #undef DEFINE_GETTER_SETTER diff --git a/simulation/traffic_simulator/include/traffic_simulator/behavior/behavior_plugin_base.hpp b/simulation/traffic_simulator/include/traffic_simulator/behavior/behavior_plugin_base.hpp index f4162f3e9b0..1706a303f60 100644 --- a/simulation/traffic_simulator/include/traffic_simulator/behavior/behavior_plugin_base.hpp +++ b/simulation/traffic_simulator/include/traffic_simulator/behavior/behavior_plugin_base.hpp @@ -36,6 +36,8 @@ namespace entity_behavior using EntityStatusDict = std::unordered_map; +using EuclideanDistancesMap = std::unordered_map, double>; + class BehaviorPluginBase { public: @@ -74,6 +76,7 @@ class BehaviorPluginBase DEFINE_GETTER_SETTER(TrafficLights, "traffic_lights", std::shared_ptr) DEFINE_GETTER_SETTER(VehicleParameters, "vehicle_parameters", traffic_simulator_msgs::msg::VehicleParameters) DEFINE_GETTER_SETTER(Waypoints, "waypoints", traffic_simulator_msgs::msg::WaypointsArray) + DEFINE_GETTER_SETTER(EuclideanDistancesMap, "euclidean_distances_map", std::shared_ptr) // clang-format on #undef DEFINE_GETTER_SETTER }; diff --git a/simulation/traffic_simulator/include/traffic_simulator/entity/entity_base.hpp b/simulation/traffic_simulator/include/traffic_simulator/entity/entity_base.hpp index bb4b7aa738c..f87c3816078 100644 --- a/simulation/traffic_simulator/include/traffic_simulator/entity/entity_base.hpp +++ b/simulation/traffic_simulator/include/traffic_simulator/entity/entity_base.hpp @@ -47,6 +47,7 @@ namespace traffic_simulator { namespace entity { +using EuclideanDistancesMap = std::unordered_map, double>; class EntityBase : public std::enable_shared_from_this { public: @@ -312,6 +313,8 @@ class EntityBase : public std::enable_shared_from_this bool verbose; + void setEuclideanDistancesMap(const std::shared_ptr & distances); + protected: std::shared_ptr status_; @@ -333,6 +336,8 @@ class EntityBase : public std::enable_shared_from_this std::unique_ptr speed_planner_; + std::shared_ptr euclidean_distances_map_; + private: virtual auto requestSpeedChangeWithConstantAcceleration( const double target_speed, const speed_change::Transition, const double acceleration, diff --git a/simulation/traffic_simulator/include/traffic_simulator/entity/entity_manager.hpp b/simulation/traffic_simulator/include/traffic_simulator/entity/entity_manager.hpp index bf0c348024b..af882ec210d 100644 --- a/simulation/traffic_simulator/include/traffic_simulator/entity/entity_manager.hpp +++ b/simulation/traffic_simulator/include/traffic_simulator/entity/entity_manager.hpp @@ -93,8 +93,9 @@ class EntityManager // update auto update(const double current_time, const double step_time) -> void; - auto updateNpcLogic(const std::string & name, const double current_time, const double step_time) - -> const CanonicalizedEntityStatus &; + auto updateNpcLogic( + const std::string & name, const double current_time, const double step_time, + const std::shared_ptr & distances) -> const CanonicalizedEntityStatus &; auto updateHdmapMarker() const -> void; @@ -269,6 +270,8 @@ class EntityManager } } + auto calculateEuclideanDistances() -> std::shared_ptr; + private: /* */ Configuration configuration_; diff --git a/simulation/traffic_simulator/src/entity/entity_base.cpp b/simulation/traffic_simulator/src/entity/entity_base.cpp index 77ff802745c..58cd7e5ac0c 100644 --- a/simulation/traffic_simulator/src/entity/entity_base.cpp +++ b/simulation/traffic_simulator/src/entity/entity_base.cpp @@ -851,5 +851,10 @@ auto EntityBase::requestSynchronize( return false; } +void EntityBase::setEuclideanDistancesMap(const std::shared_ptr & distances) +{ + euclidean_distances_map_ = distances; +} + } // namespace entity } // namespace traffic_simulator diff --git a/simulation/traffic_simulator/src/entity/entity_manager.cpp b/simulation/traffic_simulator/src/entity/entity_manager.cpp index 72d9aa63efb..565fcd9d433 100644 --- a/simulation/traffic_simulator/src/entity/entity_manager.cpp +++ b/simulation/traffic_simulator/src/entity/entity_manager.cpp @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include #include #include #include @@ -75,8 +76,11 @@ auto EntityManager::update(const double current_time, const double step_time) -> entity_ptr->setOtherStatus(all_status); } all_status.clear(); + + std::shared_ptr distances = calculateEuclideanDistances(); + for (const auto & [name, entity_ptr] : entities_) { - all_status.try_emplace(name, updateNpcLogic(name, current_time, step_time)); + all_status.try_emplace(name, updateNpcLogic(name, current_time, step_time, distances)); } for (const auto & [name, entity_ptr] : entities_) { entity_ptr->setOtherStatus(all_status); @@ -108,8 +112,8 @@ auto EntityManager::update(const double current_time, const double step_time) -> } auto EntityManager::updateNpcLogic( - const std::string & name, const double current_time, const double step_time) - -> const CanonicalizedEntityStatus & + const std::string & name, const double current_time, const double step_time, + const std::shared_ptr & distances) -> const CanonicalizedEntityStatus & { if (configuration_.verbose) { std::cout << "update " << name << " behavior" << std::endl; @@ -117,6 +121,7 @@ auto EntityManager::updateNpcLogic( auto & entity = getEntity(name); // Update npc completely if logic has started, otherwise update Autoware only - if it is Ego if (npc_logic_started_) { + entity.setEuclideanDistancesMap(distances); entity.onUpdate(current_time, step_time); } else if (entity.is()) { getEgoEntity(name).updateFieldOperatorApplication(); @@ -396,5 +401,20 @@ auto EntityManager::getWaypoints(const std::string & name) return entities_.at(name)->getWaypoints(); } } + +auto EntityManager::calculateEuclideanDistances() -> std::shared_ptr +{ + std::shared_ptr distances = std::make_shared(); + for (auto && [name_from, entity_from] : entities_) { + for (auto && [name_to, entity_to] : entities_) { + if (const auto pair = std::minmax(name_from, name_to); + distances->find(pair) == distances->end() && name_from != name_to) { + distances->emplace( + pair, math::geometry::getDistance(entity_from->getMapPose(), entity_to->getMapPose())); + } + } + } + return distances; +} } // namespace entity } // namespace traffic_simulator diff --git a/simulation/traffic_simulator/src/entity/vehicle_entity.cpp b/simulation/traffic_simulator/src/entity/vehicle_entity.cpp index 75f462e7361..0edceb591f1 100644 --- a/simulation/traffic_simulator/src/entity/vehicle_entity.cpp +++ b/simulation/traffic_simulator/src/entity/vehicle_entity.cpp @@ -140,6 +140,7 @@ auto VehicleEntity::onUpdate(const double current_time, const double step_time) behavior_plugin_ptr_->setTargetSpeed(target_speed_); auto route_lanelets = getRouteLanelets(); behavior_plugin_ptr_->setRouteLanelets(route_lanelets); + behavior_plugin_ptr_->setEuclideanDistancesMap(euclidean_distances_map_); // recalculate spline only when input data changes if (previous_route_lanelets_ != route_lanelets) {