![]() |
6.4 Programming the Khepera robot6.4.1 The controller programAmong the fields of a DifferentialWheels node, you may have notived the controller field. This field defines an executable program that will control the robot. By default executable programs are searched in the Webots controllers directory, but you can define another location in the Preferences Files and paths tab, under the User path: label. This path define a directory in webots will look for a worlds and a controllers directory. The controllers directory should contain subdirectories named after the names of the controllers (i.e., khepera in our case). This khepera directory should contain an executable file named khepera.exe on Windows or khepera on Linux. Moreover, along with the executable file, you will also find sources files and possibly makefiles or project files used to build the executable from the sources. 6.4.2 Looking at the source codeThe source code of the example controller is located in the following file under the Webots directory: controllers/khepera/khepera.c It contains the following code:
This program is made up of two functions: a main function, as in any C program and function named reset which is a callback function used for getting references to the sensors of the robot. A number of includes are necessary to use the different devices of the robot, including the differential wheels basis itself. The main function starts up by initializing the library by calling the khepera_live function, passing as an argument a pointer to the reset function declared earlier. This reset function will be called each time it is necessary to read or reread the references to the devices, called device tags. The device tag names, like "ds1", "ds2", etc. refer to the name fields you can see in the scene tree window for each device. The reset function will be called the first time from the khepera_live function. So, from there, you can assume that the device tag values have been assigned. Then, it is necessary to enable the sensor measurements we will need. The second parameter of the enable functions specifies the interval between updates for the sensor in millisecond. That is, in this example, all sensor measurements will be performed each 64 ms. Finally, then main function enters an endless loop in which the sensor values are read, the motor speeds are computed according to the sensor values and assigned to the motors, and the encoders are read and sometimes reset (although this make no special sense in this example). Please note the robot_step function at the end of the loop which takes a number of milliseconds as an argument. This function tells the simulator to run the simulation for the specified amount of time. It is necessary to include this function call, otherwise, the simulation may get frozen. 6.4.3 Compiling the controllerTo compile this source code and obtain an executable file, a different procedure is necesseray depending on your development environment. On Linux, simply go to the controller directory where the khepera.c resides, and type make. On Windows, you may do exactly the same if you are working with cygwin. If you use Dev-C++ or Microsoft Visual C++, you will need to create a project file and compile your program from your Integrated Development Environment. Template project files for both Dev-C++ and Visual C++ are available in the braiten controller directory. Once compiled, reload the world in Webots using the Revert button (or relaunch Webots) and you will see your freshly compiled run in Webots. ![]() ![]() ![]() ^ page top ^ |
E-mail to webmaster | Last updated: | Copyright © 2002 Cyberbotics Ltd. |