ROS程序设计系列 - 5.实例helloworld
1. 源由
在做《Ardupilot开源飞控之FollowMe验证平台搭建》时,涉及到MAVLink
与VINS-Mono
和ego-Planner
的消息转换问题。
为此,我们结合前面关于ROS的编程的基础知识,完成uav_mavlink
框架工程。
- 【1】ROS程序设计系列 - 1.ROS介绍
- 【2】ROS程序设计系列 - 2.ROS Package
- 【3】ROS程序设计系列 - 3.ROS Tools
- 【4】ROS程序设计系列 - 4.ROS Programming
2. 步骤
Step 1:安装ROS系统
略,详见:Linux 35.5 + JetPack v5.1.3@ros-noetic安装
Step 2:创建框架工程
$ cd ~
$ mkdir -p catkin_ws/src
$ cd catkin_ws/src
$ catkin_create_pkg hello std_msgs roscpp rospy
Created file hello/package.xml
Created file hello/CMakeLists.txt
Created folder hello/include/hello
Created folder hello/src
Successfully created files in /home/daniel/catkin_ws/src/hello. Please adjust the values in package.xml.
Step 3:检查工程结构
此时没有任何代码文件,仅仅一个hello
工程。
$ tree catkin_ws/
catkin_ws/
└── src
└── hello
├── CMakeLists.txt
├── include
│ └── hello
├── package.xml
└── src
5 directories, 2 files
Step 4:创建CPP文件
创建hello.cpp
,并打印"hello,ROS!"
$ cd ~/catkin_ws/src/hello/src
$ vi hello.cpp
$ cat hello.cpp
#include<ros/ros.h>
int main(int argc,char **argv){
ros::init(argc,argv,"hello_ros");
ros::NodeHandle nh;
ROS_INFO_STREAM("hello,ROS!");
}
Step 5:修改配置文件
鉴于创建应用程序,需要设置可执行代码和编译链接。
vi CMakeLists.txt
修改下面两行代码:
add_executable(${PROJECT_NAME} src/hello.cpp)
target_link_libraries(${PROJECT_NAME} ${catkin_LIBRARIES})
Step 6:编译工程
$ cd ~/catkin_ws
$ $ catkin_make
Base path: /home/daniel/catkin_ws
Source space: /home/daniel/catkin_ws/src
Build space: /home/daniel/catkin_ws/build
Devel space: /home/daniel/catkin_ws/devel
Install space: /home/daniel/catkin_ws/install
####
#### Running command: "cmake /home/daniel/catkin_ws/src -DCATKIN_DEVEL_PREFIX=/home/daniel/catkin_ws/devel -DCMAKE_INSTALL_PREFIX=/home/daniel/catkin_ws/install -G Unix Makefiles" in "/home/daniel/catkin_ws/build"
####
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Using CATKIN_DEVEL_PREFIX: /home/daniel/catkin_ws/devel
-- Using CMAKE_PREFIX_PATH: /opt/ros/noetic
-- This workspace overlays: /opt/ros/noetic
-- Found PythonInterp: /usr/bin/python3 (found suitable version "3.8.10", minimum required is "3")
-- Using PYTHON_EXECUTABLE: /usr/bin/python3
-- Using Debian Python package layout
-- Found PY_em: /usr/lib/python3/dist-packages/em.py
-- Using empy: /usr/lib/python3/dist-packages/em.py
-- Using CATKIN_ENABLE_TESTING: ON
-- Call enable_testing()
-- Using CATKIN_TEST_RESULTS_DIR: /home/daniel/catkin_ws/build/test_results
-- Forcing gtest/gmock from source, though one was otherwise available.
-- Found gtest sources under '/usr/src/googletest': gtests will be built
-- Found gmock sources under '/usr/src/googletest': gmock will be built
-- Found PythonInterp: /usr/bin/python3 (found version "3.8.10")
-- Found Threads: TRUE
-- Using Python nosetests: /usr/bin/nosetests3
-- catkin 0.8.10
-- BUILD_SHARED_LIBS is on
-- BUILD_SHARED_LIBS is on
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- ~~ traversing 1 packages in topological order:
-- ~~ - hello
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- +++ processing catkin package: 'hello'
-- ==> add_subdirectory(hello)
-- Configuring done
-- Generating done
-- Build files have been written to: /home/daniel/catkin_ws/build
####
#### Running command: "make -j6 -l6" in "/home/daniel/catkin_ws/build"
####
Scanning dependencies of target hello
[ 50%] Building CXX object hello/CMakeFiles/hello.dir/src/hello.cpp.o
[100%] Linking CXX executable /home/daniel/catkin_ws/devel/lib/hello/hello
[100%] Built target hello
3. 测试
Step 1: 启动ROS Core
启动一个ssh终端,执行以下命令:
$ cd ~/catkin_ws
$ source devel/setup.bash
$ roscore
... logging to /home/daniel/.ros/log/ac6ae1f0-6a8d-11ef-a182-200b7460c5b7/roslaunch-daniel-nvidia-15800.log
Checking log directory for disk usage. This may take a while.
Press Ctrl-C to interrupt
Done checking log file disk usage. Usage is <1GB.
started roslaunch server http://daniel-nvidia:38431/
ros_comm version 1.16.0
SUMMARY
========
PARAMETERS
* /rosdistro: noetic
* /rosversion: 1.16.0
NODES
auto-starting new master
process[master]: started with pid [15808]
ROS_MASTER_URI=http://daniel-nvidia:11311/
setting /run_id to ac6ae1f0-6a8d-11ef-a182-200b7460c5b7
process[rosout-1]: started with pid [15818]
started core service [/rosout]
Step 2: 启动Hello程序
启动一个ssh终端,执行以下命令:
$ cd ~/catkin_ws
$ source devel/setup.bash
$ rosrun hello hello
[ INFO] [1725434226.570962424]: hello,ROS!
4. 总结
上述方式就是类似《Linux应用程序之Helloworld入门》的第一步。
希望上述简单例程能够为后续uav_mavlink
代码建立一个基础框架。
$ tree catkin_ws/
catkin_ws/
└── src
└── hello
├── CMakeLists.txt
├── include
│ └── hello
├── package.xml
└── src
└── hello.cpp
注:示例代码下载链接。
5. 参考资料
【1】ROS程序设计系列 - 1.ROS介绍
【2】ROS程序设计系列 - 2.ROS Package
【3】ROS程序设计系列 - 3.ROS Tools
【4】ROS程序设计系列 - 4.ROS Programming