结果展示 QT版本的订阅者实例 / 官网的发布者实例

QT版本的订阅者实例

1.安装qt

我使用的版本5.14.2下载地址
现在qt下载无法访问了,需要0.0
下载 qt-opensource-linux-x64-5.14.2.run 直接运行
我选择的这些

其他环境的安装

sudo apt install gcc
sudo apt install g++
sudo apt install clang
sudo apt install clang++ 
sudo apt install make
sudo apt install cmake
sudo apt-get install libgl1-mesa-dev
sudo snap install cmake --classic

这里少了几步设置代理的,违反规定了发不上来

4.添加ROS 2 apt 仓库

sudo apt install software-properties-common
sudo add-apt-repository universe

5.添加证书

sudo apt update && sudo apt install curl gnupg lsb-release
下面这种方法会报错
//sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg

我们直接在网页下载文件
https://raw.githubusercontent.com/ros/rosdistro/master/ros.key
下载后将文件拷贝到
/usr/share/keyrings/ros-archive-keyring.gpg

添加ros仓库

echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://packages.ros.org/ros2/ubuntu $(source /etc/os-release && echo $UBUNTU_CODENAME) main" | sudo tee /etc/apt/sources.list.d/ros2.list > /dev/null

6.安装ros2包

sudo apt update
sudo apt upgrade
sudo apt install ros-humble-desktop

7.配置ros环境变量

source /opt/ros/humble/setup.bash
echo " source /opt/ros/humble/setup.bash" >> ~/.bashrc 

8.测试

发布 在命令行运行

ros2 run demo_nodes_cpp talker

订阅

ros2 run demo_nodes_py listener

9.新建QT项目

10.在pro中配置ros2环境

修改pro文件为:
注意这里如果你安装不是ros-humble-desktop或者路径不是/opt/ros/humble请修改为你的地址

#-------------------------------------------------
#
# Project created by QtCreator 2025-02-09T22:28:23
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = QtRos2Test
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

CONFIG += c++17

SOURCES += \
        main.cpp \
#        main_window.cpp \
        mainwindow.cpp
#        qnode.cpp \

HEADERS += \
#        main_window.hpp \
        mainwindow.h \
 \#        qnode.hpp
    rosth.h

FORMS += \
#        main_window.ui \
        mainwindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

INCLUDEPATH += /opt/ros/humble/include/rclcpp
DEPENDPATH +=   /opt/ros/humble/include/rclcpp

INCLUDEPATH += /opt/ros/humble/include/std_msgs
DEPENDPATH +=   /opt/ros/humble/include/std_msgs

INCLUDEPATH += /opt/ros/humble/include/rcl
DEPENDPATH +=   /opt/ros/humble/include/rcl

INCLUDEPATH += /opt/ros/humble/include/rcutils
DEPENDPATH +=   /opt/ros/humble/include/rcutils

INCLUDEPATH += /opt/ros/humble/include/rmw
DEPENDPATH +=   /opt/ros/humble/include/rmw

INCLUDEPATH += /opt/ros/humble/include/rcl_yaml_param_parser
DEPENDPATH +=   /opt/ros/humble/include/rcl_yaml_param_parser

INCLUDEPATH += /opt/ros/humble/include/rosidl_runtime_c
DEPENDPATH +=   /opt/ros/humble/include/rosidl_runtime_c

INCLUDEPATH += /opt/ros/humble/include/rosidl_typesupport_interface
DEPENDPATH +=   /opt/ros/humble/include/rosidl_typesupport_interface

INCLUDEPATH += /opt/ros/humble/include/rcpputils
DEPENDPATH +=   /opt/ros/humble/include/rcpputils

INCLUDEPATH += /opt/ros/humble/include/builtin_interfaces
DEPENDPATH +=   /opt/ros/humble/include/builtin_interfaces

INCLUDEPATH += /opt/ros/humble/include/rosidl_runtime_cpp
DEPENDPATH +=   /opt/ros/humble/include/rosidl_runtime_cpp

INCLUDEPATH += /opt/ros/humble/include/tracetools
DEPENDPATH +=   /opt/ros/humble/include/tracetools

INCLUDEPATH += /opt/ros/humble/include/rcl_interfaces
DEPENDPATH +=   /opt/ros/humble/include/rcl_interfaces

INCLUDEPATH += /opt/ros/humble/include/libstatistics_collector
DEPENDPATH +=   /opt/ros/humble/include/libstatistics_collector

INCLUDEPATH += /opt/ros/humble/include/statistics_msgs
DEPENDPATH +=   /opt/ros/humble/include/statistics_msgs

LIBS += -L/opt/ros/humble/lib -lrclcpp -lstd_msgs__rosidl_typesupport_cpp -lpthread -llibstatistics_collector

LIBS += -L/opt/ros/humble/lib -lrcl -lstatistics_msgs__rosidl_typesupport_cpp -ltracetools -lrmw -lrcutils

LIBS += -L/opt/ros/humble/lib -lrcl_interfaces__rosidl_typesupport_cpp -lrcl_yaml_param_parser -lrosgraph_msgs__rosidl_typesupport_cpp -lrmw_implementation

LIBS += -L/opt/ros/humble/lib -lament_index_cpp -lrcl_logging_interface -lrcpputils -lrosidl_typesupport_cpp -lrcl_logging_spdlog -lrcl_interfaces__rosidl_typesupport_c

LIBS += -L/opt/ros/humble/lib -lrosidl_runtime_c -lrcl_interfaces__rosidl_generator_c -lrosidl_typesupport_c -lbuiltin_interfaces__rosidl_generator_c


11.安装离线版QT-Ros版

链接地址
下载获得
qtcreator-ros-xenial-latest-offline-installer.run
全选然后下一步就可以了

12.测试代码

官网示例 订阅 / 发布

13.示例图片(过两天补上)

Logo

聚焦RDK与具身智能,汇集机器人开发教程、开源案例和技术交流

更多推荐