Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Test program to poll Neato laser distance sensor data
import serial
import time
lf = b'\n'
eom = b'\x1a'
class NeatoCommander():
def __init__(self, port="/dev/ttyACM1", timeout=1):
s = serial.Serial()
s.baudrate = 1152000
s.port = port
s.timeout = timeout
s.open()
if s.is_open:
print("Successfully opened port")
self.sp = s
self.all_responses = dict()
def now(self):
return time.perf_counter()
def send_neato_command(self, command):
self.sp.write(command + lf)
echo = self.sp.readline()
if echo[:len(command)] != command:
raise ValueError("Neato did not echo back command as expected: {}".format(command))
first_char = self.sp.read(1)
if first_char != eom:
raw_response = []
while first_char != eom:
line_remainder = self.sp.readline()
line = first_char + line_remainder
raw_response.append(line)
first_char = self.sp.read(1)
self.all_responses[self.now()] = raw_response
return len(raw_response)
else:
return 0
def exit(self):
print("Closing port")
self.sp.close()
if __name__ == "__main__":
nc = NeatoCommander()
try:
nc.send_neato_command(b"testmode on")
nc.send_neato_command(b"setldsrotation on")
marker = nc.now()
while nc.now() < marker+10:
nc.send_neato_command(b"getldsscan")
finally:
nc.send_neato_command(b"setldsrotation off")
nc.send_neato_command(b"testmode off")
nc.exit()
sorted_time = sorted(nc.all_responses.keys())
prev_response = frozenset(nc.all_responses[sorted_time[0]])
unique = 1
for timestamp in sorted_time:
now_response = frozenset(nc.all_responses[timestamp])
if len(now_response-prev_response)>0:
print("Unique data at time {}".format(timestamp))
unique += 1
else:
print("Repeated data at time{}".format(timestamp))
prev_response = now_response
print("Found {} updates in {} elements over {}".format(unique,len(nc.all_responses), sorted_time[-1]-sorted_time[0]))
@Roger-random
Copy link
Author

Roger-random commented Apr 1, 2019

Sample output from my Neato XV-21, informing us we can poll LDS data a little over 10 times a second over USB serial port in test mode.

Judging by the fact almost half of the data were repeats, that's roughly double the speed we require to keep up to date and see all LDS data.

(The first few readings were repeated due to laser distance scanner spinning up to speed.)

Successfully opened port
Closing port
Repeated data at time12653.734844544
Repeated data at time12653.831027969
Repeated data at time12653.922426622
Repeated data at time12654.014224572
Repeated data at time12654.105800506
Repeated data at time12654.207802413
Repeated data at time12654.321836516
Repeated data at time12654.412682262
Repeated data at time12654.515961331
Unique data at time 12654.631138358
Repeated data at time12654.739237724
Unique data at time 12654.843347372
Repeated data at time12654.92792638
Unique data at time 12655.019013815
Repeated data at time12655.127986532
Unique data at time 12655.218394743
Repeated data at time12655.30955299
Unique data at time 12655.397133096
Repeated data at time12655.487729343
Unique data at time 12655.577693339
Repeated data at time12655.667586016
Unique data at time 12655.758199836
Repeated data at time12655.848327568
Unique data at time 12655.93710031
Repeated data at time12656.02780497
Repeated data at time12656.11747933
Unique data at time 12656.20849305
Repeated data at time12656.316955529
Unique data at time 12656.427949531
Repeated data at time12656.518194422
Unique data at time 12656.612458837
Repeated data at time12656.719377877
Unique data at time 12656.81664632
Repeated data at time12656.933454323
Unique data at time 12657.031749555
Repeated data at time12657.122094134
Unique data at time 12657.228152066
Repeated data at time12657.342149289
Unique data at time 12657.434544042
Unique data at time 12657.534216994
Repeated data at time12657.634751114
Unique data at time 12657.735211847
Repeated data at time12657.829232836
Unique data at time 12657.922541459
Repeated data at time12658.019813574
Repeated data at time12658.112487848
Unique data at time 12658.201524324
Repeated data at time12658.294000609
Unique data at time 12658.393641167
Repeated data at time12658.481764396
Unique data at time 12658.592291657
Repeated data at time12658.681547105
Unique data at time 12658.79191696
Repeated data at time12658.882684373
Unique data at time 12658.972120454
Repeated data at time12659.061594175
Unique data at time 12659.152479366
Repeated data at time12659.241166548
Unique data at time 12659.333414223
Repeated data at time12659.422486304
Repeated data at time12659.512304241
Unique data at time 12659.6173356
Repeated data at time12659.711372287
Unique data at time 12659.805785768
Repeated data at time12659.904595018
Unique data at time 12659.992157847
Repeated data at time12660.082013577
Unique data at time 12660.17137625
Repeated data at time12660.261525735
Unique data at time 12660.352855566
Repeated data at time12660.444918474
Unique data at time 12660.53125441
Repeated data at time12660.628059128
Unique data at time 12660.723936321
Repeated data at time12660.81341798
Repeated data at time12660.916505938
Unique data at time 12661.015537233
Repeated data at time12661.101528561
Unique data at time 12661.191036079
Repeated data at time12661.282327539
Unique data at time 12661.383005984
Repeated data at time12661.471096277
Unique data at time 12661.562073435
Repeated data at time12661.651131132
Unique data at time 12661.74160102
Repeated data at time12661.832346431
Unique data at time 12661.925135246
Repeated data at time12662.013095478
Repeated data at time12662.101600697
Unique data at time 12662.192928787
Repeated data at time12662.295265388
Unique data at time 12662.382415459
Repeated data at time12662.48825029
Unique data at time 12662.601041744
Repeated data at time12662.709235186
Unique data at time 12662.80434725
Repeated data at time12662.893238078
Unique data at time 12662.982235749
Repeated data at time12663.08601849
Unique data at time 12663.20394719
Repeated data at time12663.294990778
Unique data at time 12663.385169488
Repeated data at time12663.486801589
Unique data at time 12663.582118915
Repeated data at time12663.671953784
Found 46 updates in 105 elements over 9.9371092399997

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment