app传送门:
1 。 首先在’菜单-》首选项-》Raspberry Pi Configuration‘中。修改树梅派主机的名称 统一为:pixx(其中xx为组号),防止找蓝牙设备的时候找不要到自己的主机。
修改完后,主机需要重启。
2. 开启蓝牙的发现模式,点击蓝牙图标-》选择make discoverable,进入配对模式。
3。 打开手机蓝牙设置,找到并配对你对应的树梅派主机。
4。树梅派会跳出一个是否允许配对对话框,选择ok允许。配对成功。若没有成功配对,则重复上述步骤。
5。完成上述步骤后,打开blue dot 应用app,则在app中能找到你的树梅派主机了。
6。在树梅派中安装blue dot库
打开命令行:运行安装指令。
1 |
sudo pip3 install bluedot |
安装完成后,方可进行下一步操作。
7。运行控制程序(运行控制程序后,才能用blue dot APP连接)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
from bluedot import BlueDot from gpiozero import Robot from signal import pause from gpiozero.pins.pigpio import PiGPIOFactory bd = BlueDot() #robot = Robot(left=(4, 14), right=(17, 18)) factory = PiGPIOFactory(host='raspberry00.local') robot = Robot(left=(15, 14), right=(18, 23),pin_factory=factory) bd.when_pressed = lambda:robot.forward() bd.when_released =lambda:robot.stop() pause() |
8。运行blue dot应用
9.控制小车方向:
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 |
from bluedot import BlueDot from gpiozero import Robot from signal import pause from time import sleep from gpiozero.pins.pigpio import PiGPIOFactory factory = PiGPIOFactory(host='raspberry00.local') bd = BlueDot() robot = Robot(left=(15, 14), right=(18, 23),pin_factory=factory) robot.stop() def move(pos): #print(pos.distance) if pos.top: robot.forward() elif pos.bottom: robot.backward() elif pos.left: robot.left() elif pos.right: robot.right() #sleep(0.2) bd.when_pressed = move #bd.when_moved = move bd.when_released = lambda:robot.stop() pause() |
10.控制小车方向与速度
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 |
from gpiozero import Robot from time import sleep from gpiozero.pins.pigpio import PiGPIOFactory from bluedot import BlueDot from signal import pause factory = PiGPIOFactory(host='raspberry00.local') robot = Robot(left=(15, 14), right=(18, 23),pin_factory=factory) def pos_to_values(x, y): left = y if x > 0 else y + x right = y if x < 0 else y - x return (clamped(left), clamped(right)) def clamped(v): return max(-1, min(1, v)) def drive(): while True: if bd.is_pressed: x, y = bd.position.x, bd.position.y yield pos_to_values(x, y) else: yield (0, 0) bd = BlueDot() robot.source = drive() pause() |
转载请注明:徐自远的乱七八糟小站 » 20181011 蓝牙控制小车