|
| 1 | +// Copyright 2022 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 "tier4_autoware_utils/vehicle/vehicle_state_checker.hpp" |
| 16 | + |
| 17 | +#include "tier4_autoware_utils/trajectory/trajectory.hpp" |
| 18 | + |
| 19 | +#include <string> |
| 20 | + |
| 21 | +namespace tier4_autoware_utils |
| 22 | +{ |
| 23 | +VehicleStopChecker::VehicleStopChecker(rclcpp::Node * node) |
| 24 | +: clock_(node->get_clock()), logger_(node->get_logger()) |
| 25 | +{ |
| 26 | + using std::placeholders::_1; |
| 27 | + |
| 28 | + sub_odom_ = node->create_subscription<Odometry>( |
| 29 | + "/localization/kinematic_state", rclcpp::QoS(1), |
| 30 | + std::bind(&VehicleStopChecker::onOdom, this, _1)); |
| 31 | +} |
| 32 | + |
| 33 | +bool VehicleStopChecker::isVehicleStopped(const double stop_duration = 0.0) const |
| 34 | +{ |
| 35 | + if (twist_buffer_.empty()) { |
| 36 | + return false; |
| 37 | + } |
| 38 | + |
| 39 | + constexpr double stop_velocity = 1e-3; |
| 40 | + const auto now = clock_->now(); |
| 41 | + |
| 42 | + const auto time_buffer_back = now - twist_buffer_.back().header.stamp; |
| 43 | + if (time_buffer_back.seconds() < stop_duration) { |
| 44 | + return false; |
| 45 | + } |
| 46 | + |
| 47 | + // Get velocities within stop_duration |
| 48 | + for (const auto & velocity : twist_buffer_) { |
| 49 | + if (stop_velocity <= velocity.twist.linear.x) { |
| 50 | + return false; |
| 51 | + } |
| 52 | + |
| 53 | + const auto time_diff = now - velocity.header.stamp; |
| 54 | + if (time_diff.seconds() >= stop_duration) { |
| 55 | + break; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + return true; |
| 60 | +} |
| 61 | + |
| 62 | +void VehicleStopChecker::onOdom(const Odometry::SharedPtr msg) |
| 63 | +{ |
| 64 | + odometry_ptr_ = msg; |
| 65 | + |
| 66 | + auto current_velocity = std::make_shared<TwistStamped>(); |
| 67 | + current_velocity->header = msg->header; |
| 68 | + current_velocity->twist = msg->twist.twist; |
| 69 | + |
| 70 | + twist_buffer_.push_front(*current_velocity); |
| 71 | + |
| 72 | + const auto now = clock_->now(); |
| 73 | + while (true) { |
| 74 | + // Check oldest data time |
| 75 | + const auto time_diff = now - twist_buffer_.back().header.stamp; |
| 76 | + |
| 77 | + // Finish when oldest data is newer than threshold |
| 78 | + if (time_diff.seconds() <= velocity_buffer_time_sec) { |
| 79 | + break; |
| 80 | + } |
| 81 | + |
| 82 | + // Remove old data |
| 83 | + twist_buffer_.pop_back(); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +VehicleArrivalChecker::VehicleArrivalChecker(rclcpp::Node * node) : VehicleStopChecker(node) |
| 88 | +{ |
| 89 | + using std::placeholders::_1; |
| 90 | + |
| 91 | + sub_trajectory_ = node->create_subscription<Trajectory>( |
| 92 | + "/planning/scenario_planning/trajectory", rclcpp::QoS(1), |
| 93 | + std::bind(&VehicleArrivalChecker::onTrajectory, this, _1)); |
| 94 | +} |
| 95 | + |
| 96 | +bool VehicleArrivalChecker::isVehicleStoppedAtStopPoint(const double stop_duration = 0.0) const |
| 97 | +{ |
| 98 | + if (!odometry_ptr_ || !trajectory_ptr_) { |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + if (!isVehicleStopped(stop_duration)) { |
| 103 | + return false; |
| 104 | + } |
| 105 | + |
| 106 | + const auto & p = odometry_ptr_->pose.pose.position; |
| 107 | + const auto idx = searchZeroVelocityIndex(trajectory_ptr_->points); |
| 108 | + |
| 109 | + if (!idx) { |
| 110 | + return false; |
| 111 | + } |
| 112 | + |
| 113 | + return std::abs(calcSignedArcLength(trajectory_ptr_->points, p, idx.get())) < |
| 114 | + th_arrived_distance_m; |
| 115 | +} |
| 116 | + |
| 117 | +void VehicleArrivalChecker::onTrajectory(const Trajectory::SharedPtr msg) { trajectory_ptr_ = msg; } |
| 118 | +} // namespace tier4_autoware_utils |
0 commit comments