欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

Micro ROS教程:轻松实现Best Effort订阅者编程笔记

最编程 2024-07-25 11:54:27
...

测试环境:

  • 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