human_assisted_object_detection.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import roslib; roslib.load_manifest('iri_human_assisted_object_detection')
00004 import rospy
00005 import smach
00006 import smach_ros
00007 
00008 from iri_human_assisted_object_detection.srv import haod_init,haod_detect,haod_learn
00009 from sensor_msgs.msg import Joy
00010 from wiimote import *
00011 from smach_ros import ServiceState
00012 import time
00013 import threading
00014 
00015 def init_cb(userdata, response):
00016     if response.success == True:
00017       return 'succeeded'
00018     else:
00019       time.sleep(1);
00020       return 'aborted'
00021 
00022 def assistance_needed_cb(userdata,response):
00023     if response.success == False:
00024       return 'succeeded'
00025     else:
00026       return 'aborted'
00027 
00028 @smach.cb_interface(input_keys=['valid_det'])
00029 def learn_cb(userdata,request):
00030     request.valid = userdata.valid_det
00031 
00032 class WaitUserInput(smach.State):
00033     def __init__(self):
00034         smach.State.__init__(self, outcomes=['valid_detection', 'invalid_detection', 'waiting', 'no_user_input'],output_keys=['valid_det'])
00035 
00036         self.mutex = threading.Lock()
00037         self.user_input = False
00038         self.user_answer = -1
00039         self.waiting_user_input = False
00040         self.count = 0
00041 
00042         self.subscriber = rospy.Subscriber('joy', Joy, self.callback)
00043 
00044     def callback(self, data):
00045         self.mutex.acquire()
00046         if self.waiting_user_input == True:
00047           if data.buttons[0] == 1:# the 1 button is pressed
00048             self.user_input = True 
00049             self.user_answer = 1
00050           else:
00051             if data.buttons[1] == 1: #the 2 button is pressed
00052               self.user_input = True 
00053               self.user_answer = 0
00054         self.mutex.release()
00055 
00056     def execute(self,userdata):
00057         #wait for a maximum of 5 seconds 
00058         self.mutex.acquire()
00059         self.waiting_user_input = True
00060         if self.user_input == True:
00061             if self.user_answer == 1:
00062                 self.waiting_user_input = False 
00063                 userdata.valid_det = True
00064                 self.user_input = False;
00065                 self.user_answer = -1;
00066                 self.count = 0
00067                 self.mutex.release()
00068                 return 'valid_detection'
00069             else:
00070                 self.waiting_user_input = False 
00071                 userdata.valid_det = False
00072                 self.user_input = False;
00073                 self.user_answer = -1;
00074                 self.count = 0
00075                 self.mutex.release()
00076                 return 'invalid_detection'
00077         self.count = self.count + 1
00078         self.mutex.release()
00079     
00080         time.sleep(.1)
00081         if self.count == 50:
00082             self.waiting_user_input = False 
00083             self.user_input = False;
00084             self.user_answer = -1;
00085             self.count = 0
00086             return 'no_user_input'
00087         else:
00088             return 'waiting'
00089 
00090 # main
00091 def main():
00092     rospy.init_node('human_assisted_object_detection')
00093 
00094     # Create a SMACH state machine
00095     sm = smach.StateMachine(['succeeded','aborted','preempted'])
00096     sm.userdata.valid = 0 
00097     # Define userdata
00098     #sm.userdata.sm_frame_id   = "/tibi/base_link"
00099     #sm.userdata.sm_goal_x     = 2.0
00100     #sm.userdata.sm_goal_y     = 0.0
00101     #sm.userdata.sm_goal_theta = 0.0
00102 
00103     # Add states to the State Machine
00104     with sm:
00105         smach.StateMachine.add('HAOD_INIT',
00106                                smach_ros.ServiceState('init',
00107                                                       haod_init,
00108                                                       response_cb=init_cb),
00109                                transitions={'succeeded':'HAOD_DETECT',
00110                                             'aborted':'HAOD_INIT'})
00111                                
00112         smach.StateMachine.add('HAOD_DETECT',
00113                                smach_ros.ServiceState('detect',
00114                                                       haod_detect,
00115                                                       response_cb=assistance_needed_cb),
00116                                transitions={'succeeded':'HAOD_DETECT',
00117                                             'aborted':'HAOD_USER_INPUT'})
00118 
00119         smach.StateMachine.add('HAOD_USER_INPUT',WaitUserInput(),
00120                                transitions={'valid_detection':'HAOD_LEARN',
00121                                             'invalid_detection':'HAOD_LEARN',
00122                                             'waiting':'HAOD_USER_INPUT',
00123                                             'no_user_input':'HAOD_DETECT'},
00124                                remapping={'valid_det':'valid'})
00125 
00126         smach.StateMachine.add('HAOD_LEARN',
00127                                smach_ros.ServiceState('learn',
00128                                                       haod_learn,
00129                                                       request_cb=learn_cb),
00130                                transitions={'succeeded':'HAOD_DETECT',
00131                                             'aborted':'aborted'},
00132                                remapping={'valid_det':'valid'})
00133     # Create and start the introspection server
00134     sis = smach_ros.IntrospectionServer('server_name', sm, '/SM_ROOT')
00135     sis.start()
00136 
00137     # Execute the state machine
00138     outcome = sm.execute()
00139 
00140     # Wait for ctrl-c to stop the application
00141     rospy.spin()
00142     sis.stop()
00143 
00144 
00145 if __name__ == '__main__':
00146     main()
00147 


iri_human_assisted_object_detection
Author(s): Sergi Hernandez Juan
autogenerated on Fri Dec 6 2013 20:01:33