Table des matières

TNY-360 Documentation

Python SDK

You want to control your TNY-360 using Python? We got you covered!

This section is here to get you started with the Python SDK for your TNY-360.


The tny-robotics-sdk package

The first step to control your TNY-360 with Python is to install the tny-robotics-sdk package from PyPI.

This package provides a high-level, asynchronous API to interact with your TNY-360, allowing you to send commands, receive sensor data, and manage the robot's state cleanly.

Install the package

To install the package, run the following command in your terminal or virtual environment:

pip install tny-robotics-sdk

(you can also add it to your pyproject.toml, requirements.txt or use poetry add tny-robotics-sdk depending on your setup)

Check for updates

Remember to check for updates regularly, as we are continuously improving the SDK and adding new features!

You can do this by running:

pip install --upgrade tny-robotics-sdk

Getting started with the SDK

Once you have the SDK installed, you can start using it to control your TNY-360.

Since the Python SDK relies on native asynchronous I/O, all connection and command methods are coroutines that need to be awaited inside an asyncio event loop.

Here's a couple of basic examples to get you started:

Connecting to the TNY-360

The first thing you'll want to do is connect to your TNY-360. Here's how to structure your script with an asyncio loop and handle potential errors:

connect.py
import asyncio
from tny_robotics import TNY360 # Import the TNY-360 class from the SDK

# Find the IP address of your TNY-360 (available in the network > wifi menu)
ROBOT_IP_ADDR = '192.168.4.1'

async def main():
    # Create a new instance of the TNY-360 class
    robot = TNY360(ROBOT_IP_ADDR)

    # Here we work with try/except syntax to handle any potential errors
    try:
        print('Connecting to TNY-360...')
        await robot.connect() # Wait for the asynchronous connection to be established
        print("Connected !")
    except Exception as err:
        print(f"Failed to connect to TNY-360: {err}")
        return

    # Cleanly close the connection before exiting
    await robot.disconnect()

# Run the async main function inside the event loop
if __name__ == '__main__':
    asyncio.run(main())

Sending ping commands

Now that we know how to connect, let's communicate with the robot's internal systems. Here is an example that sends 10 sequential pings and measures the average network response time:

ping.py
import asyncio
import time
from tny_robotics import TNY360

ROBOT_IP_ADDR = '192.168.4.1'

async def main():
    robot = TNY360(ROBOT_IP_ADDR)

    try:
        print('Connecting to TNY-360...')
        await robot.connect()
        print("Connected !")
    except Exception as err:
        print(f"Failed to connect to TNY-360: {err}")
        return

    # Now that we're connected, we can send ping commands to the robot
    print('Sending pings...')
    start = time.time() # We get the start time to calculate latency later
    
    for _ in range(10):
        try:
            await robot.system.ping()
        except Exception as e:
            print(f"Ping failed: {e}")
            
    end = time.time()
    
    # Calculate average response time in milliseconds
    avg_latency = ((end - start) * 1000) / 10
    print(f"Average response time: {avg_latency:.2f} ms.")

    await robot.disconnect()

if __name__ == '__main__':
    asyncio.run(main())

Moving the robot

Sending pings is fun, but you probably want to move your robot around! Python's asyncio.sleep makes it very easy to command a movement and maintain it for a specific duration:

move.py
import asyncio
from tny_robotics import TNY360

ROBOT_IP_ADDR = '192.168.4.1'

async def main():
    robot = TNY360(ROBOT_IP_ADDR)

    try:
        print('Connecting to TNY-360...')
        await robot.connect()
        print("Connected !")
    except Exception as err:
        print(f"Failed to connect to TNY-360: {err}")
        return

    # Now that we're connected, we can send movement commands to the robot
    # For example, move forward at 0.4 m/s
    print('Moving forward...')
    # set_velocity(x_ms, y_ms, z_rads)
    await robot.body.set_velocity(0.4, 0.0, 0.0) 

    # Wait asynchronously for 2 seconds while the robot keeps moving
    await asyncio.sleep(2.0)

    # Stop the robot
    print('Stopping...')
    await robot.body.set_velocity(0.0, 0.0, 0.0)
    
    await robot.disconnect()

if __name__ == '__main__':
    asyncio.run(main())

Going further

This is just the tip of the iceberg! The tny-robotics-sdk package provides a wide range of functionalities to control your TNY-360.

But since the Javascript, Python, C/C++ and other SDKs are all built the same way, we have a dedicated API Reference section where you'll find all the details about the available methods, classes, and how to use them to unleash the full potential of your TNY-360!

Have fun!