|
| 1 | +/********************************************************************* |
| 2 | + * Software License Agreement (BSD License) |
| 3 | + * |
| 4 | + * Copyright (c) 2024, Nobleo Technology |
| 5 | + * All rights reserved. |
| 6 | + * |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions |
| 9 | + * are met: |
| 10 | + * |
| 11 | + * * Redistributions of source code must retain the above copyright |
| 12 | + * notice, this list of conditions and the following disclaimer. |
| 13 | + * * Redistributions in binary form must reproduce the above |
| 14 | + * copyright notice, this list of conditions and the following |
| 15 | + * disclaimer in the documentation and/or other materials provided |
| 16 | + * with the distribution. |
| 17 | + * * Neither the name of the copyright holder nor the names of its |
| 18 | + * contributors may be used to endorse or promote products derived |
| 19 | + * from this software without specific prior written permission. |
| 20 | + * |
| 21 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 24 | + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 25 | + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 26 | + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 27 | + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 28 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 29 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 30 | + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 31 | + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 32 | + * POSSIBILITY OF SUCH DAMAGE. |
| 33 | + *********************************************************************/ |
| 34 | + |
| 35 | +/**< \author Martin Cornelis */ |
| 36 | + |
| 37 | +#include <chrono> |
| 38 | + |
| 39 | +#include "rclcpp/rclcpp.hpp" |
| 40 | +#include "rcl_interfaces/srv/set_parameters_atomically.hpp" |
| 41 | +#include "rcl_interfaces/msg/parameter.hpp" |
| 42 | + |
| 43 | +using namespace std::chrono_literals; |
| 44 | + |
| 45 | +class AddAnalyzer : public rclcpp::Node |
| 46 | +{ |
| 47 | +public: |
| 48 | + AddAnalyzer() |
| 49 | + : Node("add_analyzer_node", "", rclcpp::NodeOptions().allow_undeclared_parameters( |
| 50 | + true).automatically_declare_parameters_from_overrides(true)) |
| 51 | + { |
| 52 | + client_ = this->create_client<rcl_interfaces::srv::SetParametersAtomically>( |
| 53 | + "/analyzers/set_parameters_atomically"); |
| 54 | + } |
| 55 | + |
| 56 | + void send_request() |
| 57 | + { |
| 58 | + while (!client_->wait_for_service(1s)) { |
| 59 | + if (!rclcpp::ok()) { |
| 60 | + RCLCPP_ERROR(this->get_logger(), "Interrupted while waiting for the service. Exiting."); |
| 61 | + return; |
| 62 | + } |
| 63 | + RCLCPP_INFO_ONCE(this->get_logger(), "service not available, waiting ..."); |
| 64 | + } |
| 65 | + auto request = std::make_shared<rcl_interfaces::srv::SetParametersAtomically::Request>(); |
| 66 | + std::map<std::string, rclcpp::Parameter> parameters; |
| 67 | + |
| 68 | + if (!this->get_parameters("", parameters)) { |
| 69 | + RCLCPP_ERROR(this->get_logger(), "Failed to retrieve parameters"); |
| 70 | + } |
| 71 | + for (const auto & [param_name, param] : parameters) { |
| 72 | + // Find the suffix |
| 73 | + size_t suffix_start = param_name.find_last_of('.'); |
| 74 | + // Remove suffix if it exists |
| 75 | + if (suffix_start != std::string::npos) { |
| 76 | + std::string stripped_param_name = param_name.substr(0, suffix_start); |
| 77 | + // Check in map if the stripped param name with the added suffix "path" exists |
| 78 | + // This indicates the parameter is part of an analyzer description |
| 79 | + if (parameters.count(stripped_param_name + ".path") > 0) { |
| 80 | + auto parameter_msg = param.to_parameter_msg(); |
| 81 | + request->parameters.push_back(parameter_msg); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + auto result = client_->async_send_request(request); |
| 87 | + // Wait for the result. |
| 88 | + if (rclcpp::spin_until_future_complete(this->get_node_base_interface(), result) == |
| 89 | + rclcpp::FutureReturnCode::SUCCESS) |
| 90 | + { |
| 91 | + RCLCPP_INFO(this->get_logger(), "Parameters succesfully set"); |
| 92 | + } else { |
| 93 | + RCLCPP_ERROR(this->get_logger(), "Failed to set parameters"); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | +private: |
| 98 | + rclcpp::Client<rcl_interfaces::srv::SetParametersAtomically>::SharedPtr client_; |
| 99 | +}; |
| 100 | + |
| 101 | +int main(int argc, char ** argv) |
| 102 | +{ |
| 103 | + rclcpp::init(argc, argv); |
| 104 | + |
| 105 | + auto add_analyzer = std::make_shared<AddAnalyzer>(); |
| 106 | + add_analyzer->send_request(); |
| 107 | + rclcpp::shutdown(); |
| 108 | + |
| 109 | + return 0; |
| 110 | +} |
0 commit comments