Now that TurtleBot can go to a specific location, we want to make sure it doesn’t run out of battery power. In an earlier article we discussed auto-docking TurtleBot , so if TurtleBot knows when the batteries are low, it can autonomously charge itself.

Monitoring the Netbook’s Battery Status.

ROS Topics

ROS has a concept called “topics”. These topics are a lot like conference call lines. You can continuously listen to a conference line and whenever something is said on the line you can do something via a callback function. There is a “conference line” of sorts for the netbook’s battery status. You can listen to it and know every time the netbook’s battery increases or decreases charge.

Listing all Topics

ROS & TurtleBot’s libraries publish lots of topics. Let’s have a look at a complete list of them. On TurtleBot, open a terminal window and run:

roslaunch turtlebot_bringup minimal.launch

On the workstation, open a terminal window and run:

rostopic list

The above will give us a list of current topics. You should see /laptop_charge, which gives us information about the netbook’s battery. Let’s trying listening to it:

rostopic echo /laptop_charge

Every second you’ll see a collection of information about the netbook’s battery including charge, percentage, charge state (1 == it’s charging, 0 == it’s not) and more.

/laptop_charge/ battery status

Writing Code to Monitor the Netbook’s Battery Status

If you haven’t cloned the GitHub repo, do that first.

cd ~/helloworld/
python netbook_battery.py

Press ctrl + c to stop it. Now let’s have a look at the code.

Script to Monitor the Netbook's Power Status

In the terminal window type and run:

gedit netbook_battery.py

You can also view it on GitHub if you prefer.

You’ll notice in the initialize function there is:

rospy.Subscriber("/laptop_charge/",SmartBatteryStatus,self.NetbookPowerEventCallback)

This means, “Every time new data is published on the thread /laptop_charge/ call the function self.NetbookPowerEventCallback.”

SmartBetteryStatus is the data type which the thread contains. This data type is defined in the include command (found near the top of the script):

from smart_battery_msgs.msg import SmartBatteryStatus

Now let’s have a look at the function NetbookPowerEventCallback. The data is passed to the function and the components can be printed like this:

print("Percent: " + str(data.percentage)) 

IMPORTANT TIP: Later on we’ll check if data.percentage is larger than 50 to see if we need to recharge. Python is very strict when it comes to comparing different types of data so always type cast using int(). Example:

if (data.percentage < 50):

would result in an error but:

if(int(data.percentage) < 50):

will work perfectly.

Learn more at wiki.ros.org