Building Arduino firmware with CMake and catkin

A print out of the results of catkin and CMake for my Arduino firmware

A print out of the results of catkin and CMake for my Arduino firmware

As projects get more complex, the tools you use start to become somewhat unwieldy, and they are no longer as effective as before. YardBot runs with 2 Arduinos, and currently my process for updating their firmware requires starting an Ubuntu virtual machine running the same ROS version as the main computer. This is done with the Arduino IDE and anyone who uses it for advanced projects can tell you that the editor is sorely lacking when compared to other software. VirtualBox makes USB pass through difficult too, and having to run a full desktop to update software on a headless system is a bit overkill. All that to say I needed to improve the process and the internet said the answer to that was CMake and catkin.

The nice part of the ROS community is that many of the significant problems have been solved before, so you can generally get help easily. I needed this community multiple times while setting up, so here’s my process so others can learn.

The first thing to do is look at the dedicated rosserial catkin tutorial. This has the entire process listed, but in order to get my code to work, I had to figure out a number of issues.

The general process is as follows:

  1. Create a catkin project (probably best to have an Arduino specific project)
  2. Write your code inside the project’s firmware folder
  3. Create a CMakeLists.txt file to load the Arduino specific settings
  4. Create a second CMakeLists.txt file to handle compiling for a specific Arduino board and handle upload
  5. If your code is clean, upload to the Arduino with USB

If you follow the tutorial with the simple code provided, everything should work as expected. My problem was that I started with my full YardBot code and that means things like 3rd party libraries and ROS packages with custom messages.

Store 3rd party libraries inside firmware/libraries

Originally my Arduino code lived at ~/sketchbook, with all ROS message headers and libraries inside ~/sketchbook/libraries. After moving the code to my ROS workspace, I also copied the libraries. It’s best to remove the ros_lib folder, otherwise there are duplicates, but otherwise it works.

Follow proper coding conventions

With the Arduino, normally you can get away with things like not having function prototypes. The first time I compiled, I hit multiple errors for function not declared, so I had to go through everything and copy function prototypes to the top of the file. When I did this for the second Arduino’s firmware, it compiled on the first try.

Cleaning the workspace helps

When you add a new component, it’s a good idea to clean the workspace and compile from scratch. This means deleting the /devel and /build folders of the workspace.

Source before compiling

After cleaning the workspace, I ran into a problem multiple times where my custom message package would not be picked up, and that caused the Arduino header generator to fail. This meant the message definitions weren’t available, and errors were thrown. The forum solutions mostly included sourcing the correct files, so after running source [workspace]>/install/setup.bash before compiling, everything fell into place. I’ve added that process to my ~/.bashrc file, so hopefully that will always work now.

Have another ROS installation available

If you install the Arduino tools from apt, there shouldn’t really be a problem, but I ran into issues a few times where files that the system thought were there, weren’t. Running an update and upgrade didn’t help, so I ended up copying some files from my ROS desktop.

Choose the correct board definition

The second CMakeLists file includes the USB port and board definition so your code can get to the Arduino properly. Be sure to include the right port and board name. You can find the boards.txt from the Arduino core install, so it’s best to find it and browse to find yours. I used “mega” and “mega2560”.

It was a challenging couple of nights to figure it out, but now that it works, I can update the Arduino firmware without starting my virtual machine or switching USB cables between systems.

Posted in Software Tagged with: , , , ,
4 comments on “Building Arduino firmware with CMake and catkin
  1. Klemen says:

    Awsome work!

    I’m building an Arduino based robot myself, but am more of a software guy so I hope the components I bought will suffice to get the thing moving. I would love your thoughts on it.

    Components:
    – Arduino Motor Shield (arrives today)
    – Arduino UNO
    – Arduino Wi-fi shield
    – Robby RP5 Robot system

    OS:
    – Ubuntu

  2. william says:

    Hi Wes,

    I’m trying to build Arduino firmware with CMake and catkin as well. However, during this process, some error messages popped up.

    CMake Error at /opt/ros/indigo/share/rosserial_arduino/arduino-cmake/cmake/Platform/Arduino.cmake:909 (message):
    Invalid source file: hello.cpp
    Call Stack (most recent call first):
    /opt/ros/indigo/share/rosserial_arduino/arduino-cmake/cmake/Platform/Arduino.cmake:500 (find_arduino_libraries)
    CMakeLists.txt:10 (generate_arduino_firmware)

    — Configuring incomplete, errors occurred!
    See also “/home/odroid/catkin_ws/build/helloworld/firmware/CMakeFiles/CMakeOutput.log”.
    make[2]: *** [helloworld/CMakeFiles/helloworld_firmware] Error 1
    make[1]: *** [helloworld/CMakeFiles/helloworld_firmware.dir/all] Error 2
    make: *** [all] Error 2
    Invoking “make -j8 -l8” failed

    Any idea what went wrong?
    Thanks!

    • Wes says:

      I’m not sure what that error is. Did you review the suggested make log? It also looks like something could be wrong in the CMakeLists.txt file.

Leave a Reply

Your email address will not be published. Required fields are marked *

*