Skip to content

Instantly share code, notes, and snippets.

@chrismatthieu
Last active February 11, 2025 21:11
Show Gist options
  • Save chrismatthieu/58572a713bcc0141675df2ca3c02574a to your computer and use it in GitHub Desktop.
Save chrismatthieu/58572a713bcc0141675df2ca3c02574a to your computer and use it in GitHub Desktop.
Sample robotics.dev boiler plate code
//import robotics from 'robotics-dev';
const robotics = require('robotics-dev');
// Define ROS twist movement commands
const moveForward = {
linear: {x: 0.2, y: 0.0, z: 0.0},
angular: {x: 0.0, y: 0.0, z: 0.0}
};
const turnLeft = {
linear: {x: 0.0, y: 0.0, z: 0.0},
angular: {x: 0.0, y: 0.0, z: 0.2}
};
const turnRight = {
linear: {x: 0.0, y: 0.0, z: 0.0},
angular: {x: 0.0, y: 0.0, z: -0.2}
};
const stop = {
linear: {x: 0.0, y: 0.0, z: 0.0},
angular: {x: 0.0, y: 0.0, z: 0.0}
};
var latestImage;
var oneTime = true;
const robotId = 'ENTER ROBOTICS.DEV ROBOT ID HERE';
const apiToken = 'ENTER ROBOTICS.DEV DEVELOPER API TOKEN HERE';
// Connect SDK to robot via P2P and start listening for ROS messages
robotics.connect({robot: robotId, token: apiToken}, (ros) => {
console.log('Received p2p data:', ros);
// {
// robotId: '3bbb40c5-xxxx-yyyy-zzzz-58a12bc6893e',
// topic: '/camera2d',
// data: {
// data: xyz
// }
// }
if(ros.topic === "/camera2d"){
console.log("Base64 image: ", ros.data.data);
latestImage = ros.data.data
}
if(oneTime){
oneTime = false;
robotics.speak(robotId, 'this is a test')
console.log('Moving robot forward at 20% speed...');
robotics.twist(robotId, moveForward);
// Stop after 5 seconds
setTimeout(() => {
console.log('Stopping robot...');
robotics.twist(robotId, stop);
}, 5000);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment