-
Notifications
You must be signed in to change notification settings - Fork 682
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tier4_autoware_utils): divide into two classies
Signed-off-by: satoshi-ota <satoshi.ota928@gmail.com>
- Loading branch information
1 parent
17ea748
commit bebacf5
Showing
5 changed files
with
154 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
common/tier4_autoware_utils/include/tier4_autoware_utils/vehicle/vehicle_stop_checker.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright 2022 TIER IV, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef TIER4_AUTOWARE_UTILS__VEHICLE__VEHICLE_STOP_CHECKER_HPP_ | ||
#define TIER4_AUTOWARE_UTILS__VEHICLE__VEHICLE_STOP_CHECKER_HPP_ | ||
|
||
#include <rclcpp/rclcpp.hpp> | ||
|
||
#include <geometry_msgs/msg/twist_stamped.hpp> | ||
#include <nav_msgs/msg/odometry.hpp> | ||
|
||
#include <deque> | ||
#include <memory> | ||
|
||
namespace tier4_autoware_utils | ||
{ | ||
|
||
class VehicleStopChecker | ||
{ | ||
public: | ||
explicit VehicleStopChecker(rclcpp::Node * node); | ||
|
||
bool isVehicleStopped(const double stop_duration) const; | ||
|
||
rclcpp::Logger getLogger() { return logger_; } | ||
|
||
private: | ||
rclcpp::Subscription<nav_msgs::msg::Odometry>::SharedPtr sub_odom_; | ||
|
||
rclcpp::Clock::SharedPtr clock_; | ||
rclcpp::Logger logger_; | ||
|
||
std::deque<geometry_msgs::msg::TwistStamped> twist_buffer_; | ||
|
||
static constexpr double velocity_buffer_time_sec = 10.0; | ||
|
||
void onOdom(const nav_msgs::msg::Odometry::SharedPtr msg); | ||
}; | ||
} // namespace tier4_autoware_utils | ||
|
||
#endif // TIER4_AUTOWARE_UTILS__VEHICLE__VEHICLE_STOP_CHECKER_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
common/tier4_autoware_utils/src/vehicle/vehicle_stop_checker.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright 2022 TIER IV, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "tier4_autoware_utils/vehicle/vehicle_stop_checker.hpp" | ||
|
||
#include <string> | ||
|
||
namespace tier4_autoware_utils | ||
{ | ||
VehicleStopChecker::VehicleStopChecker(rclcpp::Node * node) | ||
: clock_(node->get_clock()), logger_(node->get_logger()) | ||
{ | ||
using std::placeholders::_1; | ||
|
||
sub_odom_ = node->create_subscription<nav_msgs::msg::Odometry>( | ||
"/localization/kinematic_state", rclcpp::QoS(1), | ||
std::bind(&VehicleStopChecker::onOdom, this, _1)); | ||
} | ||
|
||
bool VehicleStopChecker::isVehicleStopped(const double stop_duration = 0.0) const | ||
{ | ||
if (twist_buffer_.empty()) { | ||
return false; | ||
} | ||
|
||
// Get velocities within stop_duration | ||
const auto now = clock_->now(); | ||
std::vector<double> vs; | ||
for (const auto & velocity : twist_buffer_) { | ||
vs.push_back(velocity.twist.linear.x); | ||
|
||
const auto time_diff = now - velocity.header.stamp; | ||
if (time_diff.seconds() >= stop_duration) { | ||
break; | ||
} | ||
} | ||
|
||
// Check all velocities | ||
constexpr double stop_velocity = 1e-3; | ||
for (const auto & v : vs) { | ||
if (v >= stop_velocity) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
void VehicleStopChecker::onOdom(const nav_msgs::msg::Odometry::SharedPtr msg) | ||
{ | ||
auto current_velocity = std::make_shared<geometry_msgs::msg::TwistStamped>(); | ||
current_velocity->header = msg->header; | ||
current_velocity->twist = msg->twist.twist; | ||
|
||
twist_buffer_.push_front(*current_velocity); | ||
|
||
const auto now = clock_->now(); | ||
while (true) { | ||
// Check oldest data time | ||
const auto time_diff = now - twist_buffer_.back().header.stamp; | ||
|
||
// Finish when oldest data is newer than threshold | ||
if (time_diff.seconds() <= velocity_buffer_time_sec) { | ||
break; | ||
} | ||
|
||
// Remove old data | ||
twist_buffer_.pop_back(); | ||
} | ||
} | ||
} // namespace tier4_autoware_utils |