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>
47 #include <traffic_simulator_msgs/msg/behavior_parameter.hpp>
48 #include <traffic_simulator_msgs/msg/bounding_box.hpp>
49 #include <traffic_simulator_msgs/msg/entity_status_with_trajectory_array.hpp>
50 #include <traffic_simulator_msgs/msg/vehicle_parameters.hpp>
51 #include <type_traits>
52 #include <unordered_map>
55 #include <visualization_msgs/msg/marker_array.hpp>
64 explicit LaneletMarkerQoS(std::size_t depth = 1) : rclcpp::QoS(depth) { transient_local(); }
77 std::shared_ptr<rclcpp::node_interfaces::NodeTopicsInterface> node_topics_interface;
78 const rclcpp::node_interfaces::NodeParametersInterface::SharedPtr node_parameters_;
80 tf2_ros::StaticTransformBroadcaster broadcaster_;
81 tf2_ros::TransformBroadcaster base_link_broadcaster_;
83 const rclcpp::Clock::SharedPtr clock_ptr_;
85 std::unordered_map<std::string, std::shared_ptr<traffic_simulator::entity::EntityBase>> entities_;
87 bool npc_logic_started_;
89 using EntityStatusWithTrajectoryArray =
90 traffic_simulator_msgs::msg::EntityStatusWithTrajectoryArray;
91 const rclcpp::Publisher<EntityStatusWithTrajectoryArray>::SharedPtr entity_status_array_pub_ptr_;
93 using MarkerArray = visualization_msgs::msg::MarkerArray;
94 const rclcpp::Publisher<MarkerArray>::SharedPtr lanelet_marker_pub_ptr_;
96 const std::shared_ptr<hdmap_utils::HdMapUtils> hdmap_utils_ptr_;
98 MarkerArray markers_raw_;
100 std::shared_ptr<traffic_simulator::TrafficLights> traffic_lights_ptr_;
109 const std::shared_ptr<traffic_simulator::TrafficLights> & traffic_lights_ptr) ->
void
111 traffic_lights_ptr_ = traffic_lights_ptr;
114 template <
typename Node>
117 geographic_msgs::msg::GeoPoint origin;
119 if (!node.has_parameter(
"origin_latitude")) {
120 node.declare_parameter(
"origin_latitude", 0.0);
122 if (!node.has_parameter(
"origin_longitude")) {
123 node.declare_parameter(
"origin_longitude", 0.0);
125 node.get_parameter(
"origin_latitude", origin.latitude);
126 node.get_parameter(
"origin_longitude", origin.longitude);
132 template <
class NodeT,
class AllocatorT = std::allocator<
void>>
135 const rclcpp::node_interfaces::NodeParametersInterface::SharedPtr & node_parameters)
136 : configuration(configuration),
137 node_topics_interface(rclcpp::node_interfaces::get_node_topics_interface(node)),
138 node_parameters_(node_parameters),
140 base_link_broadcaster_(node),
141 clock_ptr_(node->get_clock()),
142 npc_logic_started_(false),
143 entity_status_array_pub_ptr_(rclcpp::create_publisher<EntityStatusWithTrajectoryArray>(
145 rclcpp::PublisherOptionsWithAllocator<AllocatorT>())),
146 lanelet_marker_pub_ptr_(rclcpp::create_publisher<MarkerArray>(
148 rclcpp::PublisherOptionsWithAllocator<AllocatorT>())),
150 configuration.lanelet2_map_path(),
getOrigin(*node))),
151 markers_raw_(hdmap_utils_ptr_->generateMarker())
160 #define FORWARD_TO_ENTITY(IDENTIFIER, ...) \
166 template <typename... Ts> \
167 decltype(auto) IDENTIFIER(const std::string & name, Ts &&... xs) __VA_ARGS__ \
169 return entities_.at(name)->IDENTIFIER(std::forward<decltype(xs)>(xs)...); \
170 } catch (const std::out_of_range &) { \
171 THROW_SEMANTIC_ERROR("entity : ", name, "does not exist"); \
173 static_assert(true, "")
216 #undef FORWARD_TO_ENTITY
228 const geometry_msgs::msg::PoseStamped & pose,
const bool static_transform =
true);
249 ->
std::optional<traffic_simulator_msgs::msg::Obstacle>;
252 -> const traffic_simulator_msgs::msg::PedestrianParameters &;
255 -> const traffic_simulator_msgs::msg::VehicleParameters &;
257 auto
getWaypoints(const
std::
string & name) -> traffic_simulator_msgs::msg::WaypointsArray;
259 template <typename T>
263 if (not npc_logic_started_) {
266 return entities_.at(name)->getGoalPoses();
269 if (not npc_logic_started_) {
272 std::vector<geometry_msgs::msg::Pose> poses;
273 for (
const auto & lanelet_pose : getGoalPoses<CanonicalizedLaneletPose>(name)) {
281 template <
typename EntityType>
284 return dynamic_cast<EntityType const *
>(entities_.at(name).get()) !=
nullptr;
310 template <
typename Entity,
typename Pose,
typename Parameters,
typename... Ts>
312 const std::string & name,
const Pose & pose,
const Parameters & parameters,
313 const double current_time, Ts &&...
xs)
317 std::is_same<Pose, CanonicalizedLaneletPose>,
318 std::is_same<Pose, geometry_msgs::msg::Pose>>::value,
319 "Pose must be of type CanonicalizedLaneletPose or geometry_msgs::msg::Pose");
324 if constexpr (std::is_same_v<std::decay_t<Entity>,
EgoEntity>) {
325 if (
auto iter = std::find_if(
326 std::begin(entities_), std::end(entities_),
327 [
this](
auto && each) {
return is<EgoEntity>(each.first); });
328 iter != std::end(entities_)) {
331 entity_status.type.type = traffic_simulator_msgs::msg::EntityType::EGO;
333 }
else if constexpr (std::is_same_v<std::decay_t<Entity>, VehicleEntity>) {
334 entity_status.type.type = traffic_simulator_msgs::msg::EntityType::VEHICLE;
335 }
else if constexpr (std::is_same_v<std::decay_t<Entity>, PedestrianEntity>) {
336 entity_status.type.type = traffic_simulator_msgs::msg::EntityType::PEDESTRIAN;
338 entity_status.type.type = traffic_simulator_msgs::msg::EntityType::MISC_OBJECT;
341 entity_status.subtype = parameters.subtype;
342 entity_status.time = current_time;
343 entity_status.name = name;
344 entity_status.bounding_box = parameters.bounding_box;
345 entity_status.action_status = traffic_simulator_msgs::msg::ActionStatus();
346 entity_status.action_status.current_action =
"waiting for initialize";
348 const auto include_crosswalk = [](
const auto & entity_type) {
349 return (traffic_simulator_msgs::msg::EntityType::PEDESTRIAN == entity_type.type) ||
350 (traffic_simulator_msgs::msg::EntityType::MISC_OBJECT == entity_type.type);
351 }(entity_status.type);
353 const auto matching_distance = [](
const auto & local_parameters) {
354 if constexpr (std::is_same_v<
355 std::decay_t<Parameters>, traffic_simulator_msgs::msg::VehicleParameters>) {
357 local_parameters.axles.front_axle.track_width,
358 local_parameters.axles.rear_axle.track_width) *
362 return local_parameters.bounding_box.dimensions.y * 0.5 + 1.0;
366 if constexpr (std::is_same_v<std::decay_t<Pose>,
LaneletPose>) {
368 "LaneletPose is not supported type as pose argument. Only CanonicalizedLaneletPose and "
369 "msg::Pose are supported as pose argument of EntityManager::spawnEntity().");
372 return CanonicalizedEntityStatus(entity_status, pose);
373 }
else if constexpr (std::is_same_v<std::decay_t<Pose>, geometry_msgs::msg::Pose>) {
374 entity_status.pose = pose;
376 pose, parameters.bounding_box, include_crosswalk, matching_distance, hdmap_utils_ptr_);
377 return CanonicalizedEntityStatus(entity_status, canonicalized_lanelet_pose);
381 if (
const auto [iter, success] = entities_.emplace(
382 name, std::make_unique<Entity>(
384 std::forward<decltype(
xs)>(
xs)...));
387 iter->second->setTrafficLights(traffic_lights_ptr_->getConventionalTrafficLights());
394 template <
typename MessageT,
typename... Args>
397 return rclcpp::create_publisher<MessageT>(node_topics_interface, std::forward<Args>(args)...);
400 template <
typename MessageT,
typename... Args>
403 return rclcpp::create_subscription<MessageT>(
404 node_topics_interface, std::forward<Args>(args)...);
407 void update(
const double current_time,
const double step_time);
Definition: ego_entity.hpp:37
Definition: entity_base.hpp:51
Definition: entity_manager.hpp:74
auto getGoalPoses(const std::string &name) -> std::vector< T >
Definition: entity_manager.hpp:256
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:278
decltype(auto) requestLaneChange(const std::string &name, Ts &&... xs)
Forward to arguments to the EntityBase:: requestLaneChange function.
Definition: entity_manager.hpp:195
auto createSubscription(Args &&... args)
Definition: entity_manager.hpp:397
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:183
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:190
decltype(auto) getBoundingBox(const std::string &name, Ts &&... xs) const
Forward to arguments to the EntityBase:: getBoundingBox function.
Definition: entity_manager.hpp:177
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:192
decltype(auto) getDefaultMatchingDistanceForLaneletPoseCalculation(const std::string &name, Ts &&... xs) const
Forward to arguments to the EntityBase:: getDefaultMatchingDistanceForLaneletPoseCalculation function...
Definition: entity_manager.hpp:181
decltype(auto) getEntityType(const std::string &name, Ts &&... xs) const
Forward to arguments to the EntityBase:: getEntityType function.
Definition: entity_manager.hpp:182
decltype(auto) getTraveledDistance(const std::string &name, Ts &&... xs) const
Forward to arguments to the EntityBase:: getTraveledDistance function.
Definition: entity_manager.hpp:187
decltype(auto) setLinearVelocity(const std::string &name, Ts &&... xs)
Forward to arguments to the EntityBase:: setLinearVelocity function.
Definition: entity_manager.hpp:206
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:186
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:203
decltype(auto) setAccelerationRateLimit(const std::string &name, Ts &&... xs)
Forward to arguments to the EntityBase:: setAccelerationRateLimit function.
Definition: entity_manager.hpp:200
decltype(auto) requestFollowTrajectory(const std::string &name, Ts &&... xs)
Forward to arguments to the EntityBase:: requestFollowTrajectory function.
Definition: entity_manager.hpp:194
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:179
decltype(auto) requestAcquirePosition(const std::string &name, Ts &&... xs)
Forward to arguments to the EntityBase:: requestAcquirePosition function.
Definition: entity_manager.hpp:191
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:172
decltype(auto) setDecelerationRateLimit(const std::string &name, Ts &&... xs)
Forward to arguments to the EntityBase:: setDecelerationRateLimit function.
Definition: entity_manager.hpp:204
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:188
decltype(auto) getLinearJerk(const std::string &name, Ts &&... xs) const
Forward to arguments to the EntityBase:: getLinearJerk function.
Definition: entity_manager.hpp:184
decltype(auto) requestSpeedChange(const std::string &name, Ts &&... xs)
Forward to arguments to the EntityBase:: requestSpeedChange function.
Definition: entity_manager.hpp:210
decltype(auto) setAcceleration(const std::string &name, Ts &&... xs)
Forward to arguments to the EntityBase:: setAcceleration function.
Definition: entity_manager.hpp:198
decltype(auto) getCanonicalizedStatusBeforeUpdate(const std::string &name, Ts &&... xs) const
Forward to arguments to the EntityBase:: getCanonicalizedStatusBeforeUpdate function.
Definition: entity_manager.hpp:178
decltype(auto) requestClearRoute(const std::string &name, Ts &&... xs)
Forward to arguments to the EntityBase:: requestClearRoute function.
Definition: entity_manager.hpp:193
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:176
auto isNpcLogicStarted() const -> bool
Definition: entity_manager.hpp:409
decltype(auto) getRouteLanelets(const std::string &name, Ts &&... xs) const
Forward to arguments to the EntityBase:: getRouteLanelets function.
Definition: entity_manager.hpp:185
decltype(auto) setMapPose(const std::string &name, Ts &&... xs)
Forward to arguments to the EntityBase:: setMapPose function.
Definition: entity_manager.hpp:207
decltype(auto) get2DPolygon(const std::string &name, Ts &&... xs) const
Forward to arguments to the EntityBase:: get2DPolygon function.
Definition: entity_manager.hpp:175
decltype(auto) requestWalkStraight(const std::string &name, Ts &&... xs)
Forward to arguments to the EntityBase:: requestWalkStraight function.
Definition: entity_manager.hpp:197
auto spawnEntity(const std::string &name, const Pose &pose, const Parameters ¶meters, const double current_time, Ts &&... xs)
Definition: entity_manager.hpp:307
decltype(auto) setLinearJerk(const std::string &name, Ts &&... xs)
Forward to arguments to the EntityBase:: setLinearJerk function.
Definition: entity_manager.hpp:205
auto setTrafficLights(const std::shared_ptr< traffic_simulator::TrafficLights > &traffic_lights_ptr) -> void
Definition: entity_manager.hpp:108
decltype(auto) requestSynchronize(const std::string &name, Ts &&... xs)
Forward to arguments to the EntityBase:: requestSynchronize function.
Definition: entity_manager.hpp:196
decltype(auto) setAccelerationLimit(const std::string &name, Ts &&... xs)
Forward to arguments to the EntityBase:: setAccelerationLimit function.
Definition: entity_manager.hpp:199
EntityManager(NodeT &&node, const Configuration &configuration, const rclcpp::node_interfaces::NodeParametersInterface::SharedPtr &node_parameters)
Definition: entity_manager.hpp:133
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:115
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:201
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:391
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:189
decltype(auto) getCurrentTwist(const std::string &name, Ts &&... xs) const
Forward to arguments to the EntityBase:: getCurrentTwist function.
Definition: entity_manager.hpp:180
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:209
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:202
decltype(auto) asFieldOperatorApplication(const std::string &name, Ts &&... xs) const
Forward to arguments to the EntityBase:: asFieldOperatorApplication function.
Definition: entity_manager.hpp:173
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:208
decltype(auto) cancelRequest(const std::string &name, Ts &&... xs)
Forward to arguments to the EntityBase:: cancelRequest function.
Definition: entity_manager.hpp:174
bool isStopping(const std::string &name) const
Definition: entity_manager.cpp:276
Definition: entity_manager.hpp:68
EntityMarkerQoS(std::size_t depth=100)
Definition: entity_manager.hpp:70
Definition: entity_manager.hpp:62
LaneletMarkerQoS(std::size_t depth=1)
Definition: entity_manager.hpp:64
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:160
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:73
auto toMapPose(const CanonicalizedLaneletPose &lanelet_pose) -> geometry_msgs::msg::Pose
Definition: pose.cpp:59
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:29
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:107
class definition of the traffic sink