def program(robot: robot.Robot): if robot.lift_height.distance_mm > 45: with robot.perform_off_charger(): robot.set_lift_height(0.0).wait_for_completed() count = ['three', 'two', 'one'] say = ['rock', 'paper', 'scissors'] for i in range(3): robot.say_text(say[i]) robot.display_oled_face_image(pics[count[i]], 1000, True).wait_for_completed() robot.say_text('shoot') robot.display_oled_face_image(pics['scissors'], 1000, True).wait_for_completed() time.sleep(2.0) for i in range(3): throw_input = input("Enter gesture (r / p / s): ") while throw_input not in ['r', 'p', 's']: print("Invalid input! Try again!") throw_input = input("Enter gesture (r / p / s): ") if throw_input == 'r': robot.display_oled_face_image(pics['rock'], 1000, False) elif throw_input == 'p': robot.display_oled_face_image(pics['paper'], 1000, False) else: robot.display_oled_face_image(pics['scissors'], 1000, False)
def say_result(robot, result): response_list = ['Yes, I win', 'Aw, you win', 'We have tied'] response = response_list[result] robot.say_text(response, use_cozmo_voice=ROBOT_VOICE, duration_scalar=0.3, in_parallel=True).wait_for_completed()
def disp_count_down(robot, pics): print("3, 2, 1, shoot!") # TODO: show count down count = ['three', 'two', 'one'] say = ['rock', 'paper', 'scissors'] for i in range(3): robot.say_text(say[i], use_cozmo_voice = ROBOT_VOICE) robot.display_oled_face_image(pics[count[i]], 1000, True).wait_for_completed() robot.say_text('shoot', use_cozmo_voice = ROBOT_VOICE)
def run(sdk_conn): filename = 'lab2classifier.sav' robot = sdk_conn.wait_for_robot() robot.camera.image_stream_enabled = True robot.camera.color_image_enabled = False robot.camera.enable_auto_exposure() robot.set_head_angle(util.degrees(-5.0)).wait_for_completed() state = CozmoState.IDLE img_clf = ImageClassifier() # (train_raw, train_labels) = img_clf.load_data_from_folder('./train/') # (test_raw, test_labels) = img_clf.load_data_from_folder('./test/') # # # convert images into features # train_data = img_clf.extract_image_features(train_raw) # test_data = img_clf.extract_image_features(test_raw) # # # train model and test on training data # img_clf.train_classifier(train_data, train_labels) img_clf.classifer = pickle.load(open(filename, 'rb')) while (state != None): if (state == CozmoState.IDLE): next_state = classifyImage(robot, img_clf) if (next_state == 'drone'): robot.say_text(next_state).wait_for_completed() state = CozmoState.DRONE elif (next_state == 'order'): robot.say_text(next_state).wait_for_completed() state = CozmoState.ORDER elif (next_state == 'inspection'): robot.say_text(next_state).wait_for_completed() state = CozmoState.INSPECTION elif (state == CozmoState.DRONE): pickup_cube(robot) state = CozmoState.IDLE elif (state == CozmoState.ORDER): #Drive robot in a circle robot.drive_wheels(1000, 100, None, None, 10) state = CozmoState.IDLE elif (state == CozmoState.INSPECTION): # Cozmo goes in a 20cm/side square while raising and lowering lift inspection_state(robot) state = CozmoState.IDLE
def light_cubes(robot: cozmo.robot.Robot): robot.say_text("I'm just getting ready").wait_for_completed() global x global cube_picked global y x = "" y = "" robot.say_text( "Ok I'm looking for your stuff. When it lights up I've found it" ).wait_for_completed() lookaround = robot.start_behavior( cozmo.behavior.BehaviorTypes.LookAroundInPlace) cubes = robot.world.wait_until_observe_num_objects( num=3, object_type=cozmo.objects.LightCube, timeout=60) lookaround.stop() red_light = cozmo.lights.Light(cozmo.lights.Color(rgb=(255, 0, 0))) blue_light = cozmo.lights.Light(cozmo.lights.Color(rgb=(0, 0, 255))) yellow__light = cozmo.lights.Light(cozmo.lights.Color(rgb=(255, 255, 0))) # tag each cube found as a different colour red_cube = robot.world.get_light_cube(LightCube1Id) blue_cube = robot.world.get_light_cube(LightCube2Id) yellow_cube = robot.world.get_light_cube(LightCube3Id) red_cube.set_lights(red_light) blue_cube.set_lights(blue_light) yellow_cube.set_lights(yellow__light) robot.say_text( "Ok, pick one and then press confirm and I'll get it for you" ).wait_for_completed() global root_window root_window = Tk() button1 = Button(root_window, text="Phone", bg="yellow", command=yellow_clicked, height=4, width=17) button2 = Button(root_window, text="TV Remote", bg="red", command=red_clicked, height=4, width=17) button3 = Button(root_window, text="Medication", bg="light blue", command=blue_clicked, height=4, width=17) button5 = Button(root_window, text="", bg="grey", height=4, width=17) button6 = Button(root_window, text="", bg="grey", height=4, width=17) button7 = Button(root_window, text="", bg="grey", height=4, width=17) button8 = Button(root_window, text="", bg="grey", height=4, width=17) button9 = Button(root_window, text="", bg="grey", height=4, width=17) button10 = Button(root_window, text="", bg="grey", height=4, width=17) global button4 button4 = Button(root_window, text="Cozmo is waiting", command=confirm_clicked, height=4, width=20) global label1 label1 = Label(root_window, text="Nothing selected yet.", height=4, width=25) button1.grid(row=1, column=1) button2.grid(row=1, column=2) button3.grid(row=1, column=3) button5.grid(row=2, column=1) button6.grid(row=2, column=2) button7.grid(row=2, column=3) button8.grid(row=3, column=1) button9.grid(row=3, column=2) button10.grid(row=3, column=3) label1.grid(row=2, column=4) button4.grid(row=2, column=5) root_window.mainloop() while x == "": sleep(1) if x == "Blue": cube_picked = blue_cube elif x == "Red": cube_picked = red_cube elif x == "Yellow": cube_picked = yellow_cube targ = cube_picked if len(cubes) < 3: print("Error") else: robot.pickup_object(targ, num_retries=3).wait_for_completed() robot.go_to_pose(Pose(0, 0, 0, angle_z=degrees(180)), relative_to_robot=False).wait_for_completed() robot.say_text("Is this the right one?").wait_for_completed() global confirmationWindow confirmationWindow = Tk() confirmationWindow.geometry("255x70") buttonYES = Button(confirmationWindow, text="YES", bg="green", command=yes_command, height=4, width=17) buttonNO = Button(confirmationWindow, text="NO", bg="red", command=no_command, height=4, width=17) buttonNO.grid(row=2, column=1) buttonYES.grid(row=2, column=2) confirmationWindow.mainloop() while y == "": sleep(1) if y == "yes": robot.say_text("YAY", play_excited_animation=True).wait_for_completed() robot.move_lift(-3) robot.drive_straight(distance_mm(-200), speed_mmps(80)).wait_for_completed() elif y == "no": robot.drive_straight(distance_mm(-200), speed_mmps(50)).wait_for_completed() robot.turn_in_place(degrees(180)).wait_for_completed() robot.move_lift(-5) robot.go_to_pose(Pose(0, 0, 0, angle_z=degrees(180)), relative_to_robot=False).wait_for_completed() robot.say_text( "Select something else and I'll get it.").wait_for_completed() exit
def program(condition, robot): # rock = 0, paper = 1, scissor = 2 gesture_dict = {'r': 0, 'p': 1, 's': 2} # conditions: control = 0, verbal cheat = 1, action cheat = 2 # cheat rounds cheat_rounds = np.array([4, 8, 15]) # total number of rounds n_round = 20 #pictures pics = get_pictures() print("Game starts!") robot.say_text('Let\'s play rock paper scissors', use_cozmo_voice=ROBOT_VOICE, duration_scalar=0.5).wait_for_completed() time.sleep(2.0) # set seed to keep consistency for all participants np.random.seed(SEED) # sequence longer than 20 in case of extended interactions robot_throws = np.random.randint(3, size=100) cur = 0 while (cur < n_round): second_display = None is_cheat_round = (cur + 1) in cheat_rounds cur_throw = robot_throws[cur] if (condition and is_cheat_round): print("---- Round %d (cheat round) ----" % (cur + 1)) else: print("---- Round %d ----" % (cur + 1)) robot.say_text('Round %d' % (cur + 1), use_cozmo_voice=ROBOT_VOICE, duration_scalar=0.3).wait_for_completed() disp_count_down(robot, pics) first_display = disp_throw(robot, pics, cur_throw, None) human_throw_input = input("Enter human gesture (r / p / s): ") while human_throw_input not in ['r', 'p', 's']: print("Invalid input! Try again!") human_throw_input = input("Enter human gesture (r / p / s): ") LOG.append(human_throw_input) human_throw = gesture_dict[human_throw_input] # robot win = 0, human win = 1, tie = 2 result = get_result(cur_throw, human_throw) if condition > 0 and is_cheat_round: if result == 0: # robot wins fairly. all rounds pushed back n_round += 1 cheat_rounds += 1 elif condition == 1: # verbal cheat result = 0 elif condition == 2: # action cheat cur_throw = (human_throw + 1) % 3 second_display = disp_throw(robot, pics, cur_throw, first_display) result = 0 say_result(robot, result) if not first_display.is_completed: first_display.abort() if second_display and not second_display.is_completed: second_display.abort() cur += 1 time.sleep(1) print("Game ends!") robot.say_text('The game is finished', use_cozmo_voice=ROBOT_VOICE, duration_scalar=0.6).wait_for_completed()
def cozmo_program(robot: cozmo.robot.Robot): success = True #see what Cozmo sees robot.camera.image_stream_enabled = True #connect to cubes (in case Cozmo was disconnected from the cubes) robot.world.connect_to_cubes() #identify cubes cube1 = robot.world.get_light_cube(LightCube1Id) # looks like a paperclip cube2 = robot.world.get_light_cube(LightCube2Id) # looks like a lamp / heart cube3 = robot.world.get_light_cube(LightCube3Id) # looks like the letters 'ab' over 'T' if cube1 is not None: cube1.set_lights(cozmo.lights.red_light) else: cozmo.logger.warning("Cozmo is not connected to a LightCube1Id cube - check the battery.") if cube2 is not None: cube2.set_lights(cozmo.lights.green_light) else: cozmo.logger.warning("Cozmo is not connected to a LightCube2Id cube - check the battery.") if cube3 is not None: cube3.set_lights(cozmo.lights.blue_light) else: cozmo.logger.warning("Cozmo is not connected to a LightCube3Id cube - check the battery.") #have the user tap each of the cubes, in order try: robot.say_text("Tap the red cube and make me say something.").wait_for_completed() cube1.wait_for_tap(timeout=10) except asyncio.TimeoutError: robot.say_text("The red cube was not tapped").wait_for_completed() success = False finally: cube1.set_lights_off() if success: robot.say_text("I'm moving to the right.").wait_for_completed() robot.turn_in_place(cozmo.util.degrees(-90)).wait_for_completed() robot.drive_straight(cozmo.util.distance_mm(50), cozmo.util.speed_mmps(150)).wait_for_completed() else: robot.say_text("You didn't tap the cube properly.").wait_for_completed() success = True try: robot.say_text("Tap the green cube so I can take a picture.").wait_for_completed() cube2.wait_for_tap(timeout=10) except asyncio.TimeoutError: robot.say_text("The green cube was not tapped").wait_for_completed() success = False finally: cube2.set_lights_off() if (success): robot.say_text("I see that you were paying attention. I will take a picture.").wait_for_completed() else: robot.say_text("Do you know how to tap a cube? I will take a picture anyway.").wait_for_completed() success = True new_im = robot.world.wait_for(cozmo.world.EvtNewCameraImage) new_im.image.raw_image.show() #save the raw image as a bmp file img_latest = robot.world.latest_image.raw_image img_convert = img_latest.convert('L') img_convert.save("aPhoto.bmp") #save the raw image data as a png file, named imageName imageName = "myPhoto.png" img = Image.open("aPhoto.bmp") width, height = img.size new_img = img.resize( (width, height) ) new_img.save( imageName, 'png') try: robot.say_text("Tap the blue cube and make me do something.").wait_for_completed() cube3.wait_for_tap(timeout=10) except asyncio.TimeoutError: robot.say_text("The blue cube was not tapped").wait_for_completed() success = False finally: cube3.set_lights_off() cube = robot.world.wait_for_observed_light_cube() if (success): robot.say_text("Well done! I will pop a wheelie.").wait_for_completed() else: robot.say_text("Do you know how to tap a cube? I will pop a wheelie.").wait_for_completed() success = True action = robot.pop_a_wheelie(cube, num_retries=2) action.wait_for_completed() robot.say_text("Oh, Please help me.").wait_for_completed() return
def on_object_tapped(self, event, *, obj, tap_count, tap_duration, **kw): robot.say_text("The cube was tapped").wait_for_completed() return