nao_behaviors.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 #
4 # ROS node to control NAO's built-in and user-installed behaviors using NaoQI
5 # Tested with NaoQI: 1.12
6 #
7 # Copyright (c) 2012, 2013 Miguel Sarabia
8 # Imperial College London
9 #
10 # Redistribution and use in source and binary forms, with or without
11 # modification, are permitted provided that the following conditions are met:
12 #
13 # # Redistributions of source code must retain the above copyright
14 # notice, this list of conditions and the following disclaimer.
15 # # Redistributions in binary form must reproduce the above copyright
16 # notice, this list of conditions and the following disclaimer in the
17 # documentation and/or other materials provided with the distribution.
18 # # Neither the name of the Imperial College London nor the names of its
19 # contributors may be used to endorse or promote products derived from
20 # this software without specific prior written permission.
21 #
22 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 # POSSIBILITY OF SUCH DAMAGE.
33 #
34 
35 import threading
36 import rospy
37 import actionlib
38 
39 from naoqi_driver.naoqi_node import NaoqiNode
40 
41 from naoqi_bridge_msgs.msg import RunBehaviorAction
42 
43 from naoqi_bridge_msgs.srv import (
44  GetInstalledBehaviors,
45  GetInstalledBehaviorsResponse,
46  )
47 
49  #This should be treated as a constant
50  NODE_NAME = "nao_behaviors"
51 
52  def __init__( self ):
53 
54  #Initialisation
55  NaoqiNode.__init__( self, self.NODE_NAME )
56 
57  #We need this variable to be able to call stop behavior when preempted
58  self.behavior = None
59  self.lock = threading.RLock()
60 
61  #Proxy for listingBehaviors and stopping them
62  self.behaviorProxy = self.get_proxy( "ALBehaviorManager" )
63 
64  # Register ROS services
65  self.getInstalledBehaviorsService = rospy.Service(
66  "get_installed_behaviors",
67  GetInstalledBehaviors,
69  )
70 
71  #Prepare and start actionlib server
73  "run_behavior",
74  RunBehaviorAction,
75  self.runBehavior,
76  False
77  )
78 
79  self.actionlibServer.register_preempt_callback( self.stopBehavior )
80 
81  self.actionlibServer.start()
82 
83  def getInstalledBehaviors( self, request ):
84  result = self.behaviorProxy.getInstalledBehaviors()
85  return GetInstalledBehaviorsResponse( result )
86 
87 
88  def runBehavior( self, request ):
89  #Note this function is executed from a different thread
90  rospy.logdebug(
91  "Execution of behavior: '{}' requested".format(request.behavior))
92 
93  #Check requested behavior is installed
94  if not request.behavior in self.behaviorProxy.getInstalledBehaviors():
95  error_msg = "Behavior '{}' not installed".format(request.behavior)
96  self.actionlibServer.set_aborted(text = error_msg)
97  rospy.logdebug(error_msg)
98  return
99 
100  with self.lock:
101  # Check first if we're already preempted, and return if so
102  if self.actionlibServer.is_preempt_requested():
103  self.actionlibServer.set_preempted()
104  rospy.logdebug("Behavior execution preempted before it started")
105  return
106 
107  #Save name of behavior to be run
108  self.behavior = request.behavior
109  #Execute behavior (on another thread so we can release lock)
110  taskID = self.behaviorProxy.post.runBehavior( self.behavior )
111 
112  # Wait for task to complete (or be preempted)
113  rospy.logdebug("Waiting for behavior execution to complete")
114  self.behaviorProxy.wait( taskID, 0 )
115 
116  #Evaluate results
117  with self.lock:
118  self.behavior = None
119  # If preempted, report so
120  if self.actionlibServer.is_preempt_requested() :
121  self.actionlibServer.set_preempted()
122  rospy.logdebug("Behavior execution preempted")
123  # Otherwise, set as succeeded
124  else:
125  self.actionlibServer.set_succeeded()
126  rospy.logdebug("Behavior execution succeeded")
127 
128  def stopBehavior( self ):
129  with self.lock:
130  if self.behavior and self.actionlibServer.is_active() :
131  self.behaviorProxy.stopBehavior( self.behavior )
132 
133 
134 if __name__ == '__main__':
135  node = NaoBehaviors()
136  rospy.loginfo( node.NODE_NAME + " running..." )
137  rospy.spin()
138  rospy.loginfo( node.NODE_NAME + " stopped." )
139  exit(0)
def runBehavior(self, request)
def getInstalledBehaviors(self, request)
def get_proxy(self, name, warn=True)


nao_apps
Author(s):
autogenerated on Mon Jun 10 2019 14:04:55