-
Notifications
You must be signed in to change notification settings - Fork 19
/
04_ros_chat.py
43 lines (31 loc) · 1.09 KB
/
04_ros_chat.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
import json
from compas_fab.backends import RosClient
from roslibpy import Topic
from helpers import get_current_buffer
from helpers import get_input
print('Enter your name:')
name = get_input()
client = RosClient('ros-vzby-dzht.yaler.io', port=80)
def start_chat():
print('Connected as {}! [type a message or q to quit]'.format(name))
talker = Topic(client, '/messages', 'std_msgs/String')
talker.advertise()
listener = Topic(client, '/messages', 'std_msgs/String')
listener.subscribe(receive_message)
while client.is_connected:
text = get_input()
if text == 'q': break
send_message(talker, text)
client.terminate()
def receive_message(message):
data = json.loads(message['data'])
if data['name'] != name:
print('\r[{}]: {}\r'.format(
data['name'],
data['text'].ljust(80),
))
print('> {}'.format(get_current_buffer()), end='', flush=True)
def send_message(talker, message):
talker.publish({'data': json.dumps(dict(text=message, name=name))})
client.on_ready(start_chat)
client.run_forever()