15 #ifndef TRAFFIC_SIMULATOR__ENTITY__ENTITY_MANAGER_HPP_
16 #define TRAFFIC_SIMULATOR__ENTITY__ENTITY_MANAGER_HPP_
18 #include <tf2/LinearMath/Quaternion.h>
19 #include <tf2_ros/static_transform_broadcaster.h>
20 #include <tf2_ros/transform_broadcaster.h>
24 #include <rclcpp/node_interfaces/get_node_topics_interface.hpp>
25 #include <rclcpp/node_interfaces/node_topics_interface.hpp>
26 #include <rclcpp/rclcpp.hpp>
30 #include <tf2_geometry_msgs/tf2_geometry_msgs.hpp>
46 #include <traffic_simulator_msgs/msg/behavior_parameter.hpp>
47 #include <traffic_simulator_msgs/msg/bounding_box.hpp>
48 #include <traffic_simulator_msgs/msg/entity_status_with_trajectory_array.hpp>
49 #include <traffic_simulator_msgs/msg/vehicle_parameters.hpp>
50 #include <type_traits>
51 #include <unordered_map>
54 #include <visualization_msgs/msg/marker_array.hpp>
63 explicit LaneletMarkerQoS(std::size_t depth = 1) : rclcpp::QoS(depth) { transient_local(); }
76 std::shared_ptr<rclcpp::node_interfaces::NodeTopicsInterface> node_topics_interface;
77 const rclcpp::node_interfaces::NodeParametersInterface::SharedPtr node_parameters_;
79 tf2_ros::StaticTransformBroadcaster broadcaster_;
80 tf2_ros::TransformBroadcaster base_link_broadcaster_;
82 const rclcpp::Clock::SharedPtr clock_ptr_;
84 std::unordered_map<std::string, std::shared_ptr<traffic_simulator::entity::EntityBase>> entities_;
86 bool npc_logic_started_;
88 using EntityStatusWithTrajectoryArray =
89 traffic_simulator_msgs::msg::EntityStatusWithTrajectoryArray;
90 const rclcpp::Publisher<EntityStatusWithTrajectoryArray>::SharedPtr entity_status_array_pub_ptr_;
92 using MarkerArray = visualization_msgs::msg::MarkerArray;
93 const rclcpp::Publisher<MarkerArray>::SharedPtr lanelet_marker_pub_ptr_;
95 const std::shared_ptr<hdmap_utils::HdMapUtils> hdmap_utils_ptr_;
97 MarkerArray markers_raw_;
99 std::shared_ptr<traffic_simulator::TrafficLights> traffic_lights_ptr_;
108 const std::shared_ptr<traffic_simulator::TrafficLights> & traffic_lights_ptr) ->
void
110 traffic_lights_ptr_ = traffic_lights_ptr;
113 template <
typename Node>
116 geographic_msgs::msg::GeoPoint origin;
118 if (!node.has_parameter(
"origin_latitude")) {
119 node.declare_parameter(
"origin_latitude", 0.0);
121 if (!node.has_parameter(
"origin_longitude")) {
122 node.declare_parameter(
"origin_longitude", 0.0);
124 node.get_parameter(
"origin_latitude", origin.latitude);
125 node.get_parameter(
"origin_longitude", origin.longitude);
131 template <
class NodeT,
class AllocatorT = std::allocator<
void>>
134 const rclcpp::node_interfaces::NodeParametersInterface::SharedPtr & node_parameters)
135 : configuration(configuration),
136 node_topics_interface(rclcpp::node_interfaces::get_node_topics_interface(node)),
137 node_parameters_(node_parameters),
139 base_link_broadcaster_(node),
140 clock_ptr_(node->get_clock()),
141 npc_logic_started_(false),
142 entity_status_array_pub_ptr_(rclcpp::create_publisher<EntityStatusWithTrajectoryArray>(
144 rclcpp::PublisherOptionsWithAllocator<AllocatorT>())),
145 lanelet_marker_pub_ptr_(rclcpp::create_publisher<MarkerArray>(
147 rclcpp::PublisherOptionsWithAllocator<AllocatorT>())),
149 configuration.lanelet2_map_path(),
getOrigin(*node))),
150 markers_raw_(hdmap_utils_ptr_->generateMarker())
159 #define FORWARD_TO_ENTITY(IDENTIFIER, ...) \
165 template <typename... Ts> \
166 decltype(auto) IDENTIFIER(const std::string & name, Ts &&... xs) __VA_ARGS__ \
168 return entities_.at(name)->IDENTIFIER(std::forward<decltype(xs)>(xs)...); \
169 } catch (const std::out_of_range &) { \
170 THROW_SEMANTIC_ERROR("entity : ", name, "does not exist"); \
172 static_assert(true, "")
215 #undef FORWARD_TO_ENTITY
227 const geometry_msgs::msg::PoseStamped & pose,
const bool static_transform =
true);
248 ->
std::optional<traffic_simulator_msgs::msg::Obstacle>;
251 -> const traffic_simulator_msgs::msg::PedestrianParameters &;
254 -> const traffic_simulator_msgs::msg::VehicleParameters &;
256 auto
getWaypoints(const
std::
string & name) -> traffic_simulator_msgs::msg::WaypointsArray;
258 template <typename T>
262 if (not npc_logic_started_) {
265 return entities_.at(name)->getGoalPoses();
268 if (not npc_logic_started_) {
271 std::vector<geometry_msgs::msg::Pose> poses;
272 for (
const auto & lanelet_pose : getGoalPoses<CanonicalizedLaneletPose>(name)) {
280 template <
typename EntityType>
283 return dynamic_cast<EntityType const *
>(entities_.at(name).get()) !=
nullptr;
309 template <
typename Entity,
typename Pose,
typename Parameters,
typename... Ts>
311 const std::string & name,
const Pose & pose,
const Parameters & parameters,
312 const double current_time, Ts &&...
xs)
316 std::is_same<Pose, CanonicalizedLaneletPose>,
317 std::is_same<Pose, geometry_msgs::msg::Pose>>::value,
318 "Pose must be of type CanonicalizedLaneletPose or geometry_msgs::msg::Pose");
323 if constexpr (std::is_same_v<std::decay_t<Entity>,
EgoEntity>) {
324 if (
auto iter = std::find_if(
325 std::begin(entities_), std::end(entities_),
326 [
this](
auto && each) {
return is<EgoEntity>(each.first); });
327 iter != std::end(entities_)) {
330 entity_status.type.type = traffic_simulator_msgs::msg::EntityType::EGO;
332 }
else if constexpr (std::is_same_v<std::decay_t<Entity>, VehicleEntity>) {
333 entity_status.type.type = traffic_simulator_msgs::msg::EntityType::VEHICLE;
334 }
else if constexpr (std::is_same_v<std::decay_t<Entity>, PedestrianEntity>) {
335 entity_status.type.type = traffic_simulator_msgs::msg::EntityType::PEDESTRIAN;
337 entity_status.type.type = traffic_simulator_msgs::msg::EntityType::MISC_OBJECT;
340 entity_status.subtype = parameters.subtype;
341 entity_status.time = current_time;
342 entity_status.name = name;
343 entity_status.bounding_box = parameters.bounding_box;
344 entity_status.action_status = traffic_simulator_msgs::msg::ActionStatus();
345 entity_status.action_status.current_action =
"waiting for initialize";
347 const auto include_crosswalk = [](
const auto & entity_type) {
348 return (traffic_simulator_msgs::msg::EntityType::PEDESTRIAN == entity_type.type) ||
349 (traffic_simulator_msgs::msg::EntityType::MISC_OBJECT == entity_type.type);
350 }(entity_status.type);
352 const auto matching_distance = [](
const auto & local_parameters) {
353 if constexpr (std::is_same_v<
354 std::decay_t<Parameters>, traffic_simulator_msgs::msg::VehicleParameters>) {
356 local_parameters.axles.front_axle.track_width,
357 local_parameters.axles.rear_axle.track_width) *
361 return local_parameters.bounding_box.dimensions.y * 0.5 + 1.0;
365 if constexpr (std::is_same_v<std::decay_t<Pose>,
LaneletPose>) {
367 "LaneletPose is not supported type as pose argument. Only CanonicalizedLaneletPose and "
368 "msg::Pose are supported as pose argument of EntityManager::spawnEntity().");
371 return CanonicalizedEntityStatus(entity_status, pose);
372 }
else if constexpr (std::is_same_v<std::decay_t<Pose>, geometry_msgs::msg::Pose>) {
373 entity_status.pose = pose;
375 pose, parameters.bounding_box, include_crosswalk, matching_distance, hdmap_utils_ptr_);
376 return CanonicalizedEntityStatus(entity_status, canonicalized_lanelet_pose);
380 if (
const auto [iter, success] = entities_.emplace(
381 name, std::make_unique<Entity>(
383 std::forward<decltype(
xs)>(
xs)...));
386 iter->second->setTrafficLights(traffic_lights_ptr_->getConventionalTrafficLights());
393 template <
typename MessageT,
typename... Args>
396 return rclcpp::create_publisher<MessageT>(node_topics_interface, std::forward<Args>(args)...);
399 template <
typename MessageT,
typename... Args>
402 return rclcpp::create_subscription<MessageT>(
403 node_topics_interface, std::forward<Args>(args)...);
406 void update(
const double current_time,
const double step_time);
Definition: ego_entity.hpp:35
Definition: entity_base.hpp:51
Definition: entity_manager.hpp:73
auto getGoalPoses(const std::string &name) -> std::vector< T >
Definition: entity_manager.hpp:255
void resetBehaviorPlugin(const std::string &name, const std::string &behavior_plugin_name)
Reset behavior plugin of the target entity. The internal behavior is to take over the various paramet...
Definition: entity_manager.cpp:293
bool is(const std::string &name) const
Definition: entity_manager.hpp:277
decltype(auto) requestLaneChange(const std::string &name, Ts &&... xs)
Forward to arguments to the EntityBase:: requestLaneChange function.
Definition: entity_manager.hpp:194
auto createSubscription(Args &&... args)
Definition: entity_manager.hpp:396
void updateHdmapMarker()
Definition: entity_manager.cpp:416
decltype(auto) getEntityTypename(const std::string &name, Ts &&... xs) const
Forward to arguments to the EntityBase:: getEntityTypename function.
Definition: entity_manager.hpp:182
void broadcastEntityTransform()
Definition: entity_manager.cpp:41
decltype(auto) reachPosition(const std::string &name, Ts &&... xs) const
Forward to arguments to the EntityBase:: reachPosition function.
Definition: entity_manager.hpp:189
decltype(auto) getBoundingBox(const std::string &name, Ts &&... xs) const
Forward to arguments to the EntityBase:: getBoundingBox function.
Definition: entity_manager.hpp:176
auto getCurrentAction(const std::string &name) const -> std::string
Definition: entity_manager.cpp:325
decltype(auto) requestAssignRoute(const std::string &name, Ts &&... xs)
Forward to arguments to the EntityBase:: requestAssignRoute function.
Definition: entity_manager.hpp:191
decltype(auto) getDefaultMatchingDistanceForLaneletPoseCalculation(const std::string &name, Ts &&... xs) const
Forward to arguments to the EntityBase:: getDefaultMatchingDistanceForLaneletPoseCalculation function...
Definition: entity_manager.hpp:180
decltype(auto) getEntityType(const std::string &name, Ts &&... xs) const
Forward to arguments to the EntityBase:: getEntityType function.
Definition: entity_manager.hpp:181
decltype(auto) getTraveledDistance(const std::string &name, Ts &&... xs) const
Forward to arguments to the EntityBase:: getTraveledDistance function.
Definition: entity_manager.hpp:186
decltype(auto) setLinearVelocity(const std::string &name, Ts &&... xs)
Forward to arguments to the EntityBase:: setLinearVelocity function.
Definition: entity_manager.hpp:205
bool despawnEntity(const std::string &name)
Definition: entity_manager.cpp:141
decltype(auto) getStandStillDuration(const std::string &name, Ts &&... xs) const
Forward to arguments to the EntityBase:: getStandStillDuration function.
Definition: entity_manager.hpp:185
auto getEntityNames() const -> const std::vector< std::string >
Definition: entity_manager.cpp:151
const std::string getEgoName() const
Definition: entity_manager.cpp:199
decltype(auto) setDecelerationLimit(const std::string &name, Ts &&... xs)
Forward to arguments to the EntityBase:: setDecelerationLimit function.
Definition: entity_manager.hpp:202
decltype(auto) setAccelerationRateLimit(const std::string &name, Ts &&... xs)
Forward to arguments to the EntityBase:: setAccelerationRateLimit function.
Definition: entity_manager.hpp:199
decltype(auto) requestFollowTrajectory(const std::string &name, Ts &&... xs)
Forward to arguments to the EntityBase:: requestFollowTrajectory function.
Definition: entity_manager.hpp:193
auto getEntityStatus(const std::string &name) const -> const CanonicalizedEntityStatus &
Definition: entity_manager.cpp:177
decltype(auto) getCurrentAccel(const std::string &name, Ts &&... xs) const
Forward to arguments to the EntityBase:: getCurrentAccel function.
Definition: entity_manager.hpp:178
decltype(auto) requestAcquirePosition(const std::string &name, Ts &&... xs)
Forward to arguments to the EntityBase:: requestAcquirePosition function.
Definition: entity_manager.hpp:190
void setVerbose(const bool verbose)
Definition: entity_manager.cpp:338
decltype(auto) activateOutOfRangeJob(const std::string &name, Ts &&... xs)
Forward to arguments to the EntityBase:: activateOutOfRangeJob function.
Definition: entity_manager.hpp:171
decltype(auto) setDecelerationRateLimit(const std::string &name, Ts &&... xs)
Forward to arguments to the EntityBase:: setDecelerationRateLimit function.
Definition: entity_manager.hpp:203
bool isEgoSpawned() const
Definition: entity_manager.cpp:254
decltype(auto) isControlledBySimulator(const std::string &name, Ts &&... xs)
Forward to arguments to the EntityBase:: isControlledBySimulator function.
Definition: entity_manager.hpp:187
decltype(auto) getLinearJerk(const std::string &name, Ts &&... xs) const
Forward to arguments to the EntityBase:: getLinearJerk function.
Definition: entity_manager.hpp:183
decltype(auto) requestSpeedChange(const std::string &name, Ts &&... xs)
Forward to arguments to the EntityBase:: requestSpeedChange function.
Definition: entity_manager.hpp:209
decltype(auto) setAcceleration(const std::string &name, Ts &&... xs)
Forward to arguments to the EntityBase:: setAcceleration function.
Definition: entity_manager.hpp:197
decltype(auto) getCanonicalizedStatusBeforeUpdate(const std::string &name, Ts &&... xs) const
Forward to arguments to the EntityBase:: getCanonicalizedStatusBeforeUpdate function.
Definition: entity_manager.hpp:177
decltype(auto) requestClearRoute(const std::string &name, Ts &&... xs)
Forward to arguments to the EntityBase:: requestClearRoute function.
Definition: entity_manager.hpp:192
bool entityExists(const std::string &name)
Definition: entity_manager.cpp:146
bool checkCollision(const std::string &first_entity_name, const std::string &second_entity_name)
Definition: entity_manager.cpp:117
auto getWaypoints(const std::string &name) -> traffic_simulator_msgs::msg::WaypointsArray
Definition: entity_manager.cpp:244
decltype(auto) getBehaviorParameter(const std::string &name, Ts &&... xs) const
Forward to arguments to the EntityBase:: getBehaviorParameter function.
Definition: entity_manager.hpp:175
auto isNpcLogicStarted() const -> bool
Definition: entity_manager.hpp:408
decltype(auto) getRouteLanelets(const std::string &name, Ts &&... xs) const
Forward to arguments to the EntityBase:: getRouteLanelets function.
Definition: entity_manager.hpp:184
decltype(auto) setMapPose(const std::string &name, Ts &&... xs)
Forward to arguments to the EntityBase:: setMapPose function.
Definition: entity_manager.hpp:206
decltype(auto) get2DPolygon(const std::string &name, Ts &&... xs) const
Forward to arguments to the EntityBase:: get2DPolygon function.
Definition: entity_manager.hpp:174
decltype(auto) requestWalkStraight(const std::string &name, Ts &&... xs)
Forward to arguments to the EntityBase:: requestWalkStraight function.
Definition: entity_manager.hpp:196
auto spawnEntity(const std::string &name, const Pose &pose, const Parameters ¶meters, const double current_time, Ts &&... xs)
Definition: entity_manager.hpp:306
decltype(auto) setLinearJerk(const std::string &name, Ts &&... xs)
Forward to arguments to the EntityBase:: setLinearJerk function.
Definition: entity_manager.hpp:204
auto setTrafficLights(const std::shared_ptr< traffic_simulator::TrafficLights > &traffic_lights_ptr) -> void
Definition: entity_manager.hpp:107
decltype(auto) requestSynchronize(const std::string &name, Ts &&... xs)
Forward to arguments to the EntityBase:: requestSynchronize function.
Definition: entity_manager.hpp:195
decltype(auto) setAccelerationLimit(const std::string &name, Ts &&... xs)
Forward to arguments to the EntityBase:: setAccelerationLimit function.
Definition: entity_manager.hpp:198
EntityManager(NodeT &&node, const Configuration &configuration, const rclcpp::node_interfaces::NodeParametersInterface::SharedPtr &node_parameters)
Definition: entity_manager.hpp:132
auto getHdmapUtils() -> const std::shared_ptr< hdmap_utils::HdMapUtils > &
Definition: entity_manager.cpp:187
auto getPedestrianParameters(const std::string &name) const -> const traffic_simulator_msgs::msg::PedestrianParameters &
Definition: entity_manager.cpp:222
auto getOrigin(Node &node) const
Definition: entity_manager.hpp:114
bool isInLanelet(const std::string &name, const lanelet::Id lanelet_id, const double tolerance)
Definition: entity_manager.cpp:264
decltype(auto) setBehaviorParameter(const std::string &name, Ts &&... xs)
Forward to arguments to the EntityBase:: setBehaviorParameter function.
Definition: entity_manager.hpp:200
void broadcastTransform(const geometry_msgs::msg::PoseStamped &pose, const bool static_transform=true)
Definition: entity_manager.cpp:96
void update(const double current_time, const double step_time)
Definition: entity_manager.cpp:366
auto getEntity(const std::string &name) const -> std::shared_ptr< traffic_simulator::entity::EntityBase >
Definition: entity_manager.cpp:160
visualization_msgs::msg::MarkerArray makeDebugMarker() const
Definition: entity_manager.cpp:132
auto createPublisher(Args &&... args)
Definition: entity_manager.hpp:390
auto updateNpcLogic(const std::string &name, const double current_time, const double step_time) -> const CanonicalizedEntityStatus &
Definition: entity_manager.cpp:346
auto getVehicleParameters(const std::string &name) const -> const traffic_simulator_msgs::msg::VehicleParameters &
Definition: entity_manager.cpp:233
decltype(auto) laneMatchingSucceed(const std::string &name, Ts &&... xs) const
Forward to arguments to the EntityBase:: laneMatchingSucceed function.
Definition: entity_manager.hpp:188
decltype(auto) getCurrentTwist(const std::string &name, Ts &&... xs) const
Forward to arguments to the EntityBase:: getCurrentTwist function.
Definition: entity_manager.hpp:179
auto startNpcLogic(const double current_time) -> void
Definition: entity_manager.cpp:428
decltype(auto) setVelocityLimit(const std::string &name, Ts &&... xs)
Forward to arguments to the EntityBase:: setVelocityLimit function.
Definition: entity_manager.hpp:208
auto getObstacle(const std::string &name) -> std::optional< traffic_simulator_msgs::msg::Obstacle >
Definition: entity_manager.cpp:212
decltype(auto) setControlledBySimulator(const std::string &name, Ts &&... xs)
Forward to arguments to the EntityBase:: setControlledBySimulator function.
Definition: entity_manager.hpp:201
decltype(auto) asFieldOperatorApplication(const std::string &name, Ts &&... xs) const
Forward to arguments to the EntityBase:: asFieldOperatorApplication function.
Definition: entity_manager.hpp:172
auto getNumberOfEgo() const -> std::size_t
Definition: entity_manager.cpp:192
decltype(auto) setTwist(const std::string &name, Ts &&... xs)
Forward to arguments to the EntityBase:: setTwist function.
Definition: entity_manager.hpp:207
decltype(auto) cancelRequest(const std::string &name, Ts &&... xs)
Forward to arguments to the EntityBase:: cancelRequest function.
Definition: entity_manager.hpp:173
bool isStopping(const std::string &name) const
Definition: entity_manager.cpp:276
Definition: entity_manager.hpp:67
EntityMarkerQoS(std::size_t depth=100)
Definition: entity_manager.hpp:69
Definition: entity_manager.hpp:61
LaneletMarkerQoS(std::size_t depth=1)
Definition: entity_manager.hpp:63
Definition: entity_status.hpp:32
Definition: lanelet_pose.hpp:27
#define THROW_SYNTAX_ERROR(...)
Definition: exception.hpp:62
#define THROW_SEMANTIC_ERROR(...)
Definition: exception.hpp:59
#define FORWARD_TO_ENTITY(IDENTIFIER,...)
Definition: entity_manager.hpp:159
Direction
Definition: lane_change.hpp:29
auto toCanonicalizedLaneletPose(const geometry_msgs::msg::Pose &map_pose, const bool include_crosswalk, const std::shared_ptr< hdmap_utils::HdMapUtils > &hdmap_utils_ptr) -> std::optional< CanonicalizedLaneletPose >
Definition: pose.cpp:77
auto toMapPose(const CanonicalizedLaneletPose &lanelet_pose) -> geometry_msgs::msg::Pose
Definition: pose.cpp:63
traffic_simulator_msgs::msg::EntityType EntityType
Definition: entity_status.hpp:26
traffic_simulator_msgs::msg::LaneletPose LaneletPose
Definition: lanelet_pose.hpp:22
traffic_simulator_msgs::msg::EntityStatus EntityStatus
Definition: entity_status.hpp:25
Definition: junit5.hpp:25
std::string string
Definition: junit5.hpp:26
Definition: configuration.hpp:30
auto makeEntityStatus(std::shared_ptr< hdmap_utils::HdMapUtils > hdmap_utils, traffic_simulator::lanelet_pose::CanonicalizedLaneletPose pose, traffic_simulator_msgs::msg::BoundingBox bbox, const double speed=0.0, const std::string name="default_entity_name", const uint8_t type=traffic_simulator_msgs::msg::EntityType::VEHICLE) -> traffic_simulator::EntityStatus
Definition: helper_functions.hpp:108