def distance_to_beacon(): sound = Sound() ir = InfraredSensor() while True: for i in range(10): # try replacing with ir.distance(), ir.heading() # or ir.heading_and_distance() distance = ir.heading_and_distance() if distance == None: # distance() returns None if no beacon detected print('Beacon off?', end=' ') print('Beacon off?', end=' ', file=stderr) else: # print to EV3 LCD screen # print a space instead of starting a new line print(distance, end=' ') # print to VS Code output panel print(distance, end=' ', file=stderr) sound.play_tone(1800 - 10 * distance, 0.4) sleep(0.5) print('') # start new line on EV3 screen print('', file=stderr) # start new line in VS Code
#!/usr/bin/env micropython from ev3dev2.motor import MediumMotor, OUTPUT_A from ev3dev2.sensor.lego import InfraredSensor from ev3dev2.sensor import INPUT_4 from time import sleep import math import sys #Code is here IR = InfraredSensor(INPUT_4) meMotor = MediumMotor(OUTPUT_A) x = 0 while x < 1000: BeaconHeading = IR.heading_and_distance(channel=1) print(BeaconHeading, file=sys.stderr) (Heading, Distance) = BeaconHeading print(Heading, file=sys.stderr) if Heading: print(Heading, file=sys.stderr) Heading = Heading * 2 meMotor.on(speed=Heading) else: meMotor.off() sleep(.01)
def master_function(): cl = ColorSensor() ts = TouchSensor() ir = InfraredSensor() sound = Sound() steer_pair = MoveSteering(OUTPUT_B, OUTPUT_C) medium_motor = MediumMotor() pixy = Sensor(address=INPUT_2) # assert pixy.connected, "Error while connecting Pixy camera to port 1" # pixy.mode = 'SIG1' # lcd = Sensor.Screen() lcd = Display() def top_left_channel_1_action(state): move() def bottom_left_channel_1_action(state): move() def top_right_channel_1_action(state): move() def bottom_right_channel_1_action(state): move() def move(): buttons = ir.buttons_pressed() # a list if len(buttons) == 1: medium_motor.off() if buttons == ['top_left']: steer_pair.on(steering=0, speed=40) elif buttons == ['bottom_left']: steer_pair.on(steering=0, speed=-40) elif buttons == ['top_right']: steer_pair.on(steering=100, speed=30) elif buttons == ['bottom_right']: steer_pair.on(steering=-100, speed=30) elif len(buttons) == 2: steer_pair.off() if buttons == ['top_left', 'top_right']: medium_motor.on(speed_pct=10) elif buttons == ['bottom_left', 'bottom_right']: medium_motor.on(speed_pct=-10) else: # len(buttons)==0 medium_motor.off() steer_pair.off() # Associate the event handlers with the functions defined above ir.on_channel1_top_left = top_left_channel_1_action ir.on_channel1_bottom_left = bottom_left_channel_1_action ir.on_channel1_top_right = top_right_channel_1_action ir.on_channel1_bottom_right = bottom_right_channel_1_action opts = '-a 200 -s 150 -p 70 -v' speech_pause = 0 while not ts.is_pressed: # rgb is a tuple containing three integers ir.process() red = cl.rgb[0] green = cl.rgb[1] blue = cl.rgb[2] intensity = cl.reflected_light_intensity print('{4} Red: {0}\tGreen: {1}\tBlue: {2}\tIntensity: {3}'.format( str(red), str(green), str(blue), str(intensity), speech_pause), file=stderr) lcd.clear() print(pixy.mode, file=stderr) if pixy.value(0) != 0: # Object with SIG1 detected x = pixy.value(1) y = pixy.value(2) w = pixy.value(3) h = pixy.value(4) dx = int(w / 2) # Half of the width of the rectangle dy = int(h / 2) # Half of the height of the rectangle xb = x + int(w / 2) # X-coordinate of bottom-right corner yb = y - int(h / 2) # Y-coordinate of the bottom-right corner lcd.draw.rectangle((xa, ya, xb, yb), fill='black') lcd.update() speech_pause += 1 if speech_pause == 200: # try replacing with ir.distance(), ir.heading() # or ir.heading_and_distance() distance = ir.heading_and_distance() if distance == None: # distance() returns None if no beacon detected str_en = 'Beacon off?' else: str_en = 'Beacon heading {0} and distance {1}'.format( distance[0], distance[1]) sound.speak(str_en, espeak_opts=opts + 'en+f5') print(str_en, file=stderr) speech_pause = 0 str_en = 'Terminating program' sound.speak(str_en, espeak_opts=opts + 'en+f5')