diff --git a/rclcpp/plugins/polygon_base/CHANGELOG.rst b/rclcpp/plugins/polygon_base/CHANGELOG.rst new file mode 100644 index 00000000..d68a4959 --- /dev/null +++ b/rclcpp/plugins/polygon_base/CHANGELOG.rst @@ -0,0 +1,3 @@ +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +Changelog for package polygon_base +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/rclcpp/plugins/polygon_base/CMakeLists.txt b/rclcpp/plugins/polygon_base/CMakeLists.txt new file mode 100644 index 00000000..5604fdae --- /dev/null +++ b/rclcpp/plugins/polygon_base/CMakeLists.txt @@ -0,0 +1,49 @@ +cmake_minimum_required(VERSION 3.20) +project(polygon_base) + +if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") + add_compile_options(-Wall -Wextra -Wpedantic) +endif() + +# find dependencies +find_package(ament_cmake REQUIRED) +find_package(ament_cmake_ros REQUIRED) +find_package(pluginlib REQUIRED) + +add_library(polygon_base INTERFACE) +add_library(polygon_base::polygon_base ALIAS polygon_base) +target_compile_features(polygon_base INTERFACE c_std_99 cxx_std_17) # Require C99 and C++17 +target_include_directories(polygon_base INTERFACE + $ + $ +) +target_link_libraries( + polygon_base INTERFACE + ${pluginlib_TARGETS} +) + +install( + DIRECTORY include/ + DESTINATION include/${PROJECT_NAME} +) +install( + TARGETS polygon_base + EXPORT export_${PROJECT_NAME} + ARCHIVE DESTINATION lib + LIBRARY DESTINATION lib + RUNTIME DESTINATION bin +) + +if(BUILD_TESTING) + find_package(ament_lint_auto REQUIRED) + ament_lint_auto_find_test_dependencies() +endif() + +ament_export_include_directories( + "include/${PROJECT_NAME}" +) +ament_export_targets( + export_${PROJECT_NAME} +) + +ament_package() diff --git a/rclcpp/plugins/polygon_base/include/polygon_base/regular_polygon.hpp b/rclcpp/plugins/polygon_base/include/polygon_base/regular_polygon.hpp new file mode 100644 index 00000000..37c5a9a8 --- /dev/null +++ b/rclcpp/plugins/polygon_base/include/polygon_base/regular_polygon.hpp @@ -0,0 +1,32 @@ +// Copyright 2025 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 POLYGON_BASE__REGULAR_POLYGON_HPP_ +#define POLYGON_BASE__REGULAR_POLYGON_HPP_ + +namespace polygon_base +{ +class RegularPolygon +{ +public: + virtual void initialize(double side_length) = 0; + virtual double area() = 0; + virtual ~RegularPolygon() {} + +protected: + RegularPolygon() {} +}; +} // namespace polygon_base + +#endif // POLYGON_BASE__REGULAR_POLYGON_HPP_ diff --git a/rclcpp/plugins/polygon_base/package.xml b/rclcpp/plugins/polygon_base/package.xml new file mode 100644 index 00000000..abcef47a --- /dev/null +++ b/rclcpp/plugins/polygon_base/package.xml @@ -0,0 +1,25 @@ + + + + polygon_base + 0.21.3 + Package containing examples of how to define a plugin base class interface + + Alejandro Hernandez Cordero + + Apache License 2.0 + + Jacob Perron + Maurice Alexander Purnawan + + ament_cmake_ros + + pluginlib + + ament_lint_auto + ament_lint_common + + + ament_cmake + + diff --git a/rclcpp/plugins/polygon_plugins/CHANGELOG.rst b/rclcpp/plugins/polygon_plugins/CHANGELOG.rst new file mode 100644 index 00000000..63e821c8 --- /dev/null +++ b/rclcpp/plugins/polygon_plugins/CHANGELOG.rst @@ -0,0 +1,3 @@ +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +Changelog for package polygon_plugins +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/rclcpp/plugins/polygon_plugins/CMakeLists.txt b/rclcpp/plugins/polygon_plugins/CMakeLists.txt new file mode 100644 index 00000000..f445bdf8 --- /dev/null +++ b/rclcpp/plugins/polygon_plugins/CMakeLists.txt @@ -0,0 +1,53 @@ +cmake_minimum_required(VERSION 3.20) +project(polygon_plugins) + +if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") + add_compile_options(-Wall -Wextra -Wpedantic) +endif() + +# find dependencies +find_package(ament_cmake REQUIRED) +find_package(polygon_base REQUIRED) +find_package(pluginlib REQUIRED) + +add_library(polygon_plugins SHARED + src/polygon_plugins.cpp +) +target_compile_features(polygon_plugins PUBLIC c_std_99 cxx_std_17) # Require C99 and C++17 +target_include_directories(polygon_plugins PUBLIC + $ + $ +) +target_link_libraries(polygon_plugins PUBLIC + ${polygon_base_TARGETS} + ${pluginlib_TARGETS} +) + +add_executable(area_node src/area_node.cpp) +target_include_directories(area_node PUBLIC + $ + $ +) +target_compile_features(area_node PUBLIC c_std_99 cxx_std_17) # Require C99 and C++17 +target_link_libraries( + area_node + ${polygon_base_TARGETS} + ${pluginlib_TARGETS} +) + +install( + TARGETS polygon_plugins + EXPORT export_${PROJECT_NAME} + ARCHIVE DESTINATION lib + LIBRARY DESTINATION lib + RUNTIME DESTINATION bin) +install(TARGETS area_node + DESTINATION lib/${PROJECT_NAME}) + +if(BUILD_TESTING) + find_package(ament_lint_auto REQUIRED) + ament_lint_auto_find_test_dependencies() +endif() + +pluginlib_export_plugin_description_file(polygon_base plugins.xml) +ament_package() diff --git a/rclcpp/plugins/polygon_plugins/include/polygon_plugins/polygon_plugins.hpp b/rclcpp/plugins/polygon_plugins/include/polygon_plugins/polygon_plugins.hpp new file mode 100644 index 00000000..ea50ae39 --- /dev/null +++ b/rclcpp/plugins/polygon_plugins/include/polygon_plugins/polygon_plugins.hpp @@ -0,0 +1,30 @@ +// Copyright 2025 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 POLYGON_PLUGINS__POLYGON_PLUGINS_HPP_ +#define POLYGON_PLUGINS__POLYGON_PLUGINS_HPP_ + +namespace polygon_plugins +{ + +class PolygonPlugins +{ +public: + PolygonPlugins(); + + virtual ~PolygonPlugins(); +}; +} // namespace polygon_plugins + +#endif // POLYGON_PLUGINS__POLYGON_PLUGINS_HPP_ diff --git a/rclcpp/plugins/polygon_plugins/package.xml b/rclcpp/plugins/polygon_plugins/package.xml new file mode 100644 index 00000000..db4cf1bd --- /dev/null +++ b/rclcpp/plugins/polygon_plugins/package.xml @@ -0,0 +1,26 @@ + + + + polygon_plugins + 0.21.3 + Package containing examples of how to create plugins with pluginlib + + Alejandro Hernandez Cordero + + Apache License 2.0 + + Jacob Perron + Maurice Alexander Purnawan + + ament_cmake_ros + + polygon_base + pluginlib + + ament_lint_auto + ament_lint_common + + + ament_cmake + + diff --git a/rclcpp/plugins/polygon_plugins/plugins.xml b/rclcpp/plugins/polygon_plugins/plugins.xml new file mode 100644 index 00000000..dc07bf98 --- /dev/null +++ b/rclcpp/plugins/polygon_plugins/plugins.xml @@ -0,0 +1,8 @@ + + + This is a square plugin. + + + This is a triangle plugin. + + diff --git a/rclcpp/plugins/polygon_plugins/src/area_node.cpp b/rclcpp/plugins/polygon_plugins/src/area_node.cpp new file mode 100644 index 00000000..0a2f6a32 --- /dev/null +++ b/rclcpp/plugins/polygon_plugins/src/area_node.cpp @@ -0,0 +1,45 @@ +// Copyright 2025 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. + +#include +#include +#include +#include + +int main(int argc, char ** argv) +{ + // To avoid unused parameter warnings + (void) argc; + (void) argv; + + pluginlib::ClassLoader poly_loader("polygon_base", + "polygon_base::RegularPolygon"); + + try { + std::shared_ptr triangle = + poly_loader.createSharedInstance("awesome_triangle"); + triangle->initialize(10.0); + + std::shared_ptr square = + poly_loader.createSharedInstance("polygon_plugins::Square"); + square->initialize(10.0); + + std::cout << "Triangle area: " << triangle->area() << std::endl; + std::cout << "Square area: " << square->area() << std::endl; + } catch(pluginlib::PluginlibException & ex) { + std::cout << "The plugin failed to load for some reason. Error: " << ex.what() << std::endl; + } + + return 0; +} diff --git a/rclcpp/plugins/polygon_plugins/src/polygon_plugins.cpp b/rclcpp/plugins/polygon_plugins/src/polygon_plugins.cpp new file mode 100644 index 00000000..9b877651 --- /dev/null +++ b/rclcpp/plugins/polygon_plugins/src/polygon_plugins.cpp @@ -0,0 +1,63 @@ +// Copyright 2025 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. + +#include +#include + +namespace polygon_plugins +{ +class Square : public polygon_base::RegularPolygon +{ +public: + void initialize(double side_length) override + { + side_length_ = side_length; + } + + double area() override + { + return side_length_ * side_length_; + } + +protected: + double side_length_; +}; + +class Triangle : public polygon_base::RegularPolygon +{ +public: + void initialize(double side_length) override + { + side_length_ = side_length; + } + + double area() override + { + return 0.5 * side_length_ * getHeight(); + } + + double getHeight() + { + return sqrt((side_length_ * side_length_) - ((side_length_ / 2) * (side_length_ / 2))); + } + +protected: + double side_length_; +}; +} // namespace polygon_plugins + +#include + +PLUGINLIB_EXPORT_CLASS(polygon_plugins::Square, polygon_base::RegularPolygon) +PLUGINLIB_EXPORT_CLASS(polygon_plugins::Triangle, polygon_base::RegularPolygon)