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 import roslib
00035 roslib.load_manifest('pr2_plugs_actions')
00036
00037 import rospy
00038 import actionlib
00039 from random import choice
00040 import battery
00041
00042 from pr2_plugs_msgs.msg import EmptyAction, RechargeAction, RechargeGoal, RechargeCommand, RechargeState
00043
00044 class Application():
00045 def __init__(self):
00046 self.charging_locations = rospy.get_param("~charging_locations", ['wim', 'whiteboard', 'james'])
00047
00048 self.recharge = actionlib.SimpleActionClient('recharge', RechargeAction)
00049 self.recharge.wait_for_server()
00050
00051 self.app_action = actionlib.SimpleActionServer('recharge_application_server', EmptyAction, self.application_cb)
00052
00053 def plug_in(self):
00054 if self.state == 'unplugged':
00055 goal = RechargeGoal()
00056 goal.command.command = RechargeCommand.PLUG_IN
00057 goal.command.plug_id = choice(self.charging_locations)
00058 res = self.recharge.send_goal_and_wait(goal) == actionlib.GoalStatus.SUCCEEDED
00059 if res:
00060 self.state = 'plugged_in'
00061 return res
00062 else:
00063 rospy.logerr('Calling plug_in when robot is not unplugged')
00064 return False
00065
00066
00067 def unplug(self):
00068 if self.state == 'plugged_in':
00069 goal = RechargeGoal()
00070 goal.command.command = RechargeCommand.UNPLUG
00071 self.recharge.send_goal_and_wait(goal)
00072 self.state = 'unplugged'
00073 else:
00074 rospy.logerr('Calling unplug when robot is not plugged in')
00075
00076
00077
00078 def application_cb(self, goal):
00079 rospy.loginfo('Recharge application became active.')
00080 plugged_in = False
00081 self.state = 'unplugged'
00082
00083
00084 while rospy.is_shutdown and not plugged_in and not self.app_action.is_preempt_requested():
00085 if self.plug_in():
00086 if not self.app_action.is_preempt_requested():
00087 rospy.loginfo('I think I successfully plugged myself in. But need to verify this...')
00088 battery_monitor = battery.Battery()
00089 if battery_monitor.get_state() == 'charging':
00090 rospy.loginfo("Yes, we're plugged in, I can feel my batteries getting charged.")
00091 plugged_in = True
00092 else:
00093 rospy.logerr('Plugging in failed even though the action claimed it succeeded. Will try again, but first need to unplug.')
00094 self.unplug()
00095 else:
00096 rospy.logerr("Plugging reported failure. Trying again")
00097
00098
00099
00100 while not rospy.is_shutdown():
00101 rospy.sleep(0.1)
00102 if self.app_action.is_preempt_requested():
00103 break
00104
00105
00106 if self.state == 'plugged_in':
00107 self.unplug()
00108 self.app_action.set_preempted()
00109
00110
00111
00112
00113 def main():
00114 rospy.init_node("recharge_application_monitor")
00115
00116 a = Application()
00117 rospy.spin()
00118
00119
00120 if __name__ == "__main__":
00121 main()
00122