美文网首页
espduino循迹避障小车

espduino循迹避障小车

作者: Walk_In_Jar | 来源:发表于2018-12-28 15:51 被阅读0次
#define BLINKER_WIFI
#include <Blinker.h>
#include <Servo.h>
Servo myServo;  //舵机

char auth[] = "d0cdb48b50fc";
char ssid[] = "Arduino_Wifi";
char pswd[] = "12345678";

#define LIN1       15//pwm
#define LIN2       14
#define RIN1       12//pwm
#define RIN2       13

#define BUTTON_1 "btn-switch"//总开关
#define BUTTON_2 "btn-manual"//手动模式
#define BUTTON_3 "btn-auto"//自动模式
#define BUTTON_4 "btn-track"//循迹模式
#define BUTTON_5 "btn-igncol"//忽略碰撞

#define BUTTON_6 "btn-front"//前进
#define BUTTON_7 "btn-left"//左转
#define BUTTON_8 "btn-back"//后退
#define BUTTON_9 "btn-right"//右转

#define lf D5//避障模块
#define rf D4
#define lb D2
#define rb D0

//********* 循迹模式
//#define tlS D3//左循迹
//#define trS D16//右循迹
//*********
//***********自动模式
#define outputPin D3//超声波触发
#define inputPin D16//超声波Echo回
//**********
//按钮绑定
BlinkerButton Button1(BUTTON_1);
BlinkerButton Button2(BUTTON_2);
BlinkerButton Button3(BUTTON_3);
BlinkerButton Button4(BUTTON_4);
BlinkerButton Button5(BUTTON_5);

BlinkerButton Button6(BUTTON_6);
BlinkerButton Button7(BUTTON_7);
BlinkerButton Button8(BUTTON_8);
BlinkerButton Button9(BUTTON_9);

bool isCollision = false;
bool MasterSwitch = false; //总开关
int rType = 0; // 0转弯  1 前进  2后退
bool ignCol = false;//忽略碰撞
enum ControlType
{
  manual , autos, track, def
} CType = def;

