This practical will use the Khepera robot, the same robot used in IVR.
The full user manual can be found at and you should use this for reference.

Using the Khepera

For this course, you will be using Python or MATLAB to control the Khepera, and communicating with it via a serial cable. The following instructions should be enough to get you up and running.

Plug everything in
  • Computer serial port -> interface board (black)
  • Interface board -> Khepera (using 5 pin connector, from interface board to port on top of Khepera) PLEASE BE AS GENTLE AS POSSIBLE WITH THESE CABLES
  • Power cable -> interface board
  • Battery / external power can be selected with red switch on bottom front of robot. Seeing as we're always going to have it connected, we may as well use external power. For this, the lights on the interface board should be OFF.

Turn switch on top of Khepera to ‘1’, for external control mode at 9600 baud.
  • (The turny bit is an arrow, if you look closely!)

To communicate with the Khepera from MATLAB :

Open a connection with:
s = serial(‘/dev/ttyS0’)

Send commands to the robot with:

Read command result with

Close the connection when you’re done:

To communicate with the Khepera from Python :

You will need to have the pyserial module installed, which you can find at You may also want to look over the guidelines for installing python packages on DICE, found at

Open a connection with:
import serial
s = serial.Serial(port='/dev/ttyS0', baudrate=9600, stopbits=2, timeout=1)

Send commands to the robot with:

Read command result with

Close the connection when you’re done:


Below are the actual commands which need to be sent to the Khepera over the serial connection to make it do stuff. On the Khepera Code page there are Python and MATLAB functions which wrap these commands if you'd prefer to use those.

IMPORTANT: You should read the output of a command after issuing it, with fscanf(s) (MATLAB) or s.readline() (Python), even if you’re not interested in the output (e.g. for motor commands). This prevents responses building up on the return queue, messing things up when you actually want a response!


Set wheel speeds: ‘D,left_speed,right_speed’

(use ‘D,0,0’ to stop)

Reading sensors:

Read IR command: ‘N’
Read ambient light command: 'O'
Then, in MATLAB :
sensor_readings = fscanf(s)

or, in Python :
sensor_readings = s.readline()

sensor_readings now contains the 8 IR/ambient light sensor values, in the order labelled below:

IR sensor location. From page 12 of the Khepera user manual.

These are returned as a comma separated string, so will have to be parsed to get the values!

More info on the meanings of the sensor readings can be found in the user manual, page 12 and onwards.

Wheel counters

The Khepera can keep track of wheel rotation. You can set (or reset to zero) these counts with:

Set counts: 'G, position_motor_left, position_motor_right'

and read them with

Read counts: 'H'

Reading counts returns them in the format 'h, position_motor_left, position_motor_right', so again, these will need to be parsed.

Regarding the values returned with this function, the manual states that "The unit is the pulse, that corresponds to 0,08 mm".


Control LEDs: 'L,LED_number,action_number'

LED_number should be 0 or 1

Action number is 0 for off, 1 for on, or 2 for change state