坦克智能小车第六课:蓝牙/wifi控制

Byamber

坦克智能小车第六课:蓝牙/wifi控制

概述

在前面几课中我们把esp8266 uart wifi shield仅仅作为arduino uno的扩展板使用,并没有用到它的串口转wifi功能,都只是红外控制小车运动,在本课中,将介绍如何使用esp8266 uart wifi shield的串口转wifi功能,手机连接esp8266 uart wifi shield的wifi热点,用APP控制小车运动。此外,esp8266 uart wifi shield具备蓝牙接口,将蓝牙模块插到对应接口上,连接蓝牙,同样可以用手机app控制小车运动。

原理

esp8266 uart wifi shield在工作的时候会发射一个wifi热点,wifi名称默认叫”DoitWIFI_Config”,打开手机wifi,无需密码就能连接这个热点,将esp8266 uart wifi shield上拨码开关SW1都拨到ON的位置,表示esp8266与arduino uno串口已连接在一起,这样arduino的信息可以通过串口传给esp8266,然后esp8266再将arduino的串口信息通过wifi发送到手机app;同样,手机app可以将控制指令通过wifi发送给esp8266,esp8266通过串口发送arduino,最后达到控制目的。

蓝牙也是一样的原理,将蓝牙模块插到esp8266 uart wifi shield对应的接口上,把拨码开关SW1都拨到OFF(即12)的位置,表示arduino串口与esp8266断开连接,而已蓝牙模块连接在一起。打开手机蓝牙搜索并连接蓝牙,打开手机app,选择蓝牙连接,连接蓝牙。arduino就可以将信息通过串口发送到蓝牙模块,蓝牙模块再通过蓝牙传输到手机app;同样,也可以将控制指令通过蓝牙发送到蓝牙模块,蓝牙模块在通过串口发送到arduino,最终达到控制目的。

关于esp8266 uart wifi shiel更多信息,请看这里。

ESP8266 UART WIFI Shield use guide

软件

下载http://osoyoo.com/driver/tank_robot_lesson6.zip并解压文件,用arduino IDE打开tank_robot_lesson6.ino文件,下面对部分代码做简要说明。

//WiFi / Bluetooth through the serial control
void do_Uart_Tick()
{

  char Uart_Date=0;
  if(Serial.available()) 
  {
    size_t len = Serial.available();
    uint8_t sbuf[len + 1];
    sbuf[len] = 0x00;
    Serial.readBytes(sbuf, len);
    //parseUartPackage((char*)sbuf);
    memcpy(buffUART + buffUARTIndex, sbuf, len);//ensure that the serial port can read the entire frame of data
    buffUARTIndex += len;
    preUARTTick = millis();
    if(buffUARTIndex >= MAX_PACKETSIZE - 1) 
    {
      buffUARTIndex = MAX_PACKETSIZE - 2;
      preUARTTick = preUARTTick - 200;
    }
  }
  if(buffUARTIndex > 0 && (millis() - preUARTTick >= 100))//APP send flag to modify the obstacle avoidance parameters
  { //data ready
    buffUART[buffUARTIndex] = 0x00;
    if(buffUART[0]=='C') 
    {
      Serial.println(buffUART);
      Serial.println("You have modified the parameters!");//indicates that the obstacle avoidance distance parameter has been modified
      sscanf(buffUART,"CMD%d,%d,%d",&distancelimit,&sidedistancelimit,&turntime);
    }
    else  Uart_Date=buffUART[0];
    buffUARTIndex = 0;
  }
  switch (Uart_Date)    //serial control instructions
  {
    case '2':
            Drive_Status=MANUAL_DRIVE; Drive_Num=GO_ADVANCE;Serial.println("forward"); break;
    case '4':
            Drive_Status=MANUAL_DRIVE; Drive_Num=GO_LEFT; Serial.println("turn left");break;
    case '6':
            Drive_Status=MANUAL_DRIVE; Drive_Num=GO_RIGHT; Serial.println("turn right");break;
    case '8':
            Drive_Status=MANUAL_DRIVE; Drive_Num=GO_BACK; Serial.println("go back");break;
    case '5':
            Drive_Status=MANUAL_DRIVE; Drive_Num=STOP_STOP;buzz_off();Serial.println("stop");break;
    case '3':
            Drive_Status=AUTO_DRIVE_UO; Serial.println("avoid obstacles...");break;
    case '1':
          Drive_Status=AUTO_DRIVE_LF; Serial.println("line follow...");break;
    default:break;
  }
}

上面的程序用于接收app发送的控制命令。

测试

step 1 烧录程序:用usb线把坦克下车与pc连接起来,选择正确的板子型号和端口号,将esp8266 uart wifi shield上SW1拨到off,如果插了蓝牙模块,需要拔掉蓝牙模块。把程序烧录到arduino后,如果用wifi控制就把SW1拨到ON位置;若用蓝牙控制,就把SW1拨到OFF位置,把蓝牙模块插到对应接口。

step2 下载app并安装。

app下载链接:http://osoyoo.com/driver/car.apk.

app源码链接:http://osoyoo.com/driver/BTcar-master.zip

目前,手机app只支持安卓,并且,提供手机APP源码,可以根据自己需要,修改源码,个性化定制app。

step3 打开电池盒电源,如果想用wifi控制,打开手机wifi,找到名叫“DoitWIFI_Config”的热点并连接

step4 Open app>> select WiFi mode>> then you can controller the robot car through wifi

step 5 If you want to control the robot car through Bluetooth, please turn on bluetooth of your Android phone which you have installed APP and scan bluetooth(different bluetooth module will scan different bluetooth name), Click connect and enter password “1234” or “0000” if no change (You can change wifi or bluetooth settings in Android phone to switch to control car through wifi or bluetooth):

Step 6) Open app>> select bluetooth mode>> then you can controller the robot car through bluetooth:

There are three working mode: manual control, obstacle avoidance and tracking. Users can switch freely among three working mode.
1) In manual control mode, you can click buttons (^) (V) (<) (>) to control the Robot car to move forward and backward, turn right and left. Meanwhile, the APP can observe the car real time movement.

2) Click “tracking” button of App to switch the current mode to tracking mode. The Robot car will move forward along the black line in white background. Meanwhile, the APP can observe the car real time movement. Press “||” button to stop moving and click other button to change Robot car working mode. To learn more about this mode, please review our lesson 4.

3) Click “Obstacle” button to switch the current mode to obstacle avoidance mode. The Robot car can be driven forward continuously and stopped and steered away once any obstacle in its way. Press “||” button to stop moving and then click other button to change Robot car working mode. To learn more about this mode, please review our lesson 5.

About the Author

amber administrator

You must be logged in to post a comment