17#include <rclcpp/rclcpp.hpp>
22#include <boost/uuid/uuid_generators.hpp>
23#include <boost/uuid/uuid_io.hpp>
24#include <lanelet2_core/geometry/Point.h>
25#include <trajectory_utils/trajectory_utils.hpp>
26#include <trajectory_utils/conversions/conversions.hpp>
28#include <carma_ros2_utils/carma_lifecycle_node.hpp>
30#include <Eigen/Geometry>
34#include <carma_v2x_msgs/msg/location_ecef.hpp>
35#include <carma_v2x_msgs/msg/trajectory.hpp>
36#include <carma_v2x_msgs/msg/plan_type.hpp>
40using oss = std::ostringstream;
48 : nh_(nh), wm_(wm), config_(config),mobility_response_publisher_(mobility_response_publisher), lc_status_publisher_(lc_status_publisher)
55 return rclcpp::Time(trajectory.trajectory_points.back().target_time).seconds();
60 return rclcpp::Time(trajectory.trajectory_points.front().target_time).seconds();
70 return (rclcpp::Time(trajectory.back().header.stamp) - rclcpp::Time(trajectory.front().header.stamp)).seconds();
75 std::vector<std::pair<int, lanelet::BasicPoint2d>> intersection_points;
76 boost::geometry::model::linestring<lanelet::BasicPoint2d> self_traj;
77 for (
auto tpp:self_trajectory)
79 boost::geometry::append(self_traj, tpp);
82 for (
size_t i=0;
i<incoming_trajectory.size();
i++)
84 double res = boost::geometry::distance(incoming_trajectory.at(
i), self_traj);
88 intersection_points.push_back(std::make_pair(
i, incoming_trajectory.at(
i)));
91 return intersection_points;
96 carma_planning_msgs::msg::TrajectoryPlan trajectory_plan;
97 std::vector<lanelet::BasicPoint2d> map_points;
102 auto curr_point = ecef_trajectory.location;
104 for (
size_t i = 0;
i<ecef_trajectory.offsets.size();
i++)
106 lanelet::BasicPoint2d offset_point;
107 curr_point.ecef_x += ecef_trajectory.offsets.at(
i).offset_x;
108 curr_point.ecef_y += ecef_trajectory.offsets.at(
i).offset_y;
109 curr_point.ecef_z += ecef_trajectory.offsets.at(
i).offset_z;
113 map_points.push_back(offset_point);
123 throw std::invalid_argument(
"No map projector available for ecef conversion");
126 lanelet::BasicPoint3d map_point =
map_projector_->projectECEF( {
static_cast<double>(ecef_point.ecef_x)/100.0,
static_cast<double>(ecef_point.ecef_y)/100.0,
static_cast<double>(ecef_point.ecef_z)/100.0 } , 1);
128 return lanelet::traits::to2D(map_point);
135 carma_v2x_msgs::msg::MobilityResponse out_mobility_response;
137 out_mobility_response.m_header.recipient_id = resp_recipient_id;
138 out_mobility_response.m_header.sender_bsm_id =
host_bsm_id_;
139 out_mobility_response.m_header.plan_id = req_plan_id;
140 out_mobility_response.m_header.timestamp =
nh_->now().seconds()*1000;
145 out_mobility_response.is_accepted =
true;
147 else out_mobility_response.is_accepted =
false;
149 return out_mobility_response;
155 carma_v2x_msgs::msg::MobilityRequest incoming_request = *msg;
156 carma_planning_msgs::msg::LaneChangeStatus lc_status_msg;
157 if (incoming_request.strategy ==
"carma/cooperative-lane-change")
160 RCLCPP_ERROR(
nh_->get_logger(),
"Cannot process mobility request as map projection is not yet set!");
163 if (incoming_request.plan_type.type == carma_v2x_msgs::msg::PlanType::CHANGE_LANE_LEFT || incoming_request.plan_type.type == carma_v2x_msgs::msg::PlanType::CHANGE_LANE_RIGHT)
165 RCLCPP_DEBUG(
nh_->get_logger(),
"Cooperative Lane Change Request Received");
166 lc_status_msg.status = carma_planning_msgs::msg::LaneChangeStatus::REQUEST_RECEIVED;
167 lc_status_msg.description =
"Received lane merge request";
171 RCLCPP_DEBUG(
nh_->get_logger(),
"CLC Request correctly received");
175 std::string req_sender_id = incoming_request.m_header.sender_id;
176 std::string req_plan_id = incoming_request.m_header.plan_id;
178 carma_v2x_msgs::msg::LocationECEF ecef_location = incoming_request.location;
179 carma_v2x_msgs::msg::Trajectory incoming_trajectory = incoming_request.trajectory;
180 std::string req_strategy_params = incoming_request.strategy_params;
182 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"received urgency: " <<
clc_urgency_);
185 using boost::property_tree::ptree;
187 std::istringstream strstream(req_strategy_params);
188 boost::property_tree::json_parser::read_json(strstream, pt);
189 int req_traj_speed_full = pt.get<
int>(
"s");
190 int req_traj_fractional = pt.get<
int>(
"f");
191 int start_lanelet_id = pt.get<
int>(
"sl");
192 int end_lanelet_id = pt.get<
int>(
"el");
193 double req_traj_speed =
static_cast<double>(req_traj_speed_full) +
static_cast<double>(req_traj_fractional)/10.0;
194 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"req_traj_speed" << req_traj_speed);
195 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"start_lanelet_id" << start_lanelet_id);
196 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"end_lanelet_id" << end_lanelet_id);
198 std::vector<lanelet::BasicPoint2d> req_traj_plan = {};
202 double req_expiration_sec =
static_cast<double>(incoming_request.expiration);
203 double current_time_sec =
nh_->now().seconds();
205 bool response_to_clc_req =
false;
207 double req_plan_time = req_expiration_sec - current_time_sec;
208 double req_timestamp =
static_cast<double>(incoming_request.m_header.timestamp) / 1000.0 - current_time_sec;
215 lc_status_msg.status = carma_planning_msgs::msg::LaneChangeStatus::REQUEST_ACCEPTED;
216 lc_status_msg.description =
"Accepted lane merge request";
217 response_to_clc_req =
true;
218 RCLCPP_DEBUG(
nh_->get_logger(),
"CLC accepted");
222 lc_status_msg.status = carma_planning_msgs::msg::LaneChangeStatus::REQUEST_REJECTED;
223 lc_status_msg.description =
"Rejected lane merge request";
224 response_to_clc_req =
false;
225 RCLCPP_DEBUG(
nh_->get_logger(),
"CLC rejected");
227 carma_v2x_msgs::msg::MobilityResponse outgoing_response =
compose_mobility_response(req_sender_id, req_plan_id, response_to_clc_req);
229 lc_status_msg.status = carma_planning_msgs::msg::LaneChangeStatus::RESPONSE_SENT;
230 RCLCPP_DEBUG(
nh_->get_logger(),
"response sent");
248 carma_v2x_msgs::msg::BSMCoreData bsm_core_ = msg->core_data;
253 carma_planning_msgs::srv::PlanTrajectory::Request::SharedPtr req,
254 carma_planning_msgs::srv::PlanTrajectory::Response::SharedPtr resp)
256 RCLCPP_DEBUG(
nh_->get_logger(),
"Yield_plugin was called!");
257 if (req->initial_trajectory_plan.trajectory_points.size() < 2){
258 throw std::invalid_argument(
"Empty Trajectory received by Yield");
260 rclcpp::Clock system_clock(RCL_SYSTEM_TIME);
261 rclcpp::Time start_time = system_clock.now();
263 carma_planning_msgs::msg::TrajectoryPlan original_trajectory = req->initial_trajectory_plan;
264 carma_planning_msgs::msg::TrajectoryPlan yield_trajectory;
267 double initial_velocity = req->vehicle_state.longitudinal_vel;
270 if (initial_velocity <
EPSILON)
272 initial_velocity = original_trajectory.initial_longitudinal_velocity;
278 RCLCPP_DEBUG(
nh_->get_logger(),
"Only consider high urgency clc");
281 RCLCPP_DEBUG(
nh_->get_logger(),
"Yield for CLC. We haven't received an updated negotiation this timestep");
287 RCLCPP_DEBUG(
nh_->get_logger(),
"unreliable CLC communication, switching to object avoidance");
293 RCLCPP_DEBUG(
nh_->get_logger(),
"Yield for object avoidance");
300 resp->trajectory_plan = original_trajectory;
304 yield_trajectory.header.frame_id =
"map";
305 yield_trajectory.header.stamp =
nh_->now();
306 yield_trajectory.trajectory_id = original_trajectory.trajectory_id;
307 resp->trajectory_plan = yield_trajectory;
310 rclcpp::Time end_time = system_clock.now();
312 auto duration = end_time - start_time;
313 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"ExecutionTime: " <<
std::to_string(duration.seconds()));
319 carma_planning_msgs::msg::TrajectoryPlan cooperative_trajectory;
321 double initial_pos = 0;
323 double initial_velocity = current_speed;
327 std::vector<lanelet::BasicPoint2d> host_traj_points = {};
328 for (
size_t i=0;
i<original_tp.trajectory_points.size();
i++)
330 lanelet::BasicPoint2d traj_point;
331 traj_point.x() = original_tp.trajectory_points.at(
i).x;
332 traj_point.y() = original_tp.trajectory_points.at(
i).y;
333 host_traj_points.push_back(traj_point);
337 if (!intersection_points.empty())
339 lanelet::BasicPoint2d intersection_point = intersection_points[0].second;
340 double dx = original_tp.trajectory_points[0].x - intersection_point.x();
341 double dy = original_tp.trajectory_points[0].y - intersection_point.y();
344 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"digital_gap: " << digital_gap);
346 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"Goal position (goal_pos): " << goal_pos);
349 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"Collision time: " << collision_time);
350 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"intersection num: " << intersection_points[0].first);
351 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"Planning time: " << planning_time);
356 double incoming_trajectory_speed = sqrt(dx2*dx2 + dy2*dy2)/(intersection_points[0].first *
ecef_traj_timestep_);
358 goal_velocity = std::min(goal_velocity, incoming_trajectory_speed);
361 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"goal_velocity: " << goal_velocity);
362 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"incoming_trajectory_speed: " << incoming_trajectory_speed);
364 if (planning_time > min_time)
368 cooperative_trajectory =
generate_JMT_trajectory(original_tp, initial_pos, goal_pos, initial_velocity, goal_velocity, planning_time, original_max_speed);
373 RCLCPP_DEBUG(
nh_->get_logger(),
"The incoming requested trajectory is rejected, due to insufficient gap");
374 cooperative_trajectory = original_tp;
381 RCLCPP_DEBUG(
nh_->get_logger(),
"The incoming requested trajectory does not overlap with host vehicle's trajectory");
382 cooperative_trajectory = original_tp;
385 return cooperative_trajectory;
390 double smallest_time_step = std::numeric_limits<double>::infinity();
391 for (
size_t i = 0;
i < original_tp.trajectory_points.size() - 1;
i ++)
393 smallest_time_step = std::min(smallest_time_step,
394 (rclcpp::Time(original_tp.trajectory_points.at(
i + 1).target_time)
395 - rclcpp::Time(original_tp.trajectory_points.at(
i).target_time)).seconds());
397 RCLCPP_DEBUG_STREAM(rclcpp::get_logger(
"yield_plugin"),
"smallest_time_step: " << smallest_time_step);
399 return smallest_time_step;
403 double initial_velocity,
double goal_velocity,
double planning_time,
double original_max_speed)
405 carma_planning_msgs::msg::TrajectoryPlan jmt_trajectory;
406 std::vector<carma_planning_msgs::msg::TrajectoryPlanPoint> jmt_trajectory_points;
407 jmt_trajectory_points.push_back(original_tp.trajectory_points[0]);
410 std::vector<double> calculated_speeds = {};
411 std::vector<double> new_relative_downtracks = {};
412 new_relative_downtracks.push_back(0.0);
413 calculated_speeds.push_back(initial_velocity);
414 double new_traj_accumulated_downtrack = 0.0;
415 double original_traj_accumulated_downtrack = original_traj_relative_downtracks.at(1);
419 const double initial_time = 0;
420 const double initial_accel = 0;
421 const double goal_accel = 0;
422 int new_traj_idx = 1;
423 int original_traj_idx = 1;
424 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"Following parameters used for JMT: "
425 "\ninitial_pos: " << initial_pos <<
426 "\ngoal_pos: " << goal_pos <<
427 "\ninitial_velocity: " << initial_velocity <<
428 "\ngoal_velocity: " << goal_velocity <<
429 "\ninitial_accel: " << initial_accel <<
430 "\ngoal_accel: " << goal_accel <<
431 "\nplanning_time: " << planning_time <<
432 "\noriginal_max_speed: " << original_max_speed);
435 std::vector<double> polynomial_coefficients = quintic_coefficient_calculator::quintic_coefficient_calculator(initial_pos,
443 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"Used original_max_speed: " << original_max_speed);
445 while (new_traj_accumulated_downtrack < goal_pos -
EPSILON && original_traj_idx < original_traj_relative_downtracks.size())
447 const double target_time = new_traj_idx * smallest_time_step;
448 const double downtrack_at_target_time =
polynomial_calc(polynomial_coefficients, target_time);
449 double velocity_at_target_time =
polynomial_calc_d(polynomial_coefficients, target_time);
451 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"Calculated speed velocity_at_target_time: " << velocity_at_target_time
452 <<
", downtrack_at_target_time: "<< downtrack_at_target_time <<
", target_time: " << target_time);
456 if (velocity_at_target_time < 0.0)
462 velocity_at_target_time = std::clamp(velocity_at_target_time, 0.0, original_max_speed);
465 if (downtrack_at_target_time >= original_traj_accumulated_downtrack)
467 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"Picked calculated speed velocity_at_target_time: " << velocity_at_target_time
468 <<
", downtrack_at_target_time: "<< downtrack_at_target_time <<
", target_time: " << target_time);
471 calculated_speeds.push_back(velocity_at_target_time);
472 original_traj_accumulated_downtrack += original_traj_relative_downtracks.at(original_traj_idx);
473 original_traj_idx ++;
475 new_traj_accumulated_downtrack = downtrack_at_target_time;
482 std::fill_n(std::back_inserter(calculated_speeds),
483 std::size(original_traj_relative_downtracks) - std::size(calculated_speeds),
488 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"filtered_speeds size: " << filtered_speeds.size());
491 double prev_speed = filtered_speeds.at(0);
492 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"start speed: " << prev_speed <<
", target_time: " <<
std::to_string(rclcpp::Time(original_tp.trajectory_points[0].target_time).seconds()));
494 for(
size_t i = 1;
i < original_tp.trajectory_points.size();
i++)
496 carma_planning_msgs::msg::TrajectoryPlanPoint jmt_tpp = original_tp.trajectory_points.at(
i);
500 double current_speed = goal_velocity;
502 if (
i < filtered_speeds.size())
504 current_speed = filtered_speeds.at(
i);
515 const double dt = (2 * original_traj_relative_downtracks.at(
i)) / (current_speed + prev_speed);
516 jmt_tpp.target_time = rclcpp::Time(jmt_trajectory_points.back().target_time) + rclcpp::Duration(dt*1e9);
525 jmt_tpp.target_time = rclcpp::Time(jmt_trajectory_points.back().target_time) + rclcpp::Duration(6000 * 1e9);
526 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"Zero speed = x: " << jmt_tpp.x <<
", y:" << jmt_tpp.y
527 <<
", t:" <<
std::to_string(rclcpp::Time(jmt_tpp.target_time).seconds())
528 <<
", prev_speed: " << prev_speed <<
", current_speed: " << current_speed);
532 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"Non-zero speed = x: " << jmt_tpp.x <<
", y:" << jmt_tpp.y
533 <<
", t:" <<
std::to_string(rclcpp::Time(jmt_tpp.target_time).seconds())
534 <<
", prev_speed: " << prev_speed <<
", current_speed: " << current_speed);
537 jmt_trajectory_points.push_back(jmt_tpp);
538 double insta_decel = (current_speed - prev_speed) / (rclcpp::Time(jmt_trajectory_points.at(
i).target_time).seconds() - rclcpp::Time(jmt_trajectory_points.at(
i - 1).target_time).seconds());
539 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"insta_decel: " << insta_decel );
540 prev_speed = current_speed;
543 jmt_trajectory.header = original_tp.header;
544 jmt_trajectory.trajectory_id = original_tp.trajectory_id;
545 jmt_trajectory.trajectory_points = jmt_trajectory_points;
546 jmt_trajectory.initial_longitudinal_velocity = initial_velocity;
547 return jmt_trajectory;
551 const std::vector<carma_perception_msgs::msg::PredictedState>& trajectory2,
double collision_radius,
double trajectory1_max_speed)
555 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"Starting a new collision detection, trajectory size: "
556 << trajectory1.trajectory_points.size() <<
". prediction size: " << trajectory2.size());
559 bool on_route =
false;
560 int on_route_idx = 0;
563 const auto traj2_speed{std::hypot(trajectory2.front().predicted_velocity.linear.x,
564 trajectory2.front().predicted_velocity.linear.y)};
567 if (trajectory2.size() < 2)
569 throw std::invalid_argument(
"Object on ther road doesn't have enough predicted states! Please check motion_computation is correctly applying predicted states");
571 const double predict_step_duration = (rclcpp::Time(trajectory2.at(1).header.stamp) - rclcpp::Time(trajectory2.front().header.stamp)).seconds();
574 if (predict_step_duration < 0.0)
576 throw std::invalid_argument(
"Predicted states of the object is malformed. Detected trajectory going backwards in time!");
586 int iteration_stride = std::max(1,
static_cast<int>(iteration_stride_max_time_s / predict_step_duration));
588 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"Determined iteration_stride: " << iteration_stride
589 <<
", with traj2_speed: " << traj2_speed
590 <<
", with trajectory1_max_speed: " << trajectory1_max_speed
591 <<
", with predict_step_duration: " << predict_step_duration
592 <<
", iteration_stride_max_time_s: " << iteration_stride_max_time_s);
594 for (
size_t j = 0; j < trajectory2.size(); j += iteration_stride)
596 lanelet::BasicPoint2d curr_point;
597 curr_point.x() = trajectory2.at(j).predicted_position.position.x;
598 curr_point.y() = trajectory2.at(j).predicted_position.position.y;
600 auto corresponding_lanelets =
wm_->getLaneletsFromPoint(curr_point, 8);
602 for (
const auto& llt: corresponding_lanelets)
604 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"Checking llt: " << llt.id());
613 if (on_route || traj2_has_zero_speed)
619 RCLCPP_DEBUG(
nh_->get_logger(),
"Predicted states are not on the route! ignoring");
623 double smallest_dist = std::numeric_limits<double>::infinity();
624 for (
size_t i = 0;
i < trajectory1.trajectory_points.size() - 1; ++
i)
626 auto p1a = trajectory1.trajectory_points.at(
i);
627 auto p1b = trajectory1.trajectory_points.at(
i + 1);
628 double previous_distance_between_predictions = std::numeric_limits<double>::infinity();
629 for (
size_t j = on_route_idx; j < trajectory2.size() - 1; j += iteration_stride)
631 auto p2a = trajectory2.at(j);
632 auto p2b = trajectory2.at(j + 1);
633 double p1a_t = rclcpp::Time(p1a.target_time).seconds();
634 double p1b_t = rclcpp::Time(p1b.target_time).seconds();
635 double p2a_t = rclcpp::Time(p2a.header.stamp).seconds();
636 double p2b_t = rclcpp::Time(p2b.header.stamp).seconds();
640 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"p1a.x: " << p1a.x <<
", p1a.y: " << p1a.y);
641 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"p1b.x: " << p1b.x <<
", p1b.y: " << p1b.y);
643 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"p2a.x: " << p2a.predicted_position.position.x <<
", p2a.y: " << p2a.predicted_position.position.y);
644 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"p2b.x: " << p2b.predicted_position.position.x <<
", p2b.y: " << p2b.predicted_position.position.y);
647 double dt = (p2a_t - p1a_t) / (p1b_t - p1a_t);
648 double x1 = p1a.x + dt * (p1b.x - p1a.x);
649 double y1 = p1a.y + dt * (p1b.y - p1a.y);
650 double x2 = p2a.predicted_position.position.x;
651 double y2 = p2a.predicted_position.position.y;
654 const auto distance{std::hypot(x1 - x2, y1 - y2)};
656 smallest_dist = std::min(distance, smallest_dist);
657 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"Smallest_dist: " << smallest_dist <<
", distance: " << distance <<
", dt: " << dt
658 <<
", x1: " << x1 <<
", y1: " << y1
659 <<
", x2: " << x2 <<
", y2: " << y2
666 if (previous_distance_between_predictions < distance)
668 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"Stopping search here because the distance between predictions started to increase");
671 previous_distance_between_predictions = distance;
675 RCLCPP_DEBUG(
nh_->get_logger(),
"Too far away" );
679 if (distance > collision_radius)
686 collision_result.
point1 = lanelet::BasicPoint2d(x1,y1);
687 collision_result.
point2 = lanelet::BasicPoint2d(x2,y2);
689 return collision_result;
705 RCLCPP_INFO_STREAM(
nh_->get_logger(),
"Detected an object nearby might be behind the vehicle at timestamp: " <<
std::to_string(collision_time.seconds()) <<
724 const carma_perception_msgs::msg::ExternalObject& curr_obstacle,
double original_tp_max_speed)
728 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"Object's back time: " <<
std::to_string(rclcpp::Time(curr_obstacle.predictions.back().header.stamp).seconds())
732 if (rclcpp::Time(curr_obstacle.predictions.back().header.stamp).seconds() <= plan_start_time)
737 std::vector<carma_perception_msgs::msg::PredictedState> new_list;
738 carma_perception_msgs::msg::PredictedState curr_state;
740 curr_state.header.stamp = curr_obstacle.header.stamp;
741 curr_state.predicted_position.position.x = curr_obstacle.pose.pose.position.x;
742 curr_state.predicted_position.position.y = curr_obstacle.pose.pose.position.y;
744 curr_state.predicted_velocity.linear.x = curr_obstacle.velocity.twist.linear.x;
745 curr_state.predicted_velocity.linear.y = curr_obstacle.velocity.twist.linear.y;
746 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"Object: " << curr_obstacle.id <<
", type: " <<
static_cast<int>(curr_obstacle.object_type)
747 <<
", speed_x: " << curr_obstacle.velocity.twist.linear.x <<
", speed_y: " << curr_obstacle.velocity.twist.linear.y);
748 new_list.push_back(curr_state);
749 new_list.insert(new_list.end(), curr_obstacle.predictions.cbegin(), curr_obstacle.predictions.cend());
753 if (!collision_result)
761 const double vehicle_downtrack =
wm_->routeTrackPos(collision_result.value().point1).downtrack;
762 const double object_downtrack =
wm_->routeTrackPos(collision_result.value().point2).downtrack;
764 if (
is_object_behind_vehicle(curr_obstacle.id, collision_result.value().collision_time, vehicle_downtrack, object_downtrack))
766 RCLCPP_INFO_STREAM(
nh_->get_logger(),
"Confirmed that the object: " << curr_obstacle.id <<
" is behind the vehicle at timestamp " <<
std::to_string(collision_result.value().collision_time.seconds()));
770 const auto distance{std::hypot(
771 collision_result.value().point1.x() - collision_result.value().point2.x(),
772 collision_result.value().point1.y() - collision_result.value().point2.y()
775 RCLCPP_WARN_STREAM(
nh_->get_logger(),
"Collision detected for object: " << curr_obstacle.id <<
", at timestamp " <<
std::to_string(collision_result.value().collision_time.seconds()) <<
776 ", x: " << collision_result.value().point1.x() <<
", y: " << collision_result.value().point1.y() <<
777 ", within actual downtrack distance: " << object_downtrack - vehicle_downtrack <<
778 ", and collision distance: " << distance);
780 return collision_result.value().collision_time;
784 const std::vector<carma_perception_msgs::msg::ExternalObject>& external_objects,
double original_tp_max_speed)
787 std::unordered_map<uint32_t, std::future<std::optional<rclcpp::Time>>> futures;
788 std::unordered_map<uint32_t, rclcpp::Time> collision_times;
791 for (
const auto&
object : external_objects) {
792 futures[
object.id] = std::async(std::launch::async,[
this, &original_tp, &
object, &original_tp_max_speed]{
798 for (
const auto&
object : external_objects) {
799 if (
const auto collision_time{futures.at(
object.
id).get()}) {
800 collision_times[
object.id] = collision_time.value();
804 return collision_times;
808 const std::vector<carma_perception_msgs::msg::ExternalObject>& external_objects)
810 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"ExternalObjects size: " << external_objects.size());
812 if (!
wm_->getRoute())
814 RCLCPP_WARN(
nh_->get_logger(),
"Yield plugin was not able to analyze collision since route is not available! Please check if route is set");
819 for (
const auto& llt:
wm_->getRoute()->shortestPath())
825 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"External Object List (external_objects) size: " << external_objects.size());
829 if (collision_times.empty()) {
return std::nullopt; }
831 const auto earliest_colliding_object_id{std::min_element(
832 std::cbegin(collision_times), std::cend(collision_times),
833 [](
const auto & a,
const auto & b){
return a.second < b.second; })->first};
835 const auto earliest_colliding_object{std::find_if(
836 std::cbegin(external_objects), std::cend(external_objects),
837 [&earliest_colliding_object_id](
const auto &
object) {
return object.id == earliest_colliding_object_id; })};
839 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"earliest object x: " << earliest_colliding_object->velocity.twist.linear.x
840 <<
", y: " << earliest_colliding_object->velocity.twist.linear.y);
841 return std::make_pair(*earliest_colliding_object, collision_times.at(earliest_colliding_object_id).seconds());
846 const carma_planning_msgs::msg::TrajectoryPlan& original_tp,
double timestamp_in_sec_to_predict)
848 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"timestamp_in_sec_to_predict: " <<
std::to_string(timestamp_in_sec_to_predict) <<
851 double point_b_time = 0.0;
852 carma_planning_msgs::msg::TrajectoryPlanPoint point_a;
853 carma_planning_msgs::msg::TrajectoryPlanPoint point_b;
857 for (
size_t i = 0;
i < original_tp.trajectory_points.size() - 1; ++
i)
859 point_a = original_tp.trajectory_points.at(
i);
860 point_b = original_tp.trajectory_points.at(
i + 1);
861 point_b_time = rclcpp::Time(point_b.target_time).seconds();
862 if (point_b_time >= timestamp_in_sec_to_predict)
868 auto dx = point_b.x - point_a.x;
869 auto dy = point_b.y - point_a.y;
870 const tf2::Vector3 trajectory_direction(dx, dy, 0);
872 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"timestamp_in_sec_to_predict: " <<
std::to_string(timestamp_in_sec_to_predict)
874 <<
", dx: " << dx <<
", dy: " << dy <<
", "
875 <<
", object_velocity_in_map_frame.x: " << object_velocity_in_map_frame.linear.x
876 <<
", object_velocity_in_map_frame.y: " << object_velocity_in_map_frame.linear.y);
878 if (trajectory_direction.length() < 0.001)
883 const tf2::Vector3 object_direction(object_velocity_in_map_frame.linear.x, object_velocity_in_map_frame.linear.y, 0);
885 return tf2::tf2Dot(object_direction, trajectory_direction) / trajectory_direction.length();
889 const std::vector<carma_perception_msgs::msg::ExternalObject>& external_objects,
double initial_velocity)
891 if (original_tp.trajectory_points.size() < 2)
893 RCLCPP_WARN(
nh_->get_logger(),
"Yield plugin received less than 2 points in update_traj_for_object, returning unchanged...");
900 if (!earliest_collision_obj_pair)
902 RCLCPP_DEBUG(
nh_->get_logger(),
"No collision detected, so trajectory not modified.");
906 carma_perception_msgs::msg::ExternalObject earliest_collision_obj = earliest_collision_obj_pair.value().first;
907 double earliest_collision_time_in_seconds = earliest_collision_obj_pair.value().second;
912 const lanelet::BasicPoint2d vehicle_point(original_tp.trajectory_points[0].x,original_tp.trajectory_points[0].y);
913 const double vehicle_downtrack =
wm_->routeTrackPos(vehicle_point).downtrack;
915 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"vehicle_downtrack: " << vehicle_downtrack);
917 RCLCPP_WARN_STREAM(
nh_->get_logger(),
"Collision Detected!");
919 const lanelet::BasicPoint2d object_point(earliest_collision_obj.pose.pose.position.x, earliest_collision_obj.pose.pose.position.y);
920 const double object_downtrack =
wm_->routeTrackPos(object_point).downtrack;
922 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"object_downtrack: " << object_downtrack);
924 const double object_downtrack_lead = std::max(0.0, object_downtrack - vehicle_downtrack);
925 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"object_downtrack_lead: " << object_downtrack_lead);
929 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"object's speed along trajectory at collision: " << goal_velocity);
935 RCLCPP_WARN_STREAM(
nh_->get_logger(),
"The obstacle is not moving, goal velocity is set to 0 from: " << goal_velocity);
941 if (!std::isnormal(safety_gap))
943 RCLCPP_WARN_STREAM(rclcpp::get_logger(
"yield_plugin"),
"Detected non-normal (nan, inf, etc.) safety_gap."
952 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"externally_commanded_safety_gap: " << externally_commanded_safety_gap);
954 safety_gap = std::max(safety_gap, externally_commanded_safety_gap);
958 const double initial_pos = 0.0;
959 const double original_max_speed =
max_trajectory_speed(original_tp.trajectory_points, earliest_collision_time_in_seconds);
960 const double delta_v_max = fabs(goal_velocity - original_max_speed);
961 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"delta_v_max: " << delta_v_max <<
", safety_gap: " << safety_gap);
968 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"time_required_for_comfortable_decel_in_s: " << time_required_for_comfortable_decel_in_s <<
", min_time_required_for_comfortable_decel_in_s: " << min_time_required_for_comfortable_decel_in_s);
970 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"Object avoidance planning time: " << planning_time_in_s);
972 return generate_JMT_trajectory(original_tp, initial_pos, goal_pos, initial_velocity, goal_velocity, planning_time_in_s, original_max_speed);
978 std::vector<double> downtracks;
979 downtracks.reserve(trajectory_plan.trajectory_points.size());
981 downtracks.push_back(0.0);
982 for (
size_t i=1;
i < trajectory_plan.trajectory_points.size();
i++){
984 double dx = trajectory_plan.trajectory_points.at(
i).x - trajectory_plan.trajectory_points.at(
i-1).x;
985 double dy = trajectory_plan.trajectory_points.at(
i).y - trajectory_plan.trajectory_points.at(
i-1).y;
986 downtracks.push_back(sqrt(dx*dx + dy*dy));
994 for (
size_t i = 0;
i < coeff.size();
i++)
996 double value = coeff.at(
i) * pow(
x,
static_cast<int>(coeff.size() - 1 -
i));
997 result = result + value;
1005 for (
size_t i = 0;
i < coeff.size()-1;
i++)
1007 double value =
static_cast<int>(coeff.size() - 1 -
i) * coeff.at(
i) * pow(
x,
static_cast<int>(coeff.size() - 2 -
i));
1008 result = result + value;
1015 double max_speed = 0;
1016 for(
size_t i = 0;
i < trajectory_points.size() - 2;
i++ )
1018 double dx = trajectory_points.at(
i + 1).x - trajectory_points.at(
i).x;
1019 double dy = trajectory_points.at(
i + 1).y - trajectory_points.at(
i).y;
1020 double d = sqrt(dx*dx + dy*dy);
1021 double t = (rclcpp::Time(trajectory_points.at(
i + 1).target_time).seconds() - rclcpp::Time(trajectory_points.at(
i).target_time).seconds());
1027 if (rclcpp::Time(trajectory_points.at(
i + 1).target_time).seconds() >= timestamp_in_sec_to_search_until)
1038 double desired_gap = 0;
1040 for (
size_t i = 0;
i < original_tp.trajectory_points.size();
i++)
1042 lanelet::BasicPoint2d veh_pos(original_tp.trajectory_points.at(
i).x, original_tp.trajectory_points.at(
i).y);
1043 auto llts =
wm_->getLaneletsFromPoint(veh_pos, 1);
1046 RCLCPP_WARN_STREAM(
nh_->get_logger(),
"Trajectory point: x= " << original_tp.trajectory_points.at(
i).x <<
"y="<< original_tp.trajectory_points.at(
i).y);
1048 throw std::invalid_argument(
"Trajectory Point is not on a valid lanelet.");
1050 auto digital_min_gap = llts[0].regulatoryElementsAs<lanelet::DigitalMinimumGap>();
1051 if (!digital_min_gap.empty())
1053 double digital_gap = digital_min_gap[0]->getMinimumGap();
1054 RCLCPP_DEBUG_STREAM(
nh_->get_logger(),
"Digital Gap found with value: " << digital_gap);
1055 desired_gap = std::max(desired_gap, digital_gap);
1066 map_projector_ = std::make_shared<lanelet::projection::LocalFrameProjector>(georeference.c_str());
std::string georeference_
std::set< lanelet::Id > route_llt_ids_
void plan_trajectory_callback(carma_planning_msgs::srv::PlanTrajectory::Request::SharedPtr req, carma_planning_msgs::srv::PlanTrajectory::Response::SharedPtr resp)
Service callback for trajectory planning.
std::vector< double > get_relative_downtracks(const carma_planning_msgs::msg::TrajectoryPlan &trajectory_plan) const
calculates distance between trajectory points in a plan
LaneChangeStatusCB lc_status_publisher_
void set_incoming_request_info(std::vector< lanelet::BasicPoint2d > req_trajectory, double req_speed, double req_planning_time, double req_timestamp)
set values for member variables related to cooperative behavior
std::vector< lanelet::BasicPoint2d > req_trajectory_points_
void mobilityrequest_cb(const carma_v2x_msgs::msg::MobilityRequest::UniquePtr msg)
callback for mobility request
double check_traj_for_digital_min_gap(const carma_planning_msgs::msg::TrajectoryPlan &original_tp) const
checks trajectory for minimum gap associated with it
double get_predicted_velocity_at_time(const geometry_msgs::msg::Twist &object_velocity_in_map_frame, const carma_planning_msgs::msg::TrajectoryPlan &original_tp, double timestamp_in_sec_to_predict)
Given the object velocity in map frame with x,y components, this function returns the projected veloc...
std::shared_ptr< lanelet::projection::LocalFrameProjector > map_projector_
std::vector< lanelet::BasicPoint2d > convert_eceftrajectory_to_mappoints(const carma_v2x_msgs::msg::Trajectory &ecef_trajectory) const
convert a carma trajectory from ecef frame to map frame ecef trajectory consists of the point and a s...
std::optional< std::pair< carma_perception_msgs::msg::ExternalObject, double > > get_earliest_collision_object_and_time(const carma_planning_msgs::msg::TrajectoryPlan &original_tp, const std::vector< carma_perception_msgs::msg::ExternalObject > &external_objects)
Return the earliest collision object and time of collision pair from the given trajectory and list of...
std::optional< rclcpp::Time > get_collision_time(const carma_planning_msgs::msg::TrajectoryPlan &original_tp, const carma_perception_msgs::msg::ExternalObject &curr_obstacle, double original_tp_max_speed)
Return collision time given two trajectories with one being external object with predicted steps.
void bsm_cb(const carma_v2x_msgs::msg::BSM::UniquePtr msg)
callback for bsm message
double ecef_traj_timestep_
MobilityResponseCB mobility_response_publisher_
std::string bsmIDtoString(carma_v2x_msgs::msg::BSMCoreData bsm_core)
void set_external_objects(const std::vector< carma_perception_msgs::msg::ExternalObject > &object_list)
Setter for external objects with predictions in the environment.
YieldPlugin(std::shared_ptr< carma_ros2_utils::CarmaLifecycleNode > nh, carma_wm::WorldModelConstPtr wm, YieldPluginConfig config, MobilityResponseCB mobility_response_publisher, LaneChangeStatusCB lc_status_publisher)
Constructor.
std::shared_ptr< carma_ros2_utils::CarmaLifecycleNode > nh_
void set_georeference_string(const std::string &georeference)
Setter for map projection string to define lat/lon -> map conversion.
std::unordered_map< uint32_t, rclcpp::Time > get_collision_times_concurrently(const carma_planning_msgs::msg::TrajectoryPlan &original_tp, const std::vector< carma_perception_msgs::msg::ExternalObject > &external_objects, double original_tp_max_speed)
Given the list of objects with predicted states, get all collision times concurrently using multi-thr...
YieldPluginConfig config_
std::optional< GetCollisionResult > get_collision(const carma_planning_msgs::msg::TrajectoryPlan &trajectory1, const std::vector< carma_perception_msgs::msg::PredictedState > &trajectory2, double collision_radius, double trajectory1_max_speed)
Return naive collision time and locations based on collision radius given two trajectories with one b...
std::vector< std::pair< int, lanelet::BasicPoint2d > > detect_trajectories_intersection(std::vector< lanelet::BasicPoint2d > self_trajectory, std::vector< lanelet::BasicPoint2d > incoming_trajectory) const
detect intersection point(s) of two trajectories
lanelet::BasicPoint2d ecef_to_map_point(const carma_v2x_msgs::msg::LocationECEF &ecef_point) const
convert a point in ecef frame (in cm) into map frame (in meters)
carma_planning_msgs::msg::TrajectoryPlan update_traj_for_object(const carma_planning_msgs::msg::TrajectoryPlan &original_tp, const std::vector< carma_perception_msgs::msg::ExternalObject > &external_objects, double initial_velocity)
trajectory is modified to safely avoid obstacles on the road
carma_planning_msgs::msg::TrajectoryPlan generate_JMT_trajectory(const carma_planning_msgs::msg::TrajectoryPlan &original_tp, double initial_pos, double goal_pos, double initial_velocity, double goal_velocity, double planning_time, double original_max_speed)
generate a Jerk Minimizing Trajectory(JMT) with the provided start and end conditions
carma_v2x_msgs::msg::MobilityResponse compose_mobility_response(const std::string &resp_recipient_id, const std::string &req_plan_id, bool response) const
compose a mobility response message
double req_target_plan_time_
bool cooperative_request_acceptable_
carma_planning_msgs::msg::TrajectoryPlan update_traj_for_cooperative_behavior(const carma_planning_msgs::msg::TrajectoryPlan &original_tp, double current_speed)
update trajectory for yielding to an incoming cooperative behavior
std::unordered_map< uint32_t, int > consecutive_clearance_count_for_obstacles_
std::vector< carma_perception_msgs::msg::ExternalObject > external_objects_
bool is_object_behind_vehicle(uint32_t object_id, const rclcpp::Time &collision_time, double vehicle_point, double object_downtrack)
Check if object location is behind the vehicle using estimates of the vehicle's length and route down...
int timesteps_since_last_req_
double max_trajectory_speed(const std::vector< carma_planning_msgs::msg::TrajectoryPlanPoint > &trajectory_points, double timestamp_in_sec_to_search_until) const
calculates the maximum speed in a set of tajectory points
double polynomial_calc(std::vector< double > coeff, double x) const
calculate quintic polynomial equation for a given x
double polynomial_calc_d(std::vector< double > coeff, double x) const
calculate derivative of quintic polynomial equation for a given x
carma_wm::WorldModelConstPtr wm_
std::vector< double > moving_average_filter(const std::vector< double > input, int window_size, bool ignore_first_point=true)
Extremely simplie moving average filter.
auto to_string(const UtmZone &zone) -> std::string
std::shared_ptr< const WorldModel > WorldModelConstPtr
std::function< void(const carma_planning_msgs::msg::LaneChangeStatus &)> LaneChangeStatusCB
double get_smallest_time_step_of_traj(const carma_planning_msgs::msg::TrajectoryPlan &original_tp)
double get_trajectory_start_time(const carma_planning_msgs::msg::TrajectoryPlan &trajectory)
double get_trajectory_end_time(const carma_planning_msgs::msg::TrajectoryPlan &trajectory)
std::function< void(const carma_v2x_msgs::msg::MobilityResponse &)> MobilityResponseCB
double get_trajectory_duration(const carma_planning_msgs::msg::TrajectoryPlan &trajectory)
Stuct containing the algorithm configuration values for the YieldPluginConfig.
bool always_accept_mobility_request
double max_stop_speed_in_ms
double collision_check_radius_in_m
bool enable_adjustable_gap
double speed_moving_average_window_size
double acceleration_adjustment_factor
int consecutive_clearance_count_for_obstacles_threshold
double yield_max_deceleration_in_ms2
double intervehicle_collision_distance_in_m
double obstacle_zero_speed_threshold_in_ms
bool enable_cooperative_behavior
double min_obj_avoidance_plan_time_in_s
double minimum_safety_gap_in_meters
int acceptable_passed_timesteps
double safety_collision_time_gap_in_s
Convenience class for saving collision results.
lanelet::BasicPoint2d point1
rclcpp::Time collision_time
lanelet::BasicPoint2d point2