This page highlights some of the relevant content from the Webots User Guide with respect to programming Webot controllers in Java.  All content is taken from the Webot’s User Guide, retrieved 8/23/11 from http://www.cyberbotics.com/documentation/

 

2.1.6 What is a controller ?

A controller is a computer program that controls a robot specified in a world file. Controllers can be written in any of the programming languages supported by Webots: C, C++, Java, URBI, Python or MATLABTM . When a simulation starts, Webots launches the specified controllers, each as a separate process, and it associates the controller processes with the simulated robots. Note that several robots can use the same controller code, however a distinct process will be launched for each robot.

Some programming languages need to be compiled (C and C++) other languages need to be interpreted (URBI, Python and MATLABTM ) and some need to be both compiled and interpreted (Java). For example, C and C++ controllers are compiled to platform-dependent binary executables (for example .exe under Windows). URBI, Python and MATLABTM controllers are interpreted by the corresponding run-time systems (which must be installed). Java controller need to be compiled to byte code (.class files or .jar) and then interpreted by a Java Virtual Machine.

The source files and binary files of each controller are stored together in a controller directory. A controller directory is placed in the controllers subdirectory of each Webots project.

2.3.8 Wizard Menu

The Wizard menu makes it easier to create new projects and new controllers.

The New Project Directory… menu item first prompts you to choose a filesystem location and then it creates a project directory. A project directory contains several subdirectories that are used to store the files related to a particular Webots project, i.e. world files, controller files, data files, plugins, etc. Webots remembers the current project directory and automatically opens and saves any type of file from the corresponding subdirectory of the current project directory.

The New Robot Controller… menu item allows you to create a new controller program. You will first be prompted to choose between a C, C++, Java, Python or MATLABTM controller. Then, Webots will ask you to enter the name of your controller and finally it will create all the necessary files (including a template source code file) in your current project directory.

4.5.1 Introduction to using Java

The Java API has been generated from the C++ API by using SWIG. That implies that their class hierarchy, their class names and their function names are almost identical. The Java API is currently composed of a set of about 25 classes having about 200 public functions located in the package called com.cyberbotics.webots.controller. The classes are either representations of a node of the scene tree (such as Robot, LED, etc.) or either utility classes (such as Motion, ImageRef, etc.). A complete description of these functions can be found in the reference guide while the instructions about the common way to program a Java controller can be found in the chapter 6.

5.1 Webots Built-in Editor

Webots source code editor is a multi-tab text editor specially adapted for developing Webots controllers. It is usually recommended to use this editor as it makes the compilation straightforward. The editor features syntax highlighting for Webots supported language (C/C++, Java, URBI, Python and MATLABTM ) and auto-completion for Webots C API.

5.1.1 Compiling with the Source Code Editor

The Source Code Editor can be used to compile C/C++ or Java source files into binary executable or bytecode (Java) files that can be executed in a simulation. The compilation output is printed to Webots console; errors and warnings appear in red. If you double-click an error message, Webots will highlight the corresponding source line in the editor. Note that, for compiling source code it is necessary to have the appropriate development tools installed. You will find information on the development tools here.

The Compile button launches the compilation of the currently selected source file. Only the current file is compiled, not the whole project. Webots invokes gcc, g++ or javac depending on the extension of currently selected source file.

With Java, the Build button compiles the whole project into bytecode (.class files).

The Clean button invokes make clean to delete the intermediate compilation files in the current file’s directory. The source files remain untouched.

The Make JAR file menu rebuilds the whole project and packs all the .class in a .jar. This is a convenience function that can be used to pack a complete controller prior to uploading it to one of our online contest website.

5.2.3 The ”controllers” Directory

This directory contains the controllers. Each controller is defined in a directory. A controller is referenced by the name of the directory. Here is an example of the controllers directory having one simple controller written in C which can be edited and executed.

controllers/
controllers/simple_controller/
controllers/simple_controller/Makefile
controllers/simple_controller/simple_controller.c
controllers/simple_controller/simple_controller[.exe]

** Note that the main executable name must be identical to the directory name.

Naming Conventions

The naming convention of the C++/Java/Python classes and methods directly matches the C API function names. For example, for this C function: double wb distance sensor get value(WbDeviceTag tag) there will be a matching C++/Java/Python method called get Value() located in a class called DistanceSensor. Usually the C++/Java/Python methods have the same parameters as their C API counterparts, but without the WbDeviceTag parameter.

6.4.2 Controller Class

