查看 kobuki 底座电量

#!/usr/bin/env python
import rospy
from kobuki_msgs.msg import SensorState


class kobuki_battery:
    kobuki_base_max_charge = 160  # max battery charge

    def __init__(self):
        rospy.init_node("kobuki_battery", disable_signals=True)  # this param can be used to close node

        # monitor Kobuki's power and charging status. (low battery, charging, not charging etc)
        self.kobuki_monitor = rospy.Subscriber("/mobile_base/sensors/core", SensorState,
                                               self.SensorPowerEventCallback)
        rospy.spin()

    def SensorPowerEventCallback(self, data):  # write callback function inner the class
        rospy.loginfo("Kobuki's battery is now: " + str(
            round(float(data.battery) / float(self.kobuki_base_max_charge) * 100)) + "%")
        if int(data.charger) == 0:
            rospy.loginfo("Not charging at docking station")
        else:
            rospy.loginfo("Charging at docking station")
        # self.kobuki_monitor.unregister()  # cancel subscription
        rospy.signal_shutdown(reason='only look once')  # close node


if __name__ == '__main__':
    try:
        kobuki_battery()
    except rospy.ROSInterruptException:
        rospy.loginfo("exception")

init_node 使用参数 disable_signals 可以在 callback 里面关闭 node,就只显示一次了。

[INFO] [1548402990.089208]: Kobuki's battery is now: 96.0%
[INFO] [1548402990.090261]: Not charging at docking station
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容