Search code examples
c++cmakeros

Migrating C++ code from ROS1 to ROS2 - How to use C++ serial library in ROS2?


I am migrating a package from ROS1 to ROS2. It is a motor controller driver so it communicates over USB and uses the 'serial' C++ library. I have seen other ROS2 C++ drivers similar to this one use the serial library, but am wondering how to fix this error when building:


CMake Error at CMakeLists.txt:26 (find_package):

  By not providing "Findserial.cmake" in CMAKE_MODULE_PATH this project has


asked CMake to find a package configuration file provided by "serial", but
  CMake did not find one.

  Could not find a package configuration file provided by "serial" with any
  of the following names:

    serialConfig.cmake
    serial-config.cmake

  Add the installation prefix of "serial" to CMAKE_PREFIX_PATH or set
  "serial_DIR" to a directory containing one of the above files.  If "serial"
  provides a separate development package or SDK, be sure it has been
  installed.


    ---

Failed   <<<

I include the header #include <serial/serial.h> In my file and find_package(serial REQUIRED) in CMakeLists.txt. Is there something I'm missing for this to work? This is on a fresh install, do I need to install anything? Thanks

In ROS1, you can simply use apt-get install ros-noetic-serial. This doesn't work on ROS2. When I run colcon build, I got the error above.


Solution

  • Are you sure the other drivers you've seen with ROS2 use the exact same package? From what I can tell, there is no official release of the serial package for ROS2 available (based on the conversations in the Issues section on the official GIT repository here).

    However, that being said, you can see that there are some "unofficial" versions where people have ported the exact serial library to now build with colcon in ROS2. The serial-ros2 project here and the ros2 branch of the original repository here are probably very similar at the moment and are unofficial versions of the serial package. You could give them a shot.

    Another package I came across for this use-case is the transport_drivers repository with the serial_driver package which has some development for ROS2 here. However, using this would probably involve modifying your original code to work with this package and some extra work :)

    Hope this helps!