Skip to content

Commit 524443b

Browse files
committed
feat(tier4_autoware_utils): divide into two classies
Signed-off-by: satoshi-ota <satoshi.ota928@gmail.com>
1 parent 17ea748 commit 524443b

File tree

2 files changed

+98
-63
lines changed

2 files changed

+98
-63
lines changed

common/tier4_autoware_utils/include/tier4_autoware_utils/vehicle/vehicle_state_checker.hpp

+26-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2022 Tier IV, Inc.
1+
// Copyright 2022 TIER IV, Inc.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -18,7 +18,6 @@
1818
#include <rclcpp/rclcpp.hpp>
1919

2020
#include <autoware_auto_planning_msgs/msg/trajectory.hpp>
21-
#include <autoware_auto_vehicle_msgs/msg/velocity_report.hpp>
2221
#include <geometry_msgs/msg/twist_stamped.hpp>
2322
#include <nav_msgs/msg/odometry.hpp>
2423

@@ -29,24 +28,42 @@ namespace tier4_autoware_utils
2928
{
3029

3130
using autoware_auto_planning_msgs::msg::Trajectory;
32-
using autoware_auto_vehicle_msgs::msg::VelocityReport;
3331

34-
class VehicleStateChecker
32+
class VehicleStopChecker
3533
{
3634
public:
37-
explicit VehicleStateChecker(rclcpp::Node * node);
35+
explicit VehicleStopChecker(rclcpp::Node * node);
3836

39-
bool checkStopped(const double stop_duration) const;
37+
bool isVehicleStopped(const double stop_duration) const;
4038

41-
bool checkStoppedAtStopPoint(const double stop_duration) const;
39+
rclcpp::Logger getLogger() { return logger_; }
40+
41+
private:
42+
rclcpp::Subscription<nav_msgs::msg::Odometry>::SharedPtr sub_odom_;
43+
44+
rclcpp::Clock::SharedPtr clock_;
45+
rclcpp::Logger logger_;
46+
47+
std::deque<geometry_msgs::msg::TwistStamped> twist_buffer_;
48+
49+
static constexpr double velocity_buffer_time_sec = 10.0;
50+
51+
void onOdom(const nav_msgs::msg::Odometry::SharedPtr msg);
52+
};
53+
54+
class VehicleArrivalChecker
55+
{
56+
public:
57+
explicit VehicleArrivalChecker(rclcpp::Node * node);
58+
59+
bool isVehicleStopped(const double stop_duration) const;
4260

43-
bool checkStoppedWithoutLocalization(const double stop_duration) const;
61+
bool isVehicleStoppedAtStopPoint(const double stop_duration) const;
4462

4563
rclcpp::Logger getLogger() { return logger_; }
4664

4765
private:
4866
rclcpp::Subscription<nav_msgs::msg::Odometry>::SharedPtr sub_odom_;
49-
rclcpp::Subscription<VelocityReport>::SharedPtr sub_velocity_status_;
5067
rclcpp::Subscription<Trajectory>::SharedPtr sub_trajectory_;
5168

5269
rclcpp::Clock::SharedPtr clock_;
@@ -56,13 +73,11 @@ class VehicleStateChecker
5673
Trajectory::SharedPtr trajectory_ptr_;
5774

5875
std::deque<geometry_msgs::msg::TwistStamped> twist_buffer_;
59-
std::deque<VelocityReport> velocity_status_buffer_;
6076

6177
static constexpr double velocity_buffer_time_sec = 10.0;
6278
static constexpr double th_arrived_distance_m = 1.0;
6379

6480
void onOdom(const nav_msgs::msg::Odometry::SharedPtr msg);
65-
void onVelocityStatus(const VelocityReport::SharedPtr msg);
6681
void onTrajectory(const Trajectory::SharedPtr msg);
6782
};
6883
} // namespace tier4_autoware_utils

common/tier4_autoware_utils/src/vehicle/vehicle_state_checker.cpp

