diff --git a/README.md b/README.md index 10fde67..430c3c3 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/vernie/__init__.py b/vernie/__init__.py index 6f39e2d..da76e2a 100644 --- a/vernie/__init__.py +++ b/vernie/__init__.py @@ -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): diff --git a/vernie/go_towards_light.py b/vernie/go_towards_light.py new file mode 100644 index 0000000..f61ad75 --- /dev/null +++ b/vernie/go_towards_light.py @@ -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) diff --git a/vernie/run_away_game.py b/vernie/run_away_game.py index 3f5df2a..28bdbc3 100644 --- a/vernie/run_away_game.py +++ b/vernie/run_away_game.py @@ -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)))