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

2024-09-05 08:52

本文主要是介绍ROS程序设计系列 - 5.实例helloworld,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

  • 1. 源由
  • 2. 步骤
    • Step 1:安装ROS系统
    • Step 2:创建框架工程
    • Step 3:检查工程结构
    • Step 4:创建CPP文件
    • Step 5:修改配置文件
    • Step 6:编译工程
  • 3. 测试
    • Step 1: 启动ROS Core
    • Step 2: 启动Hello程序
  • 4. 总结
  • 5. 参考资料
  • 6. 补充

1. 源由

在做《Ardupilot开源飞控之FollowMe验证平台搭建》时,涉及到MAVLinkVINS-Monoego-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└── src5 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.0SUMMARY
========PARAMETERS* /rosdistro: noetic* /rosversion: 1.16.0NODESauto-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

6. 补充

如果对ROS编程想进一步深入,可以考虑看下笔者正在进行的一个简单mavlink桥接的代码(从CPP转换到ROS工程,并进行适当的灵活配置和代码重构)。

  • uav_mavlink

在这里插入图片描述

这篇关于ROS程序设计系列 - 5.实例helloworld的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1138495

相关文章

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析

《Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析》InstantiationAwareBeanPostProcessor是Spring... 目录一、什么是InstantiationAwareBeanPostProcessor?二、核心方法解

java String.join()方法实例详解

《javaString.join()方法实例详解》String.join()是Java提供的一个实用方法,用于将多个字符串按照指定的分隔符连接成一个字符串,这一方法是Java8中引入的,极大地简化了... 目录bVARxMJava String.join() 方法详解1. 方法定义2. 基本用法2.1 拼接

Linux lvm实例之如何创建一个专用于MySQL数据存储的LVM卷组

《Linuxlvm实例之如何创建一个专用于MySQL数据存储的LVM卷组》:本文主要介绍使用Linux创建一个专用于MySQL数据存储的LVM卷组的实例,具有很好的参考价值,希望对大家有所帮助,... 目录在Centos 7上创建卷China编程组并配置mysql数据目录1. 检查现有磁盘2. 创建物理卷3. 创

Java List排序实例代码详解

《JavaList排序实例代码详解》:本文主要介绍JavaList排序的相关资料,Java排序方法包括自然排序、自定义排序、Lambda简化及多条件排序,实现灵活且代码简洁,文中通过代码介绍的... 目录一、自然排序二、自定义排序规则三、使用 Lambda 表达式简化 Comparator四、多条件排序五、

Java实例化对象的​7种方式详解

《Java实例化对象的​7种方式详解》在Java中,实例化对象的方式有多种,具体取决于场景需求和设计模式,本文整理了7种常用的方法,文中的示例代码讲解详细,有需要的可以了解下... 目录1. ​new 关键字(直接构造)​2. ​反射(Reflection)​​3. ​克隆(Clone)​​4. ​反序列化

Python解决雅努斯问题实例方案详解

《Python解决雅努斯问题实例方案详解》:本文主要介绍Python解决雅努斯问题实例方案,雅努斯问题是指AI生成的3D对象在不同视角下出现不一致性的问题,即从不同角度看物体时,物体的形状会出现不... 目录一、雅努斯简介二、雅努斯问题三、示例代码四、解决方案五、完整解决方案一、雅努斯简介雅努斯(Janu

Python开发文字版随机事件游戏的项目实例

《Python开发文字版随机事件游戏的项目实例》随机事件游戏是一种通过生成不可预测的事件来增强游戏体验的类型,在这篇博文中,我们将使用Python开发一款文字版随机事件游戏,通过这个项目,读者不仅能够... 目录项目概述2.1 游戏概念2.2 游戏特色2.3 目标玩家群体技术选择与环境准备3.1 开发环境3

Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案

《Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案》:本文主要介绍Vue3组件中getCurrentInstance()获取App实例,但是返回nu... 目录vue3组件中getCurrentInstajavascriptnce()获取App实例,但是返回n

SQL表间关联查询实例详解

《SQL表间关联查询实例详解》本文主要讲解SQL语句中常用的表间关联查询方式,包括:左连接(leftjoin)、右连接(rightjoin)、全连接(fulljoin)、内连接(innerjoin)、... 目录简介样例准备左外连接右外连接全外连接内连接交叉连接自然连接简介本文主要讲解SQL语句中常用的表