add option to change rounds & write to file; print results indefinitely

This commit is contained in:
HappyZ 2018-01-30 22:20:52 -06:00
parent bc5495786b
commit ab41f16168
1 changed files with 43 additions and 10 deletions

View File

@ -3,6 +3,7 @@
import re import re
import os import os
import time
import argparse import argparse
import subprocess import subprocess
@ -13,10 +14,14 @@ class Measurement(object):
def __init__(self, interface, ofp=None, cali=(1.0, 0.0)): def __init__(self, interface, ofp=None, cali=(1.0, 0.0)):
self.outf = None self.outf = None
self.interface = interface self.interface = interface
# default file path for config for iw ftm_request
self.config_fp = '/tmp/config_entry' self.config_fp = '/tmp/config_entry'
if ofp: if ofp:
try: try:
self.outf = open(ofp, 'w') self.outf = open(ofp, 'w')
self.outf.write(
'MAC,caliDist(cm),rawRTT(psec),rawDist(cm),time(sec)\n'
)
except Exception as e: except Exception as e:
print(str(e)) print(str(e))
self.regex = ( self.regex = (
@ -65,19 +70,29 @@ class Measurement(object):
continue continue
distance = self.cali[0] * raw_distance + self.cali[1] distance = self.cali[0] * raw_distance + self.cali[1]
result.append((mac, distance, rtt, raw_distance)) result.append((mac, distance, rtt, raw_distance))
if self.outf is not None:
self.outf.write(
"{0},{1:.2f},{2},{3},{4:.6f}\n".format(
mac, distance, rtt, raw_distance, time.time()
)
)
return result return result
def get_distance_avg(self, rounds=10): def get_distance_median(self, rounds=10):
'''
use median instead of mean for less bias with small number of rounds
'''
result = {} result = {}
avg_result = {} avg_result = {}
for i in range(rounds): for i in range(rounds):
# no guarantee that all rounds are successful
for each in self.get_distance_once(): for each in self.get_distance_once():
if each[0] not in result: if each[0] not in result:
result[each[0]] = [] result[each[0]] = []
result[each[0]].append(each[1:]) result[each[0]].append(each[1:])
for mac in result: for mac in result:
avg_result[mac] = median([x[0] for x in result[mac]]) median_result[mac] = median([x[0] for x in result[mac]])
return avg_result return median_result
def __enter__(self): def __enter__(self):
return self return self
@ -95,13 +110,25 @@ def wrapper(args):
'cf': 2462 'cf': 2462
} }
} }
counter = 1
while 1:
print('Round {0}'.format(counter))
with Measurement( with Measurement(
args['interface'], args['interface'],
ofp=args['filepath'], cali=args['cali'] ofp=args['filepath'], cali=args['cali']
) as m: ) as m:
try:
m.prepare_config_file(args['config_entry']) m.prepare_config_file(args['config_entry'])
# print(m.get_distance_once()) # only print out results
print(m.get_distance_avg()) results = m.get_distance_median(rounds=args['rounds'])
for mac in results:
print('* {0} is {1:.4f}cm away.'.format(mac, results[mac]))
except KeyboardInterrupt:
break
except Exception as e:
print(str(e))
break
counter += 1
def main(): def main():
@ -118,6 +145,12 @@ def main():
default=None, default=None,
help="if set, will write raw fetched data to file" help="if set, will write raw fetched data to file"
) )
p.add_argument(
'--rounds',
default=10,
type=int,
help="how many rounds to run one command; default is 10"
)
p.add_argument( p.add_argument(
'--interface', '-i', '--interface', '-i',
default='wlp58s0', default='wlp58s0',