当前位置:   article > 正文

【ROS2机器人入门到实战】话题订阅-控制LED_鱼香ros

鱼香ros

1.话题订阅-控制LED

写在前面

  1. 当前平台文章汇总地址:ROS2机器人从入门到实战
  2. 获取完整教程及配套资料代码,请关注公众号<鱼香ROS>获取
  3. 教程配套机器人开发平台:两驱版| 四驱版
  4. 为方便交流,搭建了机器人技术问答社区:地址 fishros.org.cn

你好,我是爱吃鱼香ROS的小鱼。本节我们正式进入到MicroROS的核心通信部分的学习中来,本节我们将通过话题订阅实现,通过话题控制LED的亮灭。

一、新建工程添加依赖

新建example11_microros_topic_sub工程

在这里插入图片描述

修改platformio.ini添加依赖

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:featheresp32]
platform = espressif32
board = featheresp32
framework = arduino
lib_deps = 
    https://gitee.com/ohhuo/micro_ros_platformio.git
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

二、编写代码-实现订阅

编辑main.cpp,代码如下,注释小鱼已经添加到代码中来了

#include <Arduino.h>
#include <micro_ros_platformio.h>

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

#include <std_msgs/msg/int32.h>

rclc_executor_t executor;
rclc_support_t support;
rcl_allocator_t allocator;
rcl_node_t node;
// 声明话题订阅者
rcl_subscription_t subscriber;
// 声明消息文件
std_msgs__msg__Int32 sub_msg;
// 定义话题接收回调函数
void callback_subscription_(const void *msgin)
{
  const std_msgs__msg__Int32 *msg = (const std_msgs__msg__Int32 *)msgin;
  if (msg->data == 0)
  {
    digitalWrite(2, HIGH);
  }
  else
  {
    digitalWrite(2, LOW);
  }
}

void setup()
{
  Serial.begin(115200);
  // 设置通过串口进行MicroROS通信
  set_microros_serial_transports(Serial);
  // 延时时一段时间,等待设置完成
  delay(2000);
  // 初始化内存分配器
  allocator = rcl_get_default_allocator();
  // 创建初始化选项
  rclc_support_init(&support, 0, NULL, &allocator);
  // 创建节点 topic_sub_test
  rclc_node_init_default(&node, "topic_sub_test", "", &support);
  // 订阅者初始化
  rclc_subscription_init_default(
      &subscriber,
      &node,
      ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32),
      "led_control");
  // 创建执行器
  rclc_executor_init(&executor, &support.context, 1, &allocator);
  // 为执行器添加一个订阅者
  rclc_executor_add_subscription(&executor, &subscriber, &sub_msg, &callback_subscription_, ON_NEW_DATA);
  // 初始化LED
  pinMode(2, OUTPUT);
}

void loop()
{
  delay(100);
  // 循环处理数据
  rclc_executor_spin_some(&executor, RCL_MS_TO_NS(100));
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65

三、代码注解

相比之前的节点代码这里主要多了这几行

  • #include <std_msgs/msg/int32.h> 添加消息类型头文件
  • rcl_subscription_t subscriber; 声明话题订阅者
  • std_msgs__msg__Int32 sub_msg; 声明消息文件,这一点和上位机不同,因为内存紧缺,所以提前定义
  • void callback_subscription_(const void *msgin) 接收到数据的回调函数
  • rclc_subscription_init_default 初始化话题订阅者
  • rclc_executor_add_subscription(&executor, &subscriber, &sub_msg, &callback_subscription_, ON_NEW_DATA);,为执行器添加一个订阅者

四、下载测试

4.1 编译下载

连接开发板,编译下载。

在这里插入图片描述

4.2 启动Agent

接着打开终端启动agent

sudo docker run -it --rm -v /dev:/dev -v /dev/shm:/dev/shm --privileged --net=host microros/micro-ros-agent:$ROS_DISTRO serial --dev /dev/ttyUSB0 -v
  • 1

点击下RST按钮,重启开发板,正常可以看到下图内容

在这里插入图片描述

4.3 查看是否连通

接着打开终端查看节点和话题

ros2 node list
ros2 topic list
  • 1
  • 2

在这里插入图片描述

4.4 测试控制

关闭LED

ros2 topic pub /led_control  std_msgs/msg/Int32 "{data: 0}" --once 
  • 1

打开LED

ros2 topic pub /led_control  std_msgs/msg/Int32 "{data: 1}" --once 
  • 1

五、总结

本节我们通过话题订阅,实现对开发板上LED的控制,下一节我们将尝试读取开发板上的VM引脚电压,并将其通过话题发布到上位机中。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/908467
推荐阅读
相关标签
  

闽ICP备14008679号