Writing ROS programs

Writing ROS programs

参考:
A Gentle Introduction to ROS
Jason M. O’Kane

  1. Creating a workspace and a package
    Creating a workspace:
    $ mkdir workspaceName
    Creating a package:
    $ catkin_create_pkg agitr
  2. 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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值