# 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

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
You are basically ready now. But to test if everything is fine, log in to your Khepera III robot
and type
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

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:
Note that this script will launch the program in the background and return immediately. The samples will be stored in the file k3-192.168.1.2/battery_measures instead of being printed in the terminal. (The folder k3-192.168.1.2 is automatically created by the k3run script.)
To see what is currently being stored, you can "tail" that file:
This will print the last 10 lines of the output file. You can even follow the measurements with the -f option:
On the robot, you can check if the battery program is running by typing
which lists all running processes.

### Stopping the Measurement

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:
If you type
now, the battery process should have disappeared.

### Alternative 1

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):
or if you want to print the samples at the same time
To terminate the measurements, just press Ctrl-C.
To download the measurements from the robot, type (on your computer):
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

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
A sample script (measure_alternative2.pl) can be found in the folder of this example.

## Parsing the Result File

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 parse_battery_voltage.pl (Perl) or parse_battery_voltage.py (Python). Both script are equivalent - choose whichever you prefer. We will stick to the Perl version here.
Launch the script as follows
The file matlab_battery_voltage now contains one number per line, which indicates the voltage of the corresponding measurement.

## Plotting the Result

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.