From 81ce664d21f4d22c8bb8b4a42f9ec2047770a9c6 Mon Sep 17 00:00:00 2001 From: Andrey Pohilko Date: Sat, 16 Sep 2017 18:14:04 +0300 Subject: [PATCH] Improve vernie part --- README.md | 1 + demo.py | 1 - vernie.py => vernie/__init__.py | 108 +++++----------------- vernie/carton_board_path.py | 27 ++++++ vernie/read_typed_commands.py | 15 +++ vernie/run_away_game.py | 31 +++++++ vernie/run_commands_file.py | 15 +++ vernie.commands => vernie/vernie.commands | 0 8 files changed, 113 insertions(+), 85 deletions(-) rename vernie.py => vernie/__init__.py (65%) create mode 100644 vernie/carton_board_path.py create mode 100644 vernie/read_typed_commands.py create mode 100644 vernie/run_away_game.py create mode 100644 vernie/run_commands_file.py rename vernie.commands => vernie/vernie.commands (100%) diff --git a/README.md b/README.md index ca3f2d5..5a47e22 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ Requires `gattlib` to be installed, currently Python 2.7 only Best way to start is to look into [demo.py](demo.py) file, and run it. +If you have Vernie assembled, you might look into and run [vernie.py](vernie/vernie.py) file. ## Features diff --git a/demo.py b/demo.py index c928458..e265e2a 100644 --- a/demo.py +++ b/demo.py @@ -2,7 +2,6 @@ from time import sleep from pylgbst import * -from vernie import say log = logging.getLogger("demo") diff --git a/vernie.py b/vernie/__init__.py similarity index 65% rename from vernie.py rename to vernie/__init__.py index 4be4be5..d4fa3aa 100644 --- a/vernie.py +++ b/vernie/__init__.py @@ -4,8 +4,28 @@ import os import re import subprocess -import gtts -import six +try: + import gtts + + + def say(text): + if isinstance(text, str): + text = text.decode("utf-8") + md5 = hashlib.md5(text.encode('utf-8')).hexdigest() + fname = "/tmp/%s.mp3" % md5 + if not os.path.exists(fname): + myre = re.compile('[[A-Za-z]', re.UNICODE) + lang = 'en' if myre.match(text) else 'ru' + + logging.getLogger('requests').setLevel(logging.getLogger('').getEffectiveLevel()) + tts = gtts.gTTS(text=text, lang=lang, slow=False) + tts.save(fname) + + with open(os.devnull, 'w') as fnull: + subprocess.call("mplayer %s" % fname, shell=True, stderr=fnull, stdout=fnull) +except: + def say(text): + sys.stdout.write("%s\n", text) from pylgbst import * @@ -14,23 +34,6 @@ backward = BACKWARD = left = LEFT = -1 straight = STRAIGHT = 0 -def say(text): - if isinstance(text, str): - text = text.decode("utf-8") - md5 = hashlib.md5(text.encode('utf-8')).hexdigest() - fname = "/tmp/%s.mp3" % md5 - if not os.path.exists(fname): - myre = re.compile('[[A-Za-z]', re.UNICODE) - lang = 'en' if myre.match(text) else 'ru' - - logging.getLogger('requests').setLevel(logging.getLogger('').getEffectiveLevel()) - tts = gtts.gTTS(text=text, lang=lang, slow=False) - tts.save(fname) - - with open(os.devnull, 'w') as fnull: - subprocess.call("mplayer %s" % fname, shell=True, stderr=fnull, stdout=fnull) - - class Vernie(MoveHub): SPEECH_LANG_MAP = { 'en': { @@ -71,13 +74,7 @@ class Vernie(MoveHub): self._head_position = 0 self.motor_external.subscribe(self._external_motor_data) - self._color_detected = COLOR_NONE - self._sensor_distance = 10 - self.color_distance_sensor.subscribe(self._color_distance_data) - - time.sleep(1) self._reset_head() - time.sleep(1) self.say("ready") def say(self, phrase): @@ -89,11 +86,6 @@ class Vernie(MoveHub): log.debug("External motor position: %s", data) self._head_position = data - def _color_distance_data(self, color, distance): - log.debug("Color & Distance data: %s %s", COLORS[color], distance) - self._sensor_distance = distance - self._color_detected = color - def _reset_head(self): self.motor_external.timed(1, -0.2) self.head(RIGHT, angle=45) @@ -115,50 +107,7 @@ class Vernie(MoveHub): self.head(STRAIGHT, speed=0.5) self.motor_AB.angled(distance * 450, speed * direction, speed * direction) - def program_carton_board(self): - self.move(FORWARD) - self.move(FORWARD) - self.turn(RIGHT) - self.move(FORWARD) - self.turn(LEFT) - self.move(FORWARD) - self.turn(RIGHT) - self.move(BACKWARD) - self.move(BACKWARD) - self.turn(LEFT) - self.move(FORWARD) - self.move(FORWARD) - self.turn(RIGHT) - self.move(FORWARD) - self.turn(RIGHT) - self.move(FORWARD, 3) - self.turn(LEFT) - self.turn(LEFT) - self.move(BACKWARD, 2) - - def read_typed_commands(self): - self.say('type commands') - - def confirmation(cmd): - self.say("ok") - - while True: - # noinspection PyUnresolvedReferences - cmd = six.moves.input("COMMAND >") - self._interpret_written_command(cmd, confirmation) - - def run_commands_file(self, filename): - self.say("commands from file") - - def confirmation(cmd): - self.say(cmd[0]) - - with open(filename) as fhd: - for cmd in fhd.readlines(): - sys.stdout.write("%s" % cmd) - self._interpret_written_command(cmd, confirmation) - - def _interpret_written_command(self, cmd, confirm): + def interpret_command(self, cmd, confirm): cmd = cmd.strip().lower().split(' ') if cmd[0] in ("head", "голова", "голова"): if cmd[-1] in ("right", "вправо", "направо"): @@ -203,14 +152,5 @@ class Vernie(MoveHub): self.say("Unknown command") self.say("commands help") - -# TODO: distance sensor game # TODO: find and follow the lightest direction game - -if __name__ == '__main__': - logging.basicConfig(level=logging.INFO) - comms.log.setLevel(logging.INFO) - - vernie = Vernie(language="ru") - # vernie.run_commands_file("./vernie.commands") - vernie.read_typed_commands() +# TODO: disable motors if state is not up diff --git a/vernie/carton_board_path.py b/vernie/carton_board_path.py new file mode 100644 index 0000000..bd3fa5a --- /dev/null +++ b/vernie/carton_board_path.py @@ -0,0 +1,27 @@ +from vernie import * + +robot = Vernie() + +robot.say("Hello") + +robot.move(FORWARD) +robot.move(FORWARD) +robot.turn(RIGHT) +robot.move(FORWARD) +robot.turn(LEFT) +robot.move(FORWARD) +robot.turn(RIGHT) +robot.move(BACKWARD) +robot.move(BACKWARD) +robot.turn(LEFT) +robot.move(FORWARD) +robot.move(FORWARD) +robot.turn(RIGHT) +robot.move(FORWARD) +robot.turn(RIGHT) +robot.move(FORWARD, 3) +robot.turn(LEFT) +robot.turn(LEFT) +robot.move(BACKWARD, 2) + +robot.say("Goodbye") diff --git a/vernie/read_typed_commands.py b/vernie/read_typed_commands.py new file mode 100644 index 0000000..9782203 --- /dev/null +++ b/vernie/read_typed_commands.py @@ -0,0 +1,15 @@ +from vernie import * + +robot = Vernie() + +robot.say('type commands') + + +def confirmation(cmd): + robot.say("ok") + + +while True: + # noinspection PyUnresolvedReferences + cmd = six.moves.input("COMMAND >") + robot.interpret_command(cmd, confirmation) diff --git a/vernie/run_away_game.py b/vernie/run_away_game.py new file mode 100644 index 0000000..1296b55 --- /dev/null +++ b/vernie/run_away_game.py @@ -0,0 +1,31 @@ +from vernie import * + +logging.basicConfig(level=logging.INFO) + +robot = Vernie() +running = True + + +def callback(color, distance): + del color # drop unused + speed = (10 - distance) / 10 + secs = (10 - distance) / 10 + logging.info("%s => %s %s", distance, speed, secs) + if speed <= 1: + robot.motor_AB.timed(secs, -speed) + + +def on_btn(pressed): + global running + if pressed: + running = False + + +robot.button.subscribe(on_btn) +robot.color_distance_sensor.subscribe(callback) + +while running: + time.sleep(1) + +robot.color_distance_sensor.unsubscribe(callback) +robot.button.unsubscribe(on_btn) diff --git a/vernie/run_commands_file.py b/vernie/run_commands_file.py new file mode 100644 index 0000000..01a800c --- /dev/null +++ b/vernie/run_commands_file.py @@ -0,0 +1,15 @@ +from vernie import * + +robot = Vernie() + +robot.say("commands from file") + + +def confirmation(cmd): + robot.say(cmd[0]) + + +with open("vernie.commands") as fhd: + for cmd in fhd.readlines(): + sys.stdout.write("%s" % cmd) + robot.interpret_command(cmd, confirmation) diff --git a/vernie.commands b/vernie/vernie.commands similarity index 100% rename from vernie.commands rename to vernie/vernie.commands