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 import roslib
00034 roslib.load_manifest('pr2_dashboard')
00035
00036 import wx
00037
00038 from pr2_msgs.msg import PowerState, PowerBoardState
00039 from pr2_power_board.srv import PowerBoardCommand, PowerBoardCommandRequest
00040
00041 from os import path
00042
00043 class PowerStateControl(wx.Window):
00044 def __init__(self, parent, id, icons_path):
00045 wx.Window.__init__(self, parent, id, wx.DefaultPosition, wx.Size(60, 32))
00046
00047 self._power_consumption = 0.0
00048 self._pct = 0
00049 self._time_remaining = roslib.rostime.Duration(0)
00050 self._ac_present = 0
00051
00052 self._left_bitmap = wx.Bitmap(path.join(icons_path, "battery-minus.png"), wx.BITMAP_TYPE_PNG)
00053 self._right_bitmap = wx.Bitmap(path.join(icons_path, "battery-plus.png"), wx.BITMAP_TYPE_PNG)
00054 self._plug_bitmap = wx.Bitmap(path.join(icons_path, "battery-charging.png"), wx.BITMAP_TYPE_PNG)
00055 self._background_bitmap = wx.Bitmap(path.join(icons_path, "battery-background.png"), wx.BITMAP_TYPE_PNG)
00056 self._green = wx.Bitmap(path.join(icons_path, "battery-green-bar.png"), wx.BITMAP_TYPE_PNG)
00057 self._yellow = wx.Bitmap(path.join(icons_path, "battery-yellow-bar.png"), wx.BITMAP_TYPE_PNG)
00058 self._red = wx.Bitmap(path.join(icons_path, "battery-red-bar.png"), wx.BITMAP_TYPE_PNG)
00059
00060 self.SetSize(wx.Size(self._left_bitmap.GetWidth() + self._right_bitmap.GetWidth() + self._background_bitmap.GetWidth(), 32))
00061
00062 self._start_x = self._left_bitmap.GetWidth()
00063 self._end_x = self.GetSize().x - self._right_bitmap.GetWidth()
00064 self._width = self._end_x - self._start_x
00065
00066 self._plugged_in = False
00067
00068 self.Bind(wx.EVT_PAINT, self.on_paint)
00069
00070 def on_paint(self, evt):
00071 dc = wx.BufferedPaintDC(self)
00072
00073 dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
00074 dc.Clear()
00075
00076 w = self.GetSize().GetWidth()
00077 h = self._left_bitmap.GetHeight()
00078
00079 color_bitmap = None
00080 if (self._pct > 0.5):
00081 color_bitmap = self._green
00082 elif (self._pct > 0.3):
00083 color_bitmap = self._yellow
00084 else:
00085 color_bitmap = self._red
00086
00087 dc.DrawBitmap(self._background_bitmap, self._start_x, 0, True)
00088
00089 color_image = color_bitmap.ConvertToImage()
00090 scaled_color_image = color_image.Rescale(round(self._width * self._pct), color_bitmap.GetHeight())
00091 color_bitmap = wx.BitmapFromImage(scaled_color_image)
00092 dc.DrawBitmap(color_bitmap, self._start_x, 0, True)
00093 dc.DrawBitmap(self._left_bitmap, 0, 0, True)
00094 dc.DrawBitmap(self._right_bitmap, self._end_x, 0, True)
00095
00096 if (self._plugged_in):
00097 dc.DrawBitmap(self._plug_bitmap,
00098 (self._start_x + self._width) / 2.0 - (self._plug_bitmap.GetWidth() / 2.0),
00099 self.GetSize().GetHeight() / 2.0 - (self._plug_bitmap.GetHeight() / 2.0))
00100
00101
00102 def set_power_state(self, msg):
00103 last_pct = self._pct
00104 last_plugged_in = self._plugged_in
00105 last_time_remaining = self._time_remaining
00106
00107 self._power_consumption = msg.power_consumption
00108 self._time_remaining = msg.time_remaining
00109 self._pct = msg.relative_capacity / 100.0
00110 self._plugged_in = msg.AC_present
00111
00112 if (last_pct != self._pct or last_plugged_in != self._plugged_in or last_time_remaining != self._time_remaining):
00113 drain_str = "remaining"
00114 if (self._plugged_in):
00115 drain_str = "to full charge"
00116 self.SetToolTip(wx.ToolTip("Battery: %.2f%% (%d minutes %s)"%(self._pct * 100.0, self._time_remaining.to_sec()/60.0, drain_str)))
00117 self.Refresh()
00118
00119 def set_stale(self):
00120 self._plugged_in = 0
00121 self._pct = 0
00122 self._time_remaining = roslib.rostime.Duration(0)
00123 self._power_consumption = 0
00124 self.SetToolTip(wx.ToolTip("Battery: Stale"))
00125
00126 self.Refresh()