ROS程序设计系列 - 5.实例helloworld

1. 源由

在做《Ardupilot开源飞控之FollowMe验证平台搭建》时,涉及到MAVLinkVINS-Monoego-Planner的消息转换问题。

为此,我们结合前面关于ROS的编程的基础知识,完成uav_mavlink框架工程。

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修改下面两行代码:

  1. add_executable(${PROJECT_NAME} src/hello.cpp)
  2. 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

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ROS(Robot Operating System)是一个用于构建机器人应用程序的开源框架。下面是创建一个ROShelloworld程序的步骤: 1. 首先,确保你已经安装了ROS。可以通过以下命令检查ROS是否已经安装: ``` $ rosversion -d ``` 2. 创建一个ROS工作空间(workspace)。在终端中执行以下命令: ``` $ mkdir -p ~/catkin_ws/src $ cd ~/catkin_ws/ $ catkin_make ``` 3. 进入到src目录,并创建一个新的ROS软件包(package)。执行以下命令: ``` $ cd ~/catkin_ws/src $ catkin_create_pkg hello_world rospy std_msgs ``` 4. 进入到刚创建的软件包目录,并创建一个Python脚本文件。执行以下命令: ``` $ cd ~/catkin_ws/src/hello_world/src $ touch hello.py $ chmod +x hello.py ``` 5. 使用文本编辑器打开hello.py文件,并将以下代码复制粘贴到文件中: ```python #!/usr/bin/env python import rospy from std_msgs.msg import String def hello_callback(msg): rospy.loginfo("Received message: %s", msg.data) if __name__ == '__main__': rospy.init_node('hello_node') rospy.Subscriber('hello_topic', String, hello_callback) rospy.spin() ``` 6. 保存并关闭文件。 7. 返回到catkin_ws目录,并编译ROS软件包。执行以下命令: ``` $ cd ~/catkin_ws/ $ catkin_make ``` 8. 运行ROS节点。在终端中执行以下命令: ``` $ roscore ``` 9. 在另一个终端中,发布一个消息到hello_topic话题。执行以下命令: ``` $ rostopic pub hello_topic std_msgs/String "Hello, World!" ``` 10. 在第一个终端中,运行hello.py脚本。执行以下命令: ``` $ rosrun hello_world hello.py ``` 11. 现在,你应该能够在第一个终端中看到输出,显示收到的消息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值