void setup() {
  Serial.begin(115200);
  BLINKER_DEBUG.stream(Serial);
  analogWriteRange(255);  //PWN 最大值设置为255
  blinker_car_init();

  Blinker.begin(auth, ssid, pswd);

  Button1.attach(button1_callback);//按钮绑定 总开关
  Button2.attach(button2_callback);//手动模式
  Button3.attach(button3_callback);//自动模式
  Button4.attach(button4_callback);//循迹模式
  Button5.attach(button5_callback);//忽略避障

  Button6.attach(button6_callback);//前进
  Button7.attach(button7_callback);//左转
  Button8.attach(button8_callback);//后退
  Button9.attach(button9_callback);//右转
}
void blinker_car_init()
{
  pinMode(LIN1, OUTPUT);//电机控制引脚初始化
  pinMode(LIN2, OUTPUT);
  pinMode(RIN1, OUTPUT);
  pinMode(RIN2, OUTPUT);
  StopCar();


  pinMode(lf, INPUT);//避障引脚初始化
  pinMode(rf, INPUT);
  pinMode(lb, INPUT);
  pinMode(rb, INPUT);

  //****************
  // pinMode(tlS, INPUT); //定义左循迹红外传感器为输入
  // pinMode(trS, INPUT); //定义右循迹红外传感器为输入
  //*************
  myServo.attach(D1);
  pinMode(outputPin, OUTPUT);//超声波传感器发射
  pinMode(inputPin, INPUT);
  //*************
}
void loop() {
  CheckCollision();
  // put your main code here, to run repeatedly:
  Blinker.run();
  if (CType == autos)
  {
    StartAuto();
  }
  else if (CType == track) {
    StartTrack();
  }
  else if (CType == def) {
    StopCar();
  }
}
void CheckCollision()//检测碰撞
{
  if (rType == 1) //前进中
  {
    if (!digitalRead(lf) || !digitalRead(rf))
    {
      if (!ignCol)
      {
        isCollision = true;
        StopCar();
      }
    }
    else
    {
      isCollision = false;
    }
  }
  else if (rType == 2) //后退中
  {
    if (!digitalRead(lb) || !digitalRead(rb))
    {
      if (!ignCol)
      {
        isCollision = true;
        StopCar();
      }
    }
    else
    {
      isCollision = false;
    }
  }
  else {
    isCollision = false;
  }
}
void StartAuto()//自动模式
{
  int pos;
  int dis[3];//距离
  blinker_car_control(255, 0, 255, 0);
  myServo.write(90);
  dis[1] = getDistance(); //中间

  if (dis[1] < 30 )
  {
    StopCar();
    for (pos = 90; pos <= 150; pos += 1)//
    {
      myServo.write(pos);              // tell servo to go to position in variable 'pos'
      Blinker.delay(15);                      // waits 15ms for the servo to reach the position
    }
    dis[2] = getDistance(); //左边
    for (pos = 150; pos >= 30; pos -= 1)
    {
      myServo.write(pos);              // tell servo to go to position in variable 'pos'
      Blinker.delay(15);                      // waits 15ms for the servo to reach the position
      if (pos == 90)
        dis[1] = getDistance(); //中间
    }
    dis[0] = getDistance(); //右边
    for (pos = 30; pos <= 90; pos += 1)
    {
      myServo.write(pos);              // tell servo to go to position in variable 'pos'
      Blinker.delay(15);                      // waits 15ms for the servo to reach the position
    }
    if (dis[0] < dis[2]) //右边距离障碍的距离比左边近
    {
      //左转
      blinker_car_control(0, 255, 255, 0);
      Blinker.delay(500);
    }
    else  //右边距离障碍的距离比左边远
    {
      //右转
      blinker_car_control(255, 0, 0, 255);
      Blinker.delay(500);
    }
  }

}
void StartTrack()
{
#if defined(tlS)
  if (!isCollision)
  {
    if (!digitalRead(tlS) && !digitalRead(trS))//都没有碰到线,前进
    {
      blinker_car_control(180, 0, 180, 0);
    }
    else if (digitalRead(tlS) && !digitalRead(trS))//右转
    {
      blinker_car_control(200, 0, 0, 0);
    }
    else if (!digitalRead(tlS) && digitalRead(trS))//左转
    {
      blinker_car_control(0, 0, 200, 0);
    }
    else//停止
    {
      StopCar();
    }
  }
#endif
}
void blinker_car_control(int in1, int in2, int in3, int in4)
{
  // 0转弯  1 前进  2后退
  if (in1 > 0)
  {
    if (in3 > 0)
    {
      rType = 1;
    } else {
      rType = 0;
    }
  }
  else if (in2 > 0)
  {
    if (in3 > 0)
    {
      rType = 0;
    } else {
      rType = 2;
    }
  }
  if (!isCollision)
  {
    analogWrite(LIN1, in1);
    analogWrite(LIN2, in2);
    analogWrite(RIN1, in3);
    analogWrite(RIN2, in4);
  }
}
void button1_callback(const String & state)//总开关
{
  if (state == BLINKER_CMD_ON)//切换为开
  {
    MasterSwitch = true;
    Button1.color("#00FF30");
    Button1.print("on");
  }
  else if (state == BLINKER_CMD_OFF) //切换为关
  {
    CType = def;
    MasterSwitch = false;
    StopCar();
    Button1.color("#FF0000");
    Button2.color("#000000");
    Button3.color("#000000");
    Button4.color("#000000");

    Button1.print("off");
    Button2.print();
    Button3.print();
    Button4.print();
  }
}

void button2_callback(const String & state)//开始手动
{
  if (CType != manual && MasterSwitch)
  {
    StopCar();
    CType = manual;
    if (state == BLINKER_CMD_BUTTON_TAP) {
      Button2.color("#00FF00");
      Button3.color("#000000");
      Button4.color("#000000");

      Button2.text("ManualModel");
      Button2.print();
      Button3.print();
      Button4.print();
    }
  }
}

