|
| 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, WITHOUT WARRANTIES OR |
| 11 | +// CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language |
| 12 | +// governing permissions and limitations under the License. |
| 13 | + |
| 14 | +#include "control_cmd_switcher.hpp" |
| 15 | + |
| 16 | +#include <chrono> |
| 17 | +#include <memory> |
| 18 | +#include <string> |
| 19 | +#include <utility> |
| 20 | + |
| 21 | +ControlCmdSwitcher::ControlCmdSwitcher(const rclcpp::NodeOptions & node_options) |
| 22 | +: Node("control_cmd_switcher", node_options) |
| 23 | +{ |
| 24 | + // Subscriber |
| 25 | + sub_main_control_cmd_ = |
| 26 | + create_subscription<autoware_auto_control_msgs::msg::AckermannControlCommand>( |
| 27 | + "~/input/main/control_cmd", rclcpp::QoS{10}, |
| 28 | + std::bind(&ControlCmdSwitcher::onMainControlCmd, this, std::placeholders::_1)); |
| 29 | + |
| 30 | + sub_sub_control_cmd_ = |
| 31 | + create_subscription<autoware_auto_control_msgs::msg::AckermannControlCommand>( |
| 32 | + "~/input/sub/control_cmd", rclcpp::QoS{10}, |
| 33 | + std::bind(&ControlCmdSwitcher::onSubControlCmd, this, std::placeholders::_1)); |
| 34 | + |
| 35 | + sub_election_status_main_ = create_subscription<tier4_system_msgs::msg::ElectionStatus>( |
| 36 | + "~/input/election/status/main", rclcpp::QoS{10}, |
| 37 | + std::bind(&ControlCmdSwitcher::onElectionStatus, this, std::placeholders::_1)); |
| 38 | + |
| 39 | + sub_election_status_sub_ = create_subscription<tier4_system_msgs::msg::ElectionStatus>( |
| 40 | + "~/input/election/status/sub", rclcpp::QoS{10}, |
| 41 | + std::bind(&ControlCmdSwitcher::onElectionStatus, this, std::placeholders::_1)); |
| 42 | + |
| 43 | + // Publisher |
| 44 | + pub_control_cmd_ = create_publisher<autoware_auto_control_msgs::msg::AckermannControlCommand>( |
| 45 | + "~/output/control_cmd", rclcpp::QoS{1}); |
| 46 | + |
| 47 | + // Initialize |
| 48 | + use_main_control_cmd_ = true; |
| 49 | +} |
| 50 | + |
| 51 | +void ControlCmdSwitcher::onMainControlCmd( |
| 52 | + const autoware_auto_control_msgs::msg::AckermannControlCommand::ConstSharedPtr msg) |
| 53 | +{ |
| 54 | + if (use_main_control_cmd_) { |
| 55 | + pub_control_cmd_->publish(*msg); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +void ControlCmdSwitcher::onSubControlCmd( |
| 60 | + const autoware_auto_control_msgs::msg::AckermannControlCommand::ConstSharedPtr msg) |
| 61 | +{ |
| 62 | + if (!use_main_control_cmd_) { |
| 63 | + pub_control_cmd_->publish(*msg); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +void ControlCmdSwitcher::onElectionStatus( |
| 68 | + const tier4_system_msgs::msg::ElectionStatus::ConstSharedPtr msg) |
| 69 | +{ |
| 70 | + if (msg->election_start_count <= 0) return; |
| 71 | + if (msg->in_election) return; |
| 72 | + if (((msg->path_info >> 3) & 0x01) == 1) { |
| 73 | + use_main_control_cmd_ = true; |
| 74 | + } else if (((msg->path_info >> 2) & 0x01) == 1) { |
| 75 | + use_main_control_cmd_ = false; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +#include <rclcpp_components/register_node_macro.hpp> |
| 80 | +RCLCPP_COMPONENTS_REGISTER_NODE(ControlCmdSwitcher) |
0 commit comments