Sounds like you're making your software as "ament-compatible package" or just "ament package" (*1) as you use colcon
to build. colcon
and ament
are IINM made along with primarilly ROS2 framework in mind, but they are independent from ROS2, so using them for software that are not ROS-dependent is totally a valid usecase IMO, if that's what you're doing.
- What do I need to add to CMakeLists.txt so I can include modbus inside motor.hpp?
Adding Dependencies section in the tutorial is a good resource.
Going a little step-by-step. 1st, define dependencies that your cmake target needs to depend on.
set(DEPS
ament_cmake
rclcpp
%YOUR_CHOICES%
)
foreach(dep IN ITEMS ${DEPS}) find_package(${dep} REQUIRED) endforeach()
Define a cmake target for your program. This line differs depending on whether you're making an executable or a library.
add_executable(%YOUR_TARGET% %PATH_TO_YOUR_CPP%)
add_library(%YOUR_TARGET% %PATH_TO_YOUR_CPP%)
Then, using the approaches explained in the tutorial, set dependency to your cmake target. The tutorial doesn't seem to clarify cases where either approach is more sutaible though, I'd say normally use ament_target_dependencies
that does multiple things at once.
ament_target_dependencies(%YOUR_TARGET% ${DEPS})
(I experienced a case where I could only get my app working with target_link_libraries
, which I asked in robotics.stackexchange.com#24837, fyi).
2 . How should I configure CMakeLists.txt so that I can include the motor class in another ros2 package?
For this question, I don't see much difference from the 1st one, so I assume this 2nd one is about "how to add dependency in "ament pkg" than just configuring CMakeLists.txt.
Defining dependency for ament package
is 2-fold:
- Dependency in packaging:
- Dependency in building software (using
cmake
via ament_cmake
) --> Explained in the answer for question-1.
In your case, I'm not sure which apt/deb
package you use for libmodbus
, but in the rosdep
key list on rosdistro
, there's libmodbus-dev (rosdistro/rosdep/base.yaml#L4047) and libmodbus5
(note the link is permalink so the info you see may be outdated). If either of those are what you need, add it in your package.xml
*1...I haven't seen yet much of these expression in ament
world though. Back in ROS1, "catkin package" was commonly mentioned in order to describe a software that is packaged using package.xml
, CMakeLists.txt
in the way Catkin defines.
UPDATE Inspired by this thread, I opened ros2_documentation!3719 to add more explanation in rosdep
's tutorial. If anyone can review and leave comments there, that will help review.