# Cyberbotics' Robot Curriculum/Beginner programming Exercises

This chapter is composed of a series of exercises for beginners. You don't need prior knowledge to go through these exercises. The aim is to learn the basics of mobile robotics by manipulating both your e-puck and Webots. First, you will discover some e-puck devices and their utility. Then, you will acquire the concept of a robot controller. And finally, you will program a simple robot behavior by using a Webots module: BotStudio. This module enables to program an e-puck robot using a graphical interface. You will discover how to use it and what are the notions related to it.

## Discovery of the e-puck [Beginner]

As explained in the chapter E-puck and Webots, an e-puck has different devices. Through this document, you will use some of them: the stepper motors, the LEDs, the accelerometer, the infrared sensors and the camera. In this exercise, you will discover the utility of each of them. The following list gives you a quick definition of these devices. You will see in the next chapter all these devices in more details.
* Stepper motor: A stepper motor is an electrical motor which breaks up a full rotation into a large number of steps. An e-puck possesses two stepper motors of 1000 steps. They can achieve a speed of about one rotation per second. The wheels of the e-puck are fixed to these motors. They are used to move the robot. They can move independently. Moreover, for knowing the position of the wheels, an incremental encoder can be used. The e-puck encoder returns the number of steps since the last reset of the encoder. For example, this ”device” can be used for turning the wheel of one turn precisely.
* LED: A LED (Light-Emitting Diode) is a small device which can emit light by using few energy. An e-puck possesses several LEDs. Notably, 8 around it, 4 in the e-puck body and 1 in front of it. The front LED is more powerful than the others. The aim of these LEDs is mainly to have a feedback on the state of the robot. They can also be used for illuminating the environment.
* Accelerometer: An accelerometer is a device which measures the total force applied on it as a 3D vector. An e-puck has a single accelerometer. If your e-puck is at rest, the accelerometer indicates at least the gravitational vector. The accelerometer can be used for detecting a collision with a wall or for detecting the fall of the robot.
* Infrared (IR) sensor: An e-puck possesses 8 infrared (IR) sensors. An IR sensor is a device which can produce an infrared light (a light which is out the range of the visible light) and which can measure the amount of the received light. It has two kind of use. First, only the received light is measured. In this configuration, the IR sensor measures the light of the nearby environment. The e-puck can detect for example from where a light illuminates it. Second, the IR sensor emits infrared light and measures the received light. If there is an obstacle in front of the IR sensor, the light will bounce on it. The light difference is bigger. So, the e-puck can estimate the distance between its IR sensors and an obstacle.
* Camera: In front of the e-puck, there is also a VGA camera. The e-puck uses it to discover its direct front environment. It can for example follow a line, detect a blob, recognize objects, etc.
Note that the stepper motors and the LEDs are actuators. This device have an effect on the environment. To the contrary, the IR sensors and the camera are sensors. They measure specific information of the environment. On the following page you can see photos of the mechanical design.
To successfully go through the following exercises, you have to know the existence of other devices. The e-puck is alimented with a Li-ION battery. It has a running life of about 3 hours. You can switch on or off your e-puck with the ON/OFF switch which is located near the right wheel. The robot has also a Bluetooth interface which allows a communication with your computer or with other e-pucks.
Finally, the e-puck has other devices (like the microphones and the speaker) that you will not use in this document because the current version of Webots doesn't support them yet.
[Q.1] What is the maximal speed of an e-puck? Give your answer in cm/s. (Hint: The wheel radius is about 2.1 cm. Look at the definition of a stepper motor above.)
[Q.2] Compare your e-puck with an actual mobile phone. Which e-puck devices were influenced by this industry?
[Q.3] Sort the following devices either into the actuator category or into the sensor category: a LED, a stepper motor, an IR sensor, a camera, a microphone, an accelerometer and a speaker.
[P.1] Find where these devices are located on your real e-puck. (Hint: look at the figure Epuck devices.png)

## Robot Controller [Beginner]

