mirror of
https://github.com/undera/pylgbst.git
synced 2020-11-18 19:37:26 -08:00
Improve vernie
This commit is contained in:
parent
cf7dee8f7b
commit
bb602dae98
@ -23,7 +23,7 @@ try:
|
|||||||
|
|
||||||
with open(os.devnull, 'w') as fnull:
|
with open(os.devnull, 'w') as fnull:
|
||||||
subprocess.call("mplayer %s" % fname, shell=True, stderr=fnull, stdout=fnull)
|
subprocess.call("mplayer %s" % fname, shell=True, stderr=fnull, stdout=fnull)
|
||||||
except:
|
except BaseException:
|
||||||
def say(text):
|
def say(text):
|
||||||
sys.stdout.write("%s\n", text)
|
sys.stdout.write("%s\n", text)
|
||||||
|
|
||||||
@ -33,9 +33,7 @@ forward = FORWARD = right = RIGHT = 1
|
|||||||
backward = BACKWARD = left = LEFT = -1
|
backward = BACKWARD = left = LEFT = -1
|
||||||
straight = STRAIGHT = 0
|
straight = STRAIGHT = 0
|
||||||
|
|
||||||
|
SPEECH_LANG_MAP = {
|
||||||
class Vernie(MoveHub):
|
|
||||||
SPEECH_LANG_MAP = {
|
|
||||||
'en': {
|
'en': {
|
||||||
'ready': "Vernie the Robot is ready.",
|
'ready': "Vernie the Robot is ready.",
|
||||||
"commands help": "Available commands are: "
|
"commands help": "Available commands are: "
|
||||||
@ -52,8 +50,12 @@ class Vernie(MoveHub):
|
|||||||
"Finished": "Робот завершает работу. Спасибо!",
|
"Finished": "Робот завершает работу. Спасибо!",
|
||||||
"commands from file": "Исполняю команды из файла",
|
"commands from file": "Исполняю команды из файла",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
VERNIE_TO_MOTOR_DEGREES = 2.7
|
||||||
|
|
||||||
|
|
||||||
|
class Vernie(MoveHub):
|
||||||
def __init__(self, language='en'):
|
def __init__(self, language='en'):
|
||||||
self.language = language
|
self.language = language
|
||||||
try:
|
try:
|
||||||
@ -79,8 +81,8 @@ class Vernie(MoveHub):
|
|||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
def say(self, phrase):
|
def say(self, phrase):
|
||||||
if phrase in self.SPEECH_LANG_MAP[self.language]:
|
if phrase in SPEECH_LANG_MAP[self.language]:
|
||||||
phrase = self.SPEECH_LANG_MAP[self.language][phrase]
|
phrase = SPEECH_LANG_MAP[self.language][phrase]
|
||||||
say(phrase)
|
say(phrase)
|
||||||
|
|
||||||
def _external_motor_data(self, data):
|
def _external_motor_data(self, data):
|
||||||
@ -101,7 +103,7 @@ class Vernie(MoveHub):
|
|||||||
def turn(self, direction, degrees=90, speed=0.3):
|
def turn(self, direction, degrees=90, speed=0.3):
|
||||||
self.head(STRAIGHT, speed=1)
|
self.head(STRAIGHT, speed=1)
|
||||||
self.head(direction, 35, 1)
|
self.head(direction, 35, 1)
|
||||||
self.motor_AB.angled(int(2.7 * degrees), speed * direction, -speed * direction)
|
self.motor_AB.angled(int(VERNIE_TO_MOTOR_DEGREES * degrees), speed * direction, -speed * direction)
|
||||||
self.head(STRAIGHT, speed=1)
|
self.head(STRAIGHT, speed=1)
|
||||||
|
|
||||||
def move(self, direction, distance=1, speed=0.3):
|
def move(self, direction, distance=1, speed=0.3):
|
||||||
@ -128,14 +130,14 @@ class Vernie(MoveHub):
|
|||||||
elif cmd[0] in ("forward", "вперёд", "вперед"):
|
elif cmd[0] in ("forward", "вперёд", "вперед"):
|
||||||
try:
|
try:
|
||||||
dist = int(cmd[-1])
|
dist = int(cmd[-1])
|
||||||
except:
|
except BaseException:
|
||||||
dist = 1
|
dist = 1
|
||||||
confirm(cmd)
|
confirm(cmd)
|
||||||
self.move(FORWARD, distance=dist)
|
self.move(FORWARD, distance=dist)
|
||||||
elif cmd[0] in ("backward", "назад"):
|
elif cmd[0] in ("backward", "назад"):
|
||||||
try:
|
try:
|
||||||
dist = int(cmd[-1])
|
dist = int(cmd[-1])
|
||||||
except:
|
except BaseException:
|
||||||
dist = 1
|
dist = 1
|
||||||
confirm(cmd)
|
confirm(cmd)
|
||||||
self.move(BACKWARD, distance=dist)
|
self.move(BACKWARD, distance=dist)
|
||||||
|
@ -66,8 +66,8 @@ try:
|
|||||||
messageString = message.decode("utf-8")
|
messageString = message.decode("utf-8")
|
||||||
a, b, c = decode_xml(messageString)
|
a, b, c = decode_xml(messageString)
|
||||||
divider = 2.0 if c > 0 else -2.0
|
divider = 2.0 if c > 0 else -2.0
|
||||||
sa = int(10 * (c + b / divider)) / 10.0
|
sa = round(c + b / divider, 2)
|
||||||
sb = int(10 * (c - b / divider)) / 10.0
|
sb = round(c - b / divider, 2)
|
||||||
logging.info("SpeedA=%s, SpeedB=%s", sa, sb)
|
logging.info("SpeedA=%s, SpeedB=%s", sa, sb)
|
||||||
robot.motor_AB.constant(sa, sb, async=True)
|
robot.motor_AB.constant(sa, sb, async=True)
|
||||||
time.sleep(0.5)
|
time.sleep(0.5)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user