|
| 1 | +// Copyright 2025, Evan Palmer |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +// of this software and associated documentation files (the "Software"), to deal |
| 5 | +// in the Software without restriction, including without limitation the rights |
| 6 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +// copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in all |
| 11 | +// copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 | +// SOFTWARE. |
| 20 | + |
| 21 | +#include "coordinator.hpp" |
| 22 | + |
| 23 | +#include <ranges> |
| 24 | + |
| 25 | +#include "lifecycle_msgs/msg/state.hpp" |
| 26 | + |
| 27 | +namespace coordinator |
| 28 | +{ |
| 29 | + |
| 30 | +ControllerCoordinator::ControllerCoordinator() |
| 31 | +: rclcpp::Node("controller_coordinator"), |
| 32 | + activate_hardware_request_(std::make_shared<controller_manager_msgs::srv::SetHardwareComponentState::Request>()), |
| 33 | + deactivate_hardware_request_(std::make_shared<controller_manager_msgs::srv::SetHardwareComponentState::Request>()), |
| 34 | + activate_controllers_request_(std::make_shared<controller_manager_msgs::srv::SwitchController::Request>()), |
| 35 | + deactivate_controllers_request_(std::make_shared<controller_manager_msgs::srv::SwitchController::Request>()) |
| 36 | +{ |
| 37 | + param_listener_ = std::make_shared<controller_coordinator::ParamListener>(this->get_node_parameters_interface()); |
| 38 | + params_ = param_listener_->get_params(); |
| 39 | + |
| 40 | + client_callback_group_ = this->create_callback_group(rclcpp::CallbackGroupType::Reentrant, true); |
| 41 | + |
| 42 | + // helper function used to wait for services to come up |
| 43 | + // this will block indefinitely |
| 44 | + auto wait_for_service = [this](const auto & client, const std::string & service_name) { |
| 45 | + while (!client->wait_for_service(std::chrono::seconds(1))) { |
| 46 | + RCLCPP_INFO(this->get_logger(), "Waiting for %s service to come up", service_name.c_str()); // NOLINT |
| 47 | + } |
| 48 | + RCLCPP_INFO(this->get_logger(), "%s service available", service_name.c_str()); // NOLINT |
| 49 | + }; |
| 50 | + |
| 51 | + // create clients |
| 52 | + const std::string hardware_service = "controller_manager/set_hardware_component_state"; |
| 53 | + hardware_client_ = this->create_client<controller_manager_msgs::srv::SetHardwareComponentState>( |
| 54 | + hardware_service, rclcpp::ServicesQoS(), client_callback_group_); |
| 55 | + wait_for_service(hardware_client_, hardware_service); |
| 56 | + |
| 57 | + const std::string switch_controller_name = "controller_manager/switch_controller"; |
| 58 | + switch_controller_client_ = this->create_client<controller_manager_msgs::srv::SwitchController>( |
| 59 | + switch_controller_name, rclcpp::ServicesQoS(), client_callback_group_); |
| 60 | + wait_for_service(switch_controller_client_, switch_controller_name); |
| 61 | + |
| 62 | + // pre-configure the hardware activation/deactivation requests |
| 63 | + activate_hardware_request_->name = params_.hardware_interface; |
| 64 | + activate_hardware_request_->target_state.id = lifecycle_msgs::msg::State::PRIMARY_STATE_ACTIVE; |
| 65 | + |
| 66 | + deactivate_hardware_request_->name = params_.hardware_interface; |
| 67 | + deactivate_hardware_request_->target_state.id = lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE; |
| 68 | + |
| 69 | + // pre-configure the controller activation/deactivation requests |
| 70 | + activate_controllers_request_->activate_controllers = params_.controller_sequence; |
| 71 | + activate_controllers_request_->strictness = controller_manager_msgs::srv::SwitchController::Request::STRICT; |
| 72 | + activate_controllers_request_->activate_asap = true; |
| 73 | + activate_controllers_request_->timeout = rclcpp::Duration::from_seconds(params_.timeout); |
| 74 | + |
| 75 | + deactivate_controllers_request_->deactivate_controllers = params_.controller_sequence; |
| 76 | + deactivate_controllers_request_->strictness = controller_manager_msgs::srv::SwitchController::Request::STRICT; |
| 77 | + deactivate_controllers_request_->activate_asap = true; |
| 78 | + deactivate_controllers_request_->timeout = rclcpp::Duration::from_seconds(params_.timeout); |
| 79 | + |
| 80 | + // create a service endpoint for users to activate or deactivate their system |
| 81 | + service_callback_group_ = this->create_callback_group(rclcpp::CallbackGroupType::Reentrant, true); |
| 82 | + activate_system_service_ = this->create_service<std_srvs::srv::SetBool>( |
| 83 | + "~/activate", |
| 84 | + [this]( |
| 85 | + const std::shared_ptr<rmw_request_id_t> /*request_header*/, |
| 86 | + const std::shared_ptr<std_srvs::srv::SetBool::Request> request, |
| 87 | + const std::shared_ptr<std_srvs::srv::SetBool::Response> response) { |
| 88 | + response->success = true; |
| 89 | + if (request->data) { |
| 90 | + RCLCPP_INFO(this->get_logger(), "Activating thruster hardware interface and controllers"); // NOLINT |
| 91 | + |
| 92 | + // activate the hardware interface |
| 93 | + hardware_client_->async_send_request( |
| 94 | + activate_hardware_request_, |
| 95 | + [this, &response]( |
| 96 | + rclcpp::Client<controller_manager_msgs::srv::SetHardwareComponentState>::SharedFuture result_response) { |
| 97 | + const auto & result = result_response.get(); |
| 98 | + if (result->ok) { |
| 99 | + RCLCPP_INFO(this->get_logger(), "Successfully activated thruster hardware interface"); // NOLINT |
| 100 | + } else { |
| 101 | + RCLCPP_ERROR(this->get_logger(), "Failed to activate thruster hardware interface"); // NOLINT |
| 102 | + response->success = false; |
| 103 | + response->message = "Failed to activate thruster hardware interface"; |
| 104 | + } |
| 105 | + }); |
| 106 | + |
| 107 | + // activate the controllers |
| 108 | + switch_controller_client_->async_send_request( |
| 109 | + activate_controllers_request_, |
| 110 | + [this, |
| 111 | + &response](rclcpp::Client<controller_manager_msgs::srv::SwitchController>::SharedFuture result_response) { |
| 112 | + const auto & result = result_response.get(); |
| 113 | + if (result->ok) { |
| 114 | + RCLCPP_INFO(this->get_logger(), "Successfully activated controllers"); // NOLINT |
| 115 | + } else { |
| 116 | + RCLCPP_ERROR(this->get_logger(), "Failed to activate controllers"); // NOLINT |
| 117 | + response->success = false; |
| 118 | + response->message = "Failed to activate controllers"; |
| 119 | + } |
| 120 | + }); |
| 121 | + } else { |
| 122 | + RCLCPP_INFO(this->get_logger(), "Deactivating controllers and thruster hardware interface"); // NOLINT |
| 123 | + |
| 124 | + // deactivate the hardware interface |
| 125 | + hardware_client_->async_send_request( |
| 126 | + deactivate_hardware_request_, |
| 127 | + [this, &response]( |
| 128 | + rclcpp::Client<controller_manager_msgs::srv::SetHardwareComponentState>::SharedFuture result_response) { |
| 129 | + const auto & result = result_response.get(); |
| 130 | + if (result->ok) { |
| 131 | + RCLCPP_INFO(this->get_logger(), "Successfully deactivated thruster hardware interface"); // NOLINT |
| 132 | + } else { |
| 133 | + RCLCPP_ERROR(this->get_logger(), "Failed to deactivate thruster hardware interface"); // NOLINT |
| 134 | + response->success = false; |
| 135 | + response->message = "Failed to deactivate thruster hardware interface"; |
| 136 | + } |
| 137 | + }); |
| 138 | + |
| 139 | + // deactivate the controllers |
| 140 | + switch_controller_client_->async_send_request( |
| 141 | + deactivate_controllers_request_, |
| 142 | + [this, |
| 143 | + &response](rclcpp::Client<controller_manager_msgs::srv::SwitchController>::SharedFuture result_response) { |
| 144 | + const auto & result = result_response.get(); |
| 145 | + if (result->ok) { |
| 146 | + RCLCPP_INFO(this->get_logger(), "Successfully deactivated controllers"); // NOLINT |
| 147 | + } else { |
| 148 | + RCLCPP_ERROR(this->get_logger(), "Failed to deactivate controllers"); // NOLINT |
| 149 | + response->success = false; |
| 150 | + response->message = "Failed to deactivate controllers"; |
| 151 | + } |
| 152 | + }); |
| 153 | + } |
| 154 | + }, |
| 155 | + rclcpp::ServicesQoS(), |
| 156 | + service_callback_group_); |
| 157 | +} |
| 158 | + |
| 159 | +} // namespace coordinator |
| 160 | + |
| 161 | +auto main(int argc, char * argv[]) -> int |
| 162 | +{ |
| 163 | + rclcpp::init(argc, argv); |
| 164 | + rclcpp::executors::MultiThreadedExecutor executor; |
| 165 | + auto node = std::make_shared<coordinator::ControllerCoordinator>(); |
| 166 | + executor.add_node(node->get_node_base_interface()); |
| 167 | + executor.spin(); |
| 168 | + rclcpp::shutdown(); |
| 169 | + return 0; |
| 170 | +} |
0 commit comments