You want to control your TNY-360 using JavaScript? We got you covered!
This section is here to get you started with the JavaScript / TypeScript SDK for your TNY-360.
The first step to control your TNY-360 with JS/TS is to install the @tny-robotics/sdk package.
This package provides a high-level API to interact with your TNY-360, allowing you to send commands, receive sensor data, and manage the robot's state.
To install the package, run the following command in your project directory:
npm install @tny-robotics/sdk
(you can replace npm with yarn or pnpm if you prefer)
Remember to check for updates regularly, as we are continuously improving the SDK and adding new features!
You can do this by running:
npm update @tny-robotics/sdk
Once you have the SDK installed, you can start using it to control your TNY-360.
Here's a couple of basic examples to get you started:
The first thing you'll want to do is connect to your TNY-360. Here's how to do it in a few lines of code:
import { TNY360 } from '@tny-robotics/sdk' // Import the TNY-360 class from the SDK
// Find the IP address of your TNY-360 (available in the network > wifi menu)
const ROBOT_IP_ADDR = '192.168.4.1';
// Create a new instance of the TNY-360 class
let robot = new TNY360(ROBOT_IP_ADDR)
// Try connecting to the robot
console.log('Connecting to TNY-360...')
robot.connect().then(() => { // robot.connect() returns a promise, so we use .then() to handle the successful connection
console.log("Connected !")
}).catch((err) => { // If there was an error during connection, we catch it here and log it
console.log(`Failed to connect to TNY-360: ${err}`)
});
Since the robot.connect() method returns a promise, you way want to use async/await syntax instead. Here's how you can do it:
import { TNY360 } from '@tny-robotics/sdk' // Import the TNY-360 class from the SDK
// Find the IP address of your TNY-360 (available in the network > wifi menu)
const ROBOT_IP_ADDR = '192.168.4.1';
// Since we want to use async/await, we need to wrap our code in an async function
async function main() {
// Create a new instance of the TNY-360 class
let robot = new TNY360(ROBOT_IP_ADDR)
// Here we work with try/catch syntax to handle any potential errors
try {
console.log('Connecting to TNY-360...')
await robot.connect() // Wait for the connection to be established
console.log("Connected !")
} catch (err) {
console.log(`Failed to connect to TNY-360: ${err}`)
return; // Exit the function if we failed to connect
}
// Now that we're connected, we can send ping commands to the robot
console.log('Sending pings...');
const start = Date.now(); // We get the time to calculate the average response time later
for (let i = 0; i < 10; i++) {
await robot.system.ping(); // NOTE : You should wrap this in a try/catch block in case the ping fails
}
const end = Date.now();
console.log(`Average response time: ${(end - start) / 10} ms.`);
}
// Call the main function to execute our code
main();
Sending pins is fun, but you probably want to move your robot around! Here's how you can do it:
import { TNY360 } from '@tny-robotics/sdk' // Import the TNY-360 class from the SDK
// Find the IP address of your TNY-360 (available in the network > wifi menu)
const ROBOT_IP_ADDR = '192.168.4.1';
async function main() {
let robot = new TNY360(ROBOT_IP_ADDR)
try {
console.log('Connecting to TNY-360...')
await robot.connect()
console.log("Connected !")
} catch (err) {
console.log(`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.4m/s
console.log('Moving forward...');
await robot.body.setVelocity(0.4, 0, 0); // setVelocity(linearX, linearY, angularZ)
// Wait for 2 seconds while the robot is moving
await new Promise(resolve => setTimeout(resolve, 2000));
// Stop the robot
console.log('Stopping...');
await robot.body.setVelocity(0, 0, 0);
}
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 SDK Documentation 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!