diff --git a/rclcpp/CMakeLists.txt b/rclcpp/CMakeLists.txt index d9194f5..0926518 100644 --- a/rclcpp/CMakeLists.txt +++ b/rclcpp/CMakeLists.txt @@ -5,6 +5,7 @@ project(rclcpp) find_package(ament_cmake REQUIRED) ament_export_dependencies(rmw) +ament_export_dependencies(rcl_interfaces) ament_export_include_directories(include) diff --git a/rclcpp/include/rclcpp/parameter.hpp b/rclcpp/include/rclcpp/parameter.hpp new file mode 100644 index 0000000..e0f01a8 --- /dev/null +++ b/rclcpp/include/rclcpp/parameter.hpp @@ -0,0 +1,240 @@ +// Copyright 2015 Open Source Robotics Foundation, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef RCLCPP_RCLCPP_PARAMETER_HPP_ +#define RCLCPP_RCLCPP_PARAMETER_HPP_ + +#include + +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace rclcpp +{ + +namespace parameter +{ + +enum ParameterType { + PARAMETER_NOT_SET=rcl_interfaces::ParameterType::PARAMETER_NOT_SET, + PARAMETER_BOOL=rcl_interfaces::ParameterType::PARAMETER_BOOL, + PARAMETER_INTEGER=rcl_interfaces::ParameterType::PARAMETER_INTEGER, + PARAMETER_DOUBLE=rcl_interfaces::ParameterType::PARAMETER_DOUBLE, + PARAMETER_STRING=rcl_interfaces::ParameterType::PARAMETER_STRING, + PARAMETER_BYTES=rcl_interfaces::ParameterType::PARAMETER_BYTES, +}; + +// Structure to store an arbitrary parameter with templated get/set methods +class ParameterVariant +{ +public: + ParameterVariant() + : name_("") + { + value_.parameter_type = rcl_interfaces::ParameterType::PARAMETER_NOT_SET; + } + ParameterVariant(const std::string & name, const bool bool_value) + : name_(name) + { + value_.bool_value = bool_value; + value_.parameter_type = rcl_interfaces::ParameterType::PARAMETER_BOOL; + } + ParameterVariant(const std::string & name, const int64_t int_value) + : name_(name) + { + value_.integer_value = int_value; + value_.parameter_type = rcl_interfaces::ParameterType::PARAMETER_INTEGER; + } + ParameterVariant(const std::string & name, const double double_value) + : name_(name) + { + value_.double_value = double_value; + value_.parameter_type = rcl_interfaces::ParameterType::PARAMETER_DOUBLE; + } + ParameterVariant(const std::string & name, const std::string & string_value) + : name_(name) + { + value_.string_value = string_value; + value_.parameter_type = rcl_interfaces::ParameterType::PARAMETER_STRING; + } + ParameterVariant(const std::string & name, const std::vector & bytes_value) + : name_(name) + { + value_.bytes_value = bytes_value; + value_.parameter_type = rcl_interfaces::ParameterType::PARAMETER_BYTES; + } + + /* Templated getter */ + template + T + get_value() const; + + inline ParameterType get_type() const {return static_cast(value_.parameter_type); } + + inline std::string get_name() const & {return name_; } + + inline rcl_interfaces::ParameterValue get_parameter_value() const + { + return value_; + } + +private: + std::string name_; + rcl_interfaces::ParameterValue value_; +}; + +template<> +inline int64_t ParameterVariant::get_value() const +{ + if (value_.parameter_type != rcl_interfaces::ParameterType::PARAMETER_INTEGER) { + // TODO: use custom exception + throw std::runtime_error("Invalid type"); + } + return value_.integer_value; +} +template<> +inline double ParameterVariant::get_value() const +{ + if (value_.parameter_type != rcl_interfaces::ParameterType::PARAMETER_DOUBLE) { + // TODO: use custom exception + throw std::runtime_error("Invalid type"); + } + return value_.double_value; +} +template<> +inline std::string ParameterVariant::get_value() const +{ + if (value_.parameter_type != rcl_interfaces::ParameterType::PARAMETER_STRING) { + // TODO: use custom exception + throw std::runtime_error("Invalid type"); + } + return value_.string_value; +} +template<> +inline bool ParameterVariant::get_value() const +{ + if (value_.parameter_type != rcl_interfaces::ParameterType::PARAMETER_BOOL) { + // TODO: use custom exception + throw std::runtime_error("Invalid type"); + } + return value_.bool_value; +} +template<> +inline std::vector ParameterVariant::get_value() const +{ + if (value_.parameter_type != rcl_interfaces::ParameterType::PARAMETER_BYTES) { + // TODO: use custom exception + throw std::runtime_error("Invalid type"); + } + return value_.bytes_value; +} + +class AsyncParametersClient +{ + +public: + AsyncParametersClient(const rclcpp::node::Node::SharedPtr & node) + : node_(node) + { + get_parameters_client_ = node_->create_client( + "get_parameters"); + get_parameter_types_client_ = node_->create_client( + "get_parameter_types"); + set_parameters_client_ = node_->create_client( + "set_parameters"); + list_parameters_client_ = node_->create_client( + "list_parameters"); + describe_parameters_client_ = node_->create_client( + "describe_parameters"); + } + + std::shared_future> + get_parameters( + std::vector names, + std::function>)> callback = nullptr) + { + std::shared_future> f; + return f; + } + + std::shared_future> + get_parameter_types( + std::vector parameter_names, + std::function>)> callback = nullptr) + { + std::shared_future> f; + return f; + } + + std::shared_future> + set_parameters( + std::vector parameters, + std::function>)> callback = nullptr) + { + std::shared_future> f; + return f; + } + + std::shared_future + set_parameters_atomically( + std::vector parameters, + std::function)> callback = nullptr) + { + std::shared_future f; + return f; + } + + std::shared_future + list_parameters( + std::vector parameter_prefixes, + uint64_t depth, + std::function)> callback = nullptr) + { + std::shared_future f; + return f; + } + +private: + const rclcpp::node::Node::SharedPtr node_; + rclcpp::client::Client::SharedPtr get_parameters_client_; + rclcpp::client::Client::SharedPtr get_parameter_types_client_; + rclcpp::client::Client::SharedPtr set_parameters_client_; + rclcpp::client::Client::SharedPtr + set_parameters_atomically_client_; + rclcpp::client::Client::SharedPtr list_parameters_client_; + rclcpp::client::Client::SharedPtr describe_parameters_client_; +}; + +} /* namespace parameter */ + +} /* namespace rclcpp */ + +#endif /* RCLCPP_RCLCPP_PARAMETER_HPP_ */ diff --git a/rclcpp/include/rclcpp/rclcpp.hpp b/rclcpp/include/rclcpp/rclcpp.hpp index 02d835a..ac907d6 100644 --- a/rclcpp/include/rclcpp/rclcpp.hpp +++ b/rclcpp/include/rclcpp/rclcpp.hpp @@ -19,6 +19,7 @@ #include #include +#include #include #include #include diff --git a/rclcpp/package.xml b/rclcpp/package.xml index f393a38..5c7e924 100644 --- a/rclcpp/package.xml +++ b/rclcpp/package.xml @@ -10,6 +10,9 @@ rmw + rcl_interfaces + rcl_interfaces + ament_lint_auto ament_lint_common