Skip to content

Instantly share code, notes, and snippets.

@ataffanel
Created August 11, 2017 09:01
Show Gist options
  • Save ataffanel/eb12936c5567bb7df0670b4c35dc6606 to your computer and use it in GitHub Desktop.
Save ataffanel/eb12936c5567bb7df0670b4c35dc6606 to your computer and use it in GitHub Desktop.
Simple script analyzing output of a loco positioning node in TWR tag mode, saves the distance measurement in a csv file
import serial
import re
nranges = 0
nsuccess = 0
current_range = 0
last_pass = False
csv = open("node_otput.csv", "w")
csv.write("range, state")
meas = [0]*100
with serial.Serial("/dev/ttyACM0", 115200) as ser:
while True:
line = ser.readline().strip().decode("utf8")
match = re.match("^distance 1: *([0-9]+)mm", line)
if line == "Interrogating anchor 1":
# Printing state
template = "\r\033[2K{} Ranges {} Failed ({:.0f}% pass), Mean: {}m"
percent_pass = 0
if nsuccess > 0:
percent_pass = 100*(nsuccess/nranges)
distance = sum(meas) / len(meas)
print(template.format(nranges, nranges-nsuccess,
percent_pass, distance), end='')
if nsuccess > 0:
csv.write("{}, {}\n".format(current_range,
1 if last_pass else 0))
nranges += 1
last_pass = False
elif match:
current_range = int(match.group(1)) / 1000.0
meas = [int(match.group(1)) / 1000.0] + meas
meas.pop()
nsuccess += 1
last_pass = True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment