-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmqtt-slamviz.py
69 lines (46 loc) · 1.6 KB
/
mqtt-slamviz.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
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python
import time
from sys import exit
from roboviz import MapVisualizer
import numpy as np
try:
import paho.mqtt.client as mqtt
except ImportError:
exit("This example requires the paho-mqtt module\nInstall with: sudo pip install paho-mqtt")
MQTT_SERVER = "test.mosquitto.org"
MQTT_PORT = 1883
MQTT_TOPIC = "safetycam/topic/slamviz"
# Set these to use authorisation
MQTT_USER = None
MQTT_PASS = None
MAP_SIZE_PIXELS = 200
MAP_SIZE_METERS = 10
# Set up a SLAM display
viz = MapVisualizer(MAP_SIZE_PIXELS, MAP_SIZE_METERS, 'SLAM', show_trajectory=True)
print("""
Public MQTT messages from {server} on port {port} to visualize SLAM!
It will monitor the {topic} topic by default, and decodes the bytearray
""".format(
server=MQTT_SERVER,
port=MQTT_PORT,
topic=MQTT_TOPIC
))
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe(MQTT_TOPIC)
def on_message(client, userdata, msg):
robotPos_bytes = msg.payload[:24]
map_bytes = msg.payload[24:]
robotPos = np.frombuffer(robotPos_bytes, dtype='float64')
robotPos = np.array(robotPos)
x, y, theta = robotPos
viz.display(x / 1000., y / 1000., theta, map_bytes)
return
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
if MQTT_USER is not None and MQTT_PASS is not None:
print("Using username: {un} and password: {pw}".format(un=MQTT_USER, pw="*" * len(MQTT_PASS)))
client.username_pw_set(username=MQTT_USER, password=MQTT_PASS)
client.connect(MQTT_SERVER, MQTT_PORT, 60)
client.loop_forever()