Code Monkey home page Code Monkey logo

rosjs's Introduction

Ros.js provides the core JavaScript API for interacting with ROS via the web. Ros.js supports publishing and subscribing to topics, getting and setting parameters, and calling services in ROS.

Ros.js uses rosbridge on the backend and connects to the server with WebSockets.

Table of Contents

How To

Connect to ROS

// Connects to the rosbridge WebSocket server.
var ros = new ROS('ws://localhost:9090');

Publish a topic

// ros.Topic provides publish and subscribe support for a ROS topic.
// Creates a geometry_msgs/Twist topic named /cmd_vel.
var cmdVel = new ros.Topic({
  node        : 'talker'
, name        : '/cmd_vel'
, messageType : 'geometry_msgs/Twist'
});

// ros.Message contains the data to publish.
var twist = new ros.Message({
  angular: {
    x: 1
  , y: 0
  , z: 0
  }
, linear: {
    x: 0
  , y: 0
  , z: 0
  }
});

// The geometry_msgs/Twist message will be published in ROS.
cmdVel.publish(twist);

Subscribe to a topic

// Create a std_msgs/String topic named /listener.
var listener = new ros.Topic({
  node        : 'chat'
, name        : '/listener'
, messageType : 'std_msgs/String'
});

// Any time a message is published to the /listener topic, the callback will
// fire.
listener.subscribe(function(message) {
  // message is an instance of ros.Message.
  console.log('Received message ' + message.data);
});

Call a service

// ros.Service provides an interface to calling ROS services.
// Creates a rospy_tutorials/AddTwoInts service client named /add_two_ints.
var addTwoIntsClient = new ros.Service({
  name        : '/add_two_ints'
, serviceType : 'rospy_tutorials/AddTwoInts'
});

// ros.ServiceRequest contains the data to send in the service call.
var request = new ros.ServiceRequest({ A: 1, B: 2});

// Calls the rospy_tutorials/AddTwoInts service with the result stored in the
// callback.
addTwoIntsClient.callService(request, function(result) {
  console.log('Result for service call on ' + addTwoIntsClient.name + ': ' + result.sum);
});

Set a param value

// ros.Param interfaces with the ROS Parameter Server.
var maxVelX = new ros.Param({
  name: 'max_vel_x'
});

// Sets the ROS param value.
maxVelX.set(0.75);

Get a param value

var maxVelX = new ros.Param({
  name: 'max_vel_x'
});

// Fetches and returns the param value in the callback.
maxVelX.get(function(value) {
  console.log('Value of ' + maxVelX.name + ' is ' + value);
});

Get ROS system info

// Retrieves the current list of topics in ROS
ros.getTopicList(function(topics) {
  console.log('Current topics in ROS: ' + topics);
});
// Fetches list of all active services in ROS
ros.getServiceList(function(services) {
  console.log('Current services in ROS: ' + services);
});
// Gets param names stored in the ROS Parameter Server
ros.getParamList(function(params) {
  console.log('Current params in ROS: ' + params);
});

Understanding Events

Ros.js follows the Observer patter, emitting (publishing) and listening (subscribing) to events.

EventEmitter2 provides event support to ros.js. The ROS, Topic, Service, and even Param objects all extend EventEmitter2 and emits events. For example, when the ROS object connects to the server, a 'connection' event is emitted. When a message is received for a topic, the 'message' event is emitted on that topic object.

To listen for an event, use on() like in the examples below:

var ros = new ROS('ws://localhost:9090');
ros.on('connection', function() {
  // This code will only be executed after ROS has connected to the server.
});

ros.on('close', function() {
  // This code will be executed when the connection to ROS disconnects.
});

Handling Errors

As described in the Events section, every ros.js class emits events. Errors are communicated as 'error' events. For example, the ros object will emit an 'error' event whenever there's an issue with ROS and a topic object will emit an event when there's an error publishing or subscribing to that topic.

It is highly recommended to at least listen for errors from the ros object, like so:

var ros = new ROS('ws://localhost:9090');
ros.on('error', function(error) {
  console.log('There was an error with ROS: ' + error);
});

License

The code is released under the BSD license. See the LICENSE file for details.

rosjs's People

Contributors

baalexander avatar sosentos avatar trevorjay avatar

Stargazers

Maxence Dalmais avatar

Watchers

James Cloos avatar Maxence Dalmais avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.