From c255a38b437d67525044bf40f92a8728b2f50c16 Mon Sep 17 00:00:00 2001 From: Andrey Pohilko Date: Wed, 20 Sep 2017 19:34:42 +0300 Subject: [PATCH] Work on vernie demo polish --- README.md | 4 ++-- pylgbst/peripherals.py | 16 ++++++++-------- vernie/__init__.py | 2 +- vernie/android_remote.py | 26 ++++++++++++++++++++------ 4 files changed, 31 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index f313a7b..241b997 100644 --- a/README.md +++ b/README.md @@ -50,11 +50,11 @@ Methods to activate motors are: - `constant(speed_primary, speed_secondary)` - enables motor with specified speed forever - `timed(time, speed_primary, speed_secondary)` - enables motor with specified speed for `time` seconds, float values accepted - `angled(angle, speed_primary, speed_secondary)` - makes motor to rotate to specified angle, `angle` value is integer degrees, can be negative and can be more than 360 for several rounds -- `stop()` - stops motor at once, it is equivalent for `constant(0, async=True)` +- `stop()` - stops motor at once, it is equivalent for `constant(0)` Parameter `speed_secondary` is used when it is motor group of `motor_AB` running together. By default, `speed_secondary` equals `speed_primary`. -All these methods are synchronous by default, except `stop()`. You can pass `async` parameter to any of them to switch into asynchronous, which means command will return immediately, without waiting for rotation to complete. +All these methods are synchronous by default, means method does not return untill it gets confirmation from Hub that command has completed. You can pass `async=True` parameter to any of methods to switch into asynchronous, which means command will return immediately, without waiting for rotation to complete. Be careful with asynchronous calls, as they make Hub to stop reporting synchronizing statuses. An example: ```python diff --git a/pylgbst/peripherals.py b/pylgbst/peripherals.py index 0ac341d..26b010d 100644 --- a/pylgbst/peripherals.py +++ b/pylgbst/peripherals.py @@ -228,7 +228,7 @@ class EncodedMotor(Peripheral): self._wrap_and_write(self.SOME_SINGLE, b"", speed_primary, speed_secondary) self._wait_sync(async) - def stop(self, async=True): + def stop(self, async=False): self.constant(0, async=async) def handle_port_data(self, data): @@ -329,23 +329,23 @@ class ColorDistanceSensor(Peripheral): def handle_port_data(self, data): if self._port_subscription_mode == self.COLOR_DISTANCE_FLOAT: - color = data[4] - distance = data[5] - partial = data[7] + color = unpack(" 0 else -2.0 - sa = round(c + b / divider, 2) - sb = round(c - b / divider, 2) + + if 0 < front_distance < 9 and c > 0: + logging.info("Something in front of Vernie [%s]!", front_distance) + c = 0 + + sa = round(c + b / divider, 1) + sb = round(c - b / divider, 1) logging.info("SpeedA=%s, SpeedB=%s", sa, sb) - robot.motor_AB.constant(sa, sb, async=True) - time.sleep(0.5) + robot.motor_AB.constant(sa, sb) + # time.sleep(0.5) finally: robot.motor_AB.stop() udp_sock.close()