+72-52
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2022 Tier IV, Inc.
1+
// Copyright 2022 TIER IV, Inc.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -20,32 +20,29 @@
2020

2121
namespace tier4_autoware_utils
2222
{
23-
VehicleStateChecker::VehicleStateChecker(rclcpp::Node * node)
23+
VehicleStopChecker::VehicleStopChecker(rclcpp::Node * node)
2424
: clock_(node->get_clock()), logger_(node->get_logger())
2525
{
2626
using std::placeholders::_1;
2727

2828
sub_odom_ = node->create_subscription<nav_msgs::msg::Odometry>(
2929
"/localization/kinematic_state", rclcpp::QoS(1),
30-
std::bind(&VehicleStateChecker::onOdom, this, _1));
31-
32-
sub_trajectory_ = node->create_subscription<Trajectory>(
33-
"/planning/scenario_planning/trajectory", rclcpp::QoS(1),
34-
std::bind(&VehicleStateChecker::onTrajectory, this, _1));
35-
36-
sub_velocity_status_ = node->create_subscription<VelocityReport>(
37-
"/vehicle/status/velocity_status", rclcpp::QoS(1),
38-
std::bind(&VehicleStateChecker::onVelocityStatus, this, _1));
30+
std::bind(&VehicleStopChecker::onOdom, this, _1));
3931
}
4032

