|
@@ -1,34 +1,118 @@
|
|
|
# 3.无线通讯-了解传输原理
|
|
|
|
|
|
+你好,我是爱吃鱼香ROS的小鱼。在前面的学习中,我们一直通过串口通信来连接MicroROS,但一直扯着跟线是不是觉得很麻烦,本节我们利用开发板上的WIFI功能尝试使用无线的方式连接Agent。
|
|
|
+
|
|
|
## 一、新建工程并添加依赖
|
|
|
|
|
|
-1.1 新建工程
|
|
|
+### 1.1 新建工程
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+### 1.2 添加依赖&修改配置
|
|
|
+
|
|
|
+修改`platformio.ini`
|
|
|
+
|
|
|
+```ini
|
|
|
+[env:featheresp32]
|
|
|
+platform = espressif32
|
|
|
+board = featheresp32
|
|
|
+framework = arduino
|
|
|
+board_microros_transport = wifi
|
|
|
+lib_deps =
|
|
|
+ https://gitee.com/ohhuo/micro_ros_platformio.git
|
|
|
+```
|
|
|
|
|
|
-1.2 添加依赖
|
|
|
+ 注意这里的配置,我们多添加了一个`board_microros_transport`,这个配置值我们给的是wifi,表示无线传输。
|
|
|
+
|
|
|
+除了WIFI还支持其他方式,比如蓝牙,但是需要们自定义协议,后续FishBot开发教程中,小鱼将带你一起实现自定义协议。
|
|
|
|
|
|
## 二、编写代码
|
|
|
|
|
|
-## 三、代码注解&API介绍
|
|
|
+代码相对于串口通信只改变了三行,主要是设置wifi传输函数——`set_microros_wifi_transports`,需要传入wifi名称,密码,电脑IP,端口号四个参数。电脑IP获取方式请参考第三部分。
|
|
|
+
|
|
|
+```c++
|
|
|
+#include <Arduino.h>
|
|
|
+#include <micro_ros_platformio.h>
|
|
|
+#include <WiFi.h>
|
|
|
+#include <rcl/rcl.h>
|
|
|
+#include <rclc/rclc.h>
|
|
|
+#include <rclc/executor.h>
|
|
|
+
|
|
|
+rclc_executor_t executor;
|
|
|
+rclc_support_t support;
|
|
|
+rcl_allocator_t allocator;
|
|
|
+rcl_node_t node;
|
|
|
+
|
|
|
+void setup()
|
|
|
+{
|
|
|
+ Serial.begin(115200);
|
|
|
+ // 设置通过WIFI进行MicroROS通信
|
|
|
+ IPAddress agent_ip;
|
|
|
+ agent_ip.fromString("192.168.2.105");
|
|
|
+ // 设置wifi名称,密码,电脑IP,端口号
|
|
|
+ set_microros_wifi_transports("fishbot", "12345678", agent_ip, 8888);
|
|
|
+ // 延时时一段时间,等待设置完成
|
|
|
+ delay(2000);
|
|
|
+ // 初始化内存分配器
|
|
|
+ allocator = rcl_get_default_allocator();
|
|
|
+ // 创建初始化选项
|
|
|
+ rclc_support_init(&support, 0, NULL, &allocator);
|
|
|
+ // 创建节点 microros_wifi
|
|
|
+ rclc_node_init_default(&node, "microros_wifi", "", &support);
|
|
|
+ // 创建执行器
|
|
|
+ rclc_executor_init(&executor, &support.context, 1, &allocator);
|
|
|
+}
|
|
|
+
|
|
|
+void loop()
|
|
|
+{
|
|
|
+ delay(100);
|
|
|
+ // 循环处理数据
|
|
|
+ rclc_executor_spin_some(&executor, RCL_MS_TO_NS(100));
|
|
|
+}
|
|
|
+
|
|
|
+```
|
|
|
+
|
|
|
|
|
|
-## 四、下载测试
|
|
|
|
|
|
-4.1 编译下载
|
|
|
+## 三、电脑IP获取
|
|
|
|
|
|
-4.2 启动Agent
|
|
|
+打开一个新的终端,输入ip -4 a | grep inet看看电脑的ip地址,一般可以看到多个网卡的,此时可以忽略172(docker)和127(本地)开头的ip地址,剩下的一般就是我们要的ip地址,比如小鱼这里的就是192.168.0.105
|
|
|
|
|
|
-4.3 查看是否连通
|
|
|
+
|
|
|
|
|
|
-4.4 测试
|
|
|
|
|
|
|
|
|
-
|
|
|
+## 四、下载测试
|
|
|
|
|
|
+### 4.1 编译下载
|
|
|
|
|
|
+
|
|
|
|
|
|
+### 4.2 启动Agent
|
|
|
|
|
|
+打开终端输入指令,注意WIFI方式和之前的指令不太一样
|
|
|
|
|
|
```shell
|
|
|
docker run -it --rm -v /dev:/dev -v /dev/shm:/dev/shm --privileged --net=host microros/micro-ros-agent:$ROS_DISTRO udp4 --port 8888 -v6
|
|
|
```
|
|
|
|
|
|
-
|
|
|
+点击RST,正确连接上可以看到
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+### 4.3 测试是否连通
|
|
|
+
|
|
|
+```
|
|
|
+ros2 node list
|
|
|
+```
|
|
|
+
|
|
|
+看到节点表示连接成功~
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+## 五、总结
|
|
|
+
|
|
|
+本节开始,我们成功通过无线的方式将MicroROS连接到WIFI上来了,那问题来了,什么时候用串口模式,什么时候用WIFI模式呢?
|
|
|
+
|
|
|
+- 串口模式,适合当我们用树莓派等主控板在机器人上时,直接串口连接树莓派即可
|
|
|
+- WIFI模式,像FishBot一样,直接无线驱动机器人,WIFI此时就很合适了
|