def initial_settings(robot: cozmo.robot.Robot): # Set Neutral Face fot Cozmo robot.play_anim_trigger(cozmo.anim.Triggers.NeutralFace, in_parallel=True).wait_for_completed() # check if Cozmo is on charger charger = robot.is_on_charger # Set Cozmo volume robot.set_robot_volume(robot_volume=0.3) # Activate face expression estimation robot.enable_facial_expression_estimation() # Activate stop on cliff robot.enable_stop_on_cliff(enable=True) # Volts on battery battery = robot.battery_voltage # If Cozmo is on charger, drive off it to start FindFaces behavior if charger is True: robot.drive_off_charger_contacts(in_parallel=True).wait_for_completed() robot.drive_straight(distance_mm(120), speed_mmps(50), in_parallel=True) # Set lift to down position5 robot.move_lift(-3) # Start "Battery check" animation robot.say_text("Checking battery level", in_parallel=True, use_cozmo_voice=Voice, duration_scalar=0.85).wait_for_completed() # Cozmo announce that he is ready to play if battery is higher then 3.6 volts - If less than 3.6 volts, Cozmo as to be placed on charger if battery > 3.69: robot.play_anim_trigger(cozmo.anim.Triggers.VC_Alrighty, in_parallel=True).wait_for_completed() robot.say_text("My battery is good! I am ready to play!", in_parallel=True, play_excited_animation=False, use_cozmo_voice=Voice, duration_scalar=0.85).wait_for_completed() robot.set_head_angle(degrees(30)).wait_for_completed() else: robot.play_anim_trigger( cozmo.anim.Triggers.SparkFailure).wait_for_completed() robot.play_anim_trigger(cozmo.anim.Triggers.NeedsSevereLowEnergyGetIn, in_parallel=True).wait_for_completed() robot.say_text( "I need to charge my battery. Please, place me on the charger.", in_parallel=True, use_cozmo_voice=Voice, duration_scalar=0.85).wait_for_completed()
def cozmo_face_response(robot: cozmo.robot.Robot): print("Running cozmo face response") try: robot.move_lift(-3) robot.set_head_angle(cozmo.robot.MAX_HEAD_ANGLE).wait_for_completed() robot.enable_facial_expression_estimation(True) face = None face = robot.world.wait_for_observed_face(timeout=30) if face and face.is_visible: robot.set_all_backpack_lights(cozmo.lights.blue_light) else: robot.set_backpack_lights_off() print(face.expression) print(face.name) print(face.face_id) print(face.expression_score) # Cozmo responds based on happy facial expression (transitioned from negative tone) # and a smiley face is displayed on his OLED screen face_response = f"I see your face with an {face.expression} expression. But I think you look happy!" robot.say_text(face_response).wait_for_completed() if face.expression == 'happy': robot.say_text(f"Yay I am so glad you have a {face.expression} face!").wait_for_completed() # time.sleep(.1) image = Image.open("cozmo_smiley_2.jpg") image = image.resize(cozmo.oled_face.dimensions(), Image.NEAREST) image = cozmo.oled_face.convert_image_to_screen_data(image) seconds = 10 for i in range(seconds): robot.display_oled_face_image(image, 1000.0) time.sleep(1.0) except asyncio.TimeoutError: print("did not find a face") pass
async def react(robot: cozmo.robot.Robot): """ Cozmo will react to the facial expressions of the first face it sees. Rules: 1. If Cozmo sees a sad face, she will try to cheer you up. 2. If Cozmo sees an angry face, she will run away. 3. If Cozmo sees a happy face, she will celebrate with you. 4. At any point, you can ask Cozmo why she did something. She will stop what she's doing, explain, and then go back to her alert "watching" state. """ robot.enable_facial_expression_estimation(enable=True) state = "finding face" face_visible = False explanation = "I'm just chilling." any_face = None def on_press(key): #print('{0} release'.format(key)) if key == Key.shift_l or key == Key.shift_r: #listen(robot) state = "explaining" while True: ########## INITIAL CHECKS ########## # check for key input # later, check for voice input (from computer microphone) listener = Listener(on_press=on_press) listener.join() if not face_visible: state = "finding face" ########## STATE MACHINE ########## print(state) if state is "init": # explanation = "I'm just getting started." robot.set_head_angle( cozmo.robot.MAX_HEAD_ANGLE).wait_for_completed() robot.move_lift(-3) if state is "finding face": # explanation = "I'm trying to find a face." any_face = None print("Looking for a face!") look_around = robot.start_behavior( cozmo.behavior.BehaviorTypes.FindFaces) try: any_face = await (robot.world.wait_for_observed_face( timeout=30)) except asyncio.TimeoutError: print("Didn't find anyone :-(") finally: # whether we find it or not, we want to stop the behavior look_around.stop() if any_face is None: print("no faces found :(") face_visible = False else: print("FOUND A FACE") face_visible = True state = "watching face" elif state is "watching face": # # explanation = "I'm just staying alert." print("face is ", any_face) robot.stop_all_motors() expression = any_face.expression print("expression is ", expression) if expression is "sad": state = "reacting to sad face" elif expression is "angry": state = "reacting to angry face" elif expression is "happy": state = "reacting to happy face" else: state = "finding face" elif state is "reacting to sad face": explanation = "You seemed sad, so I'm sad too." reaction = robot.play_anim_trigger( cozmo.anim.Triggers.CodeLabDejected) await (reaction.wait_for_completed()) print("tried to cheer you up") time.sleep(200) state = "watching face" elif state is "reacting to angry face": explanation = "You seemed angry, so I'm angry too." reaction = robot.play_anim_trigger( cozmo.anim.Triggers.CodeLabFrustrated) await (reaction.wait_for_completed()) print("ran away") time.sleep(200) state = "watching face" elif state is "reacting to happy face": explanation = "You seemed happy, so I'm celebrating!" reaction = robot.play_anim_trigger( cozmo.anim.Triggers.CodeLabPartyTime) await (reaction.wait_for_completed()) print("did a happy dance") time.sleep(200) state = "watching face" elif state is "explaining": robot.abort_all_actions() explain = cozmo.say_text(explanation) await (explain.wait_for_completed()) state = "watching face"
def follow_faces(robot: cozmo.robot.Robot): '''The core of the follow_faces program''' # Move lift down and tilt the head up robot.move_lift(-3) robot.set_head_angle(cozmo.robot.MAX_HEAD_ANGLE).wait_for_completed() robot.enable_facial_expression_estimation() face_to_follow = None checkfeelingsad = 0 checkfeelingangry = 0 checkfeelinghappy = 0 checkfeelingunknown = 0 checkfeelingneutral = 0 checkfeelingsurprised = 0 print("Press CTRL-C to quit") global sentimental while True: turn_action = None if face_to_follow: # start turning towards the face turn_action = robot.turn_towards_face(face_to_follow,in_parallel=True) # find a visible face, timeout if nothing found after a short while try: face_to_follow = robot.world.wait_for_observed_face()#timeout=30) print(face_to_follow) if face_to_follow.expression == "unknown": sentimental = "unknown" checkfeelingunknown = checkfeelingunknown + 1 if face_to_follow.expression == "happy": sentimental = "happy" checkfeelinghappy = checkfeelinghappy + 1 if face_to_follow.expression == "sad": sentimental = "sad" checkfeelingsad = checkfeelingsad + 1 if face_to_follow.expression == "surprised": sentimental = "surprised" checkfeelingsurprised = checkfeelingsurprised + 1 if face_to_follow.expression == "angry": sentimental = "angry" checkfeelingangry = checkfeelingangry + 1 if face_to_follow.expression == "neutral": sentimental = "neutral" checkfeelingneutral = checkfeelingneutral + 1 except asyncio.TimeoutError: print("Didn't find a face - exiting!") return if checkfeelingsad >= 3: print('Are you sad?') checkfeelingsad = 0 res = requests.get( 'https://icanhazdadjoke.com/', headers={"Accept": "application/json"} ) if res.status_code == requests.codes.ok: a = random.randint(1,2) if a == 1: robot.say_text("you know " + str(res.json()['joke']),in_parallel=True,duration_scalar=1.2).wait_for_completed() else: robot.say_text("Do you want some Burger?", in_parallel=True, duration_scalar=1.2).wait_for_completed() print("Cozmo says: " + str(res.json()['joke'])) else: robot.say_text(str(res.json()['oops!I ran out of jokes']),in_parallel=True).wait_for_completed() print("Cozmo says: " + 'oops!I ran out of jokes') continue elif checkfeelingangry >= 3: print('Are you ok?') checkfeelingangry = 0 robot.say_text("Are you ok? Want some lasanga?", in_parallel=True).wait_for_completed() elif checkfeelinghappy >= 3: print('You look happy which is good') checkfeelinghappy = 0 robot.say_text("You look happy which is good", in_parallel=True).wait_for_completed() elif checkfeelingneutral >= 3: print('How was your day?') functionsCozmo.motion_step(robot) functionsCozmo.motion_step(robot) checkfeelinghappy = 0 robot.say_text("How was your day?", in_parallel=True).wait_for_completed() if turn_action: # Complete the turn action if one was in progress turn_action.wait_for_completed() time.sleep(5)