当前位置: 首页 > 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.rkmt.cn/news/112165.html

相关文章:

  • ORACLE学习笔记总结(数据库归档模式的配置)
  • 小白进阶 “挖洞大神”:SRC 漏洞挖掘完整攻略(附工具包 + 系统学习路径)
  • 3步搞定老旧Mac升级:OpenCore Legacy Patcher USB启动盘制作全攻略
  • ORACLE学习笔记总结(数据库常见错误及应对措施)
  • C++ HTTP/2架构深度解析:从连接瓶颈到性能翻倍
  • LobeChat能否支持量子计算模拟?前沿科技教育助手开发
  • LaTeX公式到Word转换工具:突破性的3步智能转换方案
  • AI 论文辅助对决!虎贲等考 AI:全流程赋能,稳坐毕业论文 “最强辅助” 宝座
  • word论文插入mathtype公式方法
  • 小爱音箱智能升级实战:三步打造专属语音助手
  • 破局路侧感知困境:毫米波雷达+相机融合算法如何重塑智能交通
  • 告别“笨重”检测!VA-YOLO算法让疲劳驾驶识别更轻更快更准
  • 如何快速解密网易云NCM音乐文件:面向新手的完整指南
  • 高效集成秘籍:LobeChat对接私有化大模型全流程
  • 易语言夸克网盘操作,一键转存分享 全套源码开源
  • LobeChat能否支持自动驾驶模拟?交通场景描述与推演
  • [易语言源码] 懒人一键对接免费网验模块完工!利用TX微云实现,含拉黑源码
  • LobeChat邮件助手插件开发教程
  • 如何用Applite轻松管理Mac软件:告别复杂命令的终极指南
  • MapGIS Objects Java三维地形如何实现坡度分析
  • windows11启动进入grub命令行解决方法
  • Linux系统编程——线程
  • Molecular Operating Environment (MOE) 终极完整安装指南:快速掌握药物设计利器
  • FeHelper前端工具终极指南:快速上手的完整教程
  • MusicFree终极定制指南:3步打造你的专属音乐宇宙
  • Mac微信防撤回插件WeChatIntercept:终极完整使用指南
  • 2.2新一代信息技术及应用
  • 无奖
  • FeHelper:颠覆传统的前端开发效率倍增器
  • Flutter 进阶:构建高性能跨平台应用的实践与技巧