Skip to content

python doc

Cristina Renda López edited this page Feb 4, 2021 · 24 revisions

Robobo.py

Robobo.py is the library used to create programs for the Robobo educational robot in the Python language.

In this section, you can find basic documentation to install and start using this library, as well as some sample exercises.

For further information you can read the Robobo.py library reference, which contents the code documentation.

What do you need

To program Robobo with Python, in addition to the robotic base, the following elements are necessary:

  • A personal computer (portable or not).
  • A smartphone with the Android 7.0 operating system or higher and with a minimum screen size of 4.7" and a maximum of 5.5" (95 mm maximum thickness).
  • A WI-FI network to which the computer and the smartphone can connect, which must have internet connection.

Setting up your computer

The library runs with Python 3, so it must be installed before using the library. You can download it here. Make sure you install it with the default settings and add python to path, as shown in the image below.

How to install Python

The library depends on the websocket-client library, so it also must be installed. To do so, once you have installed Python 3, open your command prompt and type:

pip install websocket-client

Note: Pip may not be installed. In that case, you'll need to install it from this link. After doing so, close the command prompt and repeat the previous step.

You can edit your code in any text editor, but we recommend you to use Visual Studio Code, which can be downloaded here.

Finally, download the Robobo.py project from the repository and uncompress it.

How to download from the repo

Setting up your Robobo robot

You just need to turn it on by pressing the button on its back. When lit, the LEDs on the front of the Robobo base light up in red.

Setting up your smartphone

The Robobo mobile app can be freely downloaded from the Google Play store, and must be installed on the smartphone.

How to download the mobile app

Match the Robobo base with the smartphone. To do this you must go to the Android option “Bluetooth” and select in the list of devices the number of your Robobo base (the password to link them is 1234).

How to match base and smartphone

Place the smartphone in the Robobo holder so that it is firmly attached to the base.

To attach the smartphone to the Robobo base holder, you must insert it softly between the two levers of the holder, separating them with your own fingers if necessary, and slide it down until it is fixed in its lower part.

Start the Robobo app on the smartphone. Once loaded, it shows a screen that allows you to select the robot base to use, such as shown in the following image:

How to select the robot base

Note that it also shows your device IP. This is your smartphone identifier on the network you are using, and you will need it later to connect from your computer.

Hello world!

Now it's time to finally write our first script.

First, open the robobo.py project from your code editor and open the helloworld.py file.

Modify the given IP so it connects to your smartphone.

Now, execute the code. To do this in Visual Studio Code, you can select your file in the explorer, right click on it and choose "Run Phython file in terminal". If Robobo LEDs turn green and he says "Hello world", it means you did everything right. Otherwise, something is wrong and you will need to check the previous steps.

How to execute your code in Visual Studio Code

Using some functions

Why don't we make Robobo move?

Robobo has a lot of functions you can use. They are all explained in the Robobo.py library reference. If we follow the link and press Robobo.py, we can read this functions.

Note that there are three functions to make Robobo move wheels: moveWheels, moveWheelsByDegrees and moveWheelsByTime. The names are quite self explicative, but there is also a description for each function and its parameters.

Let's say we want to make Robobo spin during 4 seconds, and then stop. The easiest way to do this is using the function moveWheelsByTime. This function receives 4 parameters: rSpeed, lSpeed, duration and wait. Note that the fourth parameter has a default value, which is 'True'. This means we dont have to specify it.

To make Robobo spin, we have to move the right wheel and the left wheel in opposite directions, so we will choose a positive speed value for the right wheel and a negative speed value for the left one. This value is going to be, for example, 20.

We just have to add the following line to our code (anywhere between the connect and the disconnect functions):

rob.moveWheelsByTime(20, -20, 4)

Monitoring Robobo

While you are programming, it's very usefull to know the exact status of Robobo. For this reason, we include a monitoring screen and we recommend to keep it open while working with Robobo. To use it, open the robobo-monitor file included in the project (inside a folder with the same name) from your file explorer. The monitoring window will open in your browser.

You have to write your device IP and click the Connect button.

Sample projects

Here you will find different example projects to learn how to program Robobo with Python. These projects are small tutorials that focus on the main Python methods for Robobo. In all of them there is a challenge to solve with Robobo and a possible solution is provided. The best way to learn is trying your own solution first and, then check the solution provided.

1. Antishock Robobo

Introduction

