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

Go towards light Vernie demo

This commit is contained in:
Andrey Pohilko 2017-09-18 17:13:55 +03:00
parent 2536ed1249
commit b4b32f4090
4 changed files with 62 additions and 4 deletions

View File

@ -68,8 +68,9 @@ sudo python -c "from pylgbst.comms import *; \
- document all API methods
- make sure unit tests cover all important code
- generalize getting device info + give constants (low priority)
- subscribing to color sensor disables LED operating in `run_away_game.py`. Why?
- subscribing to 2 sensors at once causes port status to not arrive => sync mode stuck. Why?
- can we subscribe to LED?
- organize requesting and printing device info on startup - firmware version at least
## Links

View File

@ -74,7 +74,7 @@ class Vernie(MoveHub):
self._head_position = 0
self.motor_external.subscribe(self._external_motor_data)
# self._reset_head() FIXME: restore it
self._reset_head()
# self.say("ready")
time.sleep(1)
@ -101,7 +101,7 @@ class Vernie(MoveHub):
def turn(self, direction, degrees=90, speed=0.3):
self.head(STRAIGHT, speed=1)
self.head(direction, 35, 1)
self.motor_AB.angled(225 * degrees / 90, speed * direction, -speed * direction)
self.motor_AB.angled(int(2.7 * degrees), speed * direction, -speed * direction)
self.head(STRAIGHT, speed=1)
def move(self, direction, distance=1, speed=0.3):

View File

@ -0,0 +1,57 @@
from vernie import *
logging.basicConfig(level=logging.INFO)
robot = Vernie()
running = True
criterion = max
def on_change_lum(lum):
global mode_rotation
if mode_rotation:
global lum_values
lum_values.append(lum)
else:
global cur_luminosity
if cur_luminosity < 0:
cur_luminosity = lum # initial value
elif cur_luminosity != lum:
cur_luminosity = 2 # value above 1 signals end of movement, 'cause lum value is float below 1.0
def on_btn(pressed):
global running
if pressed:
running = False
robot.button.subscribe(on_btn)
robot.color_distance_sensor.subscribe(on_change_lum, CDS_MODE_LUMINOSITY, granularity=0)
while running:
mode_rotation = True
# turn around, measuring luminosity
lum_values = []
robot.turn(RIGHT, degrees=360, speed=0.2)
# get max luminosity angle
idx = lum_values.index(criterion(lum_values))
angle = int(360.0 * idx / len(lum_values))
# turn towards light
if angle > 180:
robot.turn(LEFT, degrees=360 - angle)
else:
robot.turn(RIGHT, degrees=angle)
# Now let's move until luminosity changes
# robot.color_distance_sensor.subscribe(get_changed_luminosity, CDS_MODE_LUMINOSITY, granularity=10)
mode_rotation = False
cur_luminosity = -1
while cur_luminosity < 1:
robot.move(FORWARD, 2)
robot.color_distance_sensor.unsubscribe(on_change_lum)

View File

@ -4,8 +4,8 @@ robot = Vernie()
running = True
def callback(color, distance):
robot.led.set_color(color)
speed = (10 - distance + 1) / 10.0
secs = (10 - distance + 1) / 10.0
print("Distance is %.1f inches, I'm running back with %s%% speed!" % (distance, int(speed * 100)))