1
0
mirror of https://github.com/undera/pylgbst.git synced 2020-11-18 19:37:26 -08:00

Fixing vernie

This commit is contained in:
Andrey Pohilko 2017-09-21 13:42:20 +03:00
parent c255a38b43
commit a6047b3279
4 changed files with 21 additions and 16 deletions

View File

@ -1,9 +1,9 @@
import logging import logging
import time import time
import traceback
from struct import pack, unpack from struct import pack, unpack
from threading import Thread from threading import Thread
# noinspection PyUnresolvedReferences
from six.moves import queue from six.moves import queue
from pylgbst.comms import str2hex from pylgbst.comms import str2hex
@ -66,11 +66,12 @@ class Peripheral(object):
self._port_subscription_mode = mode self._port_subscription_mode = mode
self.started() self.started()
self._port_subscribe(self._port_subscription_mode, granularity, True) self._port_subscribe(self._port_subscription_mode, granularity, True)
if callback:
self._subscribers.add(callback)
self._wait_sync(async) # having async=True leads to stuck notifications self._wait_sync(async) # having async=True leads to stuck notifications
if callback:
self._subscribers.add(callback)
def unsubscribe(self, callback=None): def unsubscribe(self, callback=None):
if callback in self._subscribers: if callback in self._subscribers:
self._subscribers.remove(callback) self._subscribers.remove(callback)
@ -98,6 +99,7 @@ class Peripheral(object):
try: try:
self.handle_port_data(data) self.handle_port_data(data)
except BaseException: except BaseException:
log.warning("%s", traceback.format_exc())
log.warning("Failed to handle port data by %s: %s", self, str2hex(data)) log.warning("Failed to handle port data by %s: %s", self, str2hex(data))
def _wait_sync(self, async): def _wait_sync(self, async):
@ -330,8 +332,8 @@ class ColorDistanceSensor(Peripheral):
def handle_port_data(self, data): def handle_port_data(self, data):
if self._port_subscription_mode == self.COLOR_DISTANCE_FLOAT: if self._port_subscription_mode == self.COLOR_DISTANCE_FLOAT:
color = unpack("<B", data[4:5])[0] color = unpack("<B", data[4:5])[0]
distance = unpack("<B", data[6:7])[0] distance = unpack("<B", data[5:6])[0]
partial = unpack("<B", data[7:0])[0] partial = unpack("<B", data[7:8])[0]
if partial: if partial:
distance += 1.0 / partial distance += 1.0 / partial
self._notify_subscribers(color, float(distance)) self._notify_subscribers(color, float(distance))

View File

@ -14,6 +14,7 @@ try:
def say(text): def say(text):
return
if isinstance(text, str): if isinstance(text, str):
text = text.decode("utf-8") text = text.decode("utf-8")
md5 = hashlib.md5(text.encode('utf-8')).hexdigest() md5 = hashlib.md5(text.encode('utf-8')).hexdigest()
@ -55,12 +56,12 @@ SPEECH_LANG_MAP = {
} }
} }
VERNIE_TO_MOTOR_DEGREES = 2.7 VERNIE_TO_MOTOR_DEGREES = 2.6
VERNIE_SINGLE_MOVE = 430
class Vernie(MoveHub): class Vernie(MoveHub):
def __init__(self, language='en'): def __init__(self, language='en'):
self.language = language
try: try:
conn = DebugServerConnection() conn = DebugServerConnection()
except BaseException: except BaseException:
@ -68,6 +69,7 @@ class Vernie(MoveHub):
conn = BLEConnection().connect() conn = BLEConnection().connect()
super(Vernie, self).__init__(conn) super(Vernie, self).__init__(conn)
self.language = language
while True: while True:
required_devices = (self.color_distance_sensor, self.motor_external) required_devices = (self.color_distance_sensor, self.motor_external)
@ -104,14 +106,14 @@ class Vernie(MoveHub):
self.motor_external.angled(direction * angle, speed) self.motor_external.angled(direction * angle, speed)
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=0.5)
self.head(direction, 35, 1) #self.head(direction, 35, 1)
self.motor_AB.angled(int(VERNIE_TO_MOTOR_DEGREES * 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=0.5)
def move(self, direction, distance=1, speed=0.3): def move(self, direction, distance=1, speed=0.2):
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 * VERNIE_SINGLE_MOVE, speed * direction, speed * direction)
def interpret_command(self, cmd, confirm): def interpret_command(self, cmd, confirm):
cmd = cmd.strip().lower().split(' ') cmd = cmd.strip().lower().split(' ')
@ -157,6 +159,3 @@ class Vernie(MoveHub):
else: else:
self.say("Unknown command") self.say("Unknown command")
self.say("commands help") self.say("commands help")
# TODO: disable motors if state is not up
# TODO: stop motors on bump?

View File

@ -1,5 +1,9 @@
import logging
from vernie import * from vernie import *
logging.basicConfig(level=logging.DEBUG)
robot = Vernie() robot = Vernie()
running = True running = True