Skip to content

Latest commit

 

History

History
 
 

examples

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Common examples and code snippets

Connect to a rosbridge endpoint and subscribe to topic

import { Ros, Topic } from '@robostack/roslib';

const ros = new Ros();
ros.connect('ws://localhost:9090');

const listenerTopic = new Topic({
  ros,
  name: '/listener',
  messageType : 'std_msgs/String'
});

listenerTopic.subscribe((message) => {
  console.log(message);
});

Publish a topic

const cmdVel = new Topic({
  ros,
  name: '/cmd_vel',
  messageType: 'geometry_msgs/Twist'
});

const twist = new Message({
  linear: { x : 0.1, y : 0.2, z : 0.3 },
  angular: { x : -0.1, y : -0.2, z : -0.3 }
});

cmdVel.publish(twist);

Call a service

const addTwoIntsClient = new Service({
  ros,
  name: '/add_two_ints',
  serviceType : 'rospy_tutorials/AddTwoInts'
});

const request = new ServiceRequest({
  a: 1,
  b: 2
});

addTwoIntsClient.callService(request, (result) => {
  console.log('Result for service call on ' + addTwoIntsClient.name + ': ' + result.sum);
});

Advertising a service

const setBoolServer = new Service({
  ros,
  name: '/set_bool',
  serviceType: 'std_srvs/SetBool'
});

setBoolServer.advertise((request, response) => {
  console.log('Received service request on ' + setBoolServer.name + ': ' + request.data);
  response['success'] = true;
  response['message'] = 'Set successfully';
  return true;
});

List all available params

ros.getParams((params) => {
  console.log(params);
});

List all available topics

ros.getTopics((topics) => {
  console.log(topics);
});

Set the value of a param

const maxVelX = new Param({
  ros,
  name: 'max_vel_y'
});

maxVelX.set(0.8);

Get the value of a param

const favoriteColor = new Param({
  ros,
  name: 'favorite_color'
});

favoriteColor.get((value) => {
  console.log(`My robot's favorite color is ${value}`);
});