|
| 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 "mrm_stop_operator.hpp" |
| 16 | + |
| 17 | +namespace mrm_stop_operator |
| 18 | +{ |
| 19 | + |
| 20 | +MrmStopOperator::MrmStopOperator(const rclcpp::NodeOptions & node_options) |
| 21 | +: Node("mrm_stop_operator", node_options) |
| 22 | +{ |
| 23 | + // Parameter |
| 24 | + params_.min_acceleration = declare_parameter<double>("min_acceleration", -4.0); |
| 25 | + params_.max_jerk = declare_parameter<double>("max_jerk", 5.0); |
| 26 | + params_.min_jerk = declare_parameter<double>("min_jerk", -5.0); |
| 27 | + |
| 28 | + // Subscriber |
| 29 | + sub_mrm_request_ = create_subscription<tier4_system_msgs::msg::MrmBehavior>( |
| 30 | + "~/input/mrm_request", 10, |
| 31 | + std::bind(&MrmStopOperator::onMrmRequest, this, std::placeholders::_1)); |
| 32 | + |
| 33 | + sub_velocity_group_ = create_callback_group(rclcpp::CallbackGroupType::MutuallyExclusive, false); |
| 34 | + rclcpp::SubscriptionOptions velocity_options = rclcpp::SubscriptionOptions(); |
| 35 | + velocity_options.callback_group = sub_velocity_group_; |
| 36 | + auto not_executed_callback = []([[maybe_unused]] const typename autoware_vehicle_msgs::msg:: |
| 37 | + VelocityReport::ConstSharedPtr msg) {}; |
| 38 | + sub_velocity_ = create_subscription<autoware_vehicle_msgs::msg::VelocityReport>( |
| 39 | + "~/input/velocity", 10, not_executed_callback, velocity_options); |
| 40 | + |
| 41 | + // Publisher |
| 42 | + pub_velocity_limit_ = create_publisher<tier4_planning_msgs::msg::VelocityLimit>( |
| 43 | + "~/output/velocity_limit", rclcpp::QoS{10}.transient_local()); |
| 44 | + pub_velocity_limit_clear_command_ = |
| 45 | + create_publisher<tier4_planning_msgs::msg::VelocityLimitClearCommand>( |
| 46 | + "~/output/velocity_limit_clear_command", rclcpp::QoS{10}.transient_local()); |
| 47 | + pub_mrm_state_ = |
| 48 | + create_publisher<autoware_adapi_v1_msgs::msg::MrmState>("~/output/mrm_state", rclcpp::QoS{1}); |
| 49 | + |
| 50 | + // Timer |
| 51 | + const auto update_period_ns = rclcpp::Rate(10).period(); |
| 52 | + timer_ = rclcpp::create_timer( |
| 53 | + this, get_clock(), update_period_ns, std::bind(&MrmStopOperator::onTimer, this)); |
| 54 | + |
| 55 | + // Service |
| 56 | + |
| 57 | + // Client |
| 58 | + |
| 59 | + // Timer |
| 60 | + |
| 61 | + // State |
| 62 | + initState(); |
| 63 | + |
| 64 | + // Diagnostics |
| 65 | +} |
| 66 | + |
| 67 | +void MrmStopOperator::onMrmRequest(const tier4_system_msgs::msg::MrmBehavior::ConstSharedPtr msg) |
| 68 | +{ |
| 69 | + if ( |
| 70 | + msg->type == tier4_system_msgs::msg::MrmBehavior::COMFORTABLE_STOP && |
| 71 | + last_mrm_request_.type != tier4_system_msgs::msg::MrmBehavior::COMFORTABLE_STOP) { |
| 72 | + tier4_planning_msgs::msg::VelocityLimit velocity_limit; |
| 73 | + velocity_limit.stamp = this->now(); |
| 74 | + velocity_limit.max_velocity = 0.0; |
| 75 | + velocity_limit.use_constraints = true; |
| 76 | + velocity_limit.constraints.min_acceleration = params_.min_acceleration; |
| 77 | + velocity_limit.constraints.max_jerk = params_.max_jerk; |
| 78 | + velocity_limit.constraints.min_jerk = params_.min_jerk; |
| 79 | + velocity_limit.sender = "mrm_stop_operator"; |
| 80 | + pub_velocity_limit_->publish(velocity_limit); |
| 81 | + |
| 82 | + last_mrm_request_ = *msg; |
| 83 | + current_mrm_state_.behavior = msg->type; |
| 84 | + current_mrm_state_.state = autoware_adapi_v1_msgs::msg::MrmState::MRM_OPERATING; |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +void MrmStopOperator::initState() |
| 89 | +{ |
| 90 | + last_mrm_request_.type = tier4_system_msgs::msg::MrmBehavior::NONE; |
| 91 | + current_mrm_state_.state = autoware_adapi_v1_msgs::msg::MrmState::NORMAL; |
| 92 | + current_mrm_state_.behavior = autoware_adapi_v1_msgs::msg::MrmState::NONE; |
| 93 | +} |
| 94 | + |
| 95 | +void MrmStopOperator::onTimer() |
| 96 | +{ |
| 97 | + if (current_mrm_state_.state == autoware_adapi_v1_msgs::msg::MrmState::MRM_OPERATING) { |
| 98 | + if (current_mrm_state_.behavior == autoware_adapi_v1_msgs::msg::MrmState::COMFORTABLE_STOP) { |
| 99 | + if (isStopped()) { |
| 100 | + current_mrm_state_.state = autoware_adapi_v1_msgs::msg::MrmState::MRM_SUCCEEDED; |
| 101 | + } else { |
| 102 | + // nothing to do |
| 103 | + } |
| 104 | + } else { |
| 105 | + // TODO |
| 106 | + } |
| 107 | + } |
| 108 | + current_mrm_state_.stamp = this->now(); |
| 109 | + pub_mrm_state_->publish(current_mrm_state_); |
| 110 | +} |
| 111 | + |
| 112 | +bool MrmStopOperator::isStopped() |
| 113 | +{ |
| 114 | + constexpr auto th_stopped_velocity = 0.001; |
| 115 | + auto current_velocity = std::make_shared<autoware_vehicle_msgs::msg::VelocityReport>(); |
| 116 | + rclcpp::MessageInfo message_info; |
| 117 | + |
| 118 | + const bool success = sub_velocity_->take(*current_velocity, message_info); |
| 119 | + if (success) { |
| 120 | + return current_velocity->longitudinal_velocity < th_stopped_velocity; |
| 121 | + } else { |
| 122 | + return false; |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +} // namespace mrm_stop_operator |
| 127 | + |
| 128 | +#include <rclcpp_components/register_node_macro.hpp> |
| 129 | +RCLCPP_COMPONENTS_REGISTER_NODE(mrm_stop_operator::MrmStopOperator) |
0 commit comments