|  | |
4.1 My first world: kiki.wbt
As a first introduction, we are going to simulate a very simple robot made
up of a box, two wheels and two infra-red sensors (see
figure 4.1), controlled by a program inspired by a
Braitenberg algorithm, in a simple environment surrounded by a wall.
 Figure 4.1: The kiki robot
4.1.1 Environment
We just want to have a simple world with a surrounding wall. We will
represent this wall using an Extrusion
node in the tree editor. The coordinates of the wall are shown in
figure 4.2.
 Figure 4.2: The kiki world
First, go to the File menu,
New item to open a new world. Then open the tree
editor (in the Edit menu). We are going to change the
lighting of the scene:
-
Select the PointLight node, and click
on the + just in front of it. You can now see the different fields of
the PointLight node. Select
ambientIntensity and enter 0.5 as a
value, then select intensity and enter
0.8, then select location and enter
0.5 0.5 0.5 as values. Press return.
-
Select the PointLight node, copy and
paste it. In this new PointLight node,
type -0.5 0.5 0.5 in the location field.
-
Repeat this copy/paste twice again with -0.5 0.5 -0.5 in the
location field of the third
PointLight node, and 0.5 0.5 -0.5 in the
location field of the fourth and last
PointLight node.
-
The scene is now better lit.
Secondly, let us create the wall:
-
Select the last Solid node and
click on the insert after button.
-
Choose a Solid node.
-
Fill in the text fields with your desired text, e.g., "wall" for the
name.
-
Select the children field and
Insert after a
Shape node.
-
Insert a new
Appearance node in the
appearance field.
Insert a new
Material node in the
material field of the
Appearance node. Select the
diffuseColor field of the
Material node and choose a color to
define the color of the wall.
-
Now insert an Extrusion node in the
geometry field of the
Shape.
-
Set the wall corner coordinates in the
crossSection field and set
convex to
FALSE.
-
In the spine field, write that the
wall ranges between 0 and 0.1 along the y axis.
-
As we want our robot to detect the wall, we have to fill in the
boundingObject field. The bounding object
can have exactly the same shape as the wall, so
insert a DEF at the level of
geometry Extrusion: WALL. Then, in the
boundingObject field,
insert USE WALL.
-
Close the tree editor, save your file and look at the result.
The wall in the tree editor is represented in figure 4.3,
while the same wall in the world editor is visible in
figure 4.4
 Figure 4.3: The wall in the tree editor
 Figure 4.4: The wall in the world window
4.1.2 Robot
The aim of this subsection is to model the kiki robot.
This robot is made up of a
DifferentialWheels node, in which we find
several children: a Transform node for the
body, two Solid nodes for the wheels, two
DistanceSensor nodes for the infra-red
sensors and a Shape node with a texture.
The origin and the axis of the coordinate system of the robot and its
dimensions are shown in figure 4.5.
 Figure 4.5:
Coordinate system and dimensions of the kiki robot
To model the body of the robot:
-
Open the tree editor.
-
Select the last Solid node.
-
Insert after a
DifferentialWheels node, give it a
name: "kiki".
-
In the children field, first introduce a
Transform node that will contain a box
shape. In the new children field,
insert after a
Shape node. Choose a color, as described
previously. In the geometry field,
insert a Box
node. Set the dimension of the box to [0.08 0.08 0.08]. Now set the
translation values to [ 0 0.06 0 ] in
the Transform node (see
figure 4.6)
 Figure 4.6:
Body of the kiki robot: a box
To model the left wheel of the robot:
-
Select the previous Transform and
insert after a
Solid node in order to model the left
wheel. Type "left wheel" in the name field, so that this
Solid node is recognized as the left
wheel of the robot and will rotate according to the motor command.
-
The axis of rotation of the wheel is x. Moreover, a
wheel is made of a Cylinder rotated of
pi/2 radians around the z axis.
To obtain proper movement of the wheel, you must pay attention not to
confuse these two rotations. consequently, you must add a
Transform node to the children of the
Solid node.
-
After adding this Transform node,
introduce a Shape with a
Cylinder in its
geometry field. The dimensions of the
cylinder are 0.01 for the height and
0.025 for the radius. Set the
rotation to [ 0 0 1 1.57 ]. Pay
attention to the sign of the rotation; if it is false, the wheel will
turn in the wrong direction.
-
In the Solid node, set the translation
to [-0.045 0.025 0] to position the left wheel, and set the rotation of
the wheel around the x axis: [1 0 0 0].
-
Give a DEF name to your
Transform: WHEEL; notice that you
positionned the wheel in translation at the level of the
Solid node, so that you can reuse the
WHEEL Transform for the right wheel.
-
Close the tree window, look at the world and save it. Use the navigation
buttons to change the point of view.
To model the right wheel of the robot:
-
Select the left wheel Solid node and
insert after another
Solid node. Type "right wheel" in the
name field. Set the translation to [0.045 0.025 0] and the rotation to
[1 0 0 0].
-
In the children,
insert after
USE WHEEL.
Press Return, close the tree window and save the file.
You can examine your robot in the world editor, move it and zoom in on
it.
The robot and its two wheels are shown in figure 4.7 and
figure 4.8.
 Figure 4.7:
Wheels of the kiki robot
 Figure 4.8:
