-
Notifications
You must be signed in to change notification settings - Fork 348
Add pluginlib tutorial code #430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: rolling
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| Changelog for package polygon_base | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> | ||
| $<INSTALL_INTERFACE:include/${PROJECT_NAME}> | ||
| ) | ||
| 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() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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_ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <?xml version="1.0"?> | ||
| <?xml-model href="http://download.ros.org/schema/package_format2.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?> | ||
| <package format="2"> | ||
| <name>polygon_base</name> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should I rename the package? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you want to do that? and into what name? and if we do that, probably the tutorial in the documentation should be changed accordingly to keep the consistency? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed that other packages are prefixed with example_rclcpp, that's why I am asking if it is required to rename it |
||
| <version>0.21.3</version> | ||
| <description>Package containing examples of how to define a plugin base class interface</description> | ||
|
|
||
| <maintainer email="[email protected]">Alejandro Hernandez Cordero</maintainer> | ||
|
|
||
| <license>Apache License 2.0</license> | ||
|
|
||
| <author email="[email protected]">Jacob Perron</author> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can add yourself here |
||
| <author email="[email protected]">Maurice Alexander Purnawan</author> | ||
|
|
||
| <buildtool_depend>ament_cmake_ros</buildtool_depend> | ||
|
|
||
| <depend>pluginlib</depend> | ||
|
|
||
| <test_depend>ament_lint_auto</test_depend> | ||
| <test_depend>ament_lint_common</test_depend> | ||
|
|
||
| <export> | ||
| <build_type>ament_cmake</build_type> | ||
| </export> | ||
| </package> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here. i think this should be generated by release tool. |
||
| Changelog for package polygon_plugins | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> | ||
| $<INSTALL_INTERFACE:include> | ||
| ) | ||
| 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 | ||
| $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> | ||
| $<INSTALL_INTERFACE:include/${PROJECT_NAME}> | ||
| ) | ||
| 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() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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_ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| <?xml version="1.0"?> | ||
| <?xml-model href="http://download.ros.org/schema/package_format2.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?> | ||
| <package format="2"> | ||
| <name>polygon_plugins</name> | ||
| <version>0.21.3</version> | ||
| <description>Package containing examples of how to create plugins with pluginlib</description> | ||
|
|
||
| <maintainer email="[email protected]">Alejandro Hernandez Cordero</maintainer> | ||
|
|
||
| <license>Apache License 2.0</license> | ||
|
|
||
| <author email="[email protected]">Jacob Perron</author> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add yourself as the author |
||
| <author email="[email protected]">Maurice Alexander Purnawan</author> | ||
|
|
||
| <buildtool_depend>ament_cmake_ros</buildtool_depend> | ||
|
|
||
| <depend>polygon_base</depend> | ||
| <depend>pluginlib</depend> | ||
|
|
||
| <test_depend>ament_lint_auto</test_depend> | ||
| <test_depend>ament_lint_common</test_depend> | ||
|
|
||
| <export> | ||
| <build_type>ament_cmake</build_type> | ||
| </export> | ||
| </package> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| <library path="polygon_plugins"> | ||
| <class type="polygon_plugins::Square" base_class_type="polygon_base::RegularPolygon"> | ||
| <description>This is a square plugin.</description> | ||
| </class> | ||
| <class type="polygon_plugins::Triangle" base_class_type="polygon_base::RegularPolygon" name="awesome_triangle"> | ||
| <description>This is a triangle plugin.</description> | ||
| </class> | ||
| </library> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 <iostream> | ||
| #include <memory> | ||
| #include <pluginlib/class_loader.hpp> | ||
| #include <polygon_base/regular_polygon.hpp> | ||
|
|
||
| int main(int argc, char ** argv) | ||
| { | ||
| // To avoid unused parameter warnings | ||
| (void) argc; | ||
| (void) argv; | ||
|
|
||
| pluginlib::ClassLoader<polygon_base::RegularPolygon> poly_loader("polygon_base", | ||
| "polygon_base::RegularPolygon"); | ||
|
|
||
| try { | ||
| std::shared_ptr<polygon_base::RegularPolygon> triangle = | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. include |
||
| poly_loader.createSharedInstance("awesome_triangle"); | ||
| triangle->initialize(10.0); | ||
|
|
||
| std::shared_ptr<polygon_base::RegularPolygon> 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; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 <cmath> | ||
| #include <polygon_base/regular_polygon.hpp> | ||
|
|
||
| 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/class_list_macros.hpp> | ||
|
|
||
| PLUGINLIB_EXPORT_CLASS(polygon_plugins::Square, polygon_base::RegularPolygon) | ||
| PLUGINLIB_EXPORT_CLASS(polygon_plugins::Triangle, polygon_base::RegularPolygon) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this supposed to be during release process? we are planning to release this package, correct?