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

Probablu bug has been fixed?

This commit is contained in:
Andrey Pohilko 2017-09-15 20:05:35 +03:00
parent 4e8708453a
commit e992310393
5 changed files with 26 additions and 21 deletions

View File

@ -8,7 +8,7 @@ import socket
import sys import sys
import time import time
import traceback import traceback
from Queue import Queue from six.moves import queue
from abc import abstractmethod from abc import abstractmethod
from binascii import unhexlify from binascii import unhexlify
from gattlib import DiscoveryService, GATTRequester from gattlib import DiscoveryService, GATTRequester
@ -42,7 +42,8 @@ class Requester(GATTRequester):
super(Requester, self).__init__(p_object, *args, **kwargs) super(Requester, self).__init__(p_object, *args, **kwargs)
self.notification_sink = None self.notification_sink = None
self._notify_queue = Queue() # noinspection PyUnresolvedReferences
self._notify_queue = queue.Queue()
self._notifier_thread = Thread(target=self._dispatch_notifications) self._notifier_thread = Thread(target=self._dispatch_notifications)
self._notifier_thread.setDaemon(True) self._notifier_thread.setDaemon(True)
self._notifier_thread.setName("Notify queue dispatcher") self._notifier_thread.setName("Notify queue dispatcher")
@ -170,7 +171,7 @@ class DebugServer(object):
self.sock.close() self.sock.close()
def _notify_dummy(self, handle, data): def _notify_dummy(self, handle, data):
log.debug("Notification from handle %s: %s", handle, hexlify(data.strip())) log.debug("Notification from handle %s: %s", handle, binascii.hexlify(data.strip()))
self._check_shutdown(data) self._check_shutdown(data)
def _notify(self, conn, handle, data): def _notify(self, conn, handle, data):

View File

@ -34,7 +34,7 @@ PORTS = {
# PACKET TYPES # PACKET TYPES
MSG_DEVICE_INFO = 0x01 MSG_DEVICE_INFO = 0x01
MSG_DEVICE_SHUTDOWN = 0x02 MSG_DEVICE_SHUTDOWN = 0x02 # sent when hub shuts down by button hold
MSG_PING_RESPONSE = 0x03 MSG_PING_RESPONSE = 0x03
MSG_PORT_INFO = 0x04 MSG_PORT_INFO = 0x04
MSG_PORT_CMD_ERROR = 0x05 MSG_PORT_CMD_ERROR = 0x05

View File

@ -21,7 +21,7 @@ class Peripheral(object):
super(Peripheral, self).__init__() super(Peripheral, self).__init__()
self.parent = parent self.parent = parent
self.port = port self.port = port
self._working = 0 # 3-state, -1 means command sent, 1 means notified on command, 0 means notified on finish self._working = False
self._subscribers = set() self._subscribers = set()
self._port_subscription_mode = None self._port_subscription_mode = None
@ -41,10 +41,12 @@ class Peripheral(object):
self._write_to_hub(MSG_SENSOR_SUBSCRIBE, params) self._write_to_hub(MSG_SENSOR_SUBSCRIBE, params)
def started(self): def started(self):
self._working = 1 log.debug("Started: %s", self)
self._working = True
def finished(self): def finished(self):
self._working = 0 log.debug("Finished: %s", self)
self._working = False
def is_working(self): def is_working(self):
return bool(self._working) return bool(self._working)
@ -79,7 +81,7 @@ class LED(Peripheral):
raise ValueError("Color %s is not in list of available colors" % color) raise ValueError("Color %s is not in list of available colors" % color)
cmd = pack("<?", do_notify) + self.SOMETHING + pack("<B", color) cmd = pack("<?", do_notify) + self.SOMETHING + pack("<B", color)
self._working = -1 self.started()
self._write_to_hub(MSG_SET_PORT_VAL, cmd) self._write_to_hub(MSG_SET_PORT_VAL, cmd)
def finished(self): def finished(self):
@ -140,7 +142,7 @@ class EncodedMotor(Peripheral):
raise ValueError("Too large value for seconds: %s", seconds) raise ValueError("Too large value for seconds: %s", seconds)
command += pack('<H', msec) command += pack('<H', msec)
self._working = -1 self.started()
self._wrap_and_write(command, speed_primary, speed_secondary) self._wrap_and_write(command, speed_primary, speed_secondary)
self.__wait_sync(async) self.__wait_sync(async)
@ -158,7 +160,7 @@ class EncodedMotor(Peripheral):
# angle # angle
command += pack('<I', angle) command += pack('<I', angle)
self._working = -1 self.started()
self._wrap_and_write(command, speed_primary, speed_secondary) self._wrap_and_write(command, speed_primary, speed_secondary)
self.__wait_sync(async) self.__wait_sync(async)
@ -166,8 +168,7 @@ class EncodedMotor(Peripheral):
if not async: if not async:
log.debug("Waiting for sync command work to finish...") log.debug("Waiting for sync command work to finish...")
while self.is_working(): while self.is_working():
log.debug("Waiting") time.sleep(0.5)
time.sleep(0.1)
log.debug("Command has finished.") log.debug("Command has finished.")
# TODO: how to tell when motor has stopped? # TODO: how to tell when motor has stopped?

View File

@ -6,5 +6,5 @@ setup(name='pylgbst',
author='Andrey Pokhilko', author='Andrey Pokhilko',
author_email='apc4@ya.ru', author_email='apc4@ya.ru',
packages=['pylgbst'], packages=['pylgbst'],
requires=['gattlib'] requires=['six', 'future', 'gattlib']
) )

View File

@ -22,20 +22,28 @@ class Vernie(MoveHub):
self._color_detected = COLOR_NONE self._color_detected = COLOR_NONE
self._sensor_distance = 10 self._sensor_distance = 10
self.color_distance_sensor.subscribe(self._color_distance_data) self.color_distance_sensor.subscribe(self._color_distance_data)
time.sleep(1)
self._reset_head()
time.sleep(1)
log.info("Vernie is ready.") log.info("Vernie is ready.")
def _external_motor_data(self, data): def _external_motor_data(self, data):
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): def _color_distance_data(self, color, distance):
log.debug("Color & Distance data: %s %s", COLORS[color], distance) #log.debug("Color & Distance data: %s %s", COLORS[color], distance)
self._sensor_distance = distance self._sensor_distance = distance
if self._color_detected != color: if self._color_detected != color:
self._color_detected = color self._color_detected = color
self.led.set_color(self._color_detected if self._color_detected != COLOR_NONE else COLOR_BLACK) self.led.set_color(self._color_detected if self._color_detected != COLOR_NONE else COLOR_BLACK)
def head_to(self, direction=RIGHT, speed=0.25, angle=25): def _reset_head(self):
self.motor_external.timed(1, -0.2)
self.head_to(RIGHT, angle=45)
def head_to(self, direction=RIGHT, speed=0.1, angle=25):
if direction == STRAIGHT: if direction == STRAIGHT:
angle = -self._head_position angle = -self._head_position
direction = 1 direction = 1
@ -43,11 +51,6 @@ class Vernie(MoveHub):
self.motor_external.angled(direction * angle, speed) self.motor_external.angled(direction * angle, speed)
def program(self): def program(self):
time.sleep(1)
self.head_to(LEFT, angle=90)
self.head_to(RIGHT, angle=50)
time.sleep(1)
while True: while True:
self.head_to(LEFT) self.head_to(LEFT)
time.sleep(1) time.sleep(1)