41-
bool VehicleStateChecker::checkStopped(const double stop_duration = 0.0) const
33+
bool VehicleStopChecker::isVehicleStopped(const double stop_duration = 0.0) const
4234
{
4335
if (twist_buffer_.empty()) {
4436
return false;
4537
}
4638

47-
// Get velocities within stop_duration
4839
const auto now = clock_->now();
40+
const auto time_buffer = now - twist_buffer_.back().header.stamp;
41+
if (time_buffer.seconds() < stop_duration) {
42+
return false;
43+
}
44+
45+
// Get velocities within stop_duration
4946
std::vector<double> vs;
5047
for (const auto & velocity : twist_buffer_) {
5148
vs.push_back(velocity.twist.linear.x);
@@ -67,38 +64,59 @@ bool VehicleStateChecker::checkStopped(const double stop_duration = 0.0) const
6764
return true;
6865
}
6966

70-
bool VehicleStateChecker::checkStoppedAtStopPoint(const double stop_duration = 0.0) const
67+
void VehicleStopChecker::onOdom(const nav_msgs::msg::Odometry::SharedPtr msg)
7168
{
72-
if (!odometry_ptr_ || !trajectory_ptr_) {
73-
return false;
74-
}
69+
auto current_velocity = std::make_shared<geometry_msgs::msg::TwistStamped>();
70+
current_velocity->header = msg->header;
71+
current_velocity->twist = msg->twist.twist;
7572

76-
if (!checkStopped(stop_duration)) {
77-
return false;
78-
}
73+
twist_buffer_.push_front(*current_velocity);
7974

80-
const auto & p = odometry_ptr_->pose.pose.position;
81-
const auto idx = searchZeroVelocityIndex(trajectory_ptr_->points);
75+
const auto now = clock_->now();
76+
while (true) {
77+
// Check oldest data time
78+
const auto time_diff = now - twist_buffer_.back().header.stamp;
8279

83-
if (!idx) {
84-
return false;
80+
// Finish when oldest data is newer than threshold
81+
if (time_diff.seconds() <= velocity_buffer_time_sec) {
82+
break;
83+
}
84+
85+
// Remove old data
86+
twist_buffer_.pop_back();
8587
}
88+
}
8689

87-
return std::abs(calcSignedArcLength(trajectory_ptr_->points, p, idx.get())) <
88-
th_arrived_distance_m;
90+
VehicleArrivalChecker::VehicleArrivalChecker(rclcpp::Node * node)
91+
: clock_(node->get_clock()), logger_(node->get_logger())
92+
{
93+
using std::placeholders::_1;
94+
95+
sub_odom_ = node->create_subscription<nav_msgs::msg::Odometry>(
96+
"/localization/kinematic_state", rclcpp::QoS(1),
97+
std::bind(&VehicleArrivalChecker::onOdom, this, _1));
98+
99+
sub_trajectory_ = node->create_subscription<Trajectory>(
100+
"/planning/scenario_planning/trajectory", rclcpp::QoS(1),
101+
std::bind(&VehicleArrivalChecker::onTrajectory, this, _1));
89102
}
90103

91-
bool VehicleStateChecker::checkStoppedWithoutLocalization(const double stop_duration = 0.0) const
104+
bool VehicleArrivalChecker::isVehicleStopped(const double stop_duration = 0.0) const
92105
{
93-
if (velocity_status_buffer_.empty()) {
106+
if (twist_buffer_.empty()) {
94107
return false;
95108
}
96109

97-
// Get velocities within stop_duration
98110
const auto now = clock_->now();
111+
const auto time_buffer = now - twist_buffer_.back().header.stamp;
112+
if (time_buffer.seconds() < stop_duration) {
113+
return false;
114+
}
115+
116+
// Get velocities within stop_duration
99117
std::vector<double> vs;
100-
for (const auto & velocity : velocity_status_buffer_) {
101-
vs.push_back(velocity.longitudinal_velocity);
118+
for (const auto & velocity : twist_buffer_) {
119+
vs.push_back(velocity.twist.linear.x);
102120

103121
const auto time_diff = now - velocity.header.stamp;
104122
if (time_diff.seconds() >= stop_duration) {
@@ -117,7 +135,28 @@ bool VehicleStateChecker::checkStoppedWithoutLocalization(const double stop_dura
117135
return true;
118136
}
119137

120-
void VehicleStateChecker::onOdom(const nav_msgs::msg::Odometry::SharedPtr msg)
138+
bool VehicleArrivalChecker::isVehicleStoppedAtStopPoint(const double stop_duration = 0.0) const
139+
{
140+
if (!odometry_ptr_ || !trajectory_ptr_) {
141+
return false;
142+
}
143+
144+
if (!isVehicleStopped(stop_duration)) {
145+
return false;
146+
}
147+
148+
const auto & p = odometry_ptr_->pose.pose.position;
149+
const auto idx = searchZeroVelocityIndex(trajectory_ptr_->points);
150+
151+
if (!idx) {
152+
return false;
153+
}
154+
155+
return std::abs(calcSignedArcLength(trajectory_ptr_->points, p, idx.get())) <
156+
th_arrived_distance_m;
157+
}
158+
159+
void VehicleArrivalChecker::onOdom(const nav_msgs::msg::Odometry::SharedPtr msg)
121160
{
122161
odometry_ptr_ = msg;
123162

@@ -142,24 +181,5 @@ void VehicleStateChecker::onOdom(const nav_msgs::msg::Odometry::SharedPtr msg)
142181
}
143182
}
144183

145-
void VehicleStateChecker::onVelocityStatus(const VelocityReport::SharedPtr msg)
146-
{
147-
velocity_status_buffer_.push_front(*msg);
148-
149-
const auto now = clock_->now();
150-
while (true) {
151-
// Check oldest data time
152-
const auto time_diff = now - velocity_status_buffer_.back().header.stamp;
153-
154-
// Finish when oldest data is newer than threshold
155-
if (time_diff.seconds() <= velocity_buffer_time_sec) {
156-
break;
157-
}
158-
159-
// Remove old data
160-
velocity_status_buffer_.pop_back();
161-
}
162-
}
163-
164-
void VehicleStateChecker::onTrajectory(const Trajectory::SharedPtr msg) { trajectory_ptr_ = msg; }
184+
void VehicleArrivalChecker::onTrajectory(const Trajectory::SharedPtr msg) { trajectory_ptr_ = msg; }
165185
} // namespace tier4_autoware_utils

0 commit comments

Comments
 (0)