# Cyberbotics' Robot Curriculum/Intermediate programming Exercises

In the previous chapter, you learned to manipulate the e-puck devices in detail by using the C programming. You have now the technical background to start this new chapter about the robotic behavior. Different techniques will be used to program the e-puck behavior. Notably, you will see that your robot has a memory.

## Program an Automaton [Intermediate]

During this exercise, you will convert an FSM of the chapter Beginner programming Exercises in the C programming. First, a simple example will be given, a new version of the exercise Simple Behavior: Finite State Machine. Then, you will create your own FSM. It will be a new version of the exercise Better Collision avoidance Algorithm.

### Open the World File

Open the following world file:

```.../worlds/intermediate_finite_state_machine.wbt
```

You should observe the same world as in the section Simple Behavior: Finite State Machine.

### An Example of FSM

According to the figure Sensors_to_actuators_loop.png, the main loop of your program should always execute its instructions in the following order:

1. Obtain the sensor values
2. Treat these values according to the expected behavior
3. Sends commands to the actuators

In our case, the behavioral part of the robot is modeled using an FSM.

[Q.1] Locate in the given C programming code where are these 3 points. Explain what operations are performed in each of these points.

[Q.2] Describe how the FSM is implemented, i.e., how the current state is modeled, which control structure is used, what kind of instructions are performed in the states, what are the conditions to switch from a state to another, etc.

[P.1] By using an FSM, implement a wall following algorithm. (Hint: start to design the structure of the automaton and then find how the transitions are fired and finally set up parameters empirically.)

## *Lawn mower* [Intermediate]

In this optional exercise, you will discover another topic of the robotic: the exhaustive research, i.e., your e-puck will move on a surface having an unknown shape, and will have to pass by every place. A cleaning robot or an automatic lawn mower is a typical application of this kind of movement. There are different ways to treat this topic. This exercise presents two of them: the random walk and the walk ”by scanning”.

### The random Walking

Open the following world file:

```.../worlds/intermediate_lawn_mower.wbt
```

You should see a grassy board with a white hedge. For the moment, the robot controller is the same as this of the previous exercise, i.e., a simple FSM which lets return your e-puck when it meets a wall. In this subsection, you will create a random walk.

When your e-puck meets a wall, it must spin on itself and go in another direction as depicted in figure below. The next figure depict a possible automaton for a random walk.

For generating a random integer between 0 and X, import the standard library (#include <stdlib.h>) and the time library (#include

```// The utility of this line is to have at every simulation a different
// series of random number
srand(time(0));

// change X by the maximal bound (in the double format)
int random_value = X * rand() / (RAND_MAX+1);
```

## Behavioral Modules [Intermediate]

### Open the World File

```.../worlds/intermediate_oam.wbt
```

## Create a line following Module [Intermediate]

### Open the World File

```.../worlds/intermediate_lfm.wbt
```

### Three modules for a Wall Following

```.../worlds/intermediate_behavior_based.wbt