Khepera III Toolbox/Examples/plot battery voltage

This example shows how to record the battery voltage over time and plot it in Matlab afterwards.

Setup edit

Boot a Khepera III robot and make sure it is connected to your computer via WLAN or USB. In the following text, we assume that your robot is connected via USB. If you are using WLAN, simply replace the +usb by +RobotID (e.g. +203).

Copy the battery program onto the robot

cd Programs
k3put +usb battery

You are basically ready now. But to test if everything is fine, log in to your Khepera III robot

k3go +usb

and type

cd your_username

This should print something like this:


The second number (7.8372) in this NMEA message reports the battery voltage in volts (V), which is usually somewhere between 8.2 V (fully charged battery) and 6.6 V (nearly empty battery).

Recording the Battery Voltage edit

We will now measure the battery voltage once every second and record the messages on the computer. Note that your robot needs to stay connected to the computer during the whole experiment.

On your computer, type the following:

k3run +usb -o battery_measures "/root/your_username/battery -r -w 1000000"

Note that this script will launch the program in the background and return immediately. The samples will be stored in the file k3- instead of being printed in the terminal. (The folder k3- is automatically created by the k3run script.)

To see what is currently being stored, you can "tail" that file:

tail k3-

This will print the last 10 lines of the output file. You can even follow the measurements with the -f option:

tail -f k3-

On the robot, you can check if the battery program is running by typing


which lists all running processes.

Stopping the Measurement edit

If your robot is running on battery, the battery voltage will decrease until it drops below about 6.6 V, at which point the KoreBot board running Linux crashes. At this point, the recording will stop automatically.

If you want to stop recording before the robot crashes, you can do this manually by killing the battery process on the robot:

killall battery

If you type


now, the battery process should have disappeared.

Alternative 1 edit

In case your robot is not connected to the computer during the experiment, you can store the samples on the robot by typing (on the robot):

./battery -r -w 1000000 > battery_measures

or if you want to print the samples at the same time

./battery -r -w 1000000 | tee battery_measures

To terminate the measurements, just press Ctrl-C.

To download the measurements from the robot, type (on your computer):

k3get +usb battery_measures

Note that the disk space on robot is very limited (only about 1 MB is free). Make sure you always download the measurements after the experiment and delete the file on the robot (rm battery_measures). The available disk space can be queried with df -h.

Alternative 2 edit

Instead of running the battery program with the -r option (continuous measurements), you can launch the battery program each time you want to measure the voltage and just take one measurement at a time. This can be useful if the time span between measurement is much longer (e.g. once every 10 minutes) or if you want to take a measurement before/after your experiment.

To achieve that, it is best to write a short script which launches the battery program every now and then by executing the following line

 ssh root@ "/root/your_username/battery" >> battery_measures

A sample script ( can be found in the folder of this example.

Parsing the Result File edit

The file battery_measures contains NMEA 0183 records, from which we need to extract the voltage now. This can be done using one of the provided scripts (Perl) or (Python). Both script are equivalent - choose whichever you prefer. We will stick to the Perl version here.

Launch the script as follows

./ < battery_measures > matlab_battery_voltage

The file matlab_battery_voltage now contains one number per line, which indicates the voltage of the corresponding measurement.

Plotting the Result edit

Now, launch Matlab and type


This is a very simple Matlab script (m-file) which loads the matlab_battery_voltage file and plots the curve.