Language : English

Specialized in the sale of robotic products! educational robots, toy robots, connected objects

You are here :
    Home > News > Educational robots > Using Thymio with Python

Python with Thymio : complete guide

Educational robots

Python is a programming language more and more used and it can be totally compatible with the educational robot Thymio. You will find in this article all the elements that will facilitate the implementation and the use of Python with the Thymio robot: what is Python, Python for Thymio, Jupyter Notebooks, Prerequisites and Installation.

With this article, whose elements have been sent to us directly by Thymio, you will have all the keys in hand for an advanced and personalized use of the programmable educational robot Thymio.

What is Python?

You've probably heard of Python, because it's in high demand these days. It is a programming language that is :

  • High-level (which means it is comparatively easier for humans to understand than low-level languages)
  • Object-oriented (which means that it is data-driven)

Its syntax is relatively simple. It is now widely used in many fields and has a thriving community with many libraries. 

Python is also an interpreted language; there must always be an interpreter on the machine that executes the code directly, without compiling, as opposed to a language like C++, where you first need a compiler to create an executable, but that executable can then be run even without a compiler installed.

Python programming language

Python and the educational robot Thymio

In the case of the educational robot Thymio, there is no Python interpreter inside the robot. Thymio runs Aseba, and has a lightweight Aseba virtual machine (VM). For Thymio to run a program, it must be Aseba bytecode.

When a program is created in VPL or Aseba, an Aseba script is compiled into Aseba bytecode by the Thymio Device Manager (TDM) and sent to the robot. Therefore, a Python module (tdmclient) has been developed to allow Python to communicate with the Thymio Device Manager (TDM).

In particular, this model allows:

  • Access to the robot's variables
  • Transpile Python code into Aseba code
  • Send Aseba code to the TDM, which then sends the appropriate bytecode to the robot

Thanks to this transpiler, it is not necessary to learn the Aseba syntax to program Thymio. There are Python commands equivalent to the Aseba Thymio API, as well as some additional functionalities (Links: TDM Client / TDM Python).

How Python Thymio works

Jupyter Notebooks

The material and tutorials including Python code that you will find here are in the form of Jupyter Notebooks. Jupyter Lab is a web application that runs in your browser and allows you to create documents containing different types of cells, such as markdown cells, code cells that can be executed directly, etc (more information).

In short, this means that instead of having a terminal where you type and execute your Python commands, you will be using a document where your code is presented in cells, as well as explanatory text cells, images, etc.

More information: Jupyter Notebooks

Prerequisites and Installation of Jupyter Notebooks

This section explains all the necessary steps before you can start Thymio exercises in Jupyter Notebooks. It is necessary to install the following items.

Thymio Suite

The installation of Thymio Suite is necessary because the driver for Thymio and TDM are required for this application. Program link: Thymio Suite.

Python 3

In order to execute Python code, you must have the interpreter installed on your computer. It is not integrated to Thymio Suite, you will have to install it separately.
You can find the latest version of Python here (version 3.8 or later required).

Link to install Python 3. This will also install another utility, pip, which is needed below.

We recommend adding Python to PATH (checkbox on the installation screen), as this will allow you to type shorter commands, but you can also type the full path to the executable if you use python or pip from the command line.

JupyterLab

JupyterLab will allow you to open Jupyter notebooks in your browser and will manage the Python interpreter. To install it, we will use pip, a utility provided with Python. 

Open a terminal (under Windows, look for "cmd" next to the start menu, under MacOS the application is called Terminal) and type :

python3 -m pip install --upgrade pip

To be sure it is up to date. Depending on your installation, the command can be "python3" or "python".

Installation Python 3 Thymio

Then type :

python3 -m pip install jupyterlab

Installation may require prior installation of the construction tools THERE.

Python sur robot Thymio

If you prefer not to use Jupyter notebooks, it is possible to use TDMclient outside of Jupyter.

TDMclient3

Tdmclient will be installed directly from the notebook (see our example notebook below).

We have prepared a series of notebooks to start with Thymio on Python. Download them, and unzip them in the folder of your choice on your computer : Link HERE 

  • Connect the thymio robot to your computer 
  • Start Thymio Suite

Leave the welcome window open, which ensures that the TDM is running. If you want to see which robots are connected, you can click on one of the programming languages to display the list, but do not start any of the interfaces.

 

Thymio TDMclient3 Notebook

Keep Thymio Suite open, either on the welcome screen or on the list of robots.

  • Open a terminal, and type this command to open Jupyter 

jupyter-lab

  • On the left, you can browse to where you unzipped the notebooks. Once you have found them, double click on Thymio_Python_start.ipynb to open the notebook.

Jupyter Notebooks for Thymio

Once you open JupyterLab and the first notebook, you can see the structure. There are cells with titles, text, links, etc., and cells with code.

If you double-click on the first cell with text, its appearance will change (you will see the source) and you can edit it. This is a markdown cell. Some markdowns allow you to format your text (for example, # to indicate headings).

Jupyter Notebooks for ThymioThen, if you run this cell (play button at the top of the interface, or shift+Enter), it will show its resulting appearance.

Jupyter Notebooks for ThymioThe other type of cell we use is code cells. These cells contain Python code. When you run them, the Python interpreter will execute the commands they contain.

Jupyter Notebooks for Thymio

Sometimes there will be an output, but not necessarily. While a cell is running, a * appears in the parentheses next to it. Once it is executed, a number appears.

Jupyter Notebooks for Thymio

The result of the execution persists between cells. For example, if you assign a value to a variable and use it later in another cell, the value is stored.

By clicking on this arrow in the top menu, you can reboot the kernel to start over.

The newsletter
Receive our great deals and news