Monitor the Netbook’s Battery Status
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:
On the workstation, open a terminal window and run:
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:
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.
Writing Code to Monitor the Netbook’s Battery Status
If you haven’t cloned the GitHub repo, do that first.
Press ctrl + c to stop it. Now let’s have a look at the code.
In the terminal window type and run:
You can also view it on GitHub if you prefer.
You’ll notice in the initialize function there is:
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):
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:
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:
would result in an error but:
will work perfectly.