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

vernie carton field demo

This commit is contained in:
Andrey Pohilko 2017-09-15 21:15:51 +03:00
parent e992310393
commit ce669eb320
2 changed files with 50 additions and 29 deletions

View File

@ -77,6 +77,9 @@ class LED(Peripheral):
SOMETHING = b'\x51\x00' SOMETHING = b'\x51\x00'
def set_color(self, color, do_notify=True): def set_color(self, color, do_notify=True):
if color == COLOR_NONE:
color = COLOR_BLACK
if color not in COLORS: if color not in COLORS:
raise ValueError("Color %s is not in list of available colors" % color) raise ValueError("Color %s is not in list of available colors" % color)

View File

@ -1,12 +1,18 @@
from pylgbst import * from pylgbst import *
right = RIGHT = 1 forward = FORWARD = right = RIGHT = 1
left = LEFT = -1 backward = BACKWARD = left = LEFT = -1
straight = STRAIGHT = 0 straight = STRAIGHT = 0
class Vernie(MoveHub): class Vernie(MoveHub):
def __init__(self, conn=None): def __init__(self):
try:
conn = DebugServerConnection()
except BaseException:
logging.debug("Failed to use debug server: %s", traceback.format_exc())
conn = BLEConnection().connect()
super(Vernie, self).__init__(conn) super(Vernie, self).__init__(conn)
while True: while True:
@ -29,51 +35,63 @@ class Vernie(MoveHub):
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:
self._color_detected = color self._color_detected = color
self.led.set_color(self._color_detected if self._color_detected != COLOR_NONE else COLOR_BLACK) if self._color_detected != COLOR_NONE:
self.led.set_color(self._color_detected)
def _reset_head(self): def _reset_head(self):
self.motor_external.timed(1, -0.2) self.motor_external.timed(1, -0.2)
self.head_to(RIGHT, angle=45) self.head_to(RIGHT, speed=45)
def head_to(self, direction=RIGHT, speed=0.1, angle=25): def head_to(self, direction=RIGHT, angle=25, speed=0.1):
if direction == STRAIGHT: if direction == STRAIGHT:
angle = -self._head_position angle = -self._head_position
direction = 1 direction = 1
self.motor_external.angled(direction * angle, speed) self.motor_external.angled(direction * angle, speed)
def turn(self, direction, degrees=90, speed=0.3):
self.head_to(STRAIGHT, speed=1)
self.head_to(direction, 35, 1)
self.motor_AB.angled(225 * degrees / 90, speed * direction, -speed * direction)
self.head_to(STRAIGHT, speed=1)
def move(self, direction, distance=1, speed=0.3):
self.head_to(STRAIGHT, speed=0.5)
self.motor_AB.angled(distance * 450, speed * direction, speed * direction)
def program(self): def program(self):
while True: # while True:
self.head_to(LEFT) self.move(FORWARD)
time.sleep(1) self.move(FORWARD)
self.turn(RIGHT)
self.head_to(STRAIGHT) self.move(FORWARD)
time.sleep(1) self.turn(LEFT)
self.move(FORWARD)
self.head_to(RIGHT) self.turn(RIGHT)
time.sleep(1) self.move(BACKWARD)
self.move(BACKWARD)
self.head_to(STRAIGHT) self.turn(LEFT)
time.sleep(1) 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)
if __name__ == '__main__': if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
comms.log.setLevel(logging.INFO) comms.log.setLevel(logging.INFO)
try: vernie = Vernie()
connection = DebugServerConnection()
except BaseException:
logging.warning("Failed to use debug server: %s", traceback.format_exc())
connection = BLEConnection().connect()
vernie = Vernie(connection)
vernie.program() vernie.program()