Skip to main content

Communication with Thymio II robot via the Thymio Device Manager

Project description

tdmclient

Python package to connect to a Thymio II robot via the Thymio Device Manager (TDM), a component of the Thymio Suite. The connection between Python and the TDM is done over TCP to the port number advertised by zeroconf.

Installation

Make sure that Thymio Suite, Python 3, and pip, the package installer for Python, are installed on your computer. You can find instructions at [https://www.thymio.org/program/], [https://www.python.org/downloads/] and [https://pypi.org/project/pip/], respectively.

Then in a terminal window, install tdmclient by typing

sudo python3 -m pip install tdmclient

on macOS or Linux. On Windows, in Windows PowerShell or Windows Terminal, just type

python3 -m pip install tdmclient

Tutorial

Connect a robot to your computer via a USB cable or the RF dongle and launch Thymio Suite. In Thymio Suite, you can click the Aseba Studio icon to check that the Thymio is recognized, and, also optionally, start Aseba Studio (select the robot and click the button "Program with Aseba Studio"). Only one client can control the robot at the same time to change a variable or run a program. If that's what you want to do from Python, either don't start Aseba Studio or unlock the robot by clicking the little lock icon in the tab title near the top left corner of the Aseba Studio window.

Some features of the library can be accessed directly from the command window by typing python3 -m tdmclient.tools.abc arguments, where abc is the name of the tool.

tdmclient.tools.tdmdiscovery

Display the address and port of TDM advertised by zeroconf until control-C is typed:

python3 -m tdmclient.tools.tdmdiscovery

tdmclient.tools.run

Run an Aseba program on the first Thymio II robot and store it into the scratchpad so that it's seen in Aseba Studio:

python3 -m tdmclient.tools.run --scratchpad examples/blink.aseba

Stop the program:

python3 -m tdmclient.tools.run --stop

Display other options:

python3 -m tdmclient.tools.run --help

tdmclient.tools.watch

Display all node changes (variables, events and program in the scratchpad) until control-C is typed:

python3 -m tdmclient.tools.watch

tdmclient.tools.variables

Run the variable browser in a window. The GUI is implemented with TK.

python3 -m tdmclient.tools.variables

At launch, the robot is unlocked, i.e. the variables are just fetched and displayed: Observe is displayed in the status area at the bottom of the window. To be able to change them, activate menu Robot>Control. Then you can click any variable, change its value and type Return to confirm or Esc to cancel.

Interactive Python

This section will describe only the use of ClientAsync, the highest-level way to interact with a robot, with asynchronous methods which behave nicely in a non-blocking way if you need to perform other tasks such as running a user interface. All the tools described above use ClientAsync, except for tdmclient.tools.tdmdiscovery which doesn't communicate with the robots.

First we'll type commands interactively by starting Python 3 without argument. To start Python 3, open a terminal window (Windows Terminal or Command Prompt in Windows, Terminal in macOS or Linux) and type python3. TDM replies should arrive quicker than typing at the keyboard. Next section shows how to interact with the TDM from a program where you wait for replies and use them immediately to run as fast as possible.

Start Python 3, then import the required class:

from tdmclient import ClientAsync

Create a client object:

client = ClientAsync()

If the TDM runs on your local computer, its address and port number will be obtained from zeroconf. You can check their value:

client.tdm_addr
client.tdm_port

The client will connect to the TDM which will send messages to us, such as one to announce the existence of a robot. There are two ways to accept and process them:

  • Call explicitly
    client.process_waiting_messages()
    
    If a robot is connected, you should find its description in an array of nodes in the client object:
    node = client.nodes[0]
    
  • Call an asynchronous function in such a way that its result is waited for. This can be done in a coroutine, a special function which is executed at the same time as other tasks your program must perform, with the await Python keyword; or handled by the helper function ClientAsync.aw. Keyword await is valid only in a function, hence we cannot call it directly from the Python prompt. In this section, we'll use ClientAsync.aw. Robots are associated to nodes. To get the first node once it's available (i.e. an object which refers to the first or only robot after having received and processed enough messages from the TDM to have this information), type
    node = ClientAsync.aw(client.wait_for_node())
    
    Avoiding calling yourself process_waiting_messages() is safer, because other methods like wait_for_node() make sure to wait until the expected reply has been received from the TDM.

The value of node is an object which contains some properties related to the robot and let you communicate with it. The node id is displayed when you just print the node:

node

or

print(node)

It's also available as a string:

node_id_str = node.id_str

Lock the robot to change variables or run programs (make sure it isn't already used in Thymio Suite):

ClientAsync.aw(node.lock())

Compile and load an Aseba program:

program = """
var on = 0  # 0=off, 1=on
timer.period[0] = 500

onevent timer0
    on = 1 - on  # "on = not on" with a syntax Aseba accepts
    leds.top = [32 * on, 32 * on, 0]
"""
r = ClientAsync.aw(node.compile(program))

The result r is None if the call is successful, or an error number if it has failed. In interactive mode, we won't store anymore the result code if we don't expect and check errors anyway. But it's usually a good thing to be more careful in programs.

No need to store the actual source code for other clients, or anything at all.

ClientAsync.aw(node.set_scratchpad("Hello, Studio!"))

Run the program compiled by compile:

ClientAsync.aw(node.run())

Stop it:

ClientAsync.aw(node.stop())

Make the robot move forward by setting both variables motor.left.target and motor.right.target:

v = {
    "motor.left.target": [50],
    "motor.right.target": [50],
}
ClientAsync.aw(node.set_variables(v))

Make the robot stop:

v = {
    "motor.left.target": [0],
    "motor.right.target": [0],
}
ClientAsync.aw(node.set_variables(v))

Unlock the robot:

ClientAsync.aw(node.unlock())

Getting variable values is done by observing changes, which requires a function. This is easier to do in a Python program file. We'll do it in the next section.

Python program

In a program, instead of executing asynchronous methods synchronously with ClientAsync.aw, we put them in an async function and we await for their result. The whole async function is executed with method run_async_program.

Moving forward, waiting for 2 seconds and stopping could be done with the following code. You can store it in a .py file or paste it directly into an interactive Python 3 session, as you prefer; but make sure you don't keep the robot locked, you wouldn't be able to lock it a second time. Quitting and restarting Python is a sure way to start from a clean state.

from tdmclient import ClientAsync

def motors(left, right):
    return {
        "motor.left.target": [left],
        "motor.right.target": [right],
    }

client = ClientAsync()

async def prog():
    node = await client.wait_for_node()
    await node.lock()
    await node.set_variables(motors(50, 50))
    await client.sleep(2)
    await node.set_variables(motors(0, 0))
    await node.unlock()

client.run_async_program(prog)

This can be simplified a little bit with the help of with constructs:

from tdmclient import ClientAsync

def motors(left, right):
    return {
        "motor.left.target": [left],
        "motor.right.target": [right],
    }

with ClientAsync() as client:
    async def prog():
        with await client.lock() as node:
            await node.set_variables(motors(50, 50))
            await client.sleep(2)
            await node.set_variables(motors(0, 0))
    client.run_async_program(prog)

To read variables, the updates must be observed with a function. The following program calculates a motor speed based on the front proximity sensor to move backward when it detects an obstacle. Instead of calling the async method set_variables which expects a result code in a message from the TDM, it just sends a message to change variables with send_set_variables without expecting any reply. The TDM will send a reply anyway, but the client will ignore it without trying to associate it with the request message. sleep() without argument (or with a negative duration) waits forever, until you interrupt it by typing control-C.

from tdmclient import ClientAsync

def motors(left, right):
    return {
        "motor.left.target": [left],
        "motor.right.target": [right],
    }

def on_variables_changed(node, data):
    try:
        prox = data["variables"]["prox.horizontal"]
        prox_front = prox[2]
        speed = -prox_front // 10
        node.send_set_variables(motors(speed, speed))
    except KeyError:
        pass  # prox.horizontal not found

with ClientAsync() as client:
    async def prog():
        with await client.lock() as node:
            await node.watch(variables=True)
            client.on_variables_changed = on_variables_changed
            await client.sleep()
    client.run_async_program(prog)

Compare with an equivalent Python program running directly on the Thymio:

@onevent
def prox():
    prox_front = prox.horizontal[2]
    speed = -prox_front // 10
    motor.left.target = speed
    motor.right.target = speed

You could save it as a .py file and run it with tdmclient.tools.run as explained above. If you want to do everything yourself, to understand precisely how tdmclient works or because you want to eventually combine processing on the Thymio and on your computer, here is a Python program running on the PC to convert it to Aseba, compile and load it, and run it.

from tdmclient import ClientAsync
from tdmclient.atranspiler import ATranspiler

thymio_program_python = r"""
@onevent
def prox():
    prox_front = prox.horizontal[2]
    speed = -prox_front // 10
    motor.left.target = speed
    motor.right.target = speed
"""

# convert program from Python to Aseba
transpiler = ATranspiler()
transpiler.set_source(thymio_program_python)
transpiler.transpile()
thymio_program_aseba = transpiler.get_output()

with ClientAsync() as client:
    async def prog():
        with await client.lock() as node:
            error = await node.compile(thymio_program_aseba)
            error = await node.run()
            await client.sleep()
    client.run_async_program(prog)

Python-to-Aseba transpiler

The official programming language of the Thymio is Aseba, a rudimentary event-driven text language. In the current official software environment, it's compiled by the TDM to machine code for a virtual processor, which is itself a program which runs on the Thymio. Virtual processors are common on many platforms; they're often referred as VM (Virtual Machine), and their machine code as bytecode.

Most programming languages for the Thymio eventually involve bytecode running on its VM. They can be divided into two main categories:

  • Programs compiled to bytecode running autonomously in the VM on the microcontroller of the Thymio. In Thymio Suite and its predecessor Aseba Studio, this includes the Aseba language; and VPL, VPL 3, and Blockly, where programs are made of graphical representations of programming constructs and converted to bytecode in two steps, first to Aseba, then from Aseba to bytecode with the standard TDM Aseba compiler.
  • Programs running on the computer and controlling the Thymio remotely. In Thymio Suite and Aseba Studio, this includes Scratch. Python programs which use thymiodirect or tdmclient can also belong to this category. A small Aseba program runs on the Thymio, receives commands and sends back data via events.

Exceptions would include programs which control remotely the Thymio exclusively by fetching and changing variables; and changing the Thymio firmware, the low-level program compiled directly for its microcontroller.

Remote programs can rely on much greater computing power and virtually unlimited data storage capacity. On the other hand, communication is slow, especially with the USB radio dongle. It restricts what can be achieved in feedback loops when commands to actuators are based on sensor measurements.

Alternative compilers belonging to the first category, not needing the approval of Mobsya, are possible and have actually been implemented. While the Thymio VM is tightly coupled to the requirements of the Aseba language, some of the most glaring Aseba language limitations can be circumvented. Ultimately, the principal restrictions are the amount of code and data memory and the execution speed.

Sending arbitrary bytecode to the Thymio cannot be done by the TDM. The TDM accepts only Aseba source code, compiles it itself, and sends the resulting bytecode to the Thymio. So with the TDM, to support alternative languages, we must convert them to Aseba programs. To send bytecode, or assembly code which is a bytecode text representation easier to understand for humans, an alternative would be the Python package thymiodirect.

Converting source code (the original text program representation) from a language to another one is known as transpilation (or transcompilation). This document describes a transpiler which converts programs from Python to Aseba. Its goal is to run Python programs locally on the Thymio, be it for autonomous programs or for control and data acquisition in cooperation with the computer via events. Only a small subset of Python is supported. Most limitations of Aseba are still present.

Features

The transpiler is implemented in class ATranspiler, completely independently of other tdmclient functionality. The input is a complete program in Python; the output is an Aseba program.

Here are the implemented features:

  • Python syntax. The official Python parser is used, hence no surprise should be expected, including with spaces, tabs, parentheses, and comments.
  • Integer and boolean base types. Both are stored as signed 16-bit numbers, without error on overflow.
  • Global variables. Variables are collected from the left-hand side part of plain assignments (assignment to variables without indexing). For arrays, there must exist at least one assignment of a list, directly or indirectly (i.e. a=[1,2];b=a is valid). Size conflicts are flagged as errors.
  • Expressions with scalar arithmetic, comparisons and boolean logic with short-circuit evaluation. Numbers and booleans can be mixed freely. The following Python operators and functions are supported: infix operators +, -, *, // (integer division), % (converted to modulo instead of remainder, whose sign can differ with negative operands), &, |, ^, <<, >>, ==, !=, >, >=, <, <=; prefix operators +, -, ~, not; and function abs.
  • Constants False and True.
  • Assignments of scalars to scalar variables or array elements; or lists to whole array variables.
  • Augmented assignments +=, -=, *=, //=, %=, &=, |=, ^=, <<=, >>=.
  • Programming constructs if elif else, while else, for in range else, pass, return. The for loop must use a range generator with 1, 2 or 3 arguments.
  • Function definitions exclusively for event handlers with the @onevent decorator. The function name must match the event name (such as def timer0(): for the first timer event). Arguments are not supported.
  • Function call emit("name") or emit("name", param1, param2, ...) to emit an event without or with parameters. The first argument must be a literal string, delimited with single or double quotes. Raw strings (prefixed with r) are allowed, f-strings or byte strings are not. Remaining arguments, if any, must be scalar expressions and are passed as event data.
  • Naked expressions which aren't a part of a statement described above, such as 1+2, are ignored. This is also the case for the ellipsis ... which can be used as a synonym of pass.

Currently, a major difference with the semantic of Python is that all variables are global. This is not satisfactory and is likely to be fixed soon. To ensure forward compatibility, variables in function definitions (currently only for event handlers) should be declared in a global statement, which is ignored currently; or be local and avoid any conflict elsewhere in the code (i.e. reusing a variable i in two loops is fine provided that the loops are not nested).

Perhaps the most noticeable missing features are the non-integer division operator / (Python has another operator // for the integer division), and the break and continue statements, also missing in Aseba and difficult to transpile to sane code without goto.

The transpilation is mostly straightforward. Mixing numeric and boolean expressions often requires splitting them into multiple statements and using temporary variables. The for loop is transpiled to an Aseba while loop because in Aseba, for is limited to constant ranges. Comments are lost because the official Python parser used for the first phase ignores them.

Example

Blinking top RGB led:

on = False
timer.period[0] = 500

@onevent
def timer0():
    global on
    on = not on
    if on:
        leds.top = [32, 32, 0]
    else:
        leds.top = [0, 0, 0]

To transpile this program, assuming it's stored in examples/blink.py:

python3 -m tdmclient.atranspiler examples/blink.py

The result is

var on
var tmp[1]

on = 0
timer.period[0] = 500

onevent timer0
    if on == 0 then
        tmp[0] = 1
    else
        tmp[0] = 0
    end
    on = tmp[0]
    if on != 0 then
        leds.top = [32, 32, 0]
    else
        leds.top = [0, 0, 0]
    end

To run this program:

python3 -m tdmclient.tools.run examples/blink.py

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tdmclient-0.1.1.tar.gz (32.6 kB view hashes)

Uploaded Source

Built Distribution

tdmclient-0.1.1-py3-none-any.whl (30.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page