ros 编写 helloworld 程序

时间:2023-02-11 09:17:37

转自:https://www.douban.com/note/519918637/

1 先建立工作空间~/catkin_ws
参阅www.douban.com/note/516838470/ 下的创建工作空间(转自Python培训)

2 在工作空间~/catkin_ws下创建包pkg目录beginner_tutorials
好处是通过创建包自动生成package.xml和CMakeLists.txt
可参阅www.douban.com/note/516843231/
xue@xue-pc:~/catkin_ws/src$ tree beginner_tutorials
beginner_tutorials
├── CMakeLists.txt
├── include
│ └── beginner_tutorials
├── package.xml
└── src
3 在/beginner_tutorials/src目录下创建hello.cpp源代码文件
xue@xue-pc:~/catkin_ws/src/beginner_tutorials/src$ vim hello.cpp
xue@xue-pc:~/catkin_ws/src/beginner_tutorials$ cd ..
xue@xue-pc:~/catkin_ws/src$ tree beginner_tutorials
beginner_tutorials
├── CMakeLists.txt
├── include
│ └── beginner_tutorials
├── package.xml
└── src
    └── hello.cpp

3 directories, 3 files

4 编写hello.cpp文件
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <ros/ros.h>
int main(int argc, char **argv) {
        ros::init(argc, argv, "helloros1234444");
        ros::NodeHandle n;
        ROS_INFO("Hello, ROS!");
        ros::spinOnce();
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

5 修改编译makefile文件 CMakeLists.txt
xue@xue-pc:~/catkin_ws/src/beginner_tutorials$ sudo vim CMakeList.txt
~~~~~~~~~~~~~~~~~~
cmake_minimum_required(VERSION 2.8.3)
project(beginner_tutorials)
find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs)
include_directories(${catkin_INCLUDE_DIRS})
add_executable(hello ./src/hello.cpp)
target_link_libraries(hello ${catkin_LIBRARIES})

6 回到~/catkin_ws编译程序
xue@xue-pc:~/catkin_ws$ catkin_make
Base path: /home/xue/catkin_ws
Source space: /home/xue/catkin_ws/src
Build space: /home/xue/catkin_ws/build
Devel space: /home/xue/catkin_ws/devel
Install space: /home/xue/catkin_ws/install
####
#### Running command: "make cmake_check_build_system" in "/home/xue/catkin_ws/build"
####
-- Using CATKIN_DEVEL_PREFIX: /home/xue/catkin_ws/devel
-- Using CMAKE_PREFIX_PATH: /home/xue/catkin_ws/devel;/opt/ros/indigo
-- This workspace overlays: /home/xue/catkin_ws/devel;/opt/ros/indigo
-- Using PYTHON_EXECUTABLE: /usr/bin/python
-- Using Debian Python package layout
-- Using empy: /usr/bin/empy
-- Using CATKIN_ENABLE_TESTING: ON
-- Call enable_testing()
-- Using CATKIN_TEST_RESULTS_DIR: /home/xue/catkin_ws/build/test_results
-- Found gtest sources under '/usr/src/gtest': gtests will be built
-- Using Python nosetests: /usr/bin/nosetests-2.7
-- catkin 0.6.14
-- BUILD_SHARED_LIBS is on
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- ~~ traversing 1 packages in topological order:
-- ~~ - beginner_tutorials
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- +++ processing catkin package: 'beginner_tutorials'
-- ==> add_subdirectory(beginner_tutorials)
-- Configuring done
-- Generating done
-- Build files have been written to: /home/xue/catkin_ws/build
####
#### Running command: "make -j8 -l8" in "/home/xue/catkin_ws/build"
####
Scanning dependencies of target hello
[100%] Building CXX object beginner_tutorials/CMakeFiles/hello.dir/src/hello.cpp.o
Linking CXX executable /home/xue/catkin_ws/devel/lib/beginner_tutorials/hello
[100%] Built target hello

7测试c++程序
7.1一个终端里运行roscore
7.2 另外一个终端里运行 rosrun beginner_tutorials hello
xue@xue-pc:~/catkin_ws$ rosrun beginner_tutorials hello
[ INFO] [1444370762.036540873]: Hello, ROS!