forked from simondlevy/NeatoPylot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneatopylot_server.py
173 lines (114 loc) · 4.52 KB
/
neatopylot_server.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python
'''
neatopylot_server.py - server code for Neato XV-11 Autopylot
Copyright (C) 2013 Suraj Bajracharya and Simon D. Levy
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
Revision history:
22-FEB-2013 Simon D. Levy Initial release
09-SEP-2014 SDL Migrated to github
'''
DEVICE = '/dev/ttyACM0' # Raspbian
#DEVICE = '/dev/tty.usbmodem1d131' # Mac OS X
#DEVICE = 'DEBUG' # Debugging
import serial
import socket
import time
import sys
# Import values and functions common to client and server
from neatopylot_header import *
# Serial-port timeout
TIMEOUT_SEC = 0.1
# Sends LF-terminated command to robot and reads result
def docommand(port, command):
port.write(command + '\n')
# main =========================================================================
# Default to no robot (test mode)
robot = None
# DEBUG is special name to enable debugging
if DEVICE != 'DEBUG':
try:
robot = serial.Serial(DEVICE, 115200, \
serial.EIGHTBITS, serial.PARITY_NONE, serial.STOPBITS_ONE, TIMEOUT_SEC)
except serial.SerialException:
print('Unable to connect to XV-11 as device ' + DEVICE)
print('Make sure XV-11 is powered on and USB cable is plugged in!')
sys.exit(1)
# Put the XV-11 into test mode
docommand(robot, 'TestMode on')
# Spin up the LIDAR
docommand(robot, 'SetLDSRotation on')
else:
print('No robot connected; running in test mode')
# Keep accepting connections on socket
while True:
# Serve a socket on the host and port specified on the command line
sock = None
while True:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.bind((HOST, PORT))
break
except socket.error as err:
print('Bind failed: ' + str(err))
time.sleep(1)
sock.listen(1)
# Accept a connection from a client
print('Waiting for client to connect ...')
try:
client, address = sock.accept()
print('Accepted connection')
except:
break
# Handle client requests till quit
while True:
try:
# Get message from client
msg = client.recv(MESSAGE_SIZE_BYTES)
# Strip trailing whitespace
msg = msg.rstrip()
if not len(msg):
print('Client quit')
break
if msg[0] == 's':
scandata = None
# Robot sends scaled LSD sensor values scaled to (0,1)
if robot:
# Run scan command
docommand(robot, 'GetLDSScan')
# Grab scan results till CTRL-Z
scandata = robot.read(MAX_SCANDATA_BYTES)
# Stubbed version sends constant distances
else:
scandata = ''
for k in range(360):
scandata = scandata + str(k) + ',1500,100,0\n'
# Send scan results to client
client.send(scandata)
elif msg[0] == 'm':
command = 'SetMotor' + msg[1:]
if robot:
docommand(robot, command)
else:
print(command)
except ValueError as ex:
break
# Done talking to client
client.close()
# Shut down the XV-11
if (robot):
print('Shutting down ...')
# Spin down the LIDAR
docommand(robot, 'SetLDSRotation off')
# Take the XV-11 out of test mode
docommand(robot, 'TestMode off')
# Close the port
robot.close()
# Wait a second while the LIDAR spins down
time.sleep(1)