Skip to content

Change of API: hide ROSMessageWrapper type #189

@kenny-y

Description

@kenny-y

Purpose:

  • Remove rclnodejs.require() from customer-visible API
  • Use string directly to create publisher/subscriber/client/service
  • Provide a function that can create a blank message/request JavaScript object for later use
  • Add Response class for writing response to client

Example code 1:

  let obj = rclnodejs.createMessageObject('sensor_msgs/msg/JointState');
  obj.name = ['Alice', 'Bob'];
  obj.position = [0, 1];
  // obj.xxx = ...

  const publisher = node.createPublisher({package: 'sensor_msgs',
                                          type: 'msg',
                                          name: 'JointState'
                                          }, 'topic_name');
   -- OR --
  const publisher = node.createPublisher('sensor_msgs/msg/JointState', 'topic_name');

  publisher.publish({name: ['Alice', 'Bob'], position: [0, 1], /*...*/});
   -- OR --
  publisher.publish(obj);

Example code 2:

const msgType = {
  package: 'std_msgs',
  type: 'msg',
  name: 'String',
};

node.createService(msgType, ...);
node.createClient(msgType, ...);
node.createPublisher(msgType, ...);
node.createSubscription(msgType, ...);

Example code 3:

const rclnodejs = require('rclnodejs');

rclnodejs.init().then(() => {
  const node = rclnodejs.createNode('publisher_message_example_node');
  const publisher = node.createPublisher('sensor_msgs/msg/JointState', 'TopicJointState');
  let count = 0;

  setInterval(function() {
    publisher.publish({
      header: {
        stamp: {
          sec: 123456,
          nanosec: 789,
        },
        frame_id: 'main frame',
      },
      name: ['Tom', 'Jerry'],
      position: [1, 2],
      velocity: [2, 3],
      effort: [4, 5, 6],
    });
    console.log(`Publish ${++count} messages.`);
  }, 1000);

  rclnodejs.spin(node);
});

Example code 4:

const rclnodejs = require('rclnodejs');

rclnodejs.init().then(() => {
  let node = rclnodejs.createNode('service_example_node');

  node.createService('example_interfaces/srv/AddTwoInts', 'add_two_ints', (request, response) => {
    console.log(`Incoming request: ${typeof request}`, request);
    let result = response.template;
    result.sum = request.a + request.b;
    console.log(`Sending response: ${typeof result}`, result, '\n--');
    response.send(result);
  });

  rclnodejs.spin(node);
});

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions