mirror of
https://github.com/undera/pylgbst.git
synced 2020-11-18 19:37:26 -08:00
Improve vernie part
This commit is contained in:
parent
79ba083abc
commit
81ce664d21
@ -3,6 +3,7 @@
|
|||||||
Requires `gattlib` to be installed, currently Python 2.7 only
|
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.
|
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
|
## Features
|
||||||
|
|
||||||
|
1
demo.py
1
demo.py
@ -2,7 +2,6 @@
|
|||||||
from time import sleep
|
from time import sleep
|
||||||
|
|
||||||
from pylgbst import *
|
from pylgbst import *
|
||||||
from vernie import say
|
|
||||||
|
|
||||||
log = logging.getLogger("demo")
|
log = logging.getLogger("demo")
|
||||||
|
|
||||||
|
@ -4,8 +4,28 @@ import os
|
|||||||
import re
|
import re
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
import gtts
|
try:
|
||||||
import six
|
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 *
|
from pylgbst import *
|
||||||
|
|
||||||
@ -14,23 +34,6 @@ backward = BACKWARD = left = LEFT = -1
|
|||||||
straight = STRAIGHT = 0
|
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):
|
class Vernie(MoveHub):
|
||||||
SPEECH_LANG_MAP = {
|
SPEECH_LANG_MAP = {
|
||||||
'en': {
|
'en': {
|
||||||
@ -71,13 +74,7 @@ class Vernie(MoveHub):
|
|||||||
self._head_position = 0
|
self._head_position = 0
|
||||||
self.motor_external.subscribe(self._external_motor_data)
|
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()
|
self._reset_head()
|
||||||
time.sleep(1)
|
|
||||||
self.say("ready")
|
self.say("ready")
|
||||||
|
|
||||||
def say(self, phrase):
|
def say(self, phrase):
|
||||||
@ -89,11 +86,6 @@ class Vernie(MoveHub):
|
|||||||
log.debug("External motor position: %s", data)
|
log.debug("External motor position: %s", data)
|
||||||
self._head_position = 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):
|
def _reset_head(self):
|
||||||
self.motor_external.timed(1, -0.2)
|
self.motor_external.timed(1, -0.2)
|
||||||
self.head(RIGHT, angle=45)
|
self.head(RIGHT, angle=45)
|
||||||
@ -115,50 +107,7 @@ class Vernie(MoveHub):
|
|||||||
self.head(STRAIGHT, speed=0.5)
|
self.head(STRAIGHT, speed=0.5)
|
||||||
self.motor_AB.angled(distance * 450, speed * direction, speed * direction)
|
self.motor_AB.angled(distance * 450, speed * direction, speed * direction)
|
||||||
|
|
||||||
def program_carton_board(self):
|
def interpret_command(self, cmd, confirm):
|
||||||
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):
|
|
||||||
cmd = cmd.strip().lower().split(' ')
|
cmd = cmd.strip().lower().split(' ')
|
||||||
if cmd[0] in ("head", "голова", "голова"):
|
if cmd[0] in ("head", "голова", "голова"):
|
||||||
if cmd[-1] in ("right", "вправо", "направо"):
|
if cmd[-1] in ("right", "вправо", "направо"):
|
||||||
@ -203,14 +152,5 @@ class Vernie(MoveHub):
|
|||||||
self.say("Unknown command")
|
self.say("Unknown command")
|
||||||
self.say("commands help")
|
self.say("commands help")
|
||||||
|
|
||||||
|
|
||||||
# TODO: distance sensor game
|
|
||||||
# TODO: find and follow the lightest direction game
|
# TODO: find and follow the lightest direction game
|
||||||
|
# TODO: disable motors if state is not up
|
||||||
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()
|
|
27
vernie/carton_board_path.py
Normal file
27
vernie/carton_board_path.py
Normal file
@ -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")
|
15
vernie/read_typed_commands.py
Normal file
15
vernie/read_typed_commands.py
Normal file
@ -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)
|
31
vernie/run_away_game.py
Normal file
31
vernie/run_away_game.py
Normal file
@ -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)
|
15
vernie/run_commands_file.py
Normal file
15
vernie/run_commands_file.py
Normal file
@ -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)
|
Loading…
x
Reference in New Issue
Block a user