In order to understand the concept of a robot controller you will play the role of the robot controller. You will perceive the sensory information coming from the sensors of the robot and you will be able to control the actuators of the robot. In this exercise, you will not actually program the behavior of the robot, but you will nevertheless control the robot.

### Open the World File

First of all, you need to open the world file of this exercise. A world file contains the entire environment of the simulation, i.e., the robot shape, the ground shape, the obstacles shape and some general information like the position of the camera and even the direction of gravitational vector. In the simulation window (window (1) in figure below), click on File | Open menu and open:
You can also open the world file by clicking on the open button on the tool box of the simulation window. The e-puck model and its environment are loaded in Webots. In the simulation window, you can see an e-puck on a green board.

### The Webots Windows and the simulation Camera

Webots can display several windows. Some of them were already introduced. You will focus especially on two of them (which are depicted on a figure):
* The simulation window (1): This window is probably the most important one. It shows a 3D representation of the simulation. In our case, you can see a virtual e-puck and its virtual environment. If you want to modify the camera orientation, just click and drag with the left button of the mouse where you want in the panel. Similarly you can modify the position of the camera by using the right button (Note for the Mac OS X users : if you have a mouse with a single button, hold down the Ctrl key and click for emulating the right click.). Finally you can also set the zoom by moving the mouse wheel. There are also two important buttons in this window: the play/stop button and the revert button. With the first one, the simulation can be either played or stopped, and with the second one, the entire simulation can be reset.
* The robot window (2): This window shows a 2D representation of the e-puck. The purpose of this window is to visualize the sensor values and the actuators values in real-time during a simulation. The figure with the robot window shows the meaning of the values that can be seen. The red integers correspond to the speed of the motors. They should be initially null. The green values below correspond to the encoders. The light measured by the IR sensor is represented by the green integers. While the distance between a IR sensor and an obstacle is represented by blue integers. So, note that the green and the blue values represent the same device. The red or black rectangles correspond to the LEDs which are respectively switched on or off. Finally, the accelerometer is represented both by a 2D vector which corresponds to the inclination of the e-puck, and by a slider which represents the norm of the acceleration. This window contains also a drop-down menu to configure the Bluetooth connection.
[P.1] By using the camera, identify where is the front and the back of your virtual e-puck. (Hint: the camera is placed in the front of the e-puck)
[P.2] Try to place the camera of the simulation window on the e-puck roof in order to see in front of it. Then, use the revert button.

### The e-puck Movements

Check that the simulation is running by clicking on the start/stop button. Then, click on the virtual e-puck in order to select it. When your e-puck is selected, white lines appears. They represent the bounds of your object for the physical simulation. You also remark red lines. They represent the direction of the IR sensors. While the magenta lines correspond to the field of view of the camera. Moreover, you can observe the camera values into a little window in the top left part of the simulation window.
On your keyboard, press the "S" key and the "X" key for respectively increasing or decreasing the speed value of the left motor. Try to press the "D" key and on the ”C” key for modifying the speed of the right motor. Now you can move the virtual robot like a remote control toy. Note that only one key can be pressed at the same time.
[P.3] Try to follow the black band around the board by using these four buttons.
[Q.1] Is it easy? What are the difficulties?
[Q.2] There are different kind of movements with an e-puck. Can you list them? (Ex: the e-puck can go forwards)
[Q.3] Try to use the keyboard arrows and the ”R” key. What is the utility of these commands? Explain the difference with the first ones. Are they more practical? Why?

### Blinded Movement [Challenge]

The aim of this subsection is to play the role of the robot controller. A robot controller perceives only the values measured by the robot sensors, treats them and sends some commands to the robot actuators as depicted in the figure. Note that the sensor values are modified by the environment, and that a robot can modify the environment with its actuators.
[P.4] Hide the simulation window (However, this window has to remain selected so that the keyboard strokes are working. A way to hide it is to move it partially off-screen) and just look at the sensor values. Try now to follow the wall as before only with the IR sensor information.
[Q.4] What information is useful? From which threshold value do you observe that a wall is close to the robot?

### Let's move your real Robot

