def __init__(self): # Creates the variables held by the Snatch3r object. self.left_motor = ev3.LargeMotor(ev3.OUTPUT_B) self.right_motor = ev3.LargeMotor(ev3.OUTPUT_C) self.arm_motor = ev3.MediumMotor(ev3.OUTPUT_A) self.touch_sensor = ev3.TouchSensor() self.color_sensor = ev3.ColorSensor() self.ir_sensor = ev3.InfraredSensor() self.beacon_seeker = ev3.BeaconSeeker(channel=1) self.pixy = ev3.Sensor(driver_name="pixy-lego") self.MAX_SPEED = 900 self.running = True self.facing = 'right' self.x_dir = 400 self.y_dir = 200 self.color = 5 self.has_moved = False self.grid_size = 0 assert self.left_motor.connected assert self.right_motor.connected assert self.arm_motor.connected assert self.touch_sensor.connected assert self.color_sensor.connected assert self.ir_sensor.connected assert self.pixy.connected
def seek_beacon(self, beacon_channel=1): """ Drives to the beacon""" beacon_seeker = ev3.BeaconSeeker(channel=beacon_channel) forward_speed = 300 turn_speed = 100 distance_0_readings = 0 while not self.touch_sensor.is_pressed: time.sleep(0.1) current_distance = beacon_seeker.distance if current_distance == -128: print("IR Remote not found") # self.stop() self.drive(-turn_speed, turn_speed) continue current_heading = beacon_seeker.heading if math.fabs(current_heading) < 2: # Close enough of a heading to move forward if current_distance > 0: self.drive(forward_speed, forward_speed) elif current_distance == 0: self.stop() distance_0_readings += 1 if distance_0_readings > 5: print("I got the beacon") return True else: if math.fabs(current_heading) > 10: print("Heading is too far off to fix, just spin: ", current_heading) self.drive(-turn_speed, turn_speed) elif current_heading < 0: self.drive(-turn_speed, turn_speed) else: self.drive(turn_speed, -turn_speed)
def seek_beacon(self): beacon_seeker = ev3.BeaconSeeker(channel=1) forward_speed = 600 turn_speed = 100 while not self.touch_sensor.is_pressed: current_heading = beacon_seeker.heading # use the beacon_seeker heading current_distance = beacon_seeker.distance # use the beacon_seeker distance if current_distance == -128: # If the IR Remote is not found just sit idle for this program until it is moved. print("IR Remote not found. Distance is -128") self.stop() else: if math.fabs(current_heading) < 2: # Close enough of a heading to move forward print("On the right heading. Distance: ", current_distance) if current_distance > 0: self.drive_forward(forward_speed, forward_speed) if current_distance == 0: time.sleep(.5) self.stop() return True # You add more! if current_heading < 0: print('Go left') self.drive_left(turn_speed, turn_speed) if current_heading > 0: print('Go right') self.drive_right(turn_speed, turn_speed) if math.fabs(current_heading) > 10: self.stop() print('Heading too far off')
def seek_beacon_pokemon(self): """Creates a specialized beacon seeking function to find the beacon and heal the pokemon party.""" beacon_seeker = ev3.BeaconSeeker(channel=1) forward_speed = 500 turn_speed = 300 while True: current_heading = beacon_seeker.heading current_distance = beacon_seeker.distance if current_distance == -128: self.drive(turn_speed, -turn_speed) else: if math.fabs(current_heading) < math.fabs(2): if current_distance == 0: self.stop_bot() time.sleep(.25) self.drive_inches(2.75, forward_speed) time.sleep(.25) self.arm_up() if self.touch_sensor.is_pressed: ev3.Sound.play("/home/robot/csse120/projects/melvinjl/recovery.wav") print("Thank you! Your Pokemon are fighting fit! We hope to see you again!") self.arm_down() break else: self.drive(forward_speed, forward_speed) elif math.fabs(current_heading) < 10: print("Adjusting heading: ", current_heading) if current_heading < 0: self.drive(-turn_speed, turn_speed) else: self.drive(turn_speed, -turn_speed) else: self.drive(turn_speed, -turn_speed)
def seek_beacon(self): rc1 = ev3.RemoteControl(channel=1) assert rc1.connected beacon_seeker = ev3.BeaconSeeker() forward_speed = 300 turn_speed = 100 slow_turn_speed = 30 while not self.touch_sensor.is_pressed: current_heading = beacon_seeker.heading current_distance = beacon_seeker.distance if current_distance == -128: print("IR Remote not found. Distance is -128") self.right(slow_turn_speed, slow_turn_speed) else: if math.fabs(current_heading) < 2: print("On the right heading. Distance: ", current_distance) if current_distance > 0: self.forward(forward_speed, forward_speed) elif current_distance <= 0: return True elif 2 <= math.fabs(current_heading) < 10: if current_heading < 0: self.left(turn_speed, turn_speed) elif current_heading > 0: self.right(turn_speed, turn_speed) elif math.fabs(current_heading) > 10: self.right(slow_turn_speed, slow_turn_speed) print("Heading too far off") rc1.process() time.sleep(0.2) print("Abandon ship!") self.stop() return False
def seek_beacon(self): beacon_seeker = ev3.BeaconSeeker(channel=1) forward_speed = 300 turn_speed = 100 while not self.touch_sensor.is_pressed: # The touch sensor can be used to abort the attempt (sometimes handy during testing) # DONE: 3. Use the beacon_seeker object to get the current heading and distance. current_heading = beacon_seeker.heading # use the beacon_seeker heading current_distance = beacon_seeker.distance # use the beacon_seeker distance print("Adjusting heading: ", current_heading) if current_distance == -128: # If the IR Remote is not found just sit idle for this program until it is moved. print("IR Remote not found. Distance is -128") self.stop() else: # DONE: 4. Implement the following strategy to find the beacon. # If the absolute value of the current_heading is less than 2, you are on the right heading. # If the current_distance is 0 return from this function, you have found the beacon! return True # If the current_distance is greater than 0 drive straight forward (forward_speed, forward_speed) # If the absolute value of the current_heading is NOT less than 2 but IS less than 10, you need to spin # If the current_heading is less than 0 turn left (-turn_speed, turn_speed) # If the current_heading is greater than 0 turn right (turn_speed, -turn_speed) # If the absolute value of current_heading is greater than 10, then stop and print Heading too far off # # Using that plan you should find the beacon if the beacon is in range. If the beacon is not in range your # robot should just sit still until the beacon is placed into view. It is recommended that you always print # something each pass through the loop to help you debug what is going on. Examples: # print("On the right heading. Distance: ", current_distance) # print("Adjusting heading: ", current_heading) # print("Heading is too far off to fix: ", current_heading) # Here is some code to help get you started if math.fabs(current_heading) < 2: # Close enough of a heading to move forward print("On the right heading. Distance: ", current_distance) # You add more! if current_distance == 1: self.forward(forward_speed, forward_speed) time.sleep(.5) self.stop() return True elif current_distance > 1: self.forward(forward_speed, forward_speed) elif math.fabs(current_heading) < 10: if current_heading < 0: self.left(turn_speed, turn_speed) elif current_heading > 0: self.right(turn_speed, turn_speed) elif math.fabs(current_heading) > 10: self.stop() print("Heading too far off.", current_heading) time.sleep(0.2) # The touch_sensor was pressed to abort the attempt if this code runs. print("Abandon ship!") self.stop() return False
def __init__(self): """constructs Snatch3r object""" self.left_motor = ev3.LargeMotor(ev3.OUTPUT_C) self.right_motor = ev3.LargeMotor(ev3.OUTPUT_B) assert self.left_motor.connected assert self.right_motor.connected self.arm_motor = ev3.MediumMotor(ev3.OUTPUT_A) assert self.arm_motor.connected self.touch_sensor = ev3.TouchSensor() assert self.touch_sensor.connected self.color_sensor = ev3.ColorSensor() assert self.color_sensor.connected self.ir_sensor = ev3.InfraredSensor() assert self.ir_sensor.connected self.beacon_seeker = ev3.BeaconSeeker(channel=1) self.pixy = ev3.Sensor(driver_name="pixy-lego") self.running = True self.mqtt_client = None
def seek_beacon(self, forward_speed, turn_speed): """Drives to the beacon at a given forward speed and turn speed""" beacon_seeker = ev3.BeaconSeeker(channel=1) while not self.touch_sensor.is_pressed: current_heading = beacon_seeker.heading current_distance = beacon_seeker.distance if current_distance == -128: print("IR Remote not found. Distance is -128") self.drive_forward(turn_speed, -turn_speed) else: if math.fabs(current_heading) < 10: print("On the right heading. Distance: ", current_distance) if current_distance <= 10: self.stop_robot() return True else: self.drive_forward(forward_speed, forward_speed) elif math.fabs(current_heading) < 20: print("Adjusting heading: ", current_heading) if current_heading < 0: self.drive_forward(-turn_speed, turn_speed) else: self.drive_forward(turn_speed, -turn_speed) time.sleep(0.2)
def new_seek_beacon(self): forward_speed = 300 turn_speed = 100 BeaconSeaker = ev3.BeaconSeeker(channel=1) while True: current_heading = BeaconSeaker.heading # use the beacon_seeker heading current_distance = BeaconSeaker.distance # use the beacon_seeker distance if current_distance == -128: # If the IR Remote is not found just sit idle for this program until it is moved. print("IR Remote not found. Distance is -128") self.stop_both() else: if math.fabs(current_heading) < 2: # Close enough of a heading to move forward print("On the right heading. Distance: ", current_distance) # You add more! if current_distance == 0: time.sleep(1) self.stop_both() return True if current_distance > 0: self.drive(forward_speed, forward_speed) if math.fabs(current_heading) > 2 and math.fabs(current_heading) < 10: if current_heading < 0: self.drive(-turn_speed, turn_speed) if current_heading > 0: self.drive(turn_speed, -turn_speed) if math.fabs(current_heading) > 10: self.stop_both() print('Heading too far off') time.sleep(0.2)
def find_prize(self): beacon_seeker = ev3.BeaconSeeker(channel=1) forward_speed = 300 turn_speed = 100 while not self.touch_sensor.is_pressed: current_heading = beacon_seeker.heading # use the beacon_seeker heading current_distance = beacon_seeker.distance # use the beacon_seeker distance if current_distance == -128: print("IR Remote not found. Distance is -128") self.stop() else: if math.fabs(current_heading) < 2: print("On the right heading. Distance: ", current_distance) if current_distance > 1: self.drive(forward_speed, forward_speed) else: self.stop() return True if 2 < math.fabs(current_heading) < 10: print("Adjusting Heading: ", current_heading) if current_heading > 0: self.drive(turn_speed, -turn_speed) if current_heading < 0: self.drive(-turn_speed, turn_speed) if math.fabs(current_heading) > 10: print("Heading is too far off to fix: ", current_heading) self.stop() time.sleep(0.2) self.stop() ev3.Sound.speak("Found Prize").wait() return False
def main(): print("--------------------------------------------") print("IR Remote") print(" - Use IR remote channel 1 to drive around") print("--------------------------------------------") robot = robo.Snatch3r() color_sensor = ev3.ColorSensor() color_sensor.MODE_COL_COLOR ir_sensor = ev3.InfraredSensor() beacon_seeker = ev3.BeaconSeeker(channel=1) assert color_sensor dc = DataContainer() my_delegate = MyDelegate() rc1 = ev3.RemoteControl(channel=1) rc1.on_red_up = lambda state: my_delegate.run_left(state, 1) rc1.on_red_down = lambda state: my_delegate.run_left(state, -1) rc1.on_blue_up = lambda state: my_delegate.run_right(state, 1) rc1.on_blue_down = lambda state: my_delegate.run_right(state, -1) rc2 = ev3.RemoteControl(channel=2) rc2.on_red_up = lambda state: my_delegate.arm_up() mqtt_client = com.MqttClient(my_delegate) my_delegate.mqtt_client = mqtt_client mqtt_client.connect_to_pc() while dc.running: rc1.process() rc2.process() color = color_sensor.color if color_sensor.color == 5: my_delegate.stop() time.sleep(0.01)
def seek_beacon(self): beacon_seeker = ev3.BeaconSeeker(channel=1) forward_speed = 300 turn_speed = 100 while not self.touch_sensor.is_pressed: current_heading = beacon_seeker.heading current_distance = beacon_seeker.distance if current_distance == -128: print('distance: -128, searching') self.turn(-turn_speed, turn_speed) else: if math.fabs(current_heading) < 2: print("On the right heading. Distance: ", current_distance) if current_distance == 1: self.stop() return True else: self.forward_drive(forward_speed, forward_speed) elif 2 < math.fabs(current_heading) < 10: if current_heading < 0: self.turn(-turn_speed, turn_speed) elif current_heading > 0: self.turn(turn_speed, -turn_speed) elif math.fabs(current_heading) > 10: print("heading too great, searching") self.turn(turn_speed, -turn_speed) time.sleep(0.2)
def set_channel(self, channel): """ Makes this sensor look for signals on the given channel. The physical Beacon has a switch that can set the channel to 1, 2, 3 or 4. """ self.channel = channel self._underlying_ir_sensor = ev3.BeaconSeeker(channel=channel)
def seek_beacon(self): seeker = ev3.BeaconSeeker(sensor=self.ir_sensor, channel=1) forward_speed = 300 turn_speed = 100 while not self.touch_sensor.is_pressed: current_heading = seeker.heading current_distance = seeker.distance if current_distance == -128: # If the IR Remote is not found just sit idle for this program until it is moved. print("IR Remote not found. Distance is -128") self.stop() else: if math.fabs(current_heading) < 2: # Close enough of a heading to move forward print("On the right heading. Distance: ", current_distance) if current_distance <= 1: time.sleep(1) self.stop() return True if current_distance > 0: self.constant_moving(forward_speed, forward_speed) elif current_heading < 0: self.constant_moving(-turn_speed, turn_speed) elif current_heading > 0: self.constant_moving(turn_speed, -turn_speed) else: print("Heading too far off") time.sleep(0.2) print("Abandon ship!") self.stop() return False
def find_toy(self): turn_speed = 200 while True: self.beacon_seeker = ev3.BeaconSeeker(channel=1) current_heading = self.beacon_seeker.heading # use the beacon_seeker heading current_distance = self.beacon_seeker.distance # use the beacon_seeker distance if current_distance == -128: # If the IR Remote is not found just sit idle for this program until it is moved. print("Can't find toy") self.stop() else: if math.fabs(current_heading) < 2: print("On the right heading. Distance: ", current_distance) # You add more! self.stop() self.read_colors() print("Read the color") return if 2 < math.fabs(current_heading) < 50: print("Adjusting Heading: ", current_heading) if current_heading > 0: self.drive(turn_speed, -turn_speed) if current_heading < 0: self.drive(-turn_speed, turn_speed) if math.fabs(current_heading) > 50: print("Heading is too far off to fix: ", current_heading) self.stop() time.sleep(0.2) self.stop() return False
def seek_beacon(self): assert self.ir_sensor beacon_seeker = ev3.BeaconSeeker(channel=1) while True: current_heading = beacon_seeker.heading current_distance = beacon_seeker.distance if current_distance == -128: print("IR Remote not found. Distance is -128") self.drive_forward(-100, 100) else: if math.fabs(current_heading) < 2: print("On the right heading. Distance: ", current_distance) if current_distance <= 1: time.sleep(1) self.drive_stop() print("Beacon found!") return True self.drive_forward(300, 300) else: print("Adjusting heading: ", current_heading) if current_heading < 0: self.drive_forward(-100, 100) else: self.drive_forward(100, -100) time.sleep(0.1)
def main(): robot = robo.Snatch3r() ev3.Sound.speak("this isn't going to WORK, nope nope nooooooooooooooooooooooooooooopppppee") mqtt_client = com.MqttClient(robot) mqtt_client.connect_to_pc() robot.mqtt_create(mqtt_client) robot.running = True beacon_seeker = ev3.BeaconSeeker(channel=1) ev3.Leds.set_color(ev3.Leds.LEFT, ev3.Leds.GREEN) time.sleep(5) k = 0 while True: # ev3.Sound.speak("beep") # time.sleep(2) beacon_seeker = ev3.BeaconSeeker(channel=1) #and math.fabs(beacon_seeker.heading) <= 10 if beacon_seeker.distance > 0 and math.fabs(beacon_seeker.heading) <= 10: robot.stop() time.sleep(3) robot.stop() ev3.Leds.set_color(ev3.Leds.LEFT, ev3.Leds.GREEN) ev3.Sound.speak("mine") if robot.seek_beacon(): ev3.Sound.play("/home/robot/csse120/assets/sounds/mine2.wav") robot.arm_up() time.sleep(1.5) ev3.Sound.play("/home/robot/csse120/assets/sounds/mine2.wav") time.sleep(7) robot.arm_down() ev3.Sound.speak("I'm done, good bye") robot.shutdown() robot.stop() elif beacon_seeker.distance < 0: ev3.Leds.set_color(ev3.Leds.LEFT, ev3.Leds.BLACK) elif beacon_seeker.distance > 5: ev3.Leds.set_color(ev3.Leds.LEFT, ev3.Leds.AMBER) else: ev3.Leds.set_color(ev3.Leds.LEFT, ev3.Leds.RED) if not robot.running: break time.sleep(0.01) k = k + 1 if k > 50: ev3.Sound.speak("beep") k = 0
def halt(self): robot = robo.Snatch3r() robot.stop() beacon_1 = ev3.BeaconSeeker(channel=1) time.sleep(1) print(beacon_1.heading_and_distance) global mqtt_client mqtt_client.send_message("update_b1", [beacon_1.heading, beacon_1.distance])
def __init__(self): self.running = True self.touch_sensor = ev3.TouchSensor() self.seeker = ev3.BeaconSeeker(channel=1) self.left_motor = ev3.LargeMotor(ev3.OUTPUT_B) self.right_motor = ev3.LargeMotor(ev3.OUTPUT_C) assert self.left_motor.connected assert self.right_motor.connected self.pixy = ev3.Sensor(driver_name="pixy-lego") self.mode = "Follow"
def seek_beacon(self): beacon_seeker = ev3.BeaconSeeker( ) # Assumes remote is set to channel 1 forward_speed = 300 turn_speed = 100 found = False while not self.touch_sensor.is_pressed: # The touch sensor can be used to abort the attempt (sometimes handy during testing) # Done: 3. Use the beacon_seeker object to get the current heading and distance. current_heading = beacon_seeker.heading # use the beacon_seeker heading current_distance = beacon_seeker.distance # use the beacon_seeker distance if current_distance == -128: # If the IR Remote is not found just sit idle for this program until it is moved. print("IR Remote not found. Distance is -128") self.stop() else: # Here is some code to help get you started if math.fabs(current_heading) < 2: # Close enough of a heading to move forward print("On the right heading. Distance: ", current_distance) # You add more! if current_distance > 0: self.left_motor.run_forever(speed_sp=forward_speed) self.right_motor.run_forever(speed_sp=forward_speed) if current_distance <= 1: self.left_motor.run_forever(speed_sp=forward_speed) self.right_motor.run_forever(speed_sp=forward_speed) time.sleep(1) self.left_motor.stop() self.right_motor.stop() found = True break if math.fabs(current_heading) > 2 < 10: print("Ajusting Heading", current_heading) if current_heading < 0: self.left_motor.run_forever(speed_sp=turn_speed) self.right_motor.run_forever(speed_sp=forward_speed) if current_heading > 0: self.left_motor.run_forever(speed_sp=forward_speed) self.right_motor.run_forever(speed_sp=turn_speed) if math.fabs(current_heading) > 10: self.left_motor.stop() self.right_motor.stop() print("Heading is too far off to fix") time.sleep(0.2) # The touch_sensor was pressed to abort the attempt if this code runs. print("Abandon ship!") self.stop() return found
def __init__(self): self.left_motor = ev3.LargeMotor(ev3.OUTPUT_B) self.right_motor = ev3.LargeMotor(ev3.OUTPUT_C) self.arm_motor = ev3.MediumMotor(ev3.OUTPUT_A) self.touch_sensor = ev3.TouchSensor() self.color_sensor = ev3.ColorSensor() self.ir_sensor = ev3.InfraredSensor() self.beacon_seeker = ev3.BeaconSeeker(channel=1) self.pixy = ev3.Sensor(driver_name='pixy-lego') assert self.color_sensor assert self.ir_sensor assert self.pixy
def __init__(self): self.running = True self.left_motor = ev3.LargeMotor(ev3.OUTPUT_B) self.right_motor = ev3.LargeMotor(ev3.OUTPUT_C) self.arm_motor = ev3.MediumMotor(ev3.OUTPUT_A) self.touch_sensor = ev3.TouchSensor() self.color_sensor = ev3.ColorSensor() self.beacon = ev3.BeaconSeeker(channel=2) self.ir = ev3.InfraredSensor() self.pixy = ev3.Sensor(driver_name="pixy-lego") self.items_in_cart = [] self.max_speed = 900
def seek_beacon(self): """ Uses the IR Sensor in BeaconSeeker mode to find the beacon. If the beacon is found this return True. If the beacon is not found and the attempt is cancelled by hitting the touch sensor, return False. Type hints: :type robot: robo.Snatch3r :rtype: bool """ # Done: 2. Create a BeaconSeeker object on channel 1. beacon_seeker = ev3.BeaconSeeker(channel=1) forward_speed = 300 turn_speed = 100 while not self.touch_sensor.is_pressed: current_heading = beacon_seeker.heading current_distance = beacon_seeker.distance if current_distance == -128: # If the IR Remote is not found just sit idle for this program until it is moved. print("IR Remote not found. Distance is -128") self.drive_forever(-1 * turn_speed, turn_speed) else: if math.fabs(current_heading) < 2: # Close enough of a heading to move forward print("On the right heading. Distance: ", current_distance) if current_distance == 1: self.drive_inches(4, 300) self.stop_motors() print("You have found the beaker!") return True if current_distance > 1: self.drive_forever(forward_speed, forward_speed) if 2 < math.fabs(current_heading) < 10: print("Adjusting heading:", current_heading) if current_heading < 0: self.drive_forever(-1 * turn_speed, turn_speed) if current_heading > 0: self.drive_forever(turn_speed, -1 * turn_speed) if math.fabs(current_heading) > 10: print("Heading is too far off to fix", current_heading) self.drive_forever(-1 * turn_speed, turn_speed) # You add more! time.sleep(0.2) # The touch_sensor was pressed to abort the attempt if this code runs. print("Abandon ship!") self.stop() return False
def seek_beacon(self): my_becon_seeker = ev3.BeaconSeeker(channel=1) forward_speed = 300 turn_speed = 100 while not self.touch_sensor.is_pressed: current_heading = my_becon_seeker.heading current_distance = my_becon_seeker.distance if current_distance == -128: self.stop() print("IR Remote not found. Distance is -128. Turning in " "attempt to find the beacon!") self.right(turn_speed, turn_speed) else: if math.fabs(current_heading) < 2: print("On the right heading. Distance: ", current_distance) if current_distance <= 1: time.sleep(0.5) self.stop() self.arm_up() time.sleep(1) self.arm_down() time.sleep(1) return True elif current_distance > 5: self.forward(forward_speed, forward_speed) elif math.fabs(current_heading) < 15 and math.fabs( current_heading) > 2: print("Adjusting heading") if current_heading < 0: self.right(turn_speed, turn_speed) elif current_heading > 0: self.left(turn_speed, turn_speed) elif math.fabs(current_heading) > 15: self.stop() print("Heading too far. Turning in attempt to find " "beacon!") self.right(turn_speed, turn_speed) time.sleep(0.2) print("Abandon ship!") self.stop() return False
def __init__(self): # Connect two large motors on output ports B and C self.left_motor = ev3.LargeMotor(ev3.OUTPUT_B) self.right_motor = ev3.LargeMotor(ev3.OUTPUT_C) # Check that the motors are actually connected assert self.left_motor.connected assert self.right_motor.connected # Connect and assert connection of medium motor to output port A self.arm_motor = ev3.MediumMotor(ev3.OUTPUT_A) assert self.arm_motor.connected # Define and check sensors are connected self.touch_sensor = ev3.TouchSensor() assert self.touch_sensor self.color_sensor = ev3.ColorSensor() assert self.color_sensor self.ir_sensor = ev3.InfraredSensor() assert self.ir_sensor self.remote_control = ev3.RemoteControl() # don't assert remote_control since it is external from the robot self.beacon_seeker = ev3.BeaconSeeker(channel=1) assert self.beacon_seeker self.beacon_seeker_2 = ev3.BeaconSeeker(channel=1) assert self.beacon_seeker_2 self.pixy = ev3.Sensor(driver_name="pixy-lego") assert self.pixy # Definition for Brickman buttons self.btn = ev3.Button() # Define recurring variables self.current_color = 0 self.reflected_light_intensity = 0 self.running = True
def seek_beacon(self): beacon_seeker = ev3.BeaconSeeker(channel=1) forward_speed = 300 turn_speed = 100 while not self.touch_sensor.is_pressed: current_distance = beacon_seeker.distance current_heading = beacon_seeker.heading if current_distance == -128: print("IR Remote not found. Distance is -128") self.turn_left(100) else: if math.fabs(current_heading) <= 2: print("current heading", current_heading) print("On the right heading. Distance: ", current_distance) self.motor_run(forward_speed, forward_speed) while True: current_distance = beacon_seeker.distance if current_distance <= 1: self.stop_motors() print('beacon') return True time.sleep(.01) elif 2 < math.fabs(current_heading) <= 20: print("current heading: ", current_heading) print("sweet the heading is right. gonna spin now") print("distance: ", current_distance) if current_heading <= 0: self.turn_right(turn_speed) else: self.turn_left(turn_speed) elif current_heading > 10: self.turn_left(100) print("Heading too far off") print("current heading:", current_heading) elif current_heading < -10: self.turn_right(100) print("Heading too far off") print("current heading:", current_heading) time.sleep(0.02)
def seek_beacon(self): """ Uses the IR Sensor in BeaconSeeker mode to find the beacon. If the beacon is found this return True. If the beacon is not found and the attempt is cancelled by hitting the touch sensor, return False. """ forward_speed = 300 turn_speed = 100 # To find the IR beacon (with the remote in beacon mode) beacon_seeker = ev3.BeaconSeeker() # Assumes remote is set to channel 1 print("Heading", beacon_seeker.heading) print("Distance", beacon_seeker.distance) while not self.touch_sensor.is_pressed: current_heading = beacon_seeker.heading # use the beacon_seeker # heading current_distance = beacon_seeker.distance # use the beacon_seeker distance if current_distance == -128: # If the IR Remote is not found just sit idle for this program until it is moved. print("IR Remote not found. Distance is -128") self.stop() else: if math.fabs(current_heading) < 2: # Close enough of a heading to move forward print("On the right heading. Distance: ", current_distance) if current_distance <= 1: self.stop() print(forward_speed) self.drive_inches(3, forward_speed) print('great') return True else: self.forward(forward_speed, forward_speed) elif math.fabs(current_heading) < 10: if current_heading < 0: self.forward(-turn_speed, turn_speed) elif current_heading > 0: self.forward(turn_speed, -turn_speed) else: print("Heading is too far off to fix: ", current_heading) time.sleep(0.02) # The touch_sensor was pressed to abort the attempt if this code runs. print("Abandon ship!") self.stop() return False
def seek_beacon(self): """ Uses the IR Sensor in BeaconSeeker mode to find the beacon. If the beacon is found this return True. If the beacon is not found and the attempt is cancelled by hitting the touch sensor, return False. """ ir_sensor = ev3.InfraredSensor() find_beacon = ev3.BeaconSeeker(ir_sensor, channel=1) while not self.touch_sensor.is_pressed: # The touch sensor can be used to abort the attempt (sometimes handy during testing) current_heading = find_beacon.heading # use the beacon_seeker heading current_distance = find_beacon.distance # use the beacon_seeker distance if current_distance == -128: # If the IR Remote is not found just sit idle for this program until it is moved. print("IR Remote not found. Distance is -128") self.shutdown() else: if math.fabs(current_heading) < 7 and math.fabs( current_heading) > 5: ev3.Sound.speak('cold') print("On the right heading. Distance: ", current_distance) if math.fabs(current_distance) < 4: self.drive_inches(4, 100) ev3.Sound.speak('warm') self.arm_up() ev3.Sound.beep() break else: self.forward(100, 100) elif math.fabs(current_heading) <= 20: if current_heading < 0: self.forward(-100, 100) else: self.forward(100, -100) print('turning to look for remote') ev3.Sound.speak('warmer') else: print(current_heading, ' , heading is too far off') ev3.Sound.speak("too cold") time.sleep(0.2) # The touch_sensor was pressed to abort the attempt if this code runs. print("Abandon ship!") self.shutdown() return False
def ir_sensor_proximity(): """ Example of using the IR sensor as a distance measuring device or as an IR-SEEK sensor. """ ir_sensor = ev3.InfraredSensor() # Potential values of the ir_sensor.proximity property # 0 (Object is 0% away) something is super close (sensor does not work well this close) # 1 to 99 (Object is 1% to 99% away) real readings will be somewhere in this range # 100 (Object is 100% away) nothing in front of the sensor print(ir_sensor.proximity) # To measure distance to a wall # To find the IR beacon (with the remote in beacon mode) beacon_seeker = ev3.BeaconSeeker() # Assumes remote is set to channel 1 print("Heading", beacon_seeker.heading) print("Distance", beacon_seeker.distance)
def seek_beacon(self): """ Uses the IR Sensor in BeaconSeeker mode to find the beacon. If the beacon is found this return True. If the beacon is not found and the attempt is cancelled by hitting the touch sensor, return False. """ beacon_sensor = ev3.BeaconSeeker(channel=1) forward_speed = 300 turn_speed = 100 while not self.touch_sensor.is_pressed: current_heading = beacon_sensor.heading current_distance = beacon_sensor.distance if current_distance == -128: print("IR Remote not found. Distance is -128") self.stop() else: if math.fabs(current_heading) < 2: print("On the right heading. Distance: ", current_distance) if current_distance == 0: self.right_forward(True, forward_speed) self.left_forward(True, forward_speed) time.sleep(0.4) self.stop() return True elif current_distance > 0: print("Beacon is in front of the robot.") self.right_forward(True, forward_speed) self.left_forward(True, forward_speed) elif math.fabs(current_heading) > 2 and math.fabs( current_heading) < 10: if current_heading < 0: print("Beacon is on the left.") self.right_forward(True, turn_speed) self.left_backward(True, turn_speed) else: print("Beacon is on the right.") self.right_backward(True, turn_speed) self.left_forward(True, turn_speed) elif math.fabs(current_heading) > 10: self.stop() print('Heading too far off.') time.sleep(0.01) print("Abandon ship!") self.stop() return False