From 7da3b8e508e7e23e70572830e77774449f9fa8e0 Mon Sep 17 00:00:00 2001 From: Andrey Pohilko Date: Sat, 23 Dec 2017 21:35:01 +0300 Subject: [PATCH] Two-color centering+snowflake --- examples/plotter/__init__.py | 16 ++++---- examples/plotter/try.py | 72 +++++++++++++++++++++++++++++++----- 2 files changed, 72 insertions(+), 16 deletions(-) diff --git a/examples/plotter/__init__.py b/examples/plotter/__init__.py index 39d8553..6e32c19 100644 --- a/examples/plotter/__init__.py +++ b/examples/plotter/__init__.py @@ -2,7 +2,7 @@ import logging import math import time -from pylgbst import MoveHub, ColorDistanceSensor, COLORS, COLOR_RED, COLOR_CYAN +from pylgbst import MoveHub, ColorDistanceSensor, COLORS, COLOR_RED, COLOR_YELLOW class Plotter(MoveHub): @@ -35,23 +35,25 @@ class Plotter(MoveHub): self.caret.constant(-self.base_speed) count = 0 max_tries = 50 - while self._marker_color != COLOR_RED and count < max_tries: + while self._marker_color not in (COLOR_RED, COLOR_YELLOW) and count < max_tries: time.sleep(30.0 / max_tries) count += 1 - logging.info("Centering tries: %s, color #%s", count, - COLORS[self._marker_color] if self._marker_color else None) + self.color_distance_sensor.unsubscribe(self._on_distance) + clr = COLORS[self._marker_color] if self._marker_color else None + logging.info("Centering tries: %s, color #%s", count, clr) if count >= max_tries: raise RuntimeError("Failed to center caret") finally: self.caret.stop() self.color_distance_sensor.unsubscribe(self._on_distance) - self.move(-self.field_width, 0) + if self._marker_color != COLOR_YELLOW: + self.move(-self.field_width, 0) def _on_distance(self, color, distance): self._marker_color = None logging.debug("Color: %s, distance %s", COLORS[color], distance) - if color in (COLOR_RED, COLOR_CYAN): + if color in (COLOR_RED, COLOR_YELLOW): if distance <= 3: self._marker_color = color @@ -110,7 +112,7 @@ class Plotter(MoveHub): return wheel_dir = movy / abs(movy) if wheel_dir == -self.__last_wheel_dir: - self.wheels.angled(270, -wheel_dir) + self.wheels.angled(230, -wheel_dir) self.__last_wheel_dir = wheel_dir @staticmethod diff --git a/examples/plotter/try.py b/examples/plotter/try.py index 2fe20d5..f8041c7 100644 --- a/examples/plotter/try.py +++ b/examples/plotter/try.py @@ -7,11 +7,13 @@ from pylgbst.comms import DebugServerConnection, BLEConnection def moves(): + plotter.move(-FIELD_WIDTH, 0) + plotter.move(FIELD_WIDTH * 2, 0) + plotter.move(-FIELD_WIDTH, 0) + plotter.move(FIELD_WIDTH, FIELD_WIDTH) plotter.move(-FIELD_WIDTH, -FIELD_WIDTH) - plotter.move(FIELD_WIDTH, 0) - plotter.move(-FIELD_WIDTH, 0) plotter.move(0, FIELD_WIDTH) plotter.move(0, -FIELD_WIDTH) @@ -47,7 +49,7 @@ def circles(): plotter.move(FIELD_WIDTH / 4.0, 0) plotter.circle(FIELD_WIDTH / 2.0) - plotter.move(FIELD_WIDTH / 4.0, 0) + plotter.move(FIELD_WIDTH / 2.0, 0) plotter.circle(FIELD_WIDTH) @@ -59,10 +61,12 @@ class LaserPlotter(Plotter): def lego(): - t = FIELD_WIDTH / 10.0 + t = FIELD_WIDTH / 5.0 h = t * 5.0 w = t * 3.0 + plotter.move(-t * 2.0, 0) + plotter.line(h, 0) plotter.line(0, t) plotter.line(-(h - t), 0) @@ -164,6 +168,56 @@ def try_speeds(): time.sleep(1) +def snowflake(): + a = [300, 232, + 351, 144, + 307, 68, + 350, 45, + 379, 94, + 413, 36, + 456, 61, + 422, 119, + 482, 118, + 481, 167, + 394, 168, + 343, 256, + 444, 256, + 488, 179, + 530, 204, + 500, 256, + 569, 256, + 582, 280] + prev = None + vals = [] + maxval = 0 + for i in range(0, len(a)): + if i % 2: + continue + + maxval = max(maxval, abs(a[i]), abs(a[i + 1])) + if prev: + vals.append((a[i] - prev[0], a[i + 1] - prev[1])) + prev = a[i], a[i + 1] + + assert len(vals) == len(a) / 2 - 1 + + vals = [(x[0] / float(maxval), x[1] / float(maxval)) for x in vals] + + logging.info("Moves: %s", vals) + zoom = FIELD_WIDTH * 2.0 + for item in vals: + plotter.line(item[0] * zoom, item[1] * zoom) + + for item in reversed(vals): + plotter.line(-item[0] * zoom, item[1] * zoom) + + for item in vals: + plotter.line(-item[0] * zoom, -item[1] * zoom) + + for item in reversed(vals): + plotter.line(item[0] * zoom, -item[1] * zoom) + + if __name__ == '__main__': logging.basicConfig(level=logging.INFO) @@ -173,19 +227,18 @@ if __name__ == '__main__': logging.warning("Failed to use debug server: %s", traceback.format_exc()) conn = BLEConnection().connect() - plotter = LaserPlotter(conn, 0.5) + plotter = LaserPlotter(conn, 0.75) FIELD_WIDTH = plotter.field_width try: - #plotter.initialize() + plotter.initialize() # plotter._tool_down() # try_speeds() - # moves() - triangle() + # triangle() # square() # cross() # romb() @@ -193,9 +246,10 @@ if __name__ == '__main__': # plotter.spiral(4, 0.02) # plotter.rectangle(FIELD_WIDTH / 5.0, FIELD_WIDTH / 5.0, solid=True) - # lego() # square_spiral() + # lego() # christmas_tree() + snowflake() pass finally: plotter.finalize()