Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 import threading
00036 import roslib
00037
00038 roslib.load_manifest('nao_driver')
00039
00040 import rospy
00041 import actionlib
00042
00043 from nao_driver import *
00044
00045 from nao_msgs.msg import(
00046 RunBehaviorAction,
00047 RunBehaviorResult
00048 )
00049
00050 from nao_msgs.srv import (
00051 GetInstalledBehaviors,
00052 GetInstalledBehaviorsResponse,
00053 )
00054
00055 class NaoBehaviors(NaoNode):
00056
00057 NODE_NAME = "nao_behaviors"
00058
00059 def __init__( self ):
00060
00061
00062 NaoNode.__init__( self )
00063 rospy.init_node( self.NODE_NAME )
00064
00065
00066 self.behavior = None
00067 self.lock = threading.RLock()
00068
00069
00070 self.behaviorProxy = self.getProxy( "ALBehaviorManager" )
00071
00072
00073 self.getInstalledBehaviorsService = rospy.Service(
00074 "get_installed_behaviors",
00075 GetInstalledBehaviors,
00076 self.getInstalledBehaviors
00077 )
00078
00079
00080 self.actionlibServer = actionlib.SimpleActionServer(
00081 "run_behavior",
00082 RunBehaviorAction,
00083 self.runBehavior,
00084 False
00085 )
00086
00087 self.actionlibServer.register_preempt_callback( self.stopBehavior )
00088
00089 self.actionlibServer.start()
00090
00091 def getInstalledBehaviors( self, request ):
00092 result = self.behaviorProxy.getInstalledBehaviors()
00093 return GetInstalledBehaviorsResponse( result )
00094
00095
00096 def runBehavior( self, request ):
00097
00098 rospy.logdebug(
00099 "Execution of behavior: '{}' requested".format(request.behavior))
00100
00101
00102 if not request.behavior in self.behaviorProxy.getInstalledBehaviors():
00103 error_msg = "Behavior '{}' not installed".format(request.behavior)
00104 self.actionlibServer.set_aborted(text = error_msg)
00105 rospy.logdebug(error_msg)
00106 return
00107
00108 with self.lock:
00109
00110 if self.actionlibServer.is_preempt_requested():
00111 self.actionlibServer.set_preempted()
00112 rospy.logdebug("Behavior execution preempted before it started")
00113 return
00114
00115
00116 self.behavior = request.behavior
00117
00118 taskID = self.behaviorProxy.post.runBehavior( self.behavior )
00119
00120
00121 rospy.logdebug("Waiting for behavior execution to complete")
00122 self.behaviorProxy.wait( taskID, 0 )
00123
00124
00125 with self.lock:
00126 self.behavior = None
00127
00128 if self.actionlibServer.is_preempt_requested() :
00129 self.actionlibServer.set_preempted()
00130 rospy.logdebug("Behavior execution preempted")
00131
00132 else:
00133 self.actionlibServer.set_succeeded()
00134 rospy.logdebug("Behavior execution succeeded")
00135
00136 def stopBehavior( self ):
00137 with self.lock:
00138 if self.behavior and self.actionlibServer.is_active() :
00139 self.behaviorProxy.stopBehavior( self.behavior )
00140
00141
00142 if __name__ == '__main__':
00143 node = NaoBehaviors()
00144 rospy.loginfo( node.NODE_NAME + " running..." )
00145 rospy.spin()
00146 rospy.loginfo( node.NODE_NAME + " stopped." )
00147 exit(0)