-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbluetoothtester.py
More file actions
68 lines (58 loc) · 2.78 KB
/
bluetoothtester.py
File metadata and controls
68 lines (58 loc) · 2.78 KB
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
import socket
import json
from pynput import keyboard # нужно установить для управления с клавиатуры
import time
msg = {
"speedA": 0, # в пакете посылается скорость на левый и правый борт тележки
"speedB": 0 #
}
speedScale = 1.00 # определяет скорость в процентах (0.90 = 90%) от максимальной абсолютной
maxAbsSpeed = 100 # максимальное абсолютное отправляемое значение скорости
sendFreq = 15 # слать 15 пакетов в секунду
def onPress(key):
""" вызывается при нажатии какой либо клавиши на клавиатуре """
global msg
global speedScale, maxAbsSpeed
if key == keyboard.Key.up: # управление стрелочками
msg["speedA"] = maxAbsSpeed * speedScale
msg["speedB"] = maxAbsSpeed * speedScale
elif key == keyboard.Key.down:
msg["speedA"] = -maxAbsSpeed * speedScale
msg["speedB"] = -maxAbsSpeed * speedScale
elif key == keyboard.Key.left:
msg["speedA"] = maxAbsSpeed * speedScale
msg["speedB"] = maxAbsSpeed * speedScale * 0.75
elif key == keyboard.Key.right:
msg["speedA"] = maxAbsSpeed * speedScale * 0.75
msg["speedB"] = maxAbsSpeed * speedScale
def onRelease(key):
""" вызывается при отпускании какой либо клавиши на клавиатуре """
global msg
global speedScale, maxAbsSpeed
if key == keyboard.Key.up: # управление стрелочками
msg["speedA"] = 0
msg["speedB"] = 0
elif key == keyboard.Key.down:
msg["speedA"] = 0
msg["speedB"] = 0
elif key == keyboard.Key.left:
msg["speedA"] = 0
msg["speedB"] = 0
elif key == keyboard.Key.right:
msg["speedA"] = 0
msg["speedB"] = 0
if __name__ == '__main__':
listener = keyboard.Listener(
on_press=onPress,
on_release=onRelease)
listener.start() # запускаем обработчик нажатия клавиш в неблокирующем режиме
sock = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM) # on python 3.9
sock.connect(("00:21:13:04:d9:63", 1)) # подключаемся к блютуз модулю HC-06 по mac-адресу
try:
while True:
sock.send(json.dumps(msg, ensure_ascii=False).encode("utf8")) # отправляем сообщение в виде json файла
#print(sock.recv(1024))
time.sleep(1 / sendFreq)
except KeyboardInterrupt:
sock.close()
listener.stop()