-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmqtt_control.py
59 lines (47 loc) · 1.21 KB
/
mqtt_control.py
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
from rm_s1_hacker import *
import time
import os
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flag, rc):
print("Connected with result code " + str(rc))
client.subscribe("Joy/x")
client.subscribe("Joy/y")
def on_disconnect(client, userdata, flag, rc):
if rc != 0:
print("Unexpected disconnection.")
joy_x = 0
joy_y = 0
def on_message(client, userdata, msg):
global joy_x
global joy_y
if msg.topic == "Joy/x":
joy_x = int(msg.payload)
elif msg.topic == "Joy/y":
joy_y = int(msg.payload)
def joy_demo():
client = mqtt.Client()
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.on_message = on_message
client.connect("localhost", 1884, 60)
#client.loop_forever()
client.loop_start()
hacker = RoboMasterHacker()
start_time = time.time()
start_time_touch = time.time()
send_interval = 0.001
send_interval_touch = 0.001
vx = 1024
vy = 1024
z = 1024
try:
while True:
vx = 1024 - joy_y
vy = 1024 + joy_x
hacker.receive_msg()
hacker.send_touch_command()
hacker.send_joy_command(int(vx), int(vy), z)
except KeyboardInterrupt:
hacker.shutdown()
if __name__ == '__main__':
joy_demo()