behavior_manager.py
Go to the documentation of this file.
00001 #! /usr/bin/python
00002 
00003 import numpy as np
00004 
00005 import roslib; roslib.load_manifest('hrl_clickable_world')
00006 import rospy
00007 import std_msgs.msg
00008 
00009 from hrl_clickable_world.srv import PerceiveButtons, ButtonAction, DisplayButtons
00010 from hrl_clickable_world.srv import ButtonActionRequest
00011 from hrl_clickable_world.srv import PerceiveButtonsResponse, ButtonActionResponse
00012 from pixel_2_3d.srv import Pixel23d
00013 
00014 NS_PREFIX = "/clickable_world/"
00015 
00016 class BehaviorManager:
00017     def __init__(self):
00018         self.behavior_names = rospy.get_param(NS_PREFIX + "behavior_names")
00019         self.robot_state = rospy.get_param(NS_PREFIX + "init_conditions")
00020         self.srv_prefix = rospy.get_param(NS_PREFIX + "behavior_namespace")
00021         self.percieve_buttons_sub = rospy.Subscriber(NS_PREFIX + "perceive_buttons",
00022                                                   std_msgs.msg.Bool,
00023                                                   self.perceive_buttons)
00024         self.display_buttons_srv = rospy.ServiceProxy(NS_PREFIX + "display_buttons",
00025                                                       DisplayButtons)
00026         self.button_action_srv = rospy.Service(NS_PREFIX + "button_action",
00027                                                ButtonAction,
00028                                                self.on_button_press)
00029         self.pixel_2_3d_srv = rospy.ServiceProxy("/pixel_2_3d/pixel_2_3d",
00030                                                       Pixel23d)
00031         self.behaviors = {}
00032         self.perception_srvs = {}
00033         self.action_srvs = {}
00034         self.button_types = []
00035         for name in self.behavior_names:
00036             behavior = rospy.get_param(NS_PREFIX + name)
00037             if behavior["perception_srv"] not in self.perception_srvs:
00038                 self.perception_srvs[behavior["perception_srv"]] = rospy.ServiceProxy(
00039                                     self.srv_prefix + behavior["perception_srv"], 
00040                                     PerceiveButtons)
00041             if behavior["action_srv"] not in self.action_srvs:
00042                 self.action_srvs[behavior["action_srv"]] = rospy.ServiceProxy(
00043                                     self.srv_prefix + behavior["action_srv"], 
00044                                     ButtonAction)
00045             self.behaviors[name] = behavior
00046 
00047     def perceive_buttons(self, msg):
00048         rospy.loginfo("Perceiving buttons")
00049         self.button_types = []
00050         cur_button_id = 1
00051         displayed_buttons = []
00052         for name in self.behavior_names:
00053             # check to see if preconditions satisfied
00054             preconds_satisfied = True
00055             if "preconditions" in self.behaviors[name]:
00056                 for condition in self.behaviors[name]["preconditions"]:
00057                     if (self.behaviors[name]["preconditions"][condition] != 
00058                                                          self.robot_state[condition]):
00059                         preconds_satisfied = False
00060 
00061             if preconds_satisfied:
00062                 # get the buttons for this perception
00063                 rospy.loginfo("Perceiving buttons for %s" % self.behaviors[name]["perception_srv"])
00064                 try:
00065                     resp = self.perception_srvs[self.behaviors[name]["perception_srv"]]()
00066                 except:
00067                     continue
00068                 cur_buttons = resp.buttons
00069                 for i, button in enumerate(cur_buttons):
00070                     self.button_types.append(name)
00071                     cur_buttons[i].id = cur_button_id
00072                     cur_buttons[i].ns = name
00073                     cur_button_id += 1
00074                 displayed_buttons.extend(cur_buttons)
00075         # tell display to show buttons
00076         self.display_buttons_srv(displayed_buttons)
00077 
00078     def on_button_press(self, req):
00079         if req.button_id == 0:
00080             return
00081         do_behav = self.behaviors[self.button_types[req.button_id-1]]
00082         self.action_srvs[do_behav["action_srv"]](req)
00083         action_success = True
00084         # TODO use result from action srv
00085         if action_success:
00086             # update robot state
00087             if "postconditions" in do_behav:
00088                 for condition in do_behav["postconditions"]:
00089                     self.robot_state[condition] = do_behav["postconditions"][condition]
00090         rospy.loginfo("Current State: " + str(self.robot_state))
00091 
00092 def main():
00093     rospy.init_node('behavior_manager')
00094     bm = BehaviorManager()
00095     rospy.spin()
00096 
00097 if __name__ == "__main__":
00098     main()


hrl_clickable_world
Author(s): Kelsey Hawkins, Advisor: Prof. Charlie Kemp (Healthcare Robotics Lab at Georgia Tech)
autogenerated on Wed Nov 27 2013 11:54:28