上节课我们实现了小飞机的前向避障,但是还是有许多的不足的地方。比如我们的飞机在一个密闭的空间内部执行任务的时候,一直持续上升会碰到障碍物那就糟糕啦!所以我们要想办法解决这个问题~
材料清单
- TT 无人机 X1
- 扩展件 X1
- Urm10超声波 gravity4p线 X1
- VL53L0X激光测距传感器 X1 链接
- 连接线 X2
- X1
步骤1 硬件连接
也是4线的连接方式,这次的通讯协议是I2C,好在我们的TT扩展件上,所有的引脚都可以用I2C的功能
注意接27 28 引脚我忘了这个!!!!!!!!!!!!!!光传感器接这个
这个引脚的连接可以看上篇文章
超声波放前面的话重心前倾,在前进过程中加速曲线会陡峭,所以这次将传感器这样放置,减少前倾加速曲线的陡峭度。
将激光测距传感器这样放置,充当前向避障传感器
一张侧剖图
斜剖图
步骤2 编写程序
这个是积木程序,注释很详细
代码
/*!
* MindPlus
* telloesp32
*
*/
#include <RMTT_Libs.h>
#include <DFRobot_URM10.h>
#include <DFRobot_VL53L0X.h>
// 创建对象
DFRobot_VL53L0X vl53l0x;
RMTT_RGB tt_rgb;
RMTT_Protocol protocol;
DFRobot_URM10 urm10;
// 主程序开始
void setup() {
tt_rgb.Init();
led_effect_init();
Serial1.begin(1000000, 23, 18, SERIAL_8N1);
// 此传感器的使用之前得执行很多的初始化工作,这个积木干这个活
vl53l0x.begin();
// 此时设置为低精度,够用而且降低处理器的负载,测量模式设置为连续
vl53l0x.setMode(vl53l0x.Low, vl53l0x.Continuous);
// 此时让我们的RGB在两个颜色之间变换
led_effect_blink(255, 255, 255, 255, 255, 255, 1);
delay(1000);
// 不要忘记这个积木的选择
protocol.startUntilControl();
delay(1000);
// 飞机预热中
protocol.sendTelloCtrlMsg("motoron");
delay(1000);
// 向上飞行50厘米
protocol.sendTelloCtrlMsg("takeoff");
// 飞机离地一米
protocol.sendTelloCtrlMsg((char *)String(String("up ")+int(50)).c_str());
}
void loop() {
// 此时飞机离地1.5米,再向前飞50厘米
protocol.sendTelloCtrlMsg((char *)String(String("up ")+int(50)).c_str());
protocol.sendTelloCtrlMsg((char *)String(String("forward ")+int(50)).c_str());
// 这个是单说向前的传感器的情况
// 此时超声波是向上避障,监测是否小于25厘米
if (((urm10.getDistanceCM(13, 14))<25)) {
// 报警的颜色,红色
tt_rgb.SetRGB(255,0,0);
// 悬停,不要做任何动作,开始避开障碍
protocol.sendTelloCtrlMsg("stop");
delay(1000);
// 先下降20厘米,避开先上的障碍物
protocol.sendTelloCtrlMsg((char *)String(String("down ")+int(20)).c_str());
delay(1000);
// 就是不在一个直角的伽罗里面,两个传感器的值都很大
// 此时超声波测量大于25的话,执行
if ((((urm10.getDistanceCM(13, 14))>25) && (vl53l0x.getDistance()<25))) {
// 寻找三个方向去避障
for (int index = 0; index < 3; index++) {
tt_rgb.SetRGB(0,0,255);
protocol.sendTelloCtrlMsg((char *)String(String("cw ")+int(90)).c_str());
protocol.sendTelloCtrlMsg((char *)String(String("forward ")+int(50)).c_str());
}
}
}
else if ((vl53l0x.getDistance()<25)) {
protocol.sendTelloCtrlMsg("stop");
delay(1000);
protocol.sendTelloCtrlMsg((char *)String(String("back ")+int(50)).c_str());
// 也是循环三个方向寻找无障碍的方向飞行
for (int index = 0; index < 3; index++) {
if ((vl53l0x.getDistance()<25)) {
tt_rgb.SetRGB(0,255,0);
delay(1000);
protocol.sendTelloCtrlMsg((char *)String(String("cw ")+int(90)).c_str());
protocol.sendTelloCtrlMsg((char *)String(String("forward ")+int(20)).c_str());
}
}
}
else {
// 这个代码的意思是如果两个传感器的值是相同的,先倒后降。然后降落
if (((urm10.getDistanceCM(13, 14))==vl53l0x.getDistance())) {
tt_rgb.SetRGB(255,255,0);
protocol.sendTelloCtrlMsg((char *)String(String("back ")+int(20)).c_str());
protocol.sendTelloCtrlMsg((char *)String(String("down ")+int(20)).c_str());
delay(1000);
protocol.sendTelloCtrlMsg("land");
}
}
}
步骤3 程序讲解
程序的的主体框架和上文类似,但有升级的地方。使用了循环语句来进行多次判断要飞行的方向,注意每次前进都是先前进再上升,是阶梯式上升。
目前我们的飞机已经有了前向+向下以及向上避障,我们的小飞机现在已经飞起来很安全啦!如果你的传感器足够多,可以加向后避障,让小飞机的安全指数更上一层楼。至于程序就是扩展相同的语句段落。感兴趣的快来试试吧!
附件
评论