news 2026/6/10 17:37:29

ros2话题通讯实践-系统检测可视化工具

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
ros2话题通讯实践-系统检测可视化工具

流程

消息接口定义

在topic_practice_ws的src文件夹下创建 包

ros2 pkg create status_interfaces --build-type ament_cmake --dependencies rosidl_default_generators builtin_interfaces --license Apache-2.0 //builtin_interfaces 是ros2中已有的一个消息接口功能包 //可以使用时间接口Time表示记录消息的时间 //rosidl_default_generators用于将自定义的消息文件 //转化成cpp py 源码的模块

在功能包的msg目录下存放消息定义文件 其必须以大写字母开头 并且只能由大小写字母组成

这里创建SystemStatus.msg

builtin_interfaces/Time stamp //记录时间戳 string host_name //系统名称 float32 cpu_percent //cpu使用率 float32 memory_percent //内存使用率 float32 memory_total //内存总量 float32 memory_available //剩余有效内存 float64 net_sent //网络发送数据总量 float64 net_recv //网络接受数据总量
//ros2 消息接口支持的9种数据类型 //bool byte char float32 float64 //int8 uint8 int16 uint16 //int32 uint32 int64 uint64 //string

定义好数据接口文件后需要在CmakeLists.txt中进行注册,申明其是消息接口文件 并添加builtin_interfaces依赖

... rosidl_generate_interfaces(${PROJECT_NAME} "msg/SystemStatus.msg" DEPENDENCIES builtin_interfaces ) ament_package()

之后最好在package.xml中添加申明

<license>Apache-2.0</license> <member_of_group>rosidl_interface_packages</member_of_group> <buildtool_depend>ament_cmake</buildtool_depend>

再次之后可以构建项目 然后通过下列代码 来查看对消息接口的构建是否完成

source install/setup.bash ros2 interfaces show status_interfaces/msg/SystemStatus

也可以看install/status_interfaces/include/目录下是否生成了cpp头文件以及install/status_interfaces/local/lib/python3.10/dist-packages目录下是否生成了status_interfaces的py库来查看

话题发布节点

进入工作空间src目录下创建包

ros2 pkg create status_publisher --build-type ament_python --dependencies rclpy status_interfaces --license Apache-2.0

在同名目录下编辑sys_status_pub.py

import rclpy from rclpy.node import Node from status_interfaces.msg import SystemStatus #获取系统cpu 内存 网络信息 import psutil #获取主机名称 import platform class SysStatusPub(Node): def __init__(self,node_name): super().__init__(node_name) self.status_publisher_=self.create_publisher( SystemStatus,'sys_status',10) self.timer=self.create_timer(1,self.timer_callback) def timer_callback(self): cpu_percent = psutil.cpu_percent() memory_info=psutil.virtual_memory() net_io_counters=psutil.net_io_counters() msg=SystemStatus() #从Node继承而来 获取节点时钟时间 通过 to_msg()转换成 uiltin_interfaces.msg.Time消息 msg.stamp=self.get_clock().now().to_msg() #获取主机名 msg.host_name=platform.node() msg.cpu_percent=cpu_percent msg.memory_percent=memory_info.percent #默认是B 除以两次1024 换成 MB msg.memory_total=memory_info.total /1024 /1024 msg.memory_available=memory_info.available /1024 /1024 msg.net_sent =net_io_counters.bytes_sent /1024 /1024 msg.net_recv=net_io_counters.bytes_recv /1024/1024 self.get_logger().info(f'publish:{str(msg)}') self.status_publisher_.publish(msg) def main(): rclpy.init() node=SysStatusPub('sys_status_pub') rclpy.spin(node) rclpy.shutdown()

在此之后 编译 运行节点就可以看到发布的信息了。 节点运行时也可以 ros2 topic echo sys_status来查看。

消息展示节点

在工作空间src下创建 包

ros2 pkg create status_display --build-type ament_cmake --dependencies rclcpp status_interfaces --license Apache-2.0

在包下的src中编写hello_qt.cpp

#include<QApplication> //提供qt应用类 #include<QLabel> //qt显示文本的组件 #include<QString> //qt中的字符串类 int main(int argc,char **argv){ QApplication app(argc,argv); QLabel* label=new QLabel(); QString message=QString::fromStdString("Hello Qt"); label->setText(message); label->show(); //和ros2 的 spin类似 都会阻塞程序 app.exec(); return 0; }

在cmakelists中添加依赖和注册

... find_package(Qt5 REQUIRED COMPONENTS Widgets) add_executable(hello_qt src/hello_qt.cpp) #qt与ros2无关所以 #用这个而不是ament_target_dependcies target_link_libraries(hello_qt Qt5::Widgets) install(TARGETS hello_qt DESTINATION lib/${PROJECT_NAME} ) ...

之后运行 可以看到一个小窗口 说明配置顺利

在display包下src中编写 sys_status_display.cpp

#include <QApplication> #include <QLabel> #include <QString> #include "rclcpp/rclcpp.hpp" #include "status_interfaces/msg/system_status.hpp" using SystemStatus = status_interfaces::msg::SystemStatus; class SysStatusDisplay:public rclcpp::Node{ private: rclcpp::Subscription<SystemStatus>::SharedPtr subscription_; QLabel* label_; public: SysStatusDisplay():Node("sys_status_display"){ //匿名函数中[&]表示其可以通过引用的方式直接捕获外界变量 从而可以直接使用label_ subscription_=this->create_subscription<SystemStatus>("sys_status",10, [&](const SystemStatus::SharedPtr msg)->void{ label_->setText(get_qstr_from_msg(msg)); }); label_=new QLabel(get_qstr_from_msg(std::make_shared<SystemStatus>())); label_->show(); } QString get_qstr_from_msg(const SystemStatus::SharedPtr msg){ std::stringstream show_str; show_str <<"====================\n" <<"time:\t"<<msg->stamp.sec<<"\ts\n" <<"user:\t"<<msg->host_name<<"\t\n" <<"cpu:\t"<<msg->cpu_percent<<"\t%\n" <<"memory-total:\t"<<msg->memory_total<<"\tMB\n" <<"memory-available:\t"<<msg->memory_available<<"\tMB\n" <<"net-sent:\t"<<msg->net_sent<<"\tMB\n" <<"net-recv\t"<<msg->net_recv<<"\tMB\n" <<"===================="; return QString::fromStdString(show_str.str()); } }; int main(int argc,char** argv){ rclcpp::init(argc,argv); QApplication app(argc,argv); auto node=std::make_shared<SysStatusDisplay>(); //spin 和 exec都会阻塞程序 所以用多线程 std::thread spin_thread([&]()->void{rclcpp::spin(node);}); spin_thread.detach(); app.exec(); rclcpp::shutdown(); return 0; }

在cmakelists中添加依赖和注册

... add_executable(sys_status_display src/sys_status_display.cpp) target_link_libraries(sys_status_display Qt5::Widgets) ament_target_dependencies(sys_status_display rclcpp status_interfaces) install(TARGETS hello_qt sys_status_display DESTINATION lib/${PROJECT_NAME} ) ...

编译 然后同时运行 发布者和订阅者就可以看到 检测窗口了

杂项

出现了python版本的问题将接口信息转化成py模块的版本与运行版本不符 下列代码解决问题

rm -rf build/status_interfaces/ install/status_interfaces/ colcon build --packages-select status_interfaces --symlink-install --cmake-args -DPYTHON_EXECUTABLE=/usr/bin/python3.10 //或在cmakelists中 //在 project(status_interfaces) 之后添加 set(PYTHON_EXECUTABLE "/usr/bin/python3.10")
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/10 6:12:23

原神智能助手终极指南:全自动游戏体验完整教程

原神智能助手终极指南&#xff1a;全自动游戏体验完整教程 【免费下载链接】better-genshin-impact &#x1f368;BetterGI 更好的原神 - 自动拾取 | 自动剧情 | 全自动钓鱼(AI) | 全自动七圣召唤 | 自动伐木 | 自动派遣 | 一键强化 - UI Automation Testing Tools For Genshi…

作者头像 李华
网站建设 2026/6/10 14:57:53

5分钟掌握轮播指示器创意样式:让你的分页点从“路人“变“主角“

还在为轮播图千篇一律的圆点分页而苦恼&#xff1f;想让你的轮播指示器成为页面设计的亮点吗&#xff1f;本文将带你突破传统思维&#xff0c;用3种惊艳的创意方案彻底改造轮播dots样式&#xff0c;让分页指示器从功能组件升级为视觉焦点&#xff01; 【免费下载链接】slick th…

作者头像 李华
网站建设 2026/6/9 15:03:25

ORACLE学习笔记总结(数据库常见错误及应对措施)

一、语句失败&#xff08;Statement Failure&#xff09;定义SQL语句因语法错误、权限不足或资源限制而无法正常执行&#xff0c;是最轻微的故障类型。常见场景语法错误&#xff1a;SELEC * FROM emp;&#xff08;拼写错误&#xff09;权限不足&#xff1a;普通用户执行DROP TA…

作者头像 李华
网站建设 2026/6/10 14:59:50

ThinkPad双风扇终极静音指南:TPFanCtrl2完整配置与优化

ThinkPad双风扇终极静音指南&#xff1a;TPFanCtrl2完整配置与优化 【免费下载链接】TPFanCtrl2 ThinkPad Fan Control 2 (Dual Fan) for Windows 10 and 11 项目地址: https://gitcode.com/gh_mirrors/tp/TPFanCtrl2 还在为ThinkPad笔记本的持续风扇噪音而烦恼吗&#…

作者头像 李华
网站建设 2026/6/10 15:01:42

VS Code远程连接树莓派超详细教程(图文)

目录 一、前置准备&#xff1a;两端环境配置 1.1 树莓派端配置&#xff08;关键步骤&#xff09; ① 确保树莓派联网 ② 开启SSH服务&#xff08;必须&#xff09; ③ 获取树莓派IP地址 ④ 确认用户名/密码 1.2 电脑端配置 ① 安装VS Code ② 安装Remote - SSH插件和p…

作者头像 李华