App Inventor2 用蓝牙与树莓派小车通讯

App Inventor开发

App Inventor 2

Android应用开发者(英语:App Inventor)是一款卡通图形界面的Android智能手机应用程序开发软件。它起先由Google提供的应用软件,现在由麻省理工学院维护及营运。

借助IA2 建立一个简易的游戏手柄。因为AI2 的蓝牙串口通讯协议是基于SPP(Serial Port Profile)串行端口配置。我们目的是并设定相对应代码,在手机发送,在树莓派解析,并对小车进行运动控制。

对应代码如下:

  • u –> 前进 gofront()

  • d –> 后退 goback()

  • l –> 左转 turnleft()

  • r –> 右转 turnright()

  • s –> 停止 istop()

每次代码运行周期为20ms

ai2截面图

树莓派蓝牙SPP设置

树莓派蓝牙配置

  1. 安装支持包

    1
    2
    sudo apt-get install pi-bluetooth
    sudo apt-get install bluetooth bluez blueman
  2. 添加pi用户到蓝牙组

    1
    2
    sudo usermod -G bluetooth -a pi
    service bluetooth status
  3. 启动/增加SPP

    1
    sudo nano /etc/systemd/system/dbus-org.bluez.service

    修改内容如下:

    1
    2
    ExecStart=/usr/lib/bluetooth/bluetoothd -C
    ExecStartPost=/usr/bin/sdptool add SP
  4. 重启,启动蓝牙串口

    1
    sudo rfcomm watch hci0

串口调试软件xgcom

1
2
3
4
5
6
7
sudo git clone https://github.com/helight/xgcom.git
sudo apt-get install make automake libglib2.0-dev libvte-dev libgtk2.0-dev
cd xgcom
sudo ./autogen.sh
sudo make
sudo make install
xgcom

展示与控制代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import serial
import time
from btbu_robot.motor import motor
bluetooth = serial.Serial("/dev/rfcomm0",9600,timeout=0.5)

#bluetooth.open()
left = motor(22, 27, 17)
right = motor(24, 23, 18)
def gofront():
left.run(70)
right.run(70)
# print("前进")

def goback():
left.run(-70)
right.run(-70)
#print("后退")

def turnleft():
left.run(-70)
right.run(70)
#print("左转")

def turnright():
#print("右转")
left.run(70)
right.run(-70)

def istop():
left.run(0)
right.run(0)
#print("停止")

status = '' # 记录当前状态
if __name__ == '__main__':
while True:
data = bluetooth.readline().decode('utf-8')
if status == 'u': # 前进
gofront()

elif status == 'd': # 后退
goback()

elif status == 'l': # 左转
turnleft()

elif status == 'r': # 右转
turnright()

elif status == 's': # 停止
istop()
while data != '':
status = data[0]
if status == 'u': # 前进
gofront()

elif status == 'd': # 后退
goback()

elif status == 'l': # 左转
turnleft()

elif status == 'r': # 右转
turnright()

elif status == 's': # 停止
istop()
data = data[1:]
time.sleep(0.02)
time.sleep(0.02)