The C++/Java/Python controller implementation should be placed in a user-defined class derived from one of the Webots class: Robot, DifferentialWheels or Supervisor. It is important that the controller class is derived from the same class as that used in Scene Tree, otherwise some methods may not be available or may not work. For example, if in the Scene Tree a robot is of type DifferentialWheels, then the corresponding C++/Java/Python controller class must extend the DifferentialWheels class. If in the Scene Tree a robot is of type Supervisor, then the C++/Java/Python controller class must be derived from the Supervisor class, etc.

Both DifferentialWheels and Supervisor are subclasses of the Robot class. Hence it is possible to call the Robot’s methods, such as, e.g., step() or getLED(), from the DifferentialWheels and Supervisor controllers. But it is not possible to call the Supervisor methods from a DifferentialWheels controller, and vice versa. For example it won’t be possible to call simulationRevert() from a DifferentialWheels controller.

Generally, the user-defined controller class should have a run() function that implements the main controller loop. That loop should contains a call to the Robot’s step() method. Then the only responsibility of the controller’s main() function is to create an instance of the user-defined controller class, call its run() method and finally delete (C++ only) the instance: see examples below. Note that the controller should never create more than one instance of a derived class, otherwise the results are undefined. Note that unlike the C API, the C++/Java/Python APIs don’t have (and don’t need) functions like wb robot init() and wb robot cleanup(). The necessary initialization and cleanup routines are automatically invoked from the constructor and destructor of the base class. In C++/Java/Python, each Webots device is implemented as a separate class, there is a DistanceSensor class, a TouchSensor class, a Servo class, etc. The various devices instances can be obtained with dedicated methods of the Robot class, like getDistanceSensor(), getTouchSensor()), etc. There is no WbDeviceTag in C++/Java/Python.

6.4.4 Java Example

import com.cyberbotics.webots.controller.*;
public class MyRobot extends Robot {
private LED led;
private DistanceSensor distanceSensor;
private static final int TIME_STEP = 32; // milliseconds
public MyRobot() {
super();
led = getLED("my_led");
distanceSensor = getDistanceSensor("my_distance_sensor");
distanceSensor.enable(TIME_STEP);
}
public void run() {
// main control loop
while (step(TIME_STEP) != -1) {
// Read the sensors, like:
double val = distanceSensor.getValue();
// Process sensor data here
// Enter here functions to send actuator commands, like:
led.set(1);
}
// Enter here exit cleanup code
}
public static void main(String[] args) {
MyRobot robot = new MyRobot();
robot.run();
}
}
** For a description of Programming Fundamentals including examples in C, refer to http://www.cyberbotics.com/guide/section6.1.php

** For complete API information on the Java library in Webots consult the Reference Guide, http://www.cyberbotics.com/reference/section10.2.php

Exercises

Create a new directory for your project, call it lab2, and then open Webots and select “Your Project”. Use the wizard to create a New Project Directory and point it to the lab2 directory you just made. In the world window, open the my_bot1.wbt world file from the first lab and resave it in the worlds folder of the lab2 directory. This will give you a basic environment to play with. Before you introduce the e-puck, you will have to add physics to the floor or the e-puck will just fall right through.

Select the floor node which should be the first Transform node in the scene tree, just after the PointLight nodes. Turn that Transform into a solid node using the Transform button. Now it is possible to define a bounding object for the floor. Create a Transform node under the boundingObject field in the floor node. Under the children of the Transform you just created under the boundingObject create a Box node and set the size to [1 0.02 1]. Set the translation of the boundingObject Transform to [0.5 -0.01 0.5].

Now we can introduce the e-puck. Select the last solid from the list (PINK_BOX) and click Add New proto and choose e-puck. This will give you an e-puck to work with. Use the Wizard to create a New Robot Controller for the e-puck in the language of your choosing (I can help you most with Java). The source file should appear in the right-most window of Webots. Use what you learned above to write some code for your robot to follow. See if you can get it to move around, light up, or present other interesting behavior.

Notes:

  • Remember you will need to select your controller for the e-puck in the Scene Tree.
  • You can examine the design of the e-puck by looking through the EPuck.proto file found under /usr/local/webots/resources/protos/robots/epuck . You will probably need to examine this file to find the names of the sensors.
  • Notice while looking at the EPuck.proto file that the e-puck is a DifferentialWheels robot.
 

One Response to Intro to Controllers

  1. […] Intro to Controllers – comprehensive overview of the Webots guide to start programming controller in Java; […]

Leave a Reply

Set your Twitter account name in your settings to use the TwitterBar Section.