You probably have a real e-puck in front of you and you would like to see it moving! Webots can communicate with an e-puck via a Bluetooth connection. It can receive some values from the e-puck sensors and send some values to command the e-puck actuators. So, Webots can play the role of the controller. This mode of operation is called a remote-control session.
In order to proceed, configure first your Bluetooth connection as explained in the section Bluetooth configuration. Stop the simulation with the start/stop button. Switch on your e-puck with the ON/OFF switch. Then, in the robot window, select your Bluetooth connection in the drop-down menu. Behind the e-puck, an orange LED should switch on. To finish press the start/stop button in order to run the program. Your e-puck should behave the same as in simulation.
[Q.5] Observe the sensor values from the real e-puck. Are they similar as the virtual ones?
[Q.6] Set the motor speeds to 10|10. When the real e-puck moves slowly, it vibrates. That does not occur in simulation. Could you explain this phenomenon?

### Your Progression

Congratulation! You finished the first exercise and stepped into the world of robotics. You already learned a lot:
* What is a sensor, an actuator and a robot controller.
* What kind of problems a robot controller must be able to solve.
* What are the basic devices of the e-puck. In particular, the stepper motors, the LEDs, the IR sensors, the accelerometer and the camera.
* How to run your mobile robot both in simulation and in reality and what is a remote-control session.
* How to perform some basic operations with Webots.

## Move your e-puck [Beginner]

You already learned what a robot controller is. In the following exercises you will create simple behaviors by using a graphical programming interface: BotStudio. This module is integrated in Webots. The aim of this exercise is to introduce BotStudio by discovering the e-puck's movement possibilities.

### Open the World File

Similarly to the first exercise, open the following world file:
Two windows are opened. The first one is the simulation window that you know. You should observe a similar world as before except that the size of the board is twice as big. This is because an e-puck needs room for moving. The second window is the BotStudio window (see figure). If the BotStudio window does not open automatically, double-click on the robot in the simulation window to open it.

### The "forward" State

A BotStudio window is composed of two main parts. The left part is a graphical representation of an automaton. You will learn to use this part and understand the automaton concept in the next exercise. The right part represents an e-puck in 2 dimensions. On this representation, you can observe the e-puck sensors values in real-time. Moreover, you can set the actuators commands. This interface has also a drop-down menu for choosing a Bluetooth connection in order to create a remote-control session. This menu is similar to the two drop-down menu of the robot window that you saw above. In top, there is a tool menu. This menu enables you to create, to load, to save or to modify an automaton. The last button (the upload button) executes your automaton on the e-puck.
In the BotStudio window, select the "forward" state (blue rectangle in the middle of the white area) just by clicking on it. A selected rectangle becomes yellow. In the right part of the BotStudio window, you can modify the actuator commands, i.e., the motors speed and the LEDs state. If you want to change the motors speed, click and drag the two yellow sliders. You can set this value between -100 and 100. 0 corresponds to a null speed, i.e., the wheel won't turn. A positive value should turn the wheel forward, and a negative one backwards. If you want to change the state of a LED, click on its corresponding gray circle (red -> on, black -> off, gray -> no modification).
Configure the "forward" state as follows: all the LEDs are alight, and the motors speeds are -30|30. Upload it on the virtual e-puck by clicking on the upload button. If the simulation is running, the virtual e-puck should change its actuators values accordingly. Note that when the simulation is launched, the right part of BotStudio displays the IR sensors.
[P.1] Set the actuators of your virtual e-puck in order to go forward, to go backwards, to follow a curve and to spin on itself.
[Q.1] For each of these moves, what are the links between the two speeds? (Example: forward : right_speed = left_speed and right_speed > 0 and left_speed > 0)
[Q.2] There are 17 LEDs on an e-puck. 9 red LEDs around the e-puck (the back LED is doubled), 1 front red LED, 4 intern green LEDs, 2 LEDs (green and red) for the power supply and 1 orange LED for the Bluetooth connection. Find where they are and with which button or operation you can switch them on. (Hint: some of them are not under your control and some of them are linked together, i.e. they cannot be switched on or off independently)