void button3_callback(const String & state)//开始自动
{
  if ( CType != autos && MasterSwitch)
  {
    CType = autos;
    if (state == BLINKER_CMD_BUTTON_TAP)
    {
      Button3.color("#00FF00");
      Button2.color("#000000");
      Button4.color("#000000");

      Button3.text("AutoModel");
      Button2.print();
      Button3.print();
      Button4.print();
    }
  }
}
void button4_callback(const String & state)//开始循迹
{
  if ( CType != track && MasterSwitch)
  {
    CType = track;
    if (state == BLINKER_CMD_BUTTON_TAP)
    {
      Button4.color("#00FF00");
      Button2.color("#000000");
      Button3.color("#000000");

      Button4.text("TrackModel");
      Button2.print();
      Button3.print();
      Button4.print();
    }
  }
}
void button5_callback(const String & state)//忽略碰撞开关
{
  if (state == BLINKER_CMD_ON)//切换为开
  {
    ignCol = true;
    Button5.color("#ff0000");
    Button5.print("on");
  }
  else if (state == BLINKER_CMD_OFF) //切换为关
  {
    ignCol = false;
    Button5.color("#000000");
    Button5.print("off");
  }
}
void button6_callback(const String & state)//前进
{
  if (state == BLINKER_CMD_BUTTON_PRESSED && CType == manual) {
    blinker_car_control(255, 0, 255, 0);
    Button6.color("#00FF00");
    Button6.print();
  }
  else if (state == BLINKER_CMD_BUTTON_RELEASED && CType == manual) {
    StopCar();
    Button6.color("#000000");
    Button6.print();
  }
}
void button7_callback(const String & state)//左转
{
  if (state == BLINKER_CMD_BUTTON_PRESSED && CType == manual) {
    blinker_car_control(0, 255, 255, 0);
    Button7.color("#00FF00");
    Button7.print();
  }
  else if (state == BLINKER_CMD_BUTTON_RELEASED && CType == manual) {
    StopCar();
    Button7.color("#000000");
    Button7.print();
  }
}
void button8_callback(const String & state)//后退
{
  if (state == BLINKER_CMD_BUTTON_PRESSED && CType == manual) {
    blinker_car_control(0, 255, 0, 255);
    Button8.color("#00FF00");
    Button8.print();
  }
  else if (state == BLINKER_CMD_BUTTON_RELEASED && CType == manual) {
    StopCar();
    Button8.color("#000000");
    Button8.print();
  }
}
void button9_callback(const String & state)//右转
{
  if (state == BLINKER_CMD_BUTTON_PRESSED && CType == manual) {
    blinker_car_control(255, 0, 0, 255);
    Button9.color("#00FF00");
    Button9.print();
  }
  else if (state == BLINKER_CMD_BUTTON_RELEASED && CType == manual) {
    StopCar();
    Button9.color("#000000");
    Button9.print();
  }
}
void StopCar() {
  analogWrite(LIN1, 0);
  analogWrite(LIN2, 0);
  analogWrite(RIN1, 0);
  analogWrite(RIN2, 0);
}
int getDistance()
{
  digitalWrite(outputPin, LOW); // 使发出发出超声波信号接口低电平2μs
  delayMicroseconds(2);
  digitalWrite(outputPin, HIGH); // 使发出发出超声波信号接口高电平10μs,这里是至少10μs
  delayMicroseconds(10);
  digitalWrite(outputPin, LOW); // 保持发出超声波信号接口低电平
  int distance = pulseIn(inputPin, HIGH); // 读出脉冲时间
  distance = distance / 58.0; // 将脉冲时间转化为距离(单位:厘米)
  Serial.println(distance); //输出距离值

  if (distance >= 50)
  {
    //如果距离小于50厘米返回数据
    return 50;
  }//如果距离小于50厘米小灯熄灭
  else
    return distance;
}

相关文章

网友评论

      本文标题:espduino循迹避障小车

      本文链接:https://www.haomeiwen.com/subject/urfelqtx.html