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 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
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
00077 s = self.ser.read(2)
00078
00079
00080 diag = diagnostic_msgs.msg.DiagnosticArray()
00081 diag.header.stamp = rospy.Time.now()
00082
00083
00084
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
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
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
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
00126 diag.status.append(stat)
00127
00128
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