Skip to main content

Communication with Thymio II robot via serial port or TCP

Project description

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"})

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

thymiodirect-0.1.2.tar.gz (14.2 kB view details)

Uploaded Source

Built Distribution

thymiodirect-0.1.2-py3-none-any.whl (18.1 kB view details)

Uploaded Python 3

File details

Details for the file thymiodirect-0.1.2.tar.gz.

File metadata

  • Download URL: thymiodirect-0.1.2.tar.gz
  • Upload date:
  • Size: 14.2 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for thymiodirect-0.1.2.tar.gz
Algorithm Hash digest
SHA256 1869c59be58999824fb38dc2cf2470d1bc8a6c5218eca78f7ec72dcfac0a08ad
MD5 82cfe8162eba724b1c998f52c9c05805
BLAKE2b-256 cd1f455cf8fe74a10db9666a8e950dce8e0cce8719404603102d548cceb871a0

See more details on using hashes here.

File details

Details for the file thymiodirect-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: thymiodirect-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 18.1 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for thymiodirect-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 fe2854cb19de043a8a2d96983d713ff30798a0b8a5ba42442b72b4f34e482a9f
MD5 dd5a302413eab9099224223be21e1f53
BLAKE2b-256 7c7587c9c79e555158ed5ae4fb9bd6a945a90801d4c0a1d5bb700ed9db99dd92

See more details on using hashes here.

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