# Khepera III Toolbox/Writing your Own Programs

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
### Linux for Korebot

First of all, make sure that make is available on your computer by typing
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/
Add the bin directory to your PATH environment variable, preferably by adding the following line to your .bashrc file:
and restarting your bash shell (terminal).
At this point, your computer should not complain any more if you just type

### Linux for Korebot 2

First of all, make sure that make is available on your computer by typing
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.
Add the bin directory to your PATH environment variable, preferably by adding the following line to your .bashrc file:
and restarting your bash shell (terminal).
At this point, your computer should not complain any more if you just type

### Mac OS X

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

To create a new program, type
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:
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
### Renaming a Program

The name of the binary executable can be changed in the Makefile by modifying the line
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

The Makefile also contains a list of modules that must be compiled with your program. Most programs will use the following modules:
If your program needs another modules (e.g. measurement), simply add these modules to that line.

### Including the libkorebot Library

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:
Note that the compiled programs will only work if the library libkorebot.so is available on the robot.

### Programming Style

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):
A copy of the original file will be saved as main.c.orig.

### SVN / Versioning System Issues

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)