def main(): robot = Robot(debug=True, verbose=True, power=20) # Sensors light = robot.init_light_sensor(PORT_2) touch = robot.init_touch_sensor(PORT_3) # Motors robot.init_servo(PORT_B) robot.init_synchronized_motors(PORT_A, PORT_C) # Initial setup robot.set_servo( SERVO_NICE, power=50 ) # It's called nice because the servo movement looks freaking awesome robot.turn_light_sensor(ON) # Light sensor calibration black, white = robot.calibrate_light(interactive=True) # Thresholds lower = 4 * (10**-1) upper = 9 * (10**-1) def until(light_sensor, touch_sensor, **kwargs): return normalize(light_sensor.get_lightness(), black, white) > upper or touch_sensor.is_pressed() sleep(1) robot.move_forward(until=until, until_args=(light, touch)) while robot.running: light_level = normalize(light.get_lightness(), black, white) if light_level > lower: robot.debug('Current light level: ', light_level) robot.turn_right( 8, 15) # Turn right 15° to see if we correct the course light_level_tmp = normalize(light.get_lightness(), black, white) if light_level_tmp > light_level: robot.debug( 'Right turn didn\'t improve course. Turning left...') robot.turn_left(8, 30) if not until(light, touch): robot.move_forward(until=until, until_args=(light, touch)) # WARNING: From here, the code is even more experimental. # I encourage you to consider this some sort of fancy pseudo-code. if touch.is_pressed(): robot.debug('Found an obstacle.') robot.turn_right(60, 270) robot.move_backwards(dist=30) robot.turn_left(power=50, degrees=270 - 45) robot.move_forward(dist=50, power=100) # Dist is given in cm robot.turn_light_sensor(OFF)
def main(): brick = locator.find_one_brick(debug=True) robot = Robot(brick, debug=True, verbose=True, power=80) # Excessive output # Motors robot.init_synchronized_motors(PORT_A, PORT_C) robot.init_servo(PORT_B) light = robot.init_light_sensor(PORT_2) robot.turn_light_sensor(ON) robot.set_servo(SERVO_DOWN) table_value = normalize(light.get_sample(), 0, 1023) print(table_value) def until(): print(normalize(light.get_sample(), 0, 1023)) return normalize(light.get_sample(), 0, 1023) < 0.3 robot.move_forward(until=until, until_args=(), until_kwargs={}, wait=True) robot.turn_light_sensor(OFF)
def until(light_sensor, touch_sensor, **kwargs): return normalize(light_sensor.get_lightness(), black, white) > upper or touch_sensor.is_pressed()
def until(light_sensor, **kwargs): # Must explicitly state **kwargs as his argument. return normalize(light_sensor.get_lightness(), light_off, light_on) < lower
def main(): brick = locator.find_one_brick(debug=True) robot = Robot(brick, debug=True, verbose=True, power=80) # Excessive output # Motors robot.init_synchronized_motors(PORT_A, PORT_C) robot.init_servo(PORT_B) # Sensors light = robot.init_light_sensor(PORT_2) sound = robot.init_sound_sensor(PORT_4) # Intial setup robot.set_servo(SERVO_UP) robot.turn_light_sensor(ON) # Calibration (light) # light_off, light_on = robot.calibrate_light(interactive=True) # FIXME: Output says 'Point the sensor to black line' light_off, light_on = 229, 900 # Sound sensor calibration quiet, loud = 15, 1024 # robot.calibrate_sound(interactive=True) # Thresholds lower = 5 * (10**-1) lower_noise = 1 # 9 * (10 ** -1) upper = 5 * (10**-1) # until = lambda *args, ls=light, **kwargs: normalize(ls.get_lightness(), light_off, light_on) > upper print('starting...') sleep(3) # TODO I should probably lock the access to light sensor. def until(light_sensor, **kwargs): # Must explicitly state **kwargs as his argument. return normalize(light_sensor.get_lightness(), light_off, light_on) < lower robot.move_forward(until=until, until_args=(light, )) while robot.running: light_level = normalize(light.get_lightness(), light_off, light_on) robot.debug('Current light level: ', light_level) if light_level < lower: robot.turn_right( 50, 15) # Turn right 15° to see if we correct the course light_level_tmp = normalize(light.get_lightness(), light_off, light_on) robot.debug('temp lecture at right', light_level_tmp) if light_level_tmp > light_level: robot.debug( 'Right turn didn\'t improve course. Turning left...') robot.turn_left(50, 30) sleep(.4) s = sound.get_sample() loudness = normalize(s, quiet, loud) robot.debug('Value of loudness is ', loudness, s) if loudness > lower_noise: robot.stop() robot.morse('SOS') robot.move_backwards(seconds=3, wait=True) if not until(light): robot.move_forward(until=until, until_args=( light, )) # FIXME: Does this cause an infinite loop? robot.turn_light_sensor(OFF) print('DONE: Won\'t do anything else.') exit(0)
def until(): print(normalize(light.get_sample(), 0, 1023)) return normalize(light.get_sample(), 0, 1023) < 0.3