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.
Then to remove this function:
th.set_variable_observer(id, None)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for thymiodirect-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9ee08d14f984ece16cd00c616a28ffae1214fe46ea699b4dd35fba839d24ceee |
|
MD5 | a6ccab66e9da550276f97d03e6ca2978 |
|
BLAKE2b-256 | bd3476907b71cbc2a81dd1229471c12bf8988ec25838b22f62645b2cb0339081 |