laptop_battery.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 # Software License Agreement (BSD License)
00004 #
00005 # Copyright (c) 2011, Willow Garage, Inc.
00006 # All rights reserved.
00007 #
00008 # Redistribution and use in source and binary forms, with or without
00009 # modification, are permitted provided that the following conditions
00010 # are met:
00011 #
00012 #  * Redistributions of source code must retain the above copyright
00013 #    notice, this list of conditions and the following disclaimer.
00014 #  * Redistributions in binary form must reproduce the above
00015 #    copyright notice, this list of conditions and the following
00016 #    disclaimer in the documentation and/or other materials provided
00017 #    with the distribution.
00018 #  * Neither the name of the Willow Garage nor the names of its
00019 #    contributors may be used to endorse or promote products derived
00020 #    from this software without specific prior written permission.
00021 #
00022 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033 # POSSIBILITY OF SUCH DAMAGE.
00034 
00035 # this was stolen from the turtlebot!
00036 import roslib
00037 roslib.load_manifest('maxwell_defs')
00038 import rospy
00039 import diagnostic_msgs.msg
00040 import subprocess
00041 
00042 def laptop_battery_monitor():
00043     diag_pub = rospy.Publisher('/diagnostics', diagnostic_msgs.msg.DiagnosticArray)
00044     rospy.init_node('laptop_battery_monitor')
00045     while not rospy.is_shutdown():
00046         
00047         #Main header
00048         diag = diagnostic_msgs.msg.DiagnosticArray()
00049         diag.header.stamp = rospy.Time.now()
00050         
00051         #battery info                                                                                                                              
00052         stat = diagnostic_msgs.msg.DiagnosticStatus()
00053         stat.name = "Laptop Battery"
00054         stat.level = diagnostic_msgs.msg.DiagnosticStatus.OK
00055         stat.message = "OK"
00056 
00057         # get values from upower
00058         cmd = ['upower', '-d']
00059         command_line = subprocess.Popen(cmd, stdout=subprocess.PIPE,  stderr=subprocess.STDOUT)
00060         (pstd_out, pstd_err) = command_line.communicate() # pstd_err should be None due to pipe above 
00061         raw_battery_stats = {}
00062         for l in pstd_out.split('\n'):
00063             l_vec = l.split(':')
00064             l_stripped = [l.strip() for l in l_vec]
00065             if len(l_stripped) == 2:
00066                 k, v = l_stripped
00067                 if k == 'voltage':
00068                     raw_battery_stats['voltage'] = v.split()[0]
00069                 elif k == 'energy-rate':
00070                     raw_battery_stats['watts'] = v.split()[0]
00071                 elif k == 'energy-full':
00072                     raw_battery_stats['energy_full_wh'] = v.split()[0]
00073                 elif k == 'percentage':
00074                     raw_battery_stats['percentage'] = v.rstrip('%')
00075                 elif k == 'on-battery':
00076                     raw_battery_stats['on_battery'] = v
00077 
00078         voltage = float(raw_battery_stats['voltage'])
00079         watts = float(raw_battery_stats['watts'])
00080         if raw_battery_stats['on_battery'] == 'no':
00081             current_direction = 1
00082         else:
00083             current_direction = -1
00084         current = current_direction * watts / voltage
00085         capacity_fraction  = float(raw_battery_stats['percentage'])/100.0
00086         energy_full = float(raw_battery_stats['energy_full_wh'])
00087         capacity = energy_full / voltage
00088         charge = capacity_fraction * capacity
00089 
00090         stat.values.append(diagnostic_msgs.msg.KeyValue("Voltage (V)", str(voltage)))
00091         stat.values.append(diagnostic_msgs.msg.KeyValue("Current (A)", str(current)))
00092         stat.values.append(diagnostic_msgs.msg.KeyValue("Charge (Ah)", str(charge)))
00093         stat.values.append(diagnostic_msgs.msg.KeyValue("Capacity (Ah)", str(capacity)))
00094 
00095         #append
00096         diag.status.append(stat)
00097         
00098         #publish
00099         diag_pub.publish(diag)
00100         rospy.sleep(1.0)
00101 
00102 
00103 if __name__ == '__main__':
00104     try:
00105         laptop_battery_monitor()
00106     except rospy.ROSInterruptException: pass
00107     except IOError: pass
00108     except KeyError: pass


maxwell_defs
Author(s): Michael Ferguson
autogenerated on Tue Jan 7 2014 11:22:13