add option to plot location

This commit is contained in:
HappyZ 2018-02-08 15:32:43 -06:00
parent 73b2050919
commit 5471f77e89
2 changed files with 72 additions and 23 deletions

View File

@ -129,20 +129,11 @@ def trilateration2d(mydict, bounds=None, verbose=False):
tmp = loc.split(',') tmp = loc.split(',')
p = Point(tmp[0], tmp[1]) p = Point(tmp[0], tmp[1])
points.append(p) points.append(p)
circles.append(Circle(p, mydict[loc][0]))
# print(len(points), len(circles))
inner_points = calcInnerPoints(circles, bounds)
if len(inner_points) is 0:
# if empty, create multiple circles based on std
circles = []
for loc in mydict:
tmp = loc.split(',')
p = Point(tmp[0], tmp[1])
if mydict[loc][1]: if mydict[loc][1]:
for r in arange( for r in arange(
max(mydict[loc][0] - mydict[loc][1], 0.01), max(mydict[loc][0] - mydict[loc][1], 0.001),
mydict[loc][0] + mydict[loc][1], mydict[loc][0] + mydict[loc][1],
10 1
): ):
circles.append(Circle(p, r)) circles.append(Circle(p, r))
else: else:
@ -182,6 +173,15 @@ def deriveLocation(args, results):
return loc return loc
def plotLocation(loc):
handler = None
try:
handler = plt.scatter(loc[0], loc[1])
plt.pause(0.01)
except Exception:
pass
return handler
if __name__ == '__main__': if __name__ == '__main__':
loc = deriveLocation( loc = deriveLocation(
{ {
@ -202,4 +202,26 @@ if __name__ == '__main__':
'34:f6:4b:5e:69:1e': (50, 50) '34:f6:4b:5e:69:1e': (50, 50)
} }
) )
print(loc) flagPlot = False
try:
import matplotlib.pyplot as plt
flagPlot = True
except Exception:
pass
if flagPlot:
fig = plt.figure()
plt.ion()
plt.xlim([-100, 300])
plt.ylim([-10, 500])
while 1:
try:
handler = plotLocation(loc)
if handler is None:
plt.close(fig)
break
handler.remove()
except KeyboardInterrupt:
plt.close(fig)
break
except Exception:
raise

View File

@ -197,6 +197,18 @@ def wrapper(args):
} }
} }
counter = 1 counter = 1
if args['plot']:
try:
import matplotlib.pyplot as plt
from libLocalization import plotLocation
args['plot'] = False
handler = None
fig = plt.figure()
plt.ion()
plt.xlim([-100, 500])
plt.ylim([-10, 500])
except Exception:
print('Cannot plot because lacking matplotlib!')
with Measurement( with Measurement(
args['interface'], args['interface'],
ofp=args['filepath'], cali=args['cali'] ofp=args['filepath'], cali=args['cali']
@ -221,6 +233,18 @@ def wrapper(args):
'* Derived location: ({0:.3f}, {1:.3f})' '* Derived location: ({0:.3f}, {1:.3f})'
.format(loc[0], loc[1]) .format(loc[0], loc[1])
) )
if args['plot']:
try:
if handler is not None:
handler.remove()
handler = plotLocation(loc)
if handler is None:
plt.close(fig)
except KeyboardInterrupt:
plt.close(fig)
break
except Exception:
raise
except KeyboardInterrupt: except KeyboardInterrupt:
break break
except Exception as e: except Exception as e:
@ -279,10 +303,13 @@ def main():
'--locs', '--locs',
default=False, default=False,
action="store_true", action="store_true",
help=( help="if set, derive location and store it to file"
"if set, derive location" +
"and store it to file"
) )
p.add_argument(
'--plot',
default=False,
action="store_true",
help="if set, will plot the derived location in realtime"
) )
try: try:
args = vars(p.parse_args()) args = vars(p.parse_args())