main_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, Robot Control and Pattern Recognition Group, Warsaw University of Technology
00006 #
00007 # All rights reserved.
00008 # 
00009 # Redistribution and use in source and binary forms, with or without
00010 # modification, are permitted provided that the following conditions are met:
00011 #     * Redistributions of source code must retain the above copyright
00012 #       notice, this list of conditions and the following disclaimer.
00013 #     * Redistributions in binary form must reproduce the above copyright
00014 #       notice, this list of conditions and the following disclaimer in the
00015 #       documentation and/or other materials provided with the distribution.
00016 #     * Neither the name of the <organization> nor the
00017 #       names of its contributors may be used to endorse or promote products
00018 #       derived from this software without specific prior written permission.
00019 # 
00020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00021 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00022 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00023 # DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
00024 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00025 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00026 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00027 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00028 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00029 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 
00031 import roslib
00032 roslib.load_manifest('elektron_monitor')
00033 
00034 import rospy
00035 
00036 import diagnostic_msgs.msg
00037 import serial
00038 import math
00039 
00040 
00041 from sound_play.msg import SoundRequest
00042 from sound_play.libsoundplay import SoundClient
00043 
00044 class Battery:
00045 
00046     def __init__(self):
00047         # open serial port
00048         self.device = rospy.get_param('~device', '/dev/ttyUSB3')
00049         self.baud = rospy.get_param('~baud', 9600)
00050         self.ser = serial.Serial(self.device, self.baud, timeout=0.3)
00051         
00052         self.diag_pub = rospy.Publisher('/diagnostics', diagnostic_msgs.msg.DiagnosticArray)
00053         
00054         self.adc_factor = 9.34 / 307
00055         self.last_voltage = 0.0
00056         
00057         self.last_beep = rospy.Time.now()
00058         self.lvl_low = 21
00059         self.lvl_crit = 19.5
00060         
00061         self.play_music = rospy.get_param('~play_music', False)
00062         
00063         if (self.play_music):
00064             self.soundhandle = SoundClient()
00065             self.snd_low = rospy.get_param('~snd_low', '')
00066             self.snd_crit = rospy.get_param('~snd_critical', '')
00067             self.snd_plugin = rospy.get_param('~snd_plugin', '')
00068             self.snd_plugout = rospy.get_param('~snd_plugout', '')
00069 
00070     def spin(self):
00071 
00072         self.ser.flushInput()
00073 
00074         while not rospy.is_shutdown():
00075 
00076             # get data
00077             s = self.ser.read(2)
00078             
00079             #Main header
00080             diag = diagnostic_msgs.msg.DiagnosticArray()
00081             diag.header.stamp = rospy.Time.now()
00082     
00083             
00084             #battery info                                                                                                                              
00085             stat = diagnostic_msgs.msg.DiagnosticStatus()
00086             stat.name = "Main Battery"
00087             stat.level = diagnostic_msgs.msg.DiagnosticStatus.OK
00088             stat.message = "OK"
00089 
00090             # check, if it was correct line
00091             if (len(s) < 2):
00092                 continue
00093             
00094             hi = ord(s[0])
00095             lo = ord(s[1])
00096             voltage = (hi*256 + lo) * self.adc_factor
00097             
00098             if (voltage < self.lvl_crit) and (rospy.Time.now() - self.last_beep > rospy.Duration(30)):
00099                 rospy.logwarn("Critical power level.")
00100                 self.last_beep = rospy.Time.now()
00101                 if (self.play_music):
00102                     self.soundhandle.playWave(self.snd_crit)
00103             else:
00104                 if (voltage < self.lvl_low) and (rospy.Time.now() - self.last_beep > rospy.Duration(120)):
00105                     rospy.logwarn("Low power level.")
00106                     self.last_beep = rospy.Time.now()
00107                     if (self.play_music):
00108                         self.soundhandle.playWave(self.snd_low)
00109                         
00110             # Just plugged in
00111             if (self.last_voltage < 26) and (voltage > 28):
00112                 rospy.loginfo("Power charger plugged in.")
00113                 if (self.play_music):
00114                     self.soundhandle.playWave(self.snd_plugin)
00115             
00116             # Just plugged out
00117             if (self.last_voltage > 28) and (voltage < 26):
00118                 rospy.loginfo("Power charger plugged out.")
00119                 if (self.play_music):
00120                     self.soundhandle.playWave(self.snd_plugout)
00121             
00122             self.last_voltage = voltage
00123             stat.values.append(diagnostic_msgs.msg.KeyValue("Voltage", str(voltage)))
00124 
00125             #append
00126             diag.status.append(stat)
00127             
00128             #publish
00129             self.diag_pub.publish(diag)
00130 
00131 
00132 
00133 if __name__ == '__main__':
00134     rospy.init_node('main_battery_monitor')      
00135     bat = Battery()
00136 
00137     try:
00138         bat.spin()
00139     except rospy.ROSInterruptException: pass
00140     except IOError: pass
00141     except KeyError: pass


elektron_monitor
Author(s): Maciej Stefanczyk
autogenerated on Mon Oct 6 2014 10:26:37