ROS and Arduino: compile code with CMake and upload to Arduino

Why use CMake to compile code and upload it to arduino?
When large software projects need to be developed, the Arduino IDE becomes clumsy in code management.
If you often want to compile code from the command line or from eclipse that can be done automatically
Finally, you can build and distribute firmware with ROS buildfarm through the CMake infrastructure of rosserial ﹣ client

This blog only talks about a simple example.
As always, I've packed my bags for you: Download address

1.

Create workspace and so on, omitted here
Under the src directory of catkin workspace

$ cd ~/catkin_ws_cmake/src/
$ catkin_create_pkg helloworld rosserial_arduino rosserial_client std_msgs

Create helloworld package with catkin ﹣ create ﹣ PKG, rely on rosserial ﹣ Arduino (need Arduino's tool chain) and rosserial ﹣ client (client library generates macros), and finally intend to use STD ﹣ msgs / string, need to rely on STD ﹣ msgs.

2.

In helloworld package, create firmware/chatter.cpp, copy and paste the following example code content:

#include <ros.h>
#include <std_msgs/String.h>
//Arduino.h header file, which contains all the Arduino functions (digital read, analogread, delay, etc.)
#include <Arduino.h>

ros::NodeHandle nh;

std_msgs::String str_msg;
ros::Publisher chatter("chatter", &str_msg);

char hello[13] = "hello world!";

void setup()
{
  nh.initNode();
  nh.advertise(chatter);
}

void loop()
{
  str_msg.data = hello;
  chatter.publish( &str_msg );
  nh.spinOnce();
  delay(1000);
}

3.

Edit the CMakeLists.txt file in the function package directory

find_package(catkin REQUIRED COMPONENTS
  rosserial_arduino
  rosserial_client
)

catkin_package()

rosserial_generate_ros_lib(
  PACKAGE rosserial_arduino
  SCRIPT make_libraries.py
)

rosserial_configure_client(
  DIRECTORY firmware
  TOOLCHAIN_FILE ${ROSSERIAL_ARDUINO_TOOLCHAIN}
)

rosserial_add_client_target(firmware hello ALL)
rosserial_add_client_target(firmware hello-upload)
  • cmake script combined with rosserial & Client
  • Instead of building the firmware directly, compile the Cmake project independently, passing the target from the catkin package to the subproject
  • The rosserial? Generate? ROS? Lib function creates the target HelloWorld? ROS? Lib, which generates the rosserial client library containing headers
  • The rosserial? Configure? Client function creates a target that configures the CMake project in a specific subdirectory to use the provided tool chain, in which case rosserial? Arduino provides a useful Arduino tool chain.
  • Finally, rosserial add client target calls each target passed in, so when we run the make command to compile
  • The catkin target of HelloWorld? Firmware? hello, which configures the firmware directory in which the hello target is compiled.

4.

Create firmware/CMakeLists.txt in the package directory. This is the second CMakeLists.txt required. This is a subproject for firmware. The content is as follows:

cmake_minimum_required(VERSION 2.8.3)

include_directories(${ROS_LIB_DIR})

# Remove this if using an Arduino without native USB (eg, other than Leonardo)
add_definitions(-DUSB_CON)

generate_arduino_firmware(hello
  SRCS chatter.cpp ${ROS_LIB_DIR}/time.cpp
  BOARD uno
  PORT /dev/ttyUSB0
)
  • The generate? Arduino? Firmware function is provided by Arduino cmake toolchain

  • It locates Arduino, connects the required libraries, etc

  • The BOARD name can be found in / hardware/arduino/avr/boards.txt. For example:

    The BOARD name of arduino uno is Uno
    arduino leonardo's BOARD is named leonardo
    I use uno board here

Compile

  1. Firmware can be compiled using catkin? Make by default, or you can specify:
catkin_make helloworld_firmware_hello
  1. Connect the Arduino development board, confirm that the device is in / dev/ttyUSB0, or modify the corresponding device number in firmware/CMakeLists.txt, compile and upload:
catkin_make helloworld_firmware_hello-upload


There will be a prompt log after completion

test

Open the new terminal separately and run

roscore
rosrun rosserial_python serial_node.py _port:=/dev/ttyUSB0
rostopic echo chatter

It's the same thing. Burning other programs is the same

Published 15 original articles, won praise 11, visited 30000+
Private letter follow

Tags: cmake Eclipse

Posted on Wed, 29 Jan 2020 00:23:20 -0800 by Spaceman-Spiff