Robots explore their environment, but they must be careful. They may find obstacles in their way or reach the edge of a precipice. If they learn to detect dangers and avoid them they won’t be damaged.

In this tutorial, you will learn how to detect obstacles and make Robobo stop before crashing. You will also learn how to turn on/off the LEDs and change their color like a light signal.

The challenge is the creation of an Anti-Shock Robobo!

Challenge

Creating a program that makes Robobo go straight until it detects an obstacle.

Through the front infrared sensors, Robobo will detect the obstacle and, as it advances, it will change the color of the LEDs to indicate the distance to the obstacle (green if far away, yellow at medium distance, and red when near). In addition, when it is close to the obstacle, it will stop to avoid the collision.

Solution

A possible solution to this challenge is the following, although there are many others. Which one is yours?

View source
from Robobo import Robobo
from utils.LED import LED
from utils.Color import Color
from utils.IR import IR
 
robobo = Robobo('10.113.36.163')
robobo.connect()
 
closeIRValue = 120
mediumIRValue = 30
farIRValue = 10
speed = 20
 
robobo.setLedColorTo(LED.All, Color.OFF)
robobo.moveWheels(speed, speed)
 
while (robobo.readIRSensor(IR.FrontC) < farIRValue)and(robobo.readIRSensor(IR.FrontRR) < farIRValue)and(robobo.readIRSensor(IR.FrontLL) < farIRValue):
    robobo.wait(0.01)
 
robobo.setLedColorTo(LED.All, Color.GREEN)
 
while (robobo.readIRSensor(IR.FrontC) < mediumIRValue)and(robobo.readIRSensor(IR.FrontRR) < mediumIRValue)and(robobo.readIRSensor(IR.FrontLL) < mediumIRValue):
    robobo.wait(0.01)
 
robobo.setLedColorTo(LED.All, Color.MAGENTA)
 
while (robobo.readIRSensor(IR.FrontC) < closeIRValue)and(robobo.readIRSensor(IR.FrontRR) < closeIRValue)and(robobo.readIRSensor(IR.FrontLL) < closeIRValue):
    robobo.wait(0.01)
 
robobo.stopMotors()
robobo.setLedColorTo(LED.All, Color.RED)
 
robobo.wait(2)
robobo.setLedColorTo(LED.All, Color.BLUE)

Additional challenge

When you have completed the “Anti-shock Robobo” challenge, we propose you a new one that uses the same sensors, but in a slightly different way, shall we try?

Put Robobo on a table and try to prevent it from falling to the ground. Perform this challenge with care, if Robobo falls it can break. Use a slow speed for the tests of your program and stay alert in case you have to catch it to prevent it from falling while you debug your program.

Note that the infrared sensors Front-L, Front-R, Back-R and Back-L are inclined towards the floor, so these are the ones to use in this new challenge.

2. Expresive Robobo

Introduction

A basic feature of future autonomous robots will be their ability to communicate with us in a natural way, through gestures, expressions, speaking… In this tutorial you will learn how to work on this aspect with Robobo, through the expressions, sounds and speech production of the robot. The challenge is the creation of an Expressive Robobo!

Challenge