### The real e-puck's IR Sensors

The aim of this subsection is to create a remote-control session with your real e-puck. This part is similar to the subsection in previous exercise where you used the real robot. There are just two differences due to the fact that the BotStudio window is used instead of the robot window. For choosing the Bluetooth connection, there is just one drop-down menu instead of two. So, please select your Bluetooth connection instead of the simulation item on the top right part of the window. Then, you have to click on the upload button for starting the remote-control session.
[P.2] Set the actuators such that the e-puck doesn't move. Try this configuration on your real e-puck by creating a remote-control session. Put your hands around your real e-puck and observe the modifications of the IR sensor values in the BotStudio window.
[Q.3] What are the values of the front left IR sensor when there is an obstacle (example: a white piece of paper) at 1 cm ? At 3 cm ? At 5 cm ? At 10 cm ? Starting from which distance is it difficult to distinguish an obstacle from the noise?

## Simple Behavior: Finite State Machine (FSM) [Beginner]

In the precedent exercise, you learned to configure a single state. One cannot speak about behavior yet because your robot doesn't interact with its environment, i.e., it moves but it hasn't any reaction. The goal of this exercise is to create a simple behavior. You will discover what an automaton is, how it is related to the robot controller concept and how to construct an automaton using BotStudio.

### Finite State Automaton

A finite state automaton (FSM) is a model of behavior. It's a possible way to program a robot controller. It's composed of a finite number of states and of some transitions between them. In our case, the states correspond to a configuration of the robot actuators (the wheels speed and the LEDs state), while the transitions correspond to a condition over the sensor values (the IR sensors and the camera), i.e., under which condition the automaton can pass from one state to another. One state is particularly important: the initial state. It's the state from where the simulation begins. During this curriculum, an automaton will have the same signification as an FSM.
BotStudio enables you to create graphically an automaton. When an automaton is created, you can test it on your virtual or real e-puck. You will observe in the following exercises that this simple way to program enables to create a large range of behaviors.

### Open the World File and move Objects

Open the following world file:
This time, there are two obstacles. You can move an object (obstacle, e-puck or even walls) by selecting the desired object, and drag and drop it by pressing the shift key. The reverse button can be pressed when you want to reset the simulation.

### Creation of a Transition

In the BotStudio window, create two states with the new state button. Name the first state ”forward” and the second one ”stop” by using the text box at right. You can change the position of a state by dragging and dropping the corresponding rectangle. Change the motors speed of these states (forward state -> motors speed: 45|45, stop state -> motors speed: 0|0). Now, you will create your first transition. Click on the new transition button. Create a link from the ”forward” state to the ”stop” state (the direction is important!). In this transition, you can specify under which condition the automaton can pass from the ”forward” state to the ”stop” state. Select this transition by clicking on its text field. It becomes yellow. Rename it to ”front obstacle”. By dragging the two highest red sliders, change the conditions values over the front IR sensors to have ”>5” for each of them. You should obtain an automaton as this which is depicted in the figure called "First automaton". Select the initial state (the ”forward” state) and test this automaton on your virtual e-puck by clicking on the upload button.
[Q.1] What is the e-puck behavior?
[Q.2] In the ”forward” state, which actuator command is used? Which condition over the IR sensors values are tested in the ”front obstacle” transition?
[P.1] Execute the same automaton on the real e-puck.
You finished your first collision avoidance algorithm, i.e., your e-puck doesn't touch any wall. This kind of algorithm is a good alternative to collision detection algorithm because a collision can engender a robot's degradation. Of course it isn't perfect, you can think about a lot of situations where your robot would still touch something.

### U-turn

