23 namespace collision_detection {
26 std::vector<carma_perception_msgs::msg::RoadwayObstacle>
WorldCollisionDetection(
const carma_perception_msgs::msg::RoadwayObstacleList& rwol,
const carma_planning_msgs::msg::TrajectoryPlan& tp,
27 const geometry_msgs::msg::Vector3& size,
const geometry_msgs::msg::Twist& velocity) {
30 RCLCPP_DEBUG_STREAM(rclcpp::get_logger(
"carma_wm::collision_detection"),
"WorldCollisionDetection");
32 std::vector<carma_perception_msgs::msg::RoadwayObstacle> rwo_collison;
35 for (
auto i : rwol.roadway_obstacles) {
37 for (
auto j :
i.object.predictions) {
39 for(
size_t k=0; k < tp.trajectory_points.size(); k++) {
41 RCLCPP_DEBUG_STREAM(rclcpp::get_logger(
"carma_wm::collision_detection"),
"in for loop");
43 double distancex = (tp.trajectory_points[k].x - j.predicted_position.position.x)*(tp.trajectory_points[k].x - j.predicted_position.position.x);
44 double distancey = (tp.trajectory_points[k].y - j.predicted_position.position.y)*(tp.trajectory_points[k].y - j.predicted_position.position.y);
46 RCLCPP_DEBUG_STREAM(rclcpp::get_logger(
"carma_wm::collision_detection"),
"tp.trajectory_points[k].x");
47 RCLCPP_DEBUG_STREAM(rclcpp::get_logger(
"carma_wm::collision_detection"), tp.trajectory_points[k].x);
49 RCLCPP_DEBUG_STREAM(rclcpp::get_logger(
"carma_wm::collision_detection"),
"j.predicted_position.position.x");
50 RCLCPP_DEBUG_STREAM(rclcpp::get_logger(
"carma_wm::collision_detection"), j.predicted_position.position.x);
52 RCLCPP_DEBUG_STREAM(rclcpp::get_logger(
"carma_wm::collision_detection"),
"tp.trajectory_points[k].y");
53 RCLCPP_DEBUG_STREAM(rclcpp::get_logger(
"carma_wm::collision_detection"), tp.trajectory_points[k].y);
55 RCLCPP_DEBUG_STREAM(rclcpp::get_logger(
"carma_wm::collision_detection"),
"j.predicted_position.position.y");
56 RCLCPP_DEBUG_STREAM(rclcpp::get_logger(
"carma_wm::collision_detection"), j.predicted_position.position.y);
59 double calcdistance = sqrt(abs(distancex + distancey));
61 rclcpp::Duration diff= rclcpp::Time(j.header.stamp) - rclcpp::Time(tp.trajectory_points[k].target_time);
63 double timediff = diff.seconds();
65 double x = (
i.object.size.x - size.x)*(
i.object.size.x - size.x);
66 double y = (
i.object.size.y - size.y)*(
i.object.size.y - size.y);
68 double car_t_x = tp.trajectory_points[k].x - tp.trajectory_points[0].x / velocity.linear.x;
71 double object_t_x = j.predicted_position.position.x -
i.object.predictions[0].predicted_position.position.x /
i.object.velocity.twist.linear.x;
76 if(calcdistance <= sqrt(
x -
y) ) {
77 rwo_collison.push_back(
i);
96 mo.
object_polygon = ObjectToBoostPolygon<polygon_t>(rwo.object.pose.pose, rwo.object.size);
98 std::tuple <__uint64_t,polygon_t> current_pose(0 , mo.
object_polygon);
99 mo.
fp.push_back(current_pose);
102 for (
auto i : rwo.object.predictions){
103 std::tuple <__uint64_t,polygon_t> future_object((
i.header.stamp.sec*1e9 +
i.header.stamp.nanosec) / 1000000, ObjectToBoostPolygon<polygon_t>(
i.predicted_position, rwo.object.size));
105 mo.
fp.push_back(future_object);
117 geometry_msgs::msg::Pose pose;
118 pose.position.x = tp.trajectory_points[0].x;
119 pose.position.y = tp.trajectory_points[0].y;
121 Eigen::Vector2d vehicle_vector = {tp.trajectory_points[1].x - tp.trajectory_points[0].x , tp.trajectory_points[1].y - tp.trajectory_points[0].y};
123 Eigen::Vector2d x_axis = {1, 0};
125 double yaw = std::acos(vehicle_vector.dot(x_axis)/(vehicle_vector.norm() * x_axis.norm()));
127 tf2::Quaternion vehicle_orientation;
128 vehicle_orientation.setRPY(0, 0, yaw);
130 pose.orientation.x = vehicle_orientation.getX();
131 pose.orientation.y = vehicle_orientation.getY();
132 pose.orientation.z = vehicle_orientation.getZ();
133 pose.orientation.w = vehicle_orientation.getW();
138 for(
size_t i=1;
i < tp.trajectory_points.size() - 1;
i++){
140 vehicle_vector = {tp.trajectory_points[
i + 1].x - tp.trajectory_points[
i].x , tp.trajectory_points[
i+1].y - tp.trajectory_points[
i].y};
141 yaw = std::acos(vehicle_vector.dot(x_axis)/(vehicle_vector.norm() * x_axis.norm()));
143 tf2::Quaternion orientation;
144 orientation.setRPY(0, 0, yaw);
146 geometry_msgs::msg::Pose trajectory_pose;
147 trajectory_pose.position.x = tp.trajectory_points[
i].x;
148 trajectory_pose.position.y = tp.trajectory_points[
i].y;
150 trajectory_pose.orientation.x = orientation.getX();
151 trajectory_pose.orientation.y = orientation.getY();
152 trajectory_pose.orientation.z = orientation.getZ();
153 trajectory_pose.orientation.w = orientation.getW();
155 std::tuple <__uint64_t,polygon_t> future_object((tp.trajectory_points[
i].target_time.sec*1e9 + tp.trajectory_points[
i].target_time.nanosec),ObjectToBoostPolygon<polygon_t>(trajectory_pose, size));
157 v.
fp.push_back(future_object);
178 std::deque<polygon_t> output;
183 if(output.size() > 0){
192 int union_polygon_size = 0;
193 for (
auto i : op.
fp){
194 if( std::get<0>(
i) <= target_time) {
195 union_polygon_size = union_polygon_size + std::get<1>(
i).outer().size();
199 std::vector<point_t> union_future_polygon_points;
200 union_future_polygon_points.reserve(union_polygon_size);
202 for (
auto i : op.
fp){
203 if( std::get<0>(
i) <= target_time) {
204 union_future_polygon_points.insert( union_future_polygon_points.end(), std::get<1>(
i).outer().begin(), std::get<1>(
i).outer().end());
209 boost::geometry::assign_points(union_polygon, union_future_polygon_points);
212 boost::geometry::convex_hull(union_polygon, hull_polygon);
214 std::vector<std::tuple <__uint64_t, collision_detection::polygon_t>> no_future;
217 return output_object;
223 tf2::Transform object_tf;
224 tf2::fromMsg(pose, object_tf);
226 double half_x_bound = size.x / 2;
227 double half_y_bound = size.y / 2;
230 tf2::Vector3 obj_p1(half_x_bound, half_y_bound, 0);
231 tf2::Vector3 obj_p2(half_x_bound, -half_y_bound, 0);
232 tf2::Vector3 obj_p3(-half_x_bound, -half_y_bound, 0);
233 tf2::Vector3 obj_p4(-half_x_bound, half_y_bound, 0);
235 tf2::Vector3 obj_p1_map = object_tf * obj_p1;
236 tf2::Vector3 obj_p2_map = object_tf * obj_p2;
237 tf2::Vector3 obj_p3_map = object_tf * obj_p3;
238 tf2::Vector3 obj_p4_map = object_tf * obj_p4;
240 point_t p1(obj_p1_map.getX(), obj_p1_map.getY());
241 point_t p2(obj_p2_map.getX(), obj_p2_map.getY());
242 point_t p3(obj_p3_map.getX(), obj_p3_map.getY());
243 point_t p4(obj_p4_map.getX(), obj_p4_map.getY());
246 p.outer().push_back(p1);
247 p.outer().push_back(p2);
248 p.outer().push_back(p3);
249 p.outer().push_back(p4);
bool DetectCollision(collision_detection::MovingObject const &ob_1, collision_detection::MovingObject const &ob_2, __uint64_t target_time)
.function is used in WorldCollisionDetection to detection collision between two Moving Object
boost::geometry::model::polygon< point_t > polygon_t
collision_detection::MovingObject ConvertVehicleToMovingObject(const carma_planning_msgs::msg::TrajectoryPlan &tp, const geometry_msgs::msg::Vector3 &size, const geometry_msgs::msg::Twist &veloctiy)
Creates collision_detection::MovingObject for the host vehicle using the veloctiy,...
boost::geometry::model::point< double, 2, boost::geometry::cs::cartesian > point_t
collision_detection::MovingObject ConvertRoadwayObstacleToMovingObject(const carma_perception_msgs::msg::RoadwayObstacle &rwo)
Convert RodwayObstable object to the collision_detection::MovingObject.
bool CheckPolygonIntersection(collision_detection::MovingObject const &ob_1, collision_detection::MovingObject const &ob_2)
.check Intersection between polygons
collision_detection::MovingObject PredictObjectPosition(collision_detection::MovingObject const &op, __uint64_t target_time)
.function is to create a monving object with a polygon that represents area that object is going to a...
std::vector< carma_perception_msgs::msg::RoadwayObstacle > WorldCollisionDetection(const carma_perception_msgs::msg::RoadwayObstacleList &rwol, const carma_planning_msgs::msg::TrajectoryPlan &tp, const geometry_msgs::msg::Vector3 &size, const geometry_msgs::msg::Twist &velocity)
Main collision detection function to be called when needed to check for collision detection of the ve...
P ObjectToBoostPolygon(const geometry_msgs::msg::Pose &pose, const geometry_msgs::msg::Vector3 &size)
function to create a polygon representing and object
std::vector< std::tuple< __uint64_t, polygon_t > > fp
geometry_msgs::msg::Vector3 linear_velocity