|
| 1 | +// Copyright 2021 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 <hazard_lights_selector/hazard_lights_selector_node.hpp> |
| 16 | + |
| 17 | +namespace hazard_lights_selector |
| 18 | +{ |
| 19 | +HazardLightsSelectorNode::HazardLightsSelectorNode(const rclcpp::NodeOptions & node_options) |
| 20 | +: Node("hazard_lights_selector", node_options) |
| 21 | +{ |
| 22 | + using std::placeholders::_1; |
| 23 | + |
| 24 | + // Parameter |
| 25 | + params_.update_rate = static_cast<int>(declare_parameter<int>("update_rate", 10)); |
| 26 | + |
| 27 | + // Subscriber |
| 28 | + sub_hazard_lights_cmd_from_path_planner_ = this->create_subscription<HazardLightsCommand>( |
| 29 | + "~/input/behavior_path_planner/hazard_lights_cmd", 1, |
| 30 | + std::bind(&HazardLightsSelectorNode::onHazardLightsCommandFromPathPlanner, this, _1)); |
| 31 | + sub_hazard_lights_cmd_from_mrm_operator_ = this->create_subscription<HazardLightsCommand>( |
| 32 | + "~/input/behavior_mrm_operator/hazard_lights_cmd", 1, |
| 33 | + std::bind(&HazardLightsSelectorNode::onHazardLightsCommandFromMrmOperator, this, _1)); |
| 34 | + |
| 35 | + // Publisher |
| 36 | + pub_hazard_lights_cmd_ = |
| 37 | + this->create_publisher<HazardLightsCommand>("~/output/hazard_lights_cmd", 1); |
| 38 | + |
| 39 | + // Timer |
| 40 | + const auto update_period_ns = rclcpp::Rate(params_.update_rate).period(); |
| 41 | + timer_ = rclcpp::create_timer( |
| 42 | + this, get_clock(), update_period_ns, std::bind(&HazardLightsSelectorNode::onTimer, this)); |
| 43 | +} |
| 44 | + |
| 45 | +void HazardLightsSelectorNode::onHazardLightsCommandFromPathPlanner( |
| 46 | + HazardLightsCommand::ConstSharedPtr msg) |
| 47 | +{ |
| 48 | + hazard_lights_command_from_path_planner_ = msg; |
| 49 | +} |
| 50 | + |
| 51 | +void HazardLightsSelectorNode::onHazardLightsCommandFromMrmOperator( |
| 52 | + HazardLightsCommand::ConstSharedPtr msg) |
| 53 | +{ |
| 54 | + hazard_lights_command_from_mrm_operator_ = msg; |
| 55 | +} |
| 56 | + |
| 57 | +void HazardLightsSelectorNode::onTimer() |
| 58 | +{ |
| 59 | + auto hazard_lights_cmd = HazardLightsCommand(); |
| 60 | + hazard_lights_cmd.stamp = this->now(); |
| 61 | + hazard_lights_cmd.command = HazardLightsCommand::DISABLE; |
| 62 | + |
| 63 | + if (hazard_lights_command_from_path_planner_ != nullptr) { |
| 64 | + if (hazard_lights_command_from_path_planner_->command == HazardLightsCommand::ENABLE) { |
| 65 | + hazard_lights_cmd.command = HazardLightsCommand::ENABLE; |
| 66 | + } |
| 67 | + } |
| 68 | + if (hazard_lights_command_from_mrm_operator_ != nullptr) { |
| 69 | + if (hazard_lights_command_from_mrm_operator_->command == HazardLightsCommand::ENABLE) { |
| 70 | + hazard_lights_cmd.command = HazardLightsCommand::ENABLE; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + pub_hazard_lights_cmd_->publish(hazard_lights_cmd); |
| 75 | +} |
| 76 | + |
| 77 | +} // namespace hazard_lights_selector |
| 78 | + |
| 79 | +#include "rclcpp_components/register_node_macro.hpp" |
| 80 | +RCLCPP_COMPONENTS_REGISTER_NODE(hazard_lights_selector::HazardLightsSelectorNode) |
0 commit comments