Creating a program that makes Robobo advance forward until it detects a nearby obstacle (just like in tutorial #1), and when this happens, it gets frightened. Therefore, once the obstacle is detected, you have to make Robobo look scared. To do this, you can make him move back, put its head back (tilt), show a face with surprise, emit a small scream, and say some phrase.

Solution

One possible solution to this challenge is the following, although there are many others. Which one is yours?

View source
async function main(){
    var Robobo = require('./lib/robobo');
 
    robobo = new Robobo('10.113.36.163');
    await robobo.connect();
 
     var closeIRValue = 30,
         speed = 30;
 
    robobo.moveWheels(speed,speed);   
    while ((robobo.readIRSensor('Front-C') < closeIRValue)and(robobo.readIRSensor('Front-RR') < closeIRValue)and(robobo.readIRSensor('Front-LL') < closeIRValue)) {
        await robobo.update()
    }
    await robobo.stopMotors(); 
 
    robobo.setLedColorTo('all','red');    
    robobo.setEmotionTo('surprised');
    robobo.playSound('disconform');    
    await robobo.moveTiltToBLK(50,15);    
    speed *= -1; 
    await robobo.moveWheelsByTimeBLK(speed,speed,2);
    robobo.sayText("Ups, I almost crash!");
    robobo.setEmotionTo('normal');
    robobo.moveTiltTo(75,15); 
}
main()

Additional challenge

When you have completed the “Robobo expressive” challenge, we propose you a new one with a little rhythm, do you like? Try to make Robobo move its head forwards and backwards as it moves in a straight line forward, as if it was rapping. At the same time it should show an expression on its face. When it encounters an obstacle, Robobo will stop, turn 90º using the wheel motors, look happy and say “This is fun, we repeat …” and finally emit a sound of joy.

3. Angry Robobo

Introduction

A fundamental property of the future autonomous robots will be their ability to interact with humans. They will have to respond naturally to different forms of communication.

In this tutorial, you will learn how to handle another typical form of interaction with Robobo, by touching the smartphone’s screen (Robobo face), showing, in this case, how it gets angry with you.

The challenge is the creation of an angry Robobo!

Challenge

Creating a program that makes Robobo react to touches on its face (Smartphone’s screen). If you touch its eye (tap), Robobo will throw its head back to escape your hand. At the same time, it will look angry and tell you not to touch the eye.

Solution

One possible solution to this challenge is the following, although there are many others. Which one is yours?

View source
from Robobo import Robobo
from utils.Emotions import Emotions
 
robobo = Robobo('10.113.36.163')
robobo.connect()
 
robobo.resetTapSensor()
robobo.setEmotionTo(Emotions.NORMAL)
robobo.moveTiltTo(75 ,15)
 
while True:
    robobo.wait(0.01)
    tap = robobo.readTapSensor();
    if tap.zone == 'eye':
        robobo.setEmotionTo(Emotions.ANGRY)
        robobo.moveTiltTo(60,15)
        robobo.sayText('Don't touch my eye')
        robobo.wait(1)
        robobo.resetTapSensor()
        robobo.setEmotionTo(Emotions.NORMAL)
        robobo.moveTiltTo(75,15)

Additional challenge

When you have completed the “Angry Robobo” challenge, we propose you this a new one where we complicate a little the robot reactions. The objective is to create a program in which Robobo shows different emotions depending on the area of the face you touch it (tap on the screen), for example, it should laugh if you touch the mouth, or show a surprise face if you touch the chin, saying “you tickle me”.

4. Spinning Robobo

Introduction

In tutorial #3, we saw a first example of tactile interaction with Robobo, that is, how the robot can react if we touch its face.

In this tutorial we will continue working on this aspect with Robobo, and you will now learn how to provoke a predefined response when we touch the face in a certain way.

The challenge is to create a Spinning Robobo!

Challenge

Creating a program that makes Robobo react to touches and gestures on its face (Smartphone screen). If you move your finger on the screen (fling) to the right or left, Robobo must turn its head to the right or left respectively and turn on the leds (one color to the right and a different color to the left).

Solution

A possible solution to this challenge is the following, although there are many others. Which one is yours?

View source
from Robobo import Robobo
from utils.LED import LED
from utils.Color import Color
from utils.Emotions import Emotions
from utils.Sounds import Sounds
 
robobo = Robobo('10.113.36.163')
robobo.connect()
 
robobo.resetFlingSensor()
robobo.setEmotionTo(Emotions.NORMAL)
robobo.setLedColorTo(LED.All, Color.OFF)
robobo.movePanTo(0, 15)
 
while True:
    robobo.wait(0.01)
    angle = robobo.readFlingAngle()
    if angle > 0:
        if (angle < 90)or(angle > 270):
            robobo.setEmotionTo(Emotions.SURPRISED)
            robobo.setLedColorTo(LED.All, Color.BLUE)
            robobo.movePanTo(-50, 30)
        else:
            robobo.setEmotionTo(Emotions.SURPRISED)
            robobo.setLedColorTo(LED.All, Color.MAGENTA)
            robobo.movePanTo(50, 30)
        robobo.movePanTo(0, 15)
        robobo.setEmotionTo(Emotions.LAUGHING)
        robobo.playSound(Sounds.LAUGH)
        robobo.resetFlingSensor()
        robobo.wait(1)
        robobo.setLedColorTo(LED.All, Color.OFF)
        robobo.setEmotionTo(Emotions.NORMAL)

Additional challenge

When you have completed the “Spinning Robobo” challenge we propose you a new one where we complicate a little the reaction of Robobo, are you prepared? The goal is to create a program in which Robobo moves its head forward/backward (TILT) or turns its body (moves the wheels) right/left (PAN) depending on the angle of the fling gesture you make on the screen of the Smartphone.

Clone this wiki locally