|
@@ -0,0 +1,168 @@
|
|
|
+# 1.话题订阅-控制LED
|
|
|
+
|
|
|
+你好,我是爱吃鱼香ROS的小鱼。本节我们正式进入到MicroROS的核心通信部分的学习中来,本节我们将通过话题订阅实现,通过话题控制LED的亮灭。
|
|
|
+
|
|
|
+## 一、新建工程添加依赖
|
|
|
+
|
|
|
+新建`example11_microros_topic_sub`工程
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+修改`platformio.ini`添加依赖
|
|
|
+
|
|
|
+```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
|
|
|
+```
|
|
|
+
|
|
|
+## 二、编写代码-实现订阅
|
|
|
+
|
|
|
+ 编辑main.cpp,代码如下,注释小鱼已经添加到代码中来了
|
|
|
+
|
|
|
+```c++
|
|
|
+#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));
|
|
|
+}
|
|
|
+
|
|
|
+```
|
|
|
+
|
|
|
+## 三、代码注解
|
|
|
+
|
|
|
+相比之前的节点代码这里主要多了这几行
|
|
|
+
|
|
|
+- `#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
|
|
|
+
|
|
|
+```shell
|
|
|
+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
|
|
|
+```
|
|
|
+
|
|
|
+点击下RST按钮,重启开发板,正常可以看到下图内容
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+### 4.3 查看是否连通
|
|
|
+
|
|
|
+接着打开终端查看节点和话题
|
|
|
+
|
|
|
+```shell
|
|
|
+ros2 node list
|
|
|
+ros2 topic list
|
|
|
+```
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+### 4.4 测试控制
|
|
|
+
|
|
|
+关闭LED
|
|
|
+
|
|
|
+```shell
|
|
|
+ros2 topic pub /led_control std_msgs/msg/Int32 "{data: 0}" --once
|
|
|
+```
|
|
|
+
|
|
|
+打开LED
|
|
|
+
|
|
|
+```shell
|
|
|
+ros2 topic pub /led_control std_msgs/msg/Int32 "{data: 1}" --once
|
|
|
+```
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+## 五、总结
|
|
|
+
|
|
|
+本节我们通过话题订阅,实现对开发板上LED的控制,下一节我们将尝试读取开发板上的VM引脚电压,并将其通过话题发布到上位机中。
|