battery_monitor.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 #################################################################
00003 ##\file
00004 #
00005 # \note
00006 #   Copyright (c) 2012 \n
00007 #   Fraunhofer Institute for Manufacturing Engineering
00008 #   and Automation (IPA) \n\n
00009 #
00010 #################################################################
00011 #
00012 # \note
00013 #   Project name: care-o-bot
00014 # \note
00015 #   ROS stack name: cob_robots
00016 # \note
00017 #   ROS package name: cob_monitoring
00018 #
00019 # \author
00020 #   Author: Florian Weisshardt
00021 # \author
00022 #   Supervised by: 
00023 #
00024 # \date Date of creation: Dec 2012
00025 #
00026 # \brief
00027 #   Monitors the battery level and announces warnings and reminders to recharge.
00028 #
00029 #################################################################
00030 #
00031 # Redistribution and use in source and binary forms, with or without
00032 # modification, are permitted provided that the following conditions are met:
00033 #
00034 #     - Redistributions of source code must retain the above copyright
00035 #       notice, this list of conditions and the following disclaimer. \n
00036 #     - Redistributions in binary form must reproduce the above copyright
00037 #       notice, this list of conditions and the following disclaimer in the
00038 #       documentation and/or other materials provided with the distribution. \n
00039 #     - Neither the name of the Fraunhofer Institute for Manufacturing
00040 #       Engineering and Automation (IPA) nor the names of its
00041 #       contributors may be used to endorse or promote products derived from
00042 #       this software without specific prior written permission. \n
00043 #
00044 # This program is free software: you can redistribute it and/or modify
00045 # it under the terms of the GNU Lesser General Public License LGPL as 
00046 # published by the Free Software Foundation, either version 3 of the 
00047 # License, or (at your option) any later version.
00048 # 
00049 # This program is distributed in the hope that it will be useful,
00050 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00051 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00052 # GNU Lesser General Public License LGPL for more details.
00053 # 
00054 # You should have received a copy of the GNU Lesser General Public 
00055 # License LGPL along with this program. 
00056 # If not, see <http://www.gnu.org/licenses/>.
00057 #
00058 #################################################################
00059 
00060 import roslib
00061 roslib.load_manifest('cob_monitoring')
00062 import rospy
00063 
00064 from pr2_msgs.msg import *
00065 
00066 from simple_script_server import *
00067 sss = simple_script_server()
00068 
00069 class battery_monitor():
00070         def __init__(self):
00071                 rospy.Subscriber("/power_state", PowerState, self.power_state_callback)
00072                 self.rate = rospy.Rate(1/10.0) # check every 10 sec
00073                 self.warn_announce_time  = rospy.Duration(300.0)
00074                 self.error_announce_time = rospy.Duration(120.0)
00075                 self.last_announced_time = rospy.Time.now()
00076 
00077         ## Battery monitoring
00078         ### TODO: make values parametrized through yaml file (depending on env ROBOT)
00079         def power_state_callback(self,msg):
00080                 if msg.relative_capacity <= 10.0:
00081                         rospy.logerr("Battery empty, recharge now! Battery state is at " + str(msg.relative_capacity) + "%.")
00082                         #TODO: print "start flashing red fast --> action call to lightmode"
00083                         if rospy.Time.now() - self.last_announced_time >= self.error_announce_time:
00084                                 sss.say(["My battery is empty, please recharge now."])
00085                                 self.last_announced_time = rospy.Time.now()
00086                 elif msg.relative_capacity <= 30.0:                     
00087                         rospy.logwarn("Battery nearly empty, consider recharging. Battery state is at " + str(msg.relative_capacity) + "%.") 
00088                         #TODO: "start flashing yellow slowly --> action call to lightmode"
00089                         if rospy.Time.now() - self.last_announced_time >= self.warn_announce_time:
00090                                 sss.say(["My battery is nearly empty, please consider recharging."])
00091                                 self.last_announced_time = rospy.Time.now()
00092                 else:
00093                         rospy.logdebug("Battery level ok.")     
00094                 
00095                 # sleep
00096                 self.rate.sleep()
00097 
00098 if __name__ == "__main__":
00099         rospy.init_node("battery_monitor")
00100         battery_monitor()
00101         rospy.spin()


cob_monitoring
Author(s): Florian Weisshardt
autogenerated on Thu Aug 27 2015 12:48:21