def close_and_check_hand(value): self.logger.debug('call close_and_check_hand') if not self.run: return utils.nearly_close_hand(self, 'RHand') signal = utils.start_monitoring_grasp_status(self.sesh, 'moving') signal.connect(grasp_status_changed)
def look_at_object(self): """Look at the object in Nao's hand. If we lose the object while we're inspecting it... - new grasp_status is 'absent' - we stop the animation and say something about losing it - restart the grasp demo Otherwise wait for the animation to finish (fut.wait) """ self.logger.debug('call look_at_object') if not self.run: return object_dropped = qi.Promise() self.thinking_sound_fut_1 = None self.thinking_sound_fut_2 = None def handle_object_dropped(object_dropped, stat): """Handle the object being dropped.""" self.logger.debug('call handle_object_dropped: {}'.format(stat)) if not self.run: return if stat == 'absent': self.logger.warning('grasped object was dropped!') object_dropped.setValue(1) utils.nearly_close_hand(self, 'RHand') signal = utils.start_monitoring_grasp_status(self.sesh, 'present') signal.connect(partial(handle_object_dropped, object_dropped)) self.anim = random.randint(1, 3) def inspect_object(object_dropped): """Observe the object with ooohs and ahhhs.""" if not self.run: return self.thinking_sound_fut_1 = utils.play_rand_thinking_sound(self, 1.5) self.thinking_sound_fut_2 = utils.play_rand_thinking_sound(self, 5) self.run_anim('ObserveObject{}'.format(self.anim)) try: object_dropped.setValue(0) except RuntimeError: self.logger.warning('"ObserveObject" animation was canceled ' + 'because object was dropped.') qi.async(inspect_object, object_dropped) if object_dropped.future().value() == 1: # dropped the object self.thinking_sound_fut_1.cancel() self.thinking_sound_fut_2.cancel() self.stop_anim('ObserveObject{}'.format(self.anim)) self.say_and_animate('ohno', 'Grasping', 'LostIt') return False else: # done looking at the object utils.stop_monitoring_grasp_status() return True
def return_object(self): """Hand the object back to the user. Extend the arm out toward the user and (A) wait for the arm to move and (B) close the fingers and detect no object. Whichever happens first stops the other task. Finally open the hand and then joke. """ self.logger.debug('call return_object') if not self.run: return self.say_and_animate('done', 'Grasping', 'ReturnObject') # offer object time.sleep(1) # try to ensure that arm monitoring doesn't false positive... object_dropped = qi.Promise() fut = object_dropped.future() # 1. Register a callback to arm movement and start detecting arm movement def handle_arm_moved(object_dropped, value): self.logger.debug('call handle_arm_moved') if not self.run: return utils.stop_monitoring_grasp_status() object_dropped.setValue(1) signal = utils.start_monitoring_arm_move(self.sesh, 'R') signal.connect(partial(handle_arm_moved, object_dropped)) # 2. asynchronously close the hand and keep checking if its empty def grasp_status_changed(object_dropped, stat): self.logger.debug('call grasp_status_changed: {}'.format(stat)) if not self.run: return utils.nearly_close_hand(self, 'RHand') if stat == 'absent': utils.stop_monitoring_arm_move() object_dropped.setValue(1) signal = utils.start_monitoring_grasp_status(self.sesh, 'present') signal.connect(partial(grasp_status_changed, object_dropped)) # proceed when one of the above two has finished try: fut.value(self.return_duration) self.run_anim('OpenHand') time.sleep(1) # for continuity self.say_and_animate('giveback', 'Grasping', 'DontForget') time.sleep(1) # for continuity except RuntimeError: self.logger.warning("give up on returning the grasped object!") self.run_anim('OpenHand') self.box.onStopped(1)