In this subsection, you will extend your automaton in order to perform a U-turn (a spin on itself of 180 degrees) after an obstacle's detection. Add a new state called ”U-turn” to your automaton. In this state, set the motors speed to 30|-30. Add a transition called ”timer1” from the ”stop” state to the ”U-turn” state. Select this transition. This time, don't change the conditions of the IR sensors but add a delay (1 [s]) to this condition by moving the yellow slider in the middle of the green circle. The figure called "The timer condition" depicts what you should obtain.
[P.2] Run the simulation (always with ”forward” state as initial state) both on the virtual and on the real e-puck.
For performing a perfect U-turn, you still have to stop the e-puck when it turned enough. Add a new timer (”timer2”) transition from ”U-turn” state to ”forward” state (see next figure).
[Q.3] With which delay for the ”timer2” transition does the robot perform a perfect U-turn? Is it the same for the real robot? Why?
[Q.4] You created an automaton which contains a loop. What are the advantage of this kind of structure?
[Q.5] Imagine the two following states: the ”forward” state and the ”stop” state. If you want to test front IR sensors values for passing from the ”forward” state to the ”stop” state, you will have two possibilities: either you create one transition in which you test the two front IR sensors values together or you create two transitions in which you test independently the two IR sensors values. What is the difference between these two solutions?
During this exercise you created an automaton step by step. But there are still several BotStudio tricks that are not mentioned above:
* For switching from ”bigger than” to ”smaller than” condition (or inversely) for an IR sensor, click on the gray part of an IR sensor slider.
* If you don't want to change the speed of a motor in a state, click on the yellow rectangle of the motor slider. The rectangle should disappear and the motors speed will keep its precedent value.
* If you want to remove a condition of a transition, set it to 0. The value should disappear.
* There is a slider which wasn't mentioned. This slider is related to the camera. You will learn more about this topic in exercise [sec:Line-following].

### Your Progression

Thanks to the two previous exercises, you learned:
* What is an FSM and how it's related with a robot behavior
* How to use BotStudio
* How to design a simple FSM
The following exercises will train you to create an FSM by yourself.

## Better Collision avoidance Algorithm [Beginner]

At this step, you know what an automaton is. Now, you will reinforce this knowledge through an exercise of parameters estimation. The structure of the automaton is given (the states and the transitions) but it doesn't contain any parameter, i.e., the actuators commands and the conditions over the sensors values aren't set. You will set these parameters empirically.

### Open the World File

Open the following world file:
You may have to increase the size of the BotStudio window to see the entire automaton.

### Collision Avoidance Automaton

Note that you can store your automaton by clicking on the save as button in the BotStudio window. You can also load it by clicking on the load button.
[P.1] Start with the given automaton (see the figure). There is only its structure, i.e., there are states and transitions but their parameters aren't set. Find the parameters of each state and each transition such that the e-puck avoids obstacles.
[P.2] Repeat the operation for the real e-puck.
[Q.1] Describe your method of research.

## The blinking e-puck [Beginner]

Until now, you just have modified existing automata. It is time for you to create them. In this practical exercise, you will design your own automaton by manipulating LEDs.

### Open the World File

Open the following world file:
Maybe you should increase the size of the BotStudio window for seeing the entire automaton. In the simulation window, if you don't see all the LEDs, you can move the camera around the robot by left clicking. For this exercise, working directly on the real e-puck is more convenient.

### Modify an Automaton

[Q.1] Without launching the automaton, describe the e-puck behavior. Verify your theory by running the simulation.
[P.1] Modify the current automaton in order to add the following behavior: the 8 LEDs will light on and switch off clockwise and they will stay on for 0.2 [s].
[P.2] Modify the current automaton in order to add the following behavior: when you cover up the real e-puck with your hands, the LEDs turn in the other direction. Use only 4 LEDs for this one (front, back, left and right).

### Create your own Automaton

