def runIrController(): rc = InfraredSensor(INPUT_1) leftMotor = LargeMotor(OUTPUT_D) rightMotor = LargeMotor(OUTPUT_A) rc.on_channel1_top_left = steer(leftMotor, 1) rc.on_channel1_top_right = steer(rightMotor, 1) rc.on_channel1_bottom_left = steer(leftMotor, -1) rc.on_channel1_bottom_right = steer(rightMotor, -1) while True: rc.process() sleep(0.01)
def remote_control_two_buttons(): steer_pair = MoveSteering(OUTPUT_B, OUTPUT_C) medium_motor = MediumMotor() ir = InfraredSensor() # Set the remote to channel 1 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 return ir
def remote_control_one_button(): steer_pair = MoveSteering(OUTPUT_B, OUTPUT_C) ir = InfraredSensor() # Set the remote to channel 1 def top_left_channel_1_action(state): if state: # state is True (pressed) or False steer_pair.on(steering=0, speed=40) else: steer_pair.off() def bottom_left_channel_1_action(state): if state: steer_pair.on(steering=0, speed=-40) else: steer_pair.off() def top_right_channel_1_action(state): if state: steer_pair.on(steering=100, speed=30) else: steer_pair.off() def bottom_right_channel_1_action(state): if state: steer_pair.on(steering=-100, speed=30) else: 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 while True: ir.process() sleep(0.01)
def bottom_right_channel_2_action(state): if state: medium_motor.on(speed=-5) else: medium_motor.off() def beacon_channel_1_action(state): if state: sys.exit() else: cont = False # Associate the event handlers with the functions defined above infra.on_channel1_top_left = top_left_channel_1_action infra.on_channel1_bottom_left = bottom_left_channel_1_action infra.on_channel1_top_right = top_right_channel_1_action infra.on_channel1_bottom_right = bottom_right_channel_1_action infra.on_channel2_top_left = top_left_channel_1_action infra.on_channel2_bottom_left = bottom_left_channel_1_action infra.on_channel2_top_right = top_right_channel_2_action infra.on_channel2_bottom_right = bottom_right_channel_2_action infra.on_channel1_beacon = beacon_channel_1_action while cont: infra.process() sleep(0.01)
motor.run_forever(speed_sp=600*direction) leds.set_color(led_group, 'GREEN') else: motor.stop(stop_action='brake') leds.all_off() return on_press def steer(motor, direction): def on_press(state): if state: motor.run_to_pos(direction) else: motor.set_pos(0) return on_press rc.on_channel1_top_left = roll(mC, 'LEFT', 1) rc.on_channel1_top_left = roll(mD, 'RIGHT', 1) rc.on_channel1_bottom_left = roll(mC, 'LEFT', -1) rc.on_channel1_bottom_left = roll(mD, 'RIGHT', -1) rc.on_channel1_top_right = steer(mA, 1) rc.on_channel1_bottom_right = steer(mA, -1) print("Robot Starting") run = True while run: rc.process() sleep(0.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')
def knife_swipe(state): if state: mm.on(-90) else: mm.off() manual_controls = True if manual_controls: irs.on_channel1_top_left = straight_forward irs.on_channel1_top_right = turn_right irs.on_channel1_bottom_left = reverse irs.on_channel1_beacon = knife_swipe irs.on_channel1_bottom_right = turn_left # lets keep this on top of the file for easy editing def main(): '''The main function of our program''' setup_brick_console() battery_check() while True: irs.process() time.sleep(0.01) # print something to the output panel in VS Code # debug_print('Hello VS Code!')
# Roll when button is pressed motor.run_forever(speed_sp=600 * direction) leds.set_color(led_group, 'GREEN') else: # Stop otherwise motor.stop(stop_action='brake') leds.all_off() return on_press # Assign event handler to each of the remote buttons rc.on_channel1_top_left = roll(lmotor, 'LEFT', 1) rc.on_channel1_bottom_left = roll(lmotor, 'LEFT', -1) rc.on_channel1_top_right = roll(rmotor, 'RIGHT', 1) rc.on_channel1_bottom_right = roll(rmotor, 'RIGHT', -1) print("Robot Starting") # Enter event processing loop while not button.any(): rc.process() # Backup when bumped an obstacle if ts.is_pressed: spkr.speak('Oops, excuse me!') for motor in (lmotor, rmotor): motor.stop(stop_action='brake') # Turn red lights on for light in ('LEFT', 'RIGHT'):