Skip to content

Instantly share code, notes, and snippets.

@tomoinn
Created January 9, 2020 18:38
Show Gist options
  • Save tomoinn/17ba28a66aa0aae21cc9c9170f0ffbb5 to your computer and use it in GitHub Desktop.
Save tomoinn/17ba28a66aa0aae21cc9c9170f0ffbb5 to your computer and use it in GitHub Desktop.
from approxeng.task import task, resource, run
import time
@resource(name='lux')
def read_light_sensor():
sensor_reading = ...
return sensor_reading
# Create the motor board object, this will depend on the hardware
# fitted to your robot.
motor_board = ...
# Tell the task framework about it, give it a name.
register_resource(name='motors', value=motor_board)
# Task to wait for the light to get too bright
@task(name='light_monitor', resources=['lux'])
def light_monitoring_task(world):
light_reading = world.lux
if light_reading > 9000:
return 'run_away'
sleep(1)
@task(name='run_away', resources=['lux','motors'])
def run_away_and_hide(world)
# Fire up the motors and get out of here!
world.motors.go_really_fast()
light_reading = world.lux
if light_reading < 1000:
# Ah, soothing darkness
return 'light_monitor'
sleep(1)
run(root_task='light_monitor')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment