Useful snippets for ROS
Set CMake options in catkin build
To pass CMake options to the catkin build tool simply use the --cmake-args command line switch. For example to set the CMAKE_BUILD_TYPE I would use:
catkin build --cmake-args -DCMAKE_BUILD_TYPE=Debug
To save the so that they are added for every call you can use the --save-config command line option :
catkin build --save-CONFIG --cmake-args -DCMAKE_BUILD_TYPE=Debug
Build a single package
To build a single package just specify its name at after catkin build, e.g.
catkin build my_package
There is also an even more convenient form. If you are in the package directory then you can simply use
catkin build --this
To just re-build the package without touching any of its dependencies use:
catkin build --this --no-deps
The --this switch can also, quite conveniently, be used for catkin clean.
Create a new package
To create a new ROS package with all the boilerplate just use:
catkin create pkg PACKAGE_NAME
Most of the options of the package.xml file can also be set from the command line.
| -l | Add licence |
| -a | Add author in "First Name, Last Name" email format |
| -m | Add maintainer |
An example invocation using all of these parameters is:
catkin create pkg demo -l MIT -a "Thibaud Chupin" thibaud.chupin@mail.com
Change the console log format
The default format string for logs in ROS is quite poor. I usually want to know what node produced each message for example. Changing it is quite simple fortunately, we just need to override the ROSCONSOLE_FORMAT shell variable. The format string I use now is:
export ROSCONSOLE_FORMAT='[${severity}] <${time}> ${node} @ ${file}:${line}
${message}'
Note the new line! This produces logs that have the following appearance.
[ INFO] <1494230701.053693261> /camera_left_extrinsic @ /home/me/Projects/Nearlab/ros_ws/src/aruco_extrinsic/src/ArucoBoardDetector.cpp:26
Listening for camera images on /lappycam/image_rect_color
[ INFO] <1494230701.060647944> /camera_left_extrinsic @ /home/me/Projects/Nearlab/ros_ws/src/aruco_extrinsic/src/ArucoBoardDetector.cpp:43
Publishing camera pose to /lappycam/pose
[ INFO] <1494230701.063317878> /camera_left_extrinsic @ /home/me/Projects/Nearlab/ros_ws/src/aruco_extrinsic/src/ArucoBoardDetector.cpp:49
Publishing detection images on /lappycam/detections
[ INFO] <1494230701.356161276> /camera_left_extrinsic @ /home/me/Projects/Nearlab/ros_ws/src/aruco_extrinsic/src/ArucoBoardDetector.cpp:55
Publishing detected marker poses and ids on /lappycam/markers
[ INFO] <1494230701.357764129> /camera_left_extrinsic @ /home/me/Projects/Nearlab/ros_ws/src/aruco_extrinsic/src/ArucoBoardDetector.cpp:59
Aruco board detector ready.
[ INFO] <1494230701.362489914> /camera_left_extrinsic @ /home/me/Projects/Nearlab/ros_ws/src/aruco_extrinsic/src/ArucoBoardDetector.cpp:66
Aruco board parameters: size = 6x4, length = 0.0222, sep = 0.004, dictionary = 1
The full list of variables that can be used in the ROS console format string cane be found in the documentation.
Debugging a node launched with rosrun
To run gdb on a node launched with rosrun simply add --prefix 'gdb -ex run' to the command line. This will start gdb and run the node.
A complete invocation might look something like this:
rosrun --prefix 'gdb -ex run' my_pkg my_node
Debugging a node launched with roslaunch
With roslaunch we don't control the command line like when we start the node with rosrun instead we can pass gdb command with the prefix parameter in the <node> declaration.
<node pkg="my_pkg" type="my_node" name="node" prefix="gb -ex run">
</node>
Check out the documentation for node to see the full list of parameters it accepts.