Body and wheels of the kiki robot
The two infra-red sensors are defined as two cylinders on the front of the
robot body. Their diameter is 0.016 m and their height is 0.004 m. You must
position these sensors properly so that the sensor rays point in the right
direction, towards the front of the robot.
-
In the chidren of the
DifferentialWheels node,
insert after a
DistanceSensor node.
-
Type the name "ir0". It will be used by the controller program.
-
Now, we will attach a cylinder shape to this sensor. In the
chidren of the
DistanceSensor node,
insert after a
Tranform node. Give a
DEF name to it: INFRARED, which you will
use for the second IR sensor.
-
In the chidren of the
Tranform node,
insert after a
Shape node. Choose an appearance and
insert a Cylinder
in the geometry field. Type 0.004 for
the height and 0.08 for the radius.
-
Set the rotation for the Tranform node
to [0 0 1 1.57] to adjust the orientation of the cylinder.
-
In the DistanceSensor node, set the
translation to position the sensor and its ray: [0.02 0.08 -0.042].
In the File menu,
Preferences, Rendering,
check the Display sensor rays box. In order to have
the ray directed towards the front of the robot, you must set the
rotation to [0 1 0 1.57].
-
In the DistanceSensor node, you must
introduce some values of distance measurements of the sensors to the
lookupTable field, according to
figure 4.9. These values are:
lookupTable [ 0 1024 0,
0.05 1024 0,
0.15 0 0 ]
|
 Figure 4.9:
Distance measurements of the kiki sensors.
-
To model the second IR sensor, select the
DistanceSensor node and
insert after a new
DistanceSensor node. Type "ir1" as a
name. Set its translation to [-0.02 0.08 -0.042] and its rotation to
[0 1 0 1.57]. In the children,
insert after USE
INFRARED. In the
lookupTable field, type the same values
as shown above.
The robot and its two sensors are shown in figure 4.10
and figure 4.11.
 Figure 4.10:
The DistanceSensor nodes of the kiki robot
 Figure 4.11:
The kiki robot and its sensors
Note:
a texture can only be mapped on an IndexedFaceSet shape. The
texCoord and
texCoordIndex entries must be filled.
The image used as a texture must be a .png or a
.jpg file, and its size must be
(2^n) * (2^n) pixels (for example 8x8, 16x16, 32x32,
64x64, 128x128 or 256x256 pixels). Transparent images are not allowed in
Webots. Moreover, PNG images should use either the 24 or 32 bit per pixel
mode (lower bpp or gray levels are not
supported). Beware of the maximum size of texture images depending on the
3D graphics board you have: some old 3D graphics boards are limited to
256x256 texture images while more powerful ones will accept 2048x2048
texture images.
To paste a texture on the face of the robot:
-
Select the last DistanceSensor node and
insert after a
Shape node.
-
In the appearance field,
insert an
Appearance node. In the
texture field of this node,
insert an
ImageTexture node with the following
URL: "kiki/kiki.png".
-
In the geometry field,
insert an
IndexedFaceSet node, with a
Coordinate node in the
coord field. Type the coordinates of
the points in the point field.
[ 0.015 0.05 -0.041,
0.015 0.03 -0.041,
-0.015 0.03 -0.041,
-0.015 0.05 -0.041 ]
|
and insert after the
coordIndex field the values 0, 1, 2, 3,
-1.
-
In the texCoord field,
insert a
TexureCoordinate node. In the
point field, enter the coordinates of
the texture:
and in the texCoordIndex field, type 3,
0, 1, 2.
-
The texture values are shown in figure 4.12.
 Figure 4.12: Defining the texture of the kiki robot
To finish with the DifferentialWheels
node, you must fill in a few more fields:
-
In the controller field, type the name
"simple". It will be used by the controller program.
-
The boundingObject field can contain a
Transform node with a
Box, as a box as a bounding object for
collision detection is sufficient to bound the kiki
robot. Insert a
Transform node in the
boundingObject field, with the
translation set to [0 0.05 -0.002] and
a Box node in its
children. Set the dimension of the
Box to [0.1 0.1 0.084].
-
In the axleLength field, enter the
length of the axle between the two wheels: 0.09 (according to
figure 4.5).
-
In the wheelRadius field, enter the
radius of the wheels: 0.025.
-
Values for other fields are shown in figure 4.13 and
the finished robot in its world is shown in
figure 4.14.
 Figure 4.13: The other fields of the DifferentialWheels node
 Figure 4.14: The kiki robot in its world
The kiki.wbt is included in the Webots distribution,
in the worlds directory.
4.1.3 A simple controller
This first controller is very simple and thus named
simple. The controller program simply reads the sensor
values and sets the two motors speeds, in such a way that
kiki avoids the obstacles.
Below is the source code for the simple.c controller:
#include <robot.h>
#include <differential_wheels.h>
#include <distance_sensor.h>
#define SPEED 100
static DeviceTag ir0,ir1;
void reset(void) {
ir0 = robot_get_device("ir0");
ir1 = robot_get_device("ir1");
// g_print("ir0=%d ir1=%d\n",ir0,ir1);
}
int main() {
gint16 left_speed,right_speed;
guint16 ir0_value,ir1_value;
robot_live(reset);
distance_sensor_enable(ir0,64);
distance_sensor_enable(ir1,64);
for(;;) { /* The robot never dies! */
ir0_value = distance_sensor_get_value(ir0);
ir1_value = distance_sensor_get_value(ir1);
if (ir1_value>200) {
left_speed = -20;
right_speed = 20;
}
else if (ir0_value>200) {
left_speed = 20;
right_speed = -20;
}
else {
left_speed =SPEED;
right_speed=SPEED;
}
/* Set the motor speeds */
differential_wheels_set_speed(left_speed,right_speed);
robot_step(64); /* run one step */
}
return 0;
|

^ page top ^
|