零知两轮小车高级版(零知mini主控)教程-超声波避障

本系列教程讲解零知小车套件以零知迷你板为主控,使用零知开源平台进行智能小车开发。
使用的硬件:零知智能小车套件-零知智能小车高级版(零知mini主控)

实现的功能:红外循迹、超声波避障、直线运行、手机蓝牙控制、灭火等基本功能。

扩展:可以在小车上加入其它传感器模块进行扩展,比如加入摄像头模块进行监控、加入更多超声波模块实现自动跟随...

高级版系列教程将使用多文件编程的方式来编写程序,教程主要包含以下几个部分:

零知两轮小车高级版(零知mini主控)系列教程-小车组装(点击跳转)

零知两轮小车高级版(零知mini主控)系列教程-程序控制电机开跑(点击跳转)

零知两轮小车高级版(零知mini主控)系列教程-加入蓝牙实现手机控制(点击跳转)

零知两轮小车高级版(零知mini主控)系列教程-红外循迹走S弯(点击跳转)

零知两轮小车高级版(零知mini主控)系列教程-超声波自动避障(点击跳转)

零知两轮小车高级版(零知mini主控)系列教程-灭火小车(点击跳转)

零知两轮小车高级版(零知mini主控)系列教程-使用PID走直线(点击跳转)

零知两轮小车高级版(零知mini主控)系列教程-魔术手(点击跳转)

零知两轮小车高级版(零知mini主控)系列教程-贴边行驶(点击跳转)

一、工具原料
(1)组装好的智能小车高级版


二、程序代码
避障小车主要是通过小车前面的超声波模块来感应障碍物并测量距离,舵机调整方向,选择无障碍的一边通行。高级版教程将使用多文件编程的方式来编写代码,完整工程可在下面下载,主程序代码如下所示:


				
/**********************************************************
*    文件: lzsmartcar_avoidance.ino      by 零知实验室([url=http://www.lingzhilab.com]www.lingzhilab.com[/url])
*    -^^- 零知开源,让电子制作变得更简单! -^^-
*    时间: 2019/12/11 15:23
*    说明: 
************************************************************/
#include "pinsdefine.h"
#include "motor.h"
#include "WS2812BIORGB.h"
#include "caravoidance.h"
 
//两轮车
#define MOTOR_2WD 1
 
#ifdef MOTOR_2WD
LZSmartCarMotor myMotor;
#else
LZSmartCarMotor myMotor(false);
#endif
 
CarAvoidance carAvoidance;
 
void doAvoidance()
{
        int dt = 10;//舵机速度
        int maxAbstacle = 28;//障碍物检测最大距离
         
                //首先探测正前方
        int disFor = 0;
        for(int i=0;i<5;i++)
        {
                disFor += carAvoidance.getDistance();
                delay(50);
        }
        disFor = disFor/5;
 
        Serial.print(" 前方距离:");
        Serial.println(disFor);
 
         
        if(disFor <= maxAbstacle)//正前方有障碍物
        {
                //停车
                myMotor.stop();
                 
                int t = 0;
                int total = 0;
                 
                //探测右边
                carAvoidance.servoRun(90,0,dt);
                 
                int disRight = 0;
                for(int i=0;i<5;i++)
                {
                        disRight += carAvoidance.getDistance();
                        delay(50);
                }
                disRight = disRight/5;
                 
                Serial.print(" 右侧距离:");
                Serial.println(disRight);
                 
                carAvoidance.servoRun(0,90,dt);
                 
                //探测左边
                carAvoidance.servoRun(90,200,dt);
                int disLeft = 0;
                for(int i=0;i<5;i++)
                {
                        disLeft += carAvoidance.getDistance();
                        delay(50);
                }
                disLeft = disLeft/5;
                 
                Serial.print(" 左侧距离:");
                Serial.println(disLeft);
                 
                carAvoidance.servoRun(200,90,dt);
                 
                int rollTime = 720;
                 
                //两边都有障碍物,掉头
                if((disLeft <= maxAbstacle) && (disRight <= maxAbstacle))
                {
                        myMotor.goWithDirec(carBack);
                        Serial.println("--后退");
                        delay(rollTime);
                }
                else if(disLeft >= disRight)
                {
                        myMotor.goWithDirec(carLeft);//左转
                        Serial.println("--左转");
                        delay(rollTime);
                }
                else if(disLeft < disRight)
                {
                        myMotor.goWithDirec(carRight);//右转
                        Serial.println("--右转");
                        delay(rollTime);
                }
        }
         
        //红外测量斜边
        int rotateSidetime = 90;
        bool isLeftIRAvoid = false;
        bool isRightIRAvoid = false;
        carAvoidance.getIRAvoidData(&isLeftIRAvoid, &isRightIRAvoid);
        if(isLeftIRAvoid){
                Serial.println("left ir avoid");
                 
                myMotor.goWithDirec(carRight);
                delay(rotateSidetime);
        }
        if(isRightIRAvoid){
                Serial.println("right ir avoid");
                 
                myMotor.goWithDirec(carLeft);
                delay(rotateSidetime);
        }
 
        myMotor.goWithDirec(carForwardSlow);
}
 
// 复位或上电后运行一次:
void setup() {
    //在这里加入初始化相关代码,只运行一次:
        Serial.begin(9600);
        Serial.println("开车啦...");
 
        WS2812BRGB_init();
         
        myMotor.stop();
 
}
 
//一直循环执行:
void loop() {
    // 在这里加入主要程序代码,重复执行:
         
        //炫酷跑马灯
        //left
        WS2812BRGB_flow(80,160,0xff0000,false);
        delay(100);
        //right
        WS2812BRGB_flow(80,160,0x00ff00,true);
        delay(100);
 
        //breath
        for(int i=0;i<5; i++)
        {
                WS2812BRGB_breath(5,0xff);
                delay(300);
        }
         
        //超声波避障
        doAvoidance();
 
}
				

完整工程:  lzsmartcar_v5.zip 

将以上代码验证上传至智能小车的零知mini板上,小车就可以实现智能避障了。