|
| 1 | +// Copyright 2024 TIER IV, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "static_centerline_optimizer/centerline_source/bag_ego_trajectory_based_centerline.hpp" |
| 16 | + |
| 17 | +#include "rclcpp/serialization.hpp" |
| 18 | +#include "rosbag2_cpp/reader.hpp" |
| 19 | +#include "static_centerline_optimizer/static_centerline_optimizer_node.hpp" |
| 20 | + |
| 21 | +#include <nav_msgs/msg/odometry.hpp> |
| 22 | + |
| 23 | +namespace static_centerline_optimizer |
| 24 | +{ |
| 25 | +std::vector<TrajectoryPoint> generate_centerline_with_bag(rclcpp::Node & node) |
| 26 | +{ |
| 27 | + const auto bag_filename = node.declare_parameter<std::string>("bag_filename"); |
| 28 | + |
| 29 | + rosbag2_cpp::Reader bag_reader; |
| 30 | + bag_reader.open(bag_filename); |
| 31 | + |
| 32 | + rclcpp::Serialization<nav_msgs::msg::Odometry> bag_serialization; |
| 33 | + std::vector<TrajectoryPoint> centerline_traj_points; |
| 34 | + while (bag_reader.has_next()) { |
| 35 | + const rosbag2_storage::SerializedBagMessageSharedPtr msg = bag_reader.read_next(); |
| 36 | + |
| 37 | + if (msg->topic_name != "/localization/kinematic_state") { |
| 38 | + continue; |
| 39 | + } |
| 40 | + |
| 41 | + rclcpp::SerializedMessage serialized_msg(*msg->serialized_data); |
| 42 | + const auto ros_msg = std::make_shared<nav_msgs::msg::Odometry>(); |
| 43 | + |
| 44 | + bag_serialization.deserialize_message(&serialized_msg, ros_msg.get()); |
| 45 | + |
| 46 | + if (!centerline_traj_points.empty()) { |
| 47 | + constexpr double epsilon = 1e-1; |
| 48 | + if ( |
| 49 | + std::abs(centerline_traj_points.back().pose.position.x - ros_msg->pose.pose.position.x) < |
| 50 | + epsilon && |
| 51 | + std::abs(centerline_traj_points.back().pose.position.y - ros_msg->pose.pose.position.y) < |
| 52 | + epsilon) { |
| 53 | + continue; |
| 54 | + } |
| 55 | + } |
| 56 | + TrajectoryPoint centerline_traj_point; |
| 57 | + centerline_traj_point.pose.position = ros_msg->pose.pose.position; |
| 58 | + // std::cerr << centerline_traj_point.pose.position.x << " " |
| 59 | + // << centerline_traj_point.pose.position.y << std::endl; |
| 60 | + centerline_traj_points.push_back(centerline_traj_point); |
| 61 | + } |
| 62 | + |
| 63 | + RCLCPP_INFO(node.get_logger(), "Extracted centerline from the bag."); |
| 64 | + |
| 65 | + // calculate rough orientation of centerline trajectory points |
| 66 | + for (size_t i = 0; i < centerline_traj_points.size(); ++i) { |
| 67 | + if (i == centerline_traj_points.size() - 1) { |
| 68 | + if (i != 0) { |
| 69 | + centerline_traj_points.at(i).pose.orientation = |
| 70 | + centerline_traj_points.at(i - 1).pose.orientation; |
| 71 | + } |
| 72 | + } else { |
| 73 | + const double yaw_angle = tier4_autoware_utils::calcAzimuthAngle( |
| 74 | + centerline_traj_points.at(i).pose.position, centerline_traj_points.at(i + 1).pose.position); |
| 75 | + centerline_traj_points.at(i).pose.orientation = |
| 76 | + tier4_autoware_utils::createQuaternionFromYaw(yaw_angle); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return centerline_traj_points; |
| 81 | +} |
| 82 | +} // namespace static_centerline_optimizer |
0 commit comments