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:
parent
e992310393
commit
ce669eb320
@ -77,6 +77,9 @@ class LED(Peripheral):
|
||||
SOMETHING = b'\x51\x00'
|
||||
|
||||
def set_color(self, color, do_notify=True):
|
||||
if color == COLOR_NONE:
|
||||
color = COLOR_BLACK
|
||||
|
||||
if color not in COLORS:
|
||||
raise ValueError("Color %s is not in list of available colors" % color)
|
||||
|
||||
|
76
vernie.py
76
vernie.py
@ -1,12 +1,18 @@
|
||||
from pylgbst import *
|
||||
|
||||
right = RIGHT = 1
|
||||
left = LEFT = -1
|
||||
forward = FORWARD = right = RIGHT = 1
|
||||
backward = BACKWARD = left = LEFT = -1
|
||||
straight = STRAIGHT = 0
|
||||
|
||||
|
||||
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)
|
||||
|
||||
while True:
|
||||
@ -29,51 +35,63 @@ class Vernie(MoveHub):
|
||||
log.info("Vernie is ready.")
|
||||
|
||||
def _external_motor_data(self, data):
|
||||
#log.debug("External motor position: %s", data)
|
||||
log.debug("External motor position: %s", data)
|
||||
self._head_position = data
|
||||
|
||||
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
|
||||
if 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._color_detected = color
|
||||
if self._color_detected != COLOR_NONE:
|
||||
self.led.set_color(self._color_detected)
|
||||
|
||||
def _reset_head(self):
|
||||
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:
|
||||
angle = -self._head_position
|
||||
direction = 1
|
||||
|
||||
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):
|
||||
while True:
|
||||
self.head_to(LEFT)
|
||||
time.sleep(1)
|
||||
|
||||
self.head_to(STRAIGHT)
|
||||
time.sleep(1)
|
||||
|
||||
self.head_to(RIGHT)
|
||||
time.sleep(1)
|
||||
|
||||
self.head_to(STRAIGHT)
|
||||
time.sleep(1)
|
||||
# while True:
|
||||
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)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
comms.log.setLevel(logging.INFO)
|
||||
|
||||
try:
|
||||
connection = DebugServerConnection()
|
||||
except BaseException:
|
||||
logging.warning("Failed to use debug server: %s", traceback.format_exc())
|
||||
connection = BLEConnection().connect()
|
||||
|
||||
vernie = Vernie(connection)
|
||||
vernie = Vernie()
|
||||
vernie.program()
|
||||
|
Loading…
x
Reference in New Issue
Block a user