micro_ros笔记——best_effort订阅者程序编写

本文介绍了如何在micro_ros环境中创建一个best_effort订阅者程序。通过修改下位机(ESP32上的micro_ros_arduino)的代码,设置定时器以200Hz频率发布数据,并在上位机编写cpp代码创建订阅者,使用`rclcpp::create_subscription`函数订阅与下位机相同的topic。实测中,成功连接并接收到了下位机发送的数据,通过rqt_graph可以查看到节点间的连接关系。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

测试环境:

  • vmware虚拟机+ubuntu20.04+ros2 foxy
  • esp32 (micro_ros_arduino)

0. 写在前面

  • micro_ros中的QoS有default和best_effort,其中使用best_effort发布数据能够最大化发布频率,但是接收端也需要使用best_effort的subscription
  • 本文编写一个best_effort的subscription

1. best_effort的subscription

1.1 下位机完整代码

  • 打开micro_ros中的示例代码:micro-ros_publisher.ino
  • 修改timer为5,即数据发布频率为200Hz
  • 使用rclc_publisher_init_best_effort替换rclc_publisher_init_default
  • 注释loop中的delay(100);
#include <micro_ros_arduino.h>

#include <stdio.h>
#include <rcl/rcl.h>
#include <rcl/error_handling.h>
#include <rclc/rclc.h>
#include <rclc/executor.h>

#include <std_msgs/msg/int32.h>

rcl_publisher_t publisher;
std_msgs__msg__Int32 msg;
rclc_executor_t executor;
rclc_support_t support;
rcl_allocator_t allocator;
rcl_node_t node;
rcl_timer_t timer;

#define LED_PIN 13

#define RCCHECK(fn) {
      rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){
     error_loop();}}
#define RCSOFTCHECK(fn) {
      rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值