5.12 Programming Webots Controllers with Python5.12.1 Introduction to PythonWhat is Python ?Python is a very high-level programming language. This language has been developped by Python Software Foundation since 1991. Python is interpreted, so the compilation phase doesn't exist. It is also a functional, object-oriented and imperative programming language. You can get more information on its official web site. Python is more and more used in robotics. For example, there exists some libraries such as pyro. Pyro allows to deal quickly with the standard robotic problems such as computer vision, neural networks or evolutionnary algorithms. On the other hand, Python fits well the Webots policy to create robotic behavior simply and quickly without necessary dealing with low-level implementation details. How to run the ExamplesOnce Python is correctly installed, you can directly launch one of the Python examples. For doing that, run the Webots worlds located by default in the projects / packages / python / worlds subdirectory of your Webots directory. How to use the Python API, the controller moduleThe Python API is comparable to the C++ one. Indeed, the class hierarchy, the class names and the function names are identical. The differences comes only from the prototypes of some functions and from the addition of some new ones. Refer to the reference manual for knowing in details these differences. For using the Python API, you just need to import the module called controller by the following instruction:
This module contains all the classes you need for running your controller. Refer to the examples for knowing how to use it. 5.12.2 Installation of PythonHow to check your Python's VersionWebots supports Python since its 2.5 version. So, an older version can produce problems. You can check the Python version by executing python --version in your favorite command prompt. This command should display the Python version. If Python is not installed (or not correctly installed), this command should inform you that python is not found. For Macintosh or for Linux UsersMost of the Linux distribution have Python already installed. Same case for the Macintoshs. However, we recommend you to check your Python version and to upgrade Python if its version is under 2.5. More information on the Python official web site. For Windows UsersFor the Windows users, you need to install Python on your computer by getting and executing the right graphical installer which can be get on the Python official web site. After the installation, you will need to set or change your PATH environment variable so that Webots is able to access the Python command. The PATH variable must be set using the Environment Variables dialog: On Windows XP this dialog can be opened like this: Choose Start, Settings, Control Panel, and double-click System . Select the Advanced tab and then Environment Variables. On Windows Vista the dialog can be opened like this: Choose Start, Computer, System Properties, Advanced system settings, Advanced tab and then Environment Variables. In the dialog, in the User variables for ... section, look for a variable named PATH. Add the bin path of Python to the right end of PATH variables. If the PATH variable does not exist you should create it. A typical value for PATH is:
Where XX stands for the version of Python. Then, you need to restart Webots so that the change is taken into account. 5.12.3 Python, C++ and SWIGIntroductionThe Python API is generated almost automatically over the C++ API by using SWIG. Modification of the Python APIIf you need to change something in the Python API, you need first to get SWIG (available on the SWIG official web site). In the projects / packages / python / src subdirectory of your Webots directory, the controller.i file is the SWIG interface which maps the C++ headers with the Python controller module, the Makefile file is used for generating the controller module (by executing make in your favorite command prompt). This generates the files of the lib / python directory (the controller module). Note also that this API is closely related to the C++ one. In most cases, you need first to modify and compile the C++ API before compiling this one. ![]() ^ page top ^ |
| E-mail to webmaster | Last updated: | Copyright © 2008 Cyberbotics Ltd. |