def turn(n, x): robot = rb.Snatch3rRobot() robot.go(x, None) start = time.time() while True: now = time.time() if now - start == n: robot.stop(rb.StopAction.BRAKE.value) break
def follow_black_circle(): """ Runs YOUR specific part of the project """ robot = rb.Snatch3rRobot() drive = robot.drive_system color = robot.color_sensor turn_right = True while True: # Moving forward until light is reflected drive.start_moving() color.wait_until_intensity_is_greater_than(10) # Try turning clockwise (to the right) counter = 0 while True: if turn_right is True: drive.start_moving(100, 0) else: drive.start_moving(0, 100) # Runs if robot is back on black within 0.25 seconds (implying that robot is turning in right direction) if color.get_reflected_intensity() < 10: break # Runs if robot has not encountered black after 0.25 seconds in direction it's turning if counter > 0.5 and color.get_reflected_intensity() > 10: print('No black detected, turn around!') # Revert to original position if turn_right is True: drive.move_for_seconds(1, -100, 0) else: drive.move_for_seconds(1, 0, -100) # Changes direction to turn in the future if turn_right is True: turn_right = False else: turn_right = True print('Turn direction changed! Now turn_right is :', turn_right) # Turn for certain amount of time while True: if turn_right is True: drive.start_moving(100, 0) else: drive.start_moving(0, 100) if color.get_reflected_intensity() < 10: break counter = counter + 0.05 print(counter, color.get_reflected_intensity()) time.sleep(0.05)
def turn(n,x): robot = rb.Snatch3rRobot() time_start = time.time() deltatime = n while True: robot.left_wheel.start_spinning(x) robot.right_wheel.start_spinning(0) if time.time() >= time_start() + deltatime: break robot.left_wheel.stop_spinning() robot.right_wheel.stop_spinning()
def run_test_go_stop(): """ Tests the go and stop Snatch3rRobot methods. """ robot = rb.Snatch3rRobot() robot.forward(20) robot.stop() robot.turn(90, 'right') robot.stop() robot.spin('right') time.sleep(2) robot.stop()
def spin_seconds(n, x): """ Causes the robot to spin for N seconds at duty cycle x. """ robot = rb.Snatch3rRobot() robot.go(x, -x) start = time.time() while True: current = time.time() if current - start >= n: robot.stop(rb.StopAction.BRAKE.value) break
def run_test_touch_sensor(): """ Tests the touch_sensor of the Snatch3rRobot. """ robot = rb.Snatch3rRobot() print() print("Testing the touch_sensor of the robot.") print("Repeatedly press and release the touch sensor.") print("Press Control-C when you are ready to stop testing.") time.sleep(1) count = 1 while True: print("{:4}.".format(count), "Touch sensor value is: ", robot.touch_sensor.get_value()) time.sleep(0.5) count = count + 1
def run_test_forward(): """ Tests the forward Snatch3rRobot method. """ robot = rb.Snatch3rRobot() robot.forward(10, 25) print(robot.right_wheel.get_degrees_spun()) print(robot.left_wheel.get_degrees_spun()) robot.left_wheel.reset_degrees_spun(0) time.sleep(2) robot.forward(5, 100) print(robot.right_wheel.get_degrees_spun()) print(robot.left_wheel.get_degrees_spun())
def run_test_spin(): robot = rb.Snatch3rRobot() robot.spin(3, 'left') print(robot.right_wheel.get_degrees_spun()) print(robot.left_wheel.get_degrees_spun()) robot.left_wheel.reset_degrees_spun(0) robot.spin() print(robot.right_wheel.get_degrees_spun()) print(robot.left_wheel.get_degrees_spun()) robot.left_wheel.reset_degrees_spun(0) robot.spin(1, 'blah') print(robot.right_wheel.get_degrees_spun()) print(robot.left_wheel.get_degrees_spun())
def run_test_go_stop(): """ Tests the go and stop Snatch3rRobot methods. """ robot = rb.Snatch3rRobot() robot.go(50, 25) time.sleep(2) robot.stop() print(robot.right_wheel.get_degrees_spun()) print(robot.left_wheel.get_degrees_spun()) robot.left_wheel.reset_degrees_spun(0) time.sleep(2) robot.go(100, 100) time.sleep(3) robot.stop(rb.StopAction.COAST.value)
def advanced_line_following(): robot = rb.Snatch3rRobot() drivesystem = robot.drive_system colorsensor = robot.color_sensor drivesystem.start_moving(50,50) while True: if colorsensor.get_reflected_intensity() > 10: drivesystem.stop_moving() direction = test_water(robot,drivesystem,colorsensor) if direction == 0: print("There is no more line") drivesystem.stop_moving() else: drivesystem.spin_in_place_degrees(direction) drivesystem.start_moving(50,50)
def run_test_color_sensor(): """ Tests the color_sensor of the Snatch3rRobot. """ robot = rb.Snatch3rRobot() print() print("Testing the color_sensor of the robot.") print("Repeatedly move the robot to different surfaces.") print("Press Control-C when you are ready to stop testing.") time.sleep(1) count = 1 while True: print( "{:4}.".format(count), "Color sensor value/color/intensity is: ", "{:3} {:3} {:3}".format(robot.color_sensor.get_value()[0], robot.color_sensor.get_value()[1], robot.color_sensor.get_value()[2]), "{:4}".format(robot.color_sensor.get_color()), "{:4}".format(robot.color_sensor.get_reflected_intensity())) time.sleep(0.5) count = count + 1
def run_test_go_stop(n): """ Tests the go and stop Snatch3rRobot methods. """ robot = rb.Snatch3rRobot() while True: robot.go(50, -50) if time.time() == time.time() + n: break print(robot.right_wheel.get_degrees_spun()) print(robot.left_wheel.get_degrees_spun()) robot.left_wheel.reset_degrees_spun(0) time.sleep(2) robot.go(100, 100) time.sleep(3) robot.stop(rb.StopAction.COAST.value) print(robot.right_wheel.get_degrees_spun()) print(robot.left_wheel.get_degrees_spun())
def run_test_drive_system(): """ Tests the drive_system of the Snatch3rRobot. """ robot = rb.Snatch3rRobot() print() print("Testing the drive_system of the robot.") print("Move at (20, 50) - that is, veer left slowly") robot.drive_system.start_moving(20, 50) time.sleep(2) robot.drive_system.stop_moving() print("Left/right wheel positions:", robot.drive_system.left_wheel.get_degrees_spun(), robot.drive_system.right_wheel.get_degrees_spun()) time.sleep(1) print() print("Spin clockwise at half speed for 2.5 seconds") robot.drive_system.move_for_seconds(2.5, 50, -50) print("Left/right wheel positions:", robot.drive_system.left_wheel.get_degrees_spun(), robot.drive_system.right_wheel.get_degrees_spun()) robot.drive_system.left_wheel.reset_degrees_spun() robot.drive_system.right_wheel.reset_degrees_spun(2000) time.sleep(1) print() # print("Move forward at full speed for 1.5 seconds, coast to stop") # robot.drive_system.start_moving() # time.sleep(1.5) # robot.drive_system.stop_moving(rb.StopAction.COAST) print("Left/right wheel positions:", robot.drive_system.left_wheel.get_degrees_spun(), robot.drive_system.right_wheel.get_degrees_spun()) robot.drive_system.go_straight_inches(10)
def go_forward(left_wheel_duty_cycle_percent, right_wheel_duty_cycle_percent): robot = rb.Snatch3rRobot() dl = left_wheel_duty_cycle_percent d2 = right_wheel_duty_cycle_percent robot.go(dl, d2)
def run_test_forward(): robot = rb.Snatch3rRobot() robot.forward(3)
def run_tests(): """ Runs various tests. """ run_test_go_stop() def run_test_go_stop(): """ Tests the go and stop Snatch3rRobot methods. """ robot = rb.Snatch3rRobot() robot.go(50, 25) time.sleep(2) robot.stop() print(robot.right_wheel.get_degrees_spun()) print(robot.left_wheel.get_degrees_spun()) robot.left_wheel.reset_degrees_spun(0) time.sleep(2) robot.go(100, 100) time.sleep(3) robot.stop(rb.StopAction.COAST.value) print(robot.right_wheel.get_degrees_spun()) print(robot.left_wheel.get_degrees_spun()) #main() robot = rb.Snatch3rRobot() robot.turn(100, 120, 'right')
def turn_test(): robot = rb.Snatch3rRobot() turn(robot,20,20)
def turning(x, N): robot = rb.Snatch3rRobot() robot.left_wheel.start_spinning(x) robot.right_wheel.start_spinning(-x) time.sleep(N) robot.stop(rb.StopAction.COAST.value)
def run_test_turn(): robot = rb.Snatch3rRobot() seconds = 5 duty_cycle = 50 robot.turn(seconds, duty_cycle)
def spin_test(): robot = rb.Snatch3rRobot() spin(robot, 5,100)
def run_test_turn_for_N_seconds(): robot = rb.Snatch3rRobot() robot.right_wheel.turn("clockwise", 10, 5) print(robot.right_wheel.get_degrees_spun()) print(robot.left_wheel.get_degrees_spun())
def forward_test(): robot = rb.Snatch3rRobot() robot.forward_for_n_seconds(5, 25) robot.stop()
def run_test_forward_for(): robot = rb.Snatch3rRobot() robot.forward_for(6, 75)
def test(): testrobot = rb.Snatch3rRobot() testrobot.go_forward()
def test_forward(seconds): robot = rb.Snatch3rRobot() robot.move_forward(seconds)
def run_test_spin(): rob = rb.Snatch3rRobot() rob.forward(4) rob.turn(69)
def run_test_spinning(): robot = rb.Snatch3rRobot() robot.spinning(100, -100) robot.left_wheel.motor.duty_cycle