例によって参考程度になればと貼っておきます。
他にもっといい方法あれば教えてください。
一応ノードも作りました。
#! /usr/bin/env python import re class AcpiChecker(): """read /proc/acpi/battery/*** files for check the PC's battery""" def __init__(self, battery_name): self._info_path = "/proc/acpi/battery/" + battery_name + "/info" self._state_path = "/proc/acpi/battery/" + battery_name + "/state" def get_capacity(self): f = open(self._info_path) for line in f: match = re.search(r'^last full capacity:\s*(\d+)', line) if match: capacity = match.groups()[0] f.close() return capacity def get_state(self): f = open(self._state_path) for line in f: match = re.search(r'^remaining capacity:\s*(\d+)', line) if match: state = match.groups()[0] f.close() return state def get_rate(self): return float(self.get_state()) / float(self.get_capacity()) if __name__=='__main__': checker = AcpiChecker('BAT0') print checker.get_rate()
ノードはこんな感じでてきとーに作ってみました。
#! /usr/bin/env python import roslib roslib.load_manifest('otl_battery_checker') import rospy from std_msgs.msg import Float64 from battery_check import AcpiChecker class BatteryCheckerNode(): """check battery and publish the charge rate""" def __init__(self, battery_name='BAT0'): self._checker = AcpiChecker(battery_name) self._pub = rospy.Publisher('/pc/battery_rate', Float64) def proc(self): msg = Float64() msg.data = self._checker.get_rate() self._pub.publish(msg) if __name__=='__main__': rospy.init_node('pc_battery_checker') battery_name = 'BAT0' if rospy.has_param('~device'): battery_name = rospy.get_param('~device') check = BatteryCheckerNode(battery_name) rate = rospy.Rate(1) while not rospy.is_shutdown(): check.proc() rate.sleep()
参考にしたサイト
0 件のコメント:
コメントを投稿