< >
Home » Turbot与Python教程 » Turbot与python编程-实现获取上网本电池信息

Turbot与python编程-实现获取上网本电池信息

Turbot与python教程-实现获取上网本电池信息

说明:

  • 介绍如何实现通过python控制turbot实现获取上网本电池信息

代码:

  • 参考代码:github
  • 实现代码:
# Monitor the netbook's battery level

import roslib
import rospy
from smart_battery_msgs.msg import SmartBatteryStatus #for netbook battery

class netbook_battery():

    def __init__(self):
        rospy.init_node("netbook_battery")      

        #monitor netbook's battery status.  Everytime anything changes call the call back function self.NetbookPowerEventCallback and pass the data regarding the current battery status
        rospy.Subscriber("/laptop_charge/",SmartBatteryStatus,self.NetbookPowerEventCallback)

        #rospy.spin() tells the program to not exit until you press ctrl + c.  If this wasn't there... it'd subscribe to /laptop_charge/ then immediatly exit (therefore stop "listening" to the thread).
        rospy.spin();


    def NetbookPowerEventCallback(self,data):
        print("Percent: " + str(data.percentage)) 
        print("Charge: " + str(data.charge))
        if(int(data.charge_state) == 1):
            print("Currently charging")
        else:
            print("Not charging")
        print("-----")
        #Tip: try print(data) for a complete list of information available in the /laptop_charge/ thread

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

演示:

  • 主机,新终端,启动底盘
$ roslaunch turbot_bringup minimal.launch
  • 从机,新终端,启动脚本
$ rosrun turbot_code netbookBattery.py

纠错,疑问,交流: 请进入讨论区点击加入Q群

获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号


标签: turbot与python编程