Navigation Menu

Skip to content

python doc

Alma Mallo Casdelo edited this page Sep 15, 2021 · 24 revisions

Robobo and python

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.

If you have already performed these initial steps, you should go directly to 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. It also must allow communication between devices.

How to allow communication between devices

Setting up your computer

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

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

Once you have installed Python 3, go to the terminal in Visual Studio Code and type the following command to install the robobopy library:

pip install robobopy

If you can't see the terminal, click 'View' on the upper menu and select 'Terminal'.

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.

You can read some basic information about the sensors and actuators on the base here.

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.

Note the following line:

rob = Robobo("10.113.36.150")

In that line, you are connecting to an IP. You have to modify the given IP so it connects to your smartphone. After that, save the file.

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'.

How to execute your code in Visual Studio Code

If Robobo LEDs turn green and it says 'Hello world', it means you did everything right. Otherwise, something is wrong and you will need to check the previous steps. Specifically, make sure:

  1. Both your mobile and your computer are connected to the same WIFI network, which allows share accross devices.
  2. You wrote the right IP. You can check it again in your mobile app.
  3. The Robobo base is connected via bluetooth to your smartphone.
  4. You installed the websocket-client library.

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.

We have an online monitor accessible through the url: http://monitor.theroboboproject.com/

If you want to use it offline, you can download it from the following repository: https://github.com/mintforpeople/robobo-monitor

To use it, open the index.html file included in the project (inside a folder with the same name) from your browser.

How to open a file from a browser

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

How to connect your device to the monitoring window

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)

Save the file, execute your code now, and Robobo should spin.

Let's complicate things a bit! This time, we are going to make Robobo move straight until it reaches an obstacle, and stop before colliding.

Remember we had three functions to make Robobo move wheels. This time, we don't want to move wheels by time as we did in the previous example. We are going to use a different function: moveWheels. If you check the Robobo.py library reference, you will notice this functions receives two arguments: rSpeed and lSpeed. Both wheels will move at the same speed, for example 10. Delete the line we added before (we don't want Robobo to spin anymore) and add to the code the following line (again, anywhere between the connect and the disconnect functions):

rob.moveWheels(10, 10)

If we executed the code now, Robobo would move straight forever, but we don't want that. We need to check the distance to any possible obstacle. To do this, we are going to use the front center IR sensor. Again, if we check the Robobo.py library reference, we can find the function readIRSensor. This function receives one unique parameter, which represents the IR sensor we want to use. How do we do that?

Let's clarify some things. The first line of our code was:

from robobopy.Robobo import Robobo

This line imports the Robobo library, and allows us to use all the things defined there. However, some things are defined in other places and we have to import them separately. It's the case of the IR sensors. They are defined in utils, and we can import them with the following line:

from robobopy.utils.IR import IR

We always write all the imports at the very beginning of our code, and this allows us to use the imported things at any time (this is, at any place in our code below the imports). We can check which are the available sensors... guess where? Yes! Again in the Robobo.py library reference. Don't add it to the code yet, but the function we need to use is this one:

rob.readIRSensor(IR.FrontC)

This function reads the IR sensor and returns a value. The value depends on several conditions, such as the ambient lighting. Bring your hand near the sensors and then move it away. Check the monitoring window in order to have an idea of the range of values the IR sensors are returning. We have chosen a 120 value to represent a quite close object. Note that 120 is not the distance, but the IR value, which is larger for shorter distances.

Monitoring IR sensors

If we call the function once, we will read the IR sensor we want just once, and get a single value. We need Robobo to read the IR sensor many times, so we are going to use this function in a loop, just like this (now it's writing time):

while (rob.readIRSensor(IR.FrontC) < 120):
    rob.wait(0.01)

rob.stopMotors()

The translation of this code would be something like this: 'Robobo checks the front center IR sensor and compares it to the given value (120), so it knows the distance to nearby objects. While the distance is large enough, Robobo waits 0.01 seconds and checks the IR sensor again. We can make Robobo wait or we can make it check the IR sensor as many times as it can, but 0.01 seconds is a very short time that makes us save some computational resources and still works for our purpose. In the moment that distance is too short, Robobo stops checking that distance and follows the next instruction, which is stop moving'.

Our code should look like this right now:

# This imports the library    
from robobopy.Robobo import Robobo

from robobopy.utils.IR import IR

# This creates an instance of the Robobo class with the indicated IP address
# You have to modify next line
rob = Robobo("10.113.36.150")

# This connects to the robobo base
rob.connect()

# This makes Robobo say "Hello World"
rob.sayText("Hello world")

rob.moveWheels(10, 10)

while (rob.readIRSensor(IR.FrontC) < 120):
    rob.wait(0.01)

rob.stopMotors()

# This disconnects from the robobo base
rob.disconnect()

Let's execute it again and check it works fine. Be careful this time! Robobo can detect obstacles, but still can't detect the end of the surface, so you may have to catch Robobo to prevent it from falling.

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 robobopy.Robobo import Robobo
from robobopy.utils.LED import LED
from robobopy.utils.Color import Color
from robobopy.utils.IR import IR

# You have to modify next line 
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
from robobopy.Robobo import Robobo
from robobopy.utils.LED import LED
from robobopy.utils.Color import Color
from robobopy.utils.IR import IR
from robobopy.utils.Emotions import Emotions
from robobopy.utils.Sounds import Sounds
 
 # You have to modify next line
robobo = Robobo('192.168.0.17')
robobo.connect()
 
closeIRValue = 30
speed = 30
 
robobo.setLedColorTo(LED.All, Color.OFF)
robobo.moveWheels(speed, speed)
 
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.setEmotionTo(Emotions.SURPRISED)
robobo.playSound(Sounds.DISCOMFORT)
robobo.moveTiltTo(50, 15)

speed *= -1

robobo.moveWheelsByTime(speed, speed, 2)

robobo.sayText("Ups, I almost crash!")
robobo.setEmotionTo(Emotions.NORMAL)
robobo.moveTiltTo(75, 15)
 
robobo.disconnect()

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 robobopy.Robobo import Robobo
from robobopy.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 robobopy.Robobo import Robobo
from robobopy.utils.LED import LED
from robobopy.utils.Color import Color
from robobopy.utils.Emotions import Emotions
from robobopy.utils.Sounds import Sounds

# You have to modify next line
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.


Empresa subvencionada por el CDTI (Presupuestos Generales del Estado a cargo de la aplicación 27.12.467C.74908) Concretamente, mediante el programa de Ayudas destinadas a nuevos proyectos empresariales de empresas innovadoras (Programa NEOTEC), al amparo de la Orden ECC/1333/2015, de 2 de julio.

Clone this wiki locally