Publishers

You can create publishers on a given topic using a NodeHandle from rosnodejs.

const nh = rosnodejs.nh;
const pub = nh.advertise('/topic', 'std_msgs/String');

When creating publishers, you can specify the type of message to publish with either a string, as above, or using the class itself.

const std_msgs = rosnodejs.require('std_msgs');

const nh = rosnodejs.nh;
const pub = nh.advertise('/topic', std_msgs.msg.String);

Initialization

rosnodejs Publishers also provide a number of options to allow you to customize their behavior for your use case. These are provided through an optional object after specifying the message type.

const options = {...};
nh.advertise('/topic', std_msgs.msg.String, options);

The additional options are

  • latching

    • Enable 'latching' on the connection. When a connection is latched, the last message published is saved and sent to any future subscribers that connect. False by default.

    tcpNoDelay

    • Sets noDelay on the publisher's socket. False by default.

    queueSize

    • This determines the size of the outgoing message queue for this publisher. Consecutive calls to publish will add new messages to the queue up to queueSize, at which point older messages will be dropped off. Interacts with throttleMs below. Default is 1.

    throttleMs

    • This determines the minimum interval at which this publisher's outgoing message queue will be handled. When calls to publish are made, the message is placed onto a queue and a timeout is set with throttleMs - when that timeout expires, all the messages that have made it into your queue (as determined by queueSize) will be sent. If throttleMs is negative, any call to publish will be handled immediately without being sent to the back of the event loop. Default is 0.

Publishing

Calls to publish should be passed an instance of the desired message object. So, if you have advertised a publisher with a type std_msgs/String, you would publish a message like so.

const pub = nh.advertise('/my_topic', 'std_msgs/String');
...
const msg = new std_msgs.msg.String();
msg.data = 'Hello!';
pub.publish(msg)

You can also pass a generic object with the required fields filled in.

pub.publish({ data: 'Hello!' });

When publishing, it is possible to specify a one-time override to the throttleMs setting on the publisher. For example, if in a specific instance you want to skip the event loop, you could do the following

pub.publish(msg, -1);

Events

rosnodejs Publishers are also event emitters. If desired, you can add handlers for the following events

  • registered

    • This will be fired when the publisher has successfully been registered with the master

    connection

    • This will be fired when a new subscriber connects to the publisher. This will not be fired until after the connection header has been handled and validated.

    disconnect

    • This will be fired when a subscriber disconnects from the publisher.
  • error

    • This will be fired when an error occurs on the publisher.

Complete Example

const std_msgs = rosnodejs.require('std_msgs');

rosnodejs.initNode('my_node')
.then((nh) => {
  const pub = nh.advertise('/my_topic', std_msgs.msg.String);
  const msg = new std_msgs.msg.String();
  msg.data = 'Hello, World';
  setInterval(() => {
    pub.publish(msg);
  }, 1000);
});

Subscribers

You also create subscribers on a given topic using a NodeHandle from rosnodejs.

const callback = function(msg) {
  console.log(msg.data);
}

const nh = rosnodejs.nh;
const sub = nh.subscribe('/topic', 'std_msgs/String', callback);

As with publishers, when creating subscribers you can specify the type of message to subscribe to with either a string or using the class itself.

const std_msgs = rosnodejs.require('std_msgs');

const nh = rosnodejs.nh;
const sub = nh.subscribe('/topic', std_msgs.msg.String, () => {});

Initialization

rosnodejs Subscribers also provide a few options during initialization. These are provided via an optional object after specifying the callback function.

const options = {...};
const sub = nh.subscribe('/topic', std_msgs.msg.String, () => {}, options);

The possible options are

  • queueSize

    • This determines the size of the incoming message queue for this subscriber. Before being handled, incoming messages will be queued up to queueSize at which point older messages will be dropped. Interacts with throttleMs below. Default is 1.

  • throttleMs

    • This determines the minimum interval at which this subscriber incoming message queue will be handled. When messages are received they are placed onto a queue and a timeout is set with throttleMs - when that timeout expires, all the messages that have made it into the queue (as determined by queueSize) will be sent to the provided callback function in order. If throttleMs is negative, messages will be handled as soon as they are available without interacting with the event loop. Default is 0.

Events

rosnodejs Subscribers are also event emitters. If desired, you can add handlers for two events

  • registered

    • This will be fired when the subscriber has successfully been registered with the master

    message

    • This will be fired when a new message is ready to be handled. When subscribing to the topic through the NodeHandle, the callback you provide is bound to this event for you.

    connection

    • This will be fired when a new publisher connects to the subscriber. This will not be fired until after the connection header has been handled and validated.

  • disconnect

    • This will be fired when a publisher disconnects from the publisher.
  • error

    • This will be fired when an error occurs on the subscriber.

Wiki: rosnodejs/overview/Publishers and Subscribers (last edited 2017-09-05 18:31:06 by chris-smith)