• Diff for "rosserial_arduino/Tutorials/CMake"
Differences between revisions 6 and 7
Revision 6 as of 2011-07-12 02:22:03
Size: 4529
Editor: AdamStambler
Comment:
Revision 7 as of 2011-07-12 02:41:55
Size: 5651
Editor: AdamStambler
Comment:
Deletions are marked like this. Additions are marked like this.
Line 84: Line 84:
== CMakeList.txt == == CMakeLists.txt ==
Line 86: Line 86:
Open the CMakeLists.txt in your package directory and compare it to the CMakeList below.

{{{
#!cplusplus block=cmake
rosbuild_find_ros_package(rosserial_arduino)
include(${rosserial_arduino_PACKAGE_PATH}/cmake_scripts/rosserial.cmake)
#====================================================================#
# Chatter #
#====================================================================#


set(FIRMWARE_NAME chatter)

set(${FIRMWARE_NAME}_BOARD atmega328) # Arduino Target board
set(${FIRMWARE_NAME}_SRCS # Firmware sources
       src/chatter.cpp
       )
set(${FIRMWARE_NAME}_LIBS )

set(${FIRMWARE_NAME}_PORT /dev/ttyUSB0) # Serial upload port
generate_ros_firmware(${FIRMWARE_NAME})

}}}

With rosserial_arduino, we are cross compiling our source code for the AVR processor. As a result, we cannot use the standard ROS CMake file directly. We take ROS's standard first two lines, and then delete the rest. Then, include the special rosserial_arduino cmake functions.

<<CodeRef(cmake,0,5)>>

Note: This tutorial assumes that you have completed the previous tutorials: Hello World Publisher.
(!) Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

CMake with rosserial_arduino

Description: This tutorial shows how to use the CMake build system with rosserial_arduino.

Tutorial Level: ADVANCED

Next Tutorial: Servo Controller

When you are doing large software projects, the Arduino IDE quickly becomes unwieldy. You often want to be able to compile your project from the command line or use a different IDE like Eclipse where you can use autocompletion. By building your rosserial_arduino project using CMake, you can take advantage of both of these tools.

For this tutorial, we are going to make a hello world rosserial_arduino package.

Making Your Project

Making your rosserial_arduino project is just like making any other ROS package. You create a package on your ROS_PACKAGE_PATH:

roscreate-pkg helloworld rosserial_arduino std_msgs

As usual, use roscreate-pkg to create a package named helloworld which depends on rosserial_arduino and std_msgs. You must depend on rosserial_arduino and since we are going to use std_msgs/String messages, the package depends on std_msgs.

Souce Code

Copy the the source code below and make a file called src/chatter.cpp in your helloworld package.

Toggle line numbers
   1 /*
   2  * rosserial Publisher Example
   3  * Prints "hello world!"
   4  */
   5 
   6 #include <WProgram.h>
   7 #include <ros.h>
   8 #include <std_msgs/String.h>
   9 
  10 ros::NodeHandle nh;
  11 
  12 std_msgs::String str_msg;
  13 ros::Publisher chatter("chatter", &str_msg);
  14 
  15 unsigned char hello[13] = "hello world!";
  16 
  17 void setup()
  18 {
  19   nh.initNode();
  20   nh.advertise(chatter);
  21 }
  22 
  23 void loop()
  24 {
  25   str_msg.data = hello;
  26   chatter.publish( &str_msg );
  27   nh.spinOnce();
  28   delay(1000);
  29 }

This program is almost exactly the same as the HelloWorld covered in the Publisher Tutorial. The only difference is

Toggle line numbers
   6 ros::NodeHandle nh;

When you are compiling a cpp file outside of the Arduino IDE, you need to explicitly include a header file which contains all of the Arduino functions (digitalRead, analogRead, delay, etc.).

If you are unsure if you are going to be using a file with the Arduino IDE versus CMake, just add this line at the top of your file. It never hurts and it makes sure your file is always compatible with non-Arduino IDE build systems.

CMakeLists.txt

Open the CMakeLists.txt in your package directory and compare it to the CMakeList below.

Toggle line numbers
   1 rosbuild_find_ros_package(rosserial_arduino)
   2 include(${rosserial_arduino_PACKAGE_PATH}/cmake_scripts/rosserial.cmake)
   3 #====================================================================#
   4 #  Chatter                                                           #
   5 #====================================================================#
   6 
   7 
   8 set(FIRMWARE_NAME chatter)
   9 
  10 set(${FIRMWARE_NAME}_BOARD atmega328)               # Arduino Target board
  11 set(${FIRMWARE_NAME}_SRCS #                                                     Firmware sources
  12                                                         src/chatter.cpp
  13                                                         )  
  14 set(${FIRMWARE_NAME}_LIBS )
  15 
  16 set(${FIRMWARE_NAME}_PORT /dev/ttyUSB0)            # Serial upload port
  17 generate_ros_firmware(${FIRMWARE_NAME})

With rosserial_arduino, we are cross compiling our source code for the AVR processor. As a result, we cannot use the standard ROS CMake file directly. We take ROS's standard first two lines, and then delete the rest. Then, include the special rosserial_arduino cmake functions.

Error: No code_block found

Testing

# - Generate firmware for rosserial_arduino Devices
# generate_ros_firmware(TARGET_NAME)
#        TARGET_NAME - Name of target
# Creates a Arduino firmware target.
#
# The target options can be configured by setting options of
# the following format:
#      ${TARGET_NAME}${SUFFIX}
# The following suffixes are availabe:
#      _SRCS           # Sources
#      _HDRS           # Headers
#      _LIBS           # Libraries to linked in
#      _BOARD          # Board name (such as uno, mega2560, ...)
#      _PORT           # Serial port, for upload and serial targets [OPTIONAL]
#      _AFLAGS         # Override global Avrdude flags for target
#      _SERIAL         # Serial command for serial target           [OPTIONAL]
#      _NO_AUTOLIBS    # Disables Arduino library detection
#      _CUSTOM_COM     # use a custom implementation of fx_putc and fx_getc for rosserial

# Here is a short example for a target named test:
#       set(test_SRCS  test.cpp)
#       set(test_HDRS  test.h)
#       set(test_BOARD uno)
#       generate_ros_firmware(test)

Additional Info

rosserial_arduino CMake implementation is found under rosserial_arduino/cmake_scripts. It is based on this arduino-cmake project. You should never need to edit these files. You just need to include them into your CMakeList

Wiki: rosserial_arduino/Tutorials/CMake (last edited 2023-05-12 22:47:09 by ChristianBauer)