ROS and Arduino: use CMake to compile the code and upload to arduino

Why use CMake to compile the code and upload to arduino?
When the need to develop large software projects point, Arduino IDE on application code becomes more awkward.
If you often want to eclipse from the command line or compile the code can automatically complete the
final you can CMake infrastructure rosserial_client, you build and distribute firmware using the ROS buildfarm

The blog said that only a simple example.
The old way, to everyone I tidied package: Download

1.

Create a work space, here a little
src directory under the workspace catkin

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

Use catkin_create_pkg create helloworld package, dependent rosserial_arduino (required Arduino tool chain) and rosserial_client (client libraries generate macros), and finally plan to use std_msgs / String, need to rely on std_msgs.

2.

In helloworld package, establishing firmware / chatter.cpp, copy and paste the contents of the following sample code:

#include <ros.h>
#include <std_msgs/String.h>
//Arduino.h头文件,它包含了所有的Arduino函数(digitalRead, 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.

CMakeLists.txt file editing 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)
  • Rosserial_client combination of cmake script
  • Here are not built directly firmware, but an independent project to compile Cmake pass from catkin package to target subprojects
  • rosserial_generate_ros_lib function creates a target helloworld_ros_lib, it generates rosserial client library contains header information
  • rosserial_configure_client function creates a target, it will configure CMake project in a specific subdirectory, you can use the tool chain provided, in this case, rosserial_arduino Arduino provides a useful tool chain.
  • Finally rosserial_add_client_target pass over the objectives of each call, so when we run the make command to compile
  • helloworld_firmware_hello of catkin target, it will configure the firmware directory in which to compile hello goal.

4.

Created under the package directory firmware / CMakeLists.txt, which is required for the second CMakeLists.txt, this is a sub-project for the firmware, which reads 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
)
  • generate_arduino_firmware function is provided by arduino-cmake toolchain

  • It positioning Arduino, required for connecting libraries

  • BOARD name can be found in /hardware/arduino/avr/boards.txt such as:

    arduino uno of BOARD named uno
    the Arduino leonardo leonardo called the BOARD
    I used here is uno board

Compile

  1. Firmware can be compiled using the default catkin_make, you can also specify:
catkin_make helloworld_firmware_hello
  1. Arduino development board is connected, confirm that the device is in the / dev / ttyUSB0, or modify the device number firmware / CMakeLists.txt inside corresponding compiler upload:
catkin_make helloworld_firmware_hello-upload

Here Insert Picture Description
There is such a prompt after the completion of the log

test

Respectively, to open a new terminal and run

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

Here Insert Picture DescriptionThe truth is the same, the same programming other programs

Published 15 original articles · won praise 11 · views 30000 +

Guess you like

Origin blog.csdn.net/weixin_44827364/article/details/104106916