mirror of
https://github.com/undera/pylgbst.git
synced 2020-11-18 19:37:26 -08:00
Work on plotter
This commit is contained in:
parent
0b4be6ac42
commit
258c259042
0
examples/__init__.py
Normal file
0
examples/__init__.py
Normal file
67
examples/plotter/__init__.py
Normal file
67
examples/plotter/__init__.py
Normal file
@ -0,0 +1,67 @@
|
||||
import logging
|
||||
import traceback
|
||||
|
||||
from pylgbst import MoveHub, BLEConnection
|
||||
from pylgbst.comms import DebugServerConnection
|
||||
|
||||
BASE_SPEED = 0.5
|
||||
|
||||
|
||||
class Plotter(MoveHub):
|
||||
def __init__(self, connection=None):
|
||||
super(Plotter, self).__init__(connection)
|
||||
self.xpos = 0
|
||||
self.ypos = 0
|
||||
self.is_tool_down = False
|
||||
|
||||
def initialize(self):
|
||||
# TODO: ensure caret is in the middle
|
||||
self.xpos = 0
|
||||
self.ypos = 0
|
||||
self.is_tool_down = False
|
||||
|
||||
def _tool_down(self):
|
||||
self.motor_external.angled(270)
|
||||
self.is_tool_down = True
|
||||
|
||||
def _tool_up(self):
|
||||
self.motor_external.angled(-270)
|
||||
self.is_tool_down = False
|
||||
|
||||
def move_to(self, movx, movy):
|
||||
if self.is_tool_down:
|
||||
self._tool_up()
|
||||
self._transfer_to(movx, movy)
|
||||
|
||||
def line_to(self, movx, movy):
|
||||
if not self.is_tool_down:
|
||||
self._tool_down()
|
||||
self._transfer_to(movx, movy)
|
||||
|
||||
def _transfer_to(self, movx, movy):
|
||||
angle = max(movy, movx)
|
||||
speed_a = BASE_SPEED
|
||||
speed_b = BASE_SPEED * 1.5
|
||||
self.motor_AB.angled(angle, speed_a, speed_b)
|
||||
|
||||
def finalize(self):
|
||||
if self.is_tool_down:
|
||||
self._tool_up()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
try:
|
||||
connection = DebugServerConnection()
|
||||
except BaseException:
|
||||
logging.warning("Failed to use debug server: %s", traceback.format_exc())
|
||||
connection = BLEConnection().connect()
|
||||
|
||||
plotter = Plotter(connection)
|
||||
plotter.initialize()
|
||||
# plotter.move_to(100, 100)
|
||||
# plotter.move_to(100, -100)
|
||||
# plotter.move_to(-100, -100)
|
||||
# plotter.move_to(-100, 100)
|
||||
plotter.finalize()
|
@ -1,3 +1,4 @@
|
||||
import json
|
||||
import logging
|
||||
import matplotlib.pyplot as plt
|
||||
import time
|
||||
@ -11,6 +12,7 @@ class Tracer(object):
|
||||
def __init__(self, fname):
|
||||
super(Tracer, self).__init__()
|
||||
self.threshold = 64
|
||||
self.orig_fname = fname
|
||||
self.orig = Image.open(fname)
|
||||
self.conv1 = self.remove_transparency(self.orig)
|
||||
self.conv1 = self.conv1.convert("L")
|
||||
@ -19,9 +21,10 @@ class Tracer(object):
|
||||
self.dst.fill(False)
|
||||
self.mark = numpy.copy(self.dst)
|
||||
# start in center
|
||||
self.height, self.width = self.dst.shape[0:2]
|
||||
self.height, self.width = self.dst.shanape[0:2]
|
||||
self.posy = self.height / 2
|
||||
self.posx = self.width / 2
|
||||
self.lines = []
|
||||
|
||||
def remove_transparency(self, im, bg_colour=(255, 255, 255)):
|
||||
# from https://stackoverflow.com/questions/35859140/remove-transparency-alpha-from-any-image-using-pil
|
||||
@ -50,8 +53,12 @@ class Tracer(object):
|
||||
# move until we find new pixels
|
||||
self._move_while_you_can()
|
||||
|
||||
logging.info("Done")
|
||||
with open(self.orig_fname + ".json", "wt") as fhd:
|
||||
fhd.write(json.dumps(self.lines))
|
||||
|
||||
def _has_unchecked_pixels(self):
|
||||
ix, iy = numpy.where(self.mark == False)
|
||||
ix, iy = numpy.where(self.mark == False) # FIXME: highly inefficient
|
||||
return len(ix) or len(iy)
|
||||
|
||||
def is_src(self, posx, posy):
|
||||
@ -96,18 +103,26 @@ class Tracer(object):
|
||||
if direction in (0, 2):
|
||||
radius += 1
|
||||
|
||||
logging.info("End of image")
|
||||
return False
|
||||
|
||||
def _move_while_you_can(self):
|
||||
# time.sleep(0.1)
|
||||
logging.debug("%s:%s=%s", self.posy, self.posx, self.src[self.posy][self.posx])
|
||||
|
||||
dirs = self._check_directions()
|
||||
dirs = self._check_directions() # TODO: use stack of this knowledge to speed-up walktrough
|
||||
dx, dy, length = self._get_best_direction(dirs)
|
||||
|
||||
self.dst[self.posy][self.posx] = True
|
||||
self.mark[self.posy][self.posx] = True
|
||||
|
||||
line = {
|
||||
"x1": self.posx, "y1": self.posy,
|
||||
"x2": self.posx + dx * length, "y2": self.posy + dy * length,
|
||||
"len": length
|
||||
}
|
||||
self.lines.append(line)
|
||||
logging.info("%s", line)
|
||||
|
||||
for n in range(0, length):
|
||||
self.posy += dy
|
||||
self.posx += dx
|
||||
@ -182,7 +197,7 @@ class TracerVisualizer(object):
|
||||
|
||||
if __name__ == "__main__":
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
trc = Tracer("test2.png")
|
||||
trc = Tracer("test3.png")
|
||||
|
||||
TracerVisualizer(trc).run()
|
||||
time.sleep(5)
|
Loading…
x
Reference in New Issue
Block a user