Khepera III Toolbox/Writing your Own Programs

From Wikibooks, open books for an open world
< Khepera III Toolbox
Jump to: navigation, search

Even though the standard programs allow you to interact with all sensors and actuators of the robot, you probably want to implement your own algorithms. The Khepera III Toolbox provides you with a nice and easy-to-use API for accessing the sensors and actuators of the robot, and several hundred lines of sample code in C. In addition, a Makefile is available.

Preparing the Environment[edit]

Linux for Korebot[edit]

First of all, make sure that make is available on your computer by typing

make -h

If the computer complains that he could not find make, install it using the package management system of your Linux distribution.

Next, you need to install the ARM compiler toolchain. Download the file korebot-tools-i386-0.1.2.tar.gz (KoreBot Compilation Toolchain for Linux i386 host) from the K-Team website and unzip it somewhere in your home directory, or in /usr/local/

tar -xfz korebot-tools-i386-0.1.2.tar.gz

Add the bin directory to your PATH environment variable, preferably by adding the following line to your .bashrc file:

export PATH=$PATH:/path/to/your/arm/toolchain/bin

and restarting your bash shell (terminal).

At this point, your computer should not complain any more if you just type

arm-linux-gcc -h

Linux for Korebot 2[edit]

First of all, make sure that make is available on your computer by typing

make -h

If the computer complains that he could not find make, install it using the package management system of your Linux distribution.

Next, you need to install the ARM compiler toolchain. Download the file korebot2-oetools-light-1.0-kb1.2.tar.bz2 from the K-Team website and unzip it somewhere in /usr/local/. Doing it in /usr/local is necessary so the compiler can find the libraries.

wget http://ftp.k-team.com/KorebotII/software/light_toolchain/korebot2-oetools-light-1.0-kb1.2.tar.bz2
tar xvjf korebot2-oetools-light-1.0-kb1.2.tar.bz2
sudo mv korebot2-oetools-1.0/ /usr/local/

Add the bin directory to your PATH environment variable, preferably by adding the following line to your .bashrc file:

export PATH=$PATH:/usr/local/korebot2-oetools-1.0/tmp/cross/bin/

and restarting your bash shell (terminal).

At this point, your computer should not complain any more if you just type

arm-angstrom-linux-gnueabi-gcc --help

Mac OS X[edit]

At the time of writing, we are not aware of any ARM compiler toolchain for Mac OS X, although I'm pretty sure there must be one out there. All other steps should be the same as on Linux (see above).

Creating a New Program[edit]

To create a new program, type

cd my_khepera3_project_directory
k3-create-program my_new_program

where my_new_program denotes the name of your program. This creates a folder my_new_program and copies a sample main.c file as well as a Makefile in there.

To check if everything is all right, try to compile the new program:

cd my_new_program
make

This should create a binary executable my_new_program which you can copy onto the robot and execute there.

You can now edit the file main.c and implement your algorithm there. In case you have a complex algorithm to implement, you can of course create additional *.c and *.h files. All *.c in the project folder will automatically be passed to the compiler when launching make.

Miscellaneous[edit]

Renaming a Program[edit]

The name of the binary executable can be changed in the Makefile by modifying the line

TARGET := my_new_program

Note that by convention, the executable should always have the same name as the project folder. (The script k3put relies on that when specifying a program folder.)

Including Modules[edit]

The Makefile also contains a list of modules that must be compiled with your program. Most programs will use the following modules:

MODULES := khepera3 commandline i2cal

If your program needs another modules (e.g. measurement), simply add these modules to that line.

Including the libkorebot Library[edit]

Even though the Khepera III Toolbox does not need the libkorebot library (and implements nearly its whole functionality), you may have code that needs to be compiled against that library. To do that, add the following lines to your Makefile:

LIBKOREBOT := /path/to/libkorebot-1.10/build-korebot/
INCS_ADD   := -I $(LIBKOREBOT)/include 
LIBS_ADD   := -L $(LIBKOREBOT)/lib -lkorebot

Note that the compiled programs will only work if the library libkorebot.so is available on the robot.

Programming Style[edit]

Programming style is an art rather than an exact science. The Khepera III Toolbox uses a rather compact programming style, but does not follow any specific convention. For your own programs, you are of course free to stick to your own coding style.

There are tools available to automatically format your source code conforming to some rules. One of these tools is Artistic Style, a very easy to use open source program. After installing this program, the following command applies minimal formatting (even spacing between the operators and indentation using tabs):

astyle -p --indent=tab main.c

A copy of the original file will be saved as main.c.orig.

SVN / Versioning System Issues[edit]

We strongly suggest using a versioning system such as SVN (Subversion) to store your Khepera III programs. SVN is probably the most prominent system today, and easy to set up and use.

If you are using a versioning system, you should check in the following files of your program:

  • All source files (*.c, *.h)
  • The Makefile
  • The compiled program (such that a compiled copy is available for those who don't have a compiler set up)
  • Any additional files that the program needs

You should not check in:

  • Intermediate compilation files (*.o, *.d)