A way to design an automaton is first to identify the possible actuators configurations, to create a state for each of these configurations, to set the parameters of these states, to establish the conditions to pass from a state to another, to create a transition for each of these conditions and finally to set the parameters of these conditions. Unfortunately it is not always so easy. For example, in an automaton, it's possible to have two states with identical actuators commands. Indeed, if you see somebody who is running across a street without any context, you don't know if he's running to catch a bus or if he's leaving a building in fire. He seems identical but it's internal state is different.
[P.3] Create a new automaton (press the new graph button in BotStudio). Choose only the four following LEDs: the front LED, the back one, the left one and the right one. The goal is to switch on the LED corresponding to the side of the obstacle. Note that if there are obstacles on two sides of the robot, two LEDs should be on ! Don't do the case with an obstacle on three or four sides.
[Q.2] If I proposed to repeat the exercise by using the 8 LEDs around the e-puck and with all the cases up to 8 obstacles, would you do it? Why? Do you find a limitation in BotStudio?

## *E-puck Dance* [Beginner]

The goal of this exercise is to put the fire on the dance floor with your virtual e-puck by creating the e-puck dance. You can imagine dance as a succession of movements with the same rhythm. You will be able to model that easily in a finite state automaton.

### Open the World File

Open the following world file:
This opens a disco dance floor. Moreover, there is already a very little example of what you can achieve. I hope you will find a better e-puck dance than the existing one.

### Imagine your Dance

[P.1] Observe the existing dance. The automaton has a loop shape. The time of every transition is identical. Create a new automaton or modify the existing one. First of all, choose a rhythm. The chosen rhythm of the example is a movement every second. It implies that every timer is set to 1[s]. Then, you should create a state for each movement you want to see during the global loop (note that if you want to have twice the same movement during the main loop, you have to create two states). Then, you have to set the states parameters according to your rhythm. Finally, link each state with a timer transition. (Hints: for producing a beautiful dance, LEDs are welcome. You can also perform semi-movements)

## Line following [Beginner]

The goal of this exercise is to explore the last device available in BotStudio: the camera. With the e-puck camera, you can obtain information about the ground in front of it. BotStudio computes ”in real time” the center of the black line in front of the e-puck. The camera is another e-puck sensor and the center of the front line is the sensor value of this camera.

### A linear Camera

An e-puck has a camera in front of it. Its resolution is 480x640. For technical issues it has a width of 480 and a height of 640. The most important information for following a line is the last line of the camera (see the figure called "The field of view of the linear camera"). For this reason, only the last line of the camera is sent from the e-puck to Webots. Finally an algorithm is applied on this last line in order to find the center of the black line.
The problem is that the e-puck sees only at about 5.5 cm in front of it, and sees a line of about 4.5 cm of width. Moreover, this information is refreshed about 2 times per seconds. It is little information!
[Q.1] Imagine that, every 5 s, at 4 m in front of you, you can only see a line of 3 m wide with always the same angle. What will be your strategy for following the line? Imagine that the line is intricate. What is the most important factor for following the line?

### Open the World File

Open the following world file:
This opens a long world. A black line is drawn on the ground. Remark that there is a grain (Gaussian noise) on the e-puck camera values in order to be more realistic. Indeed, a real camera doesn't acquire perfect values. Some noise is always present on a real camera, it comes from several factors.

### Line following Automaton

In the BotStudio interface, you will also find a condition over the camera (figure called "The linear camera condition in BotStudio") represented by a slider. The value represents the center of the black line in front of the robot. When a transition is selected, you can change the condition over the camera by dragging the slider, and you can change the direction of the test by clicking on the text field (ex: ”<5” becomes ”>5”). Remark that if there is no line in front of the robot, the center value can be wrong.
[P.1] Run the given automaton both in the simulation and in the reality. For creating a real environment, you can draw a line with a large black pen on a big white piece of paper (ex: A2 format). The black line must pass far from the paper bounds.
[P.2] Observe the direction (bigger than or smaller than) of the two conditions.
[P.3] Try to let go the e-puck as fast as possible by changing parameters of the states and of the transitions.

## Rally [Beginner] [Challenge]

Open the following world file:
[P.1] Create an automaton which can perform a complete turn of this path. (Hint: adapt your speed!)

## Notes

* ↑ More information on: Stepper motor
* ↑ More information on: Led
* ↑ More information on: Accelerometer
* ↑ Noise is an unwanted perturbation. More information on : Noise
* ↑ Source and more information on : Finite state automaton