当前位置: 首页 > news >正文

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")
http://www.jsqmd.com/news/101377/

相关文章:

  • 拿捏 React 组件通讯:从父子到跨组件的「传功秘籍」
  • 如何快速为OBS直播添加专业级VST音频效果:终极完整指南
  • tensorflow 零基础吃透:tf.sparse.SparseTensor 与核心 TensorFlow API 的协同使用
  • 入职宇树Web前端开发,30K双休有点爽
  • ORACLE学习笔记总结(数据库归档模式的配置)
  • Applite:告别命令行,用图形界面轻松管理macOS软件包
  • 3步搞定老旧Mac升级:OpenCore Legacy Patcher USB启动盘制作全攻略
  • ORACLE学习笔记总结(数据库常见错误及应对措施)
  • 小白进阶 “挖洞大神”:SRC 漏洞挖掘完整攻略(附工具包 + 系统学习路径)
  • tensorflow 零基础吃透:TensorFlow 张量切片与数据插入(附目标检测 / NLP 实战场景)
  • WebPlotDigitizer:科研图表数据提取的终极完整指南
  • ThinkPad双风扇终极静音指南:TPFanCtrl2完整配置与优化
  • 微信网页版终极解决方案:wechat-need-web插件一键突破访问限制
  • C++ HTTP/2架构深度解析:从连接瓶颈到性能翻倍
  • [特殊字符]️ 羽毛球检测数据集介绍-1686张图片 运动赛事分析 智能健身设备 自动裁判系统 体育视频内容分析 机器人运动训练
  • Qwen3-32B实测:单卡A100跑出180+吞吐
  • BetterNCM终极个性化定制:从零打造专属网易云音乐深度改造方案
  • 车辆轮胎寿命预测检测数据集介绍-410张图片 车辆安全检测 车队管理和维护 智能汽车与ADAS系统 轮胎生产质量控制 道路交通监管 二手车评估与交易
  • Wallpaper_Engine终极指南:快速免费获取创意工坊壁纸的完整方案
  • 显卡驱动彻底清理终极指南:高效解决驱动冲突问题
  • ComfyUI-Manager完整教程:5步掌握AI绘画插件高效管理
  • 写论文软件挑到崩溃?听劝!别试了!虎贲等考 AI 已经把 “毕业级论文” 标准答案写好了
  • LobeChat能否支持量子计算模拟?前沿科技教育助手开发
  • 论文写作word插入公式显示灰色解决办法
  • 无人机赋能的双上行链路协同非正交多址接入中的自适应解码机制附Matlab代码
  • 实战避坑指南:小爱音箱变身智能AI助手的性能优化全攻略
  • LaTeX公式到Word转换工具:突破性的3步智能转换方案
  • AI 论文辅助对决!虎贲等考 AI:全流程赋能,稳坐毕业论文 “最强辅助” 宝座
  • 探索ChinaAdminDivisonSHP:地理数据背后的数字艺术
  • word论文插入mathtype公式方法