BNO055 I2C node in C++ for ROS1/ROS2 -- how to avoid dual CMakeLists.txt?

I’ve been MIA in the robotics/ROS scene for a while, just coming back to revive some old work and port it to ROS2.

I have, in the past, created dual ROS1+ROS2 packages (that work in both colcon and catkin), e.g.

I’m however having trouble figuring out a way to do the same for a C++ node since I can’t use the same conditionals in CMakeLists.txt. Here’s my best shot at an update of my BNO055 I2C node in C++:

As you can see there are two CMakeLists.txt files, one for ROS1 and one for ROS2. Copying the correct one to “CMakeLists.txt” will enable this repo to work in ROS1 or ROS2 respectively.

Is there any way to combine these two into one CMakeLists.txt and avoid the extra step?


Why exactly you can’t use conditionals? CMake is almost a programming language, so that would seem weird to me.

1 Like

I think there are two possibilities.

The first is to use the environment variable ROS_VERSION that is set by the ros_environment package.

elseif(${ROS_VERSION} EQUAL 2)

The second way is to create a vanilla cmake package. That means to avoid using catkin or ament macros. This has some downsides, because you’d have to to more work in your package. For example you need to install your package.xml. Correctly export your library and install a PackageConfig.cmake file in the correct location. But this can work, and is useful for ROS agnostic C++ libraries.

1 Like

Otherwise, this would be better suited for a question on .

Thanks for sharing your work. :slight_smile:

The BNO055 interface looks very interesting. Could you please elaborate what the difference to GitHub - RoboticArts/ros_imu_bno055: ROS driver for the BNO055 IMU using serial communication. Also implements configuration and calibration of the IMU is? Presumably that one is using serial instead of I2C and doesn’t support ROS2, right?

I would appreciate it if you added a differentiation in your README, just as you did for GitHub - mdrwiega/bosch_imu_driver: ROS driver for IMU Bosch BNO055

Hi @ct2034,

I wasn’t aware of that repo when I created mine, but it looks like a Python driver. Mine is C++ and much less CPU usage at high data rates. I should probably also add a UART version, mine only does I2C at the moment but it is fairly easy to add that.

Mine is also targeted at supporting both ROS1 and ROS2.


I came across this set of packages recently which does exactly what you want. Should be good enough as an example: fkie-multi-agent-suite/fkie_mas_discovery/CMakeLists.txt at master · fkie/fkie-multi-agent-suite · GitHub

@swan Thank you so much! This is a super useful example and I think I can work off this. Also thanks @Rayman I’ll try that method as well.

1 Like