Skip to main content

thymiodirect

Python package to connect to a Thymio II robot with its native binary protocol via a serial port (virtual port over a wired USB or wireless USB dongle) or a TCP port (asebaswitch or Thymio simulator).

Example

The steps below are borrowed from the help you obtain by typing

import thymiodirect
help(thymiodirect)

Import the required classes:

from thymiodirect import Connection
from thymiodirect import Thymio

Set the serial port the Thymio is connected to (depending on your configuration, the default port is not what you want):

port = Connection.serial_default_port()

Create a Thymio connection object with a callback to be notified when the robot is ready and start the connection (or just wait a few seconds):

th = Thymio(serial_port=port,
            on_connect=lambda node_id:print(f"{node_id} is connected"))
th.connect()

Get id of the first (or only) Thymio:

id = th.first_node()

Get a variable:

th[id]["prox.horizontal"]

Set a variable (scalar or array):

th[id]["leds.top"] = [0, 0, 32]

Define a function called after new variable values have been fetched. The front and rear proximity sensors are used to make the robot move forward or backward. Decision to move or stop is based on the difference of these sensor readings.

prox_prev = 0
def obs(node_id):
    global prox_prev
    prox = (th[node_id]["prox.horizontal"][5]
            - th[node_id]["prox.horizontal"][2]) // 10
    if prox != prox_prev:
        th[node_id]["motor.left.target"] = prox
        th[node_id]["motor.right.target"] = prox
        print(prox)
        if prox > 5:
            th[id]["leds.top"] = [0, 32, 0]
        elif prox < -5:
            th[id]["leds.top"] = [32, 32, 0]
        elif abs(prox) < 3:
            th[id]["leds.top"] = [0, 0, 32]
        prox_prev = prox

Install this function:

th.set_variable_observer(id, obs)

Make the robot move forward by putting your hand behind it, or backward by putting your hand in front of it.

Remove this function:

th.set_variable_observer(id, None)

By default, all the Thymio variables are fetched 10 times per second. This can be changed with options passed to the Thymio constructor. Here is how you would fetch 5 times per second (every 0.2 second) a chunk of variable data which covers prox.horizontal and prox.ground:

th = Thymio(serial_port=port,
            on_connect=lambda node_id:print(f"{node_id} is connected"),
            refreshing_rate=0.2,
            refreshing_coverage={"prox.horizontal", "prox.ground"})

Release files for thymiodirect 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for thymiodirect 0.1.2
File Size Uploaded
thymiodirect-0.1.2.tar.gz 14.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for thymiodirect 0.1.2
File Interpreter ABI Platform
thymiodirect-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 32.2 kB

Release files / thymiodirect-0.1.2.tar.gz

Download URL thymiodirect-0.1.2.tar.gz
Size 14.2 kB
Tags Source
SHA-256 checksum
How to use checksums
1869c59be58999824fb38dc2cf2470d1bc8a6c5218eca78f7ec72dcfac0a08ad
BLAKE2b-256 checksum
How to use checksums
cd1f455cf8fe74a10db9666a8e950dce8e0cce8719404603102d548cceb871a0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.7.9

Release files / thymiodirect-0.1.2-py3-none-any.whl

Download URL thymiodirect-0.1.2-py3-none-any.whl
Size 18.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fe2854cb19de043a8a2d96983d713ff30798a0b8a5ba42442b72b4f34e482a9f
BLAKE2b-256 checksum
How to use checksums
7c7587c9c79e555158ed5ae4fb9bd6a945a90801d4c0a1d5bb700ed9db99dd92
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.7.9

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page