documentation

View project on GitHub

Creer un package ROS

##Révisions

Auteure : Eva Date : 5 decembre 2014
Idiot-proofé par :
Source: http://www.cse.sc.edu/~jokane/agitr/agitr-letter.pdf

First, source your environment:

source /opt/ros/**YOUR ROS VERSION HERE**/setup.bash  
example: source /opt/ros/indigo/setup.bash

##Creating a workspace Packages that you create should live together in a directory called a workspace.
$ mkdir -p ~/workspace_name/src
$ cd ~/workspace_name/src
$ catkin_init_workspac

Even though the workspace is empty (there are no packages in the ‘src’ folder, just a single CMakeLists.txt link) you can still “build” the workspace:

$ cd ~/worspace_name/
$ catkin_make

The catkin_make command is a convenience tool for working with catkin workspaces. If you look in your current directory you should now have a ‘build’ and ‘devel’ folder. Inside the ‘devel’ folder you can see that there are now several setup.*sh files. Sourcing any of these files will overlay this workspace on top of your environment.

Before continuing source your new setup.*sh file:

$ source devel/setup.bash

##Creating a package
$ cd ~/worspace_name/src
$ catkin_create_pkg package_name

This command creates a directory to hold the package and creates two configuration files inside that directory.

  • the manifest, package.xml (most of the information in this file is not utilized by ROS, neither at build time nor at run time, and only becomes really important if you release your package publicly)
  • the CMkaeLists.txt, wich contains a list of build instructions including what executables should be created, what source files to use to build each of them, and where to find the include files and libraries needed for those executables.

##Including a program

This source file, named sourcefile.cpp, belongs in your package folder, right next to package.xml and CMake-Lists.txt.

Example of a simple “Hello ROS!” program:

// This is a ROS version of the standard “hello , world” program.
// This header defines the standard ROS classes.
# include <ros/ros.h>
int main(int argc, char** argv)
{
// Initialize the ROS system. Must be called once at the beginning of the program. The last parameter is a string
//containing the default name of your node.
ros::init (argc, argv, “helloros”);
// Establish this program as a ROS node.
ros::NodeHandle nh;
// Send some output as a log message.
ROS_INFO_STREAM(“Hello, ROS!”) ;
}

##Compiling the program

Editing the CMakeList.txt:

# What version of CMake is needed?
cmake_minimum_required(VERSION 2.8.3)

# Name of this package.
project (package_name)

# Find the catkin build system, and any other packages on which we depend.
find_package (catkin REQUIRED COMPONENTS other package_names)

# Declare our catkin package.
catkin_package ()

# Specify locations of header files .
include_directories (include ${catkin_INCLUDE_DIRS})

# Declare the executable , along with its source files. If
# there are multiple executables, use multiple copies of
# this line.
add_executable (executable_name1 sourcefile1.cpp)
add_executable (executable_name2 sourcefile2.cpp)

# Specify libraries against which to link. Again, this
# line should be copied for each distinct executable in
# the package.
target_link_libraries (executable_name1 ${catkin_LIBRARIES})
target_link_libraries (executable_name2 ${catkin_LIBRARIES})

Once your CMakeList.txt is set up, you can build your workspace and compile all of the executables in all of its packages using this command in your workspace directory:
catkin_make

If there are compile errors, you’ll see them here. After correcting them, you can catkin-make again to complete the build process.

The final step is to execute a script called setup.bash, which is created by catkin_make inside the devel subdirectory of your workspace:
source devel/setup.bash

##Executing a program

Start by running the ROS master by using this command:
roscore

You should allow the master to continue running for the entire time that you’re using ROS. One reasonable workflow is to start roscore in one terminal, then open other terminals for your “real” work. There are not many reasons to stop roscore, except when you’ve finished working with ROS. When you reach that point, you can stop the master by typing Ctrl-C in its terminal.

Once you’ve started roscore, you can run programs that use ROS. A running instance of a ROS program is called a node.
Note: the phrase “running instance of” in this definition is important. If we execute multiple copies of the same program at the same time—taking care to ensure that each uses a different node name—each of those copies is treated as a separate node.

The basic command to create a node is rosrun:
rosrun package_name executable_name

*ROS provides a few ways to get information about the nodes that a re running at any particular time. To get a list of running nodes, try this command:
rosnode list

*You can explicitly set the name of a node as part of the rosrun command:
rosrun package_name executable_name __name:=node-name

*Inspecting a node: you can get some information about a particular node using this command:
rosnode info node-name

*Killing a node:
rosnode kill node-name