Writing ROS programs
参考:
A Gentle Introduction to ROS
Jason M. O’Kane
- Creating a workspace and a package
Creating a workspace:
$ mkdir workspaceName
Creating a package:
$ catkin_create_pkg agitr
- A simple program
(1) Writing source file,
named hello.cpp, belongs in your package folder, right next to package.xml and CMake-Lists.txt.
(2) Compiling the Hello program.
Declaring dependencies. To list dependencies, edit the CMakeLists.txt in your package directory.
$ find_package(catkin REQUIRED COMPONENTS roscpp)
list dependencies in the package manifest (package.xml), using the build-
_depend and run_depend elements:
Steps:
$ source /opt/ros/hydro/setup.bash
$ mkdir -p ~/ros_workspace/src
$ cd src
~/ros_workspace/src$ catkin_create_pkg agitr
~/ros_workspace/src/ros_package$ mkdir src
当package中的.cpp文件很多时,建一个src放源代码文件会有帮助,.cpp文件不多时也可以建。- Declaring dependencies
In CMakeLists.txt:
Dependencies on other catkin packages can be added in a COMPONENTS section on this line:
find_package(catkin REQUIRED COMPONENTS package-names)
比如:
find_package(catkin REQUIRED COMPONENTS
cv_bridge
dynamic_reconfigure
image_transport
image_geometry
kobuki_msgs
nodelet
pcl_ros
roscpp
std_msgs
turtlebot_msgs
visualization_msgs
tf
pcl_conversions
)
In manifest.xml:
list dependencies in the package manifest (package.xml)
We should also list dependencies in the package manifest (package.xml), using the build_depend and run_depend elements:
<build_depend>package-name</build_depend>
<run_depend>package-name</run_depend>
In our example, the hello program needs roscpp both at build time and at run time, so the
manifest should contain:
<build_depend>roscpp</build_depend>
<run_depend>roscpp</run_depend>
- Declaring an executable
In CMakeLists.txt:
add two lines to CMakeLists.txt declaring
the executable we would like to create. The general form is
add_executable(executable-name source-files)
target_link_libraries(executable-name ${catkin_LIBRARIES})
例子:
add_executable(hello src/hello.cpp)
target_link_libraries(hello ${catkin_LIBRARIES})
- Building the workspace
Once your CMakeLists.txt is set up, you can build your workspace—including
compiling all of the executables in all of its packages—using this command:
In workspace directory:
catkin_make