Skip to content

Repository files navigation

rosnodejs Build Status

Install

npm install rosnodejs

Start a node

constrosnodejs=require('rosnodejs');rosnodejs.initNode('/my_node').then(()=>{// do stuff});

Publish/Subscribe

constnh=rosnodejs.nh;constsub=nh.subscribe('/chatter','std_msgs/String',(msg)=>{console.log('Got msg on chatter: %j',msg);});constpub=nh.advertise('/chatter','std_msgs/String');pub.publish({data: "hi"});

The advertise function takes an options object as optional third argument:

{queueSize: 1,latching: false//throttleMs: 0}

If you plan to publish messages right after another in the same function, set throttleMs: -1. Otherwise only the last message will be sent.

Udp transport (Experimental)

constnh=rosnodejs.nh;constsub=nh.subscribe('/chatter','std_msgs/String',(msg)=>{console.log('Got msg on chatter: %j',msg);},{transports: ["TCPROS","UDPROS"],// specify transports, default ["TCPROS"]dgramSize: 1500// optional: datagram packet size, default: 1500 bytes});constpub=nh.advertise('/chatter','std_msgs/String');pub.publish({data: "hi"});

Services

constservice=nh.advertiseService('/add_two_ints','beginner_tutorials/AddTwoInts',(req,res)=>{res.sum=req.a+req.b;returntrue;});constclient=nh.serviceClient('/add_two_ints','beginner_tutorials/AddTwoInts');client.call({a: 1,b: 2});

Params

nh.setParam('val',2);nh.getParam('val').then((val)=>{// do stuff});

Generating Messages

Messages can be generated in a number of ways depending on the versions of ROS and Node.js you're using.

  • catkin - works in ROS Kinetic and later for Node.js v6+.
$ catkin_make
OR
$ catkin build
  • loadAllPackages() - One-time "build" call available through rosnodejs for versions of ROS before Kinetic and Node.js v6+. Should be called separately and in advance of processes attempting to use messages.
constrosnodejs=require('rosnodejs');rosnodejs.loadAllPackages();
  • On the fly - all versions of ROS and Node.js 4.5+. When generating on the fly, messages can not be required until the node has initialized.
constrosnodejs=require('rosnodejs');rosnodejs.initNode('my_node',{onTheFly: true}).then(()=>{conststdMsgs=rosnodejs.require('std_msgs');
...
}
Pre-KineticKinetic & Later
Node.js >= v6loadAllPackages(), on the flycatkin, loadAllPackages(), on the fly
Node.js < v6on the flyon the fly

Using Messages

constsensorMsgs=rosnodejs.require('sensor_msgs');constimage=newsensorMsgs.msg.Image();consttemperature=newsensorMsgs.msg.Temperature({temperature: 32});constSetCameraInfo=sensorMsgs.srv.SetCameraInfo;constsetRequest=newSetCameraInfo.Request();// messages can be used when advertising/subscribingconstnh=rosnodejs.nh;constStringMsg=rosnodejs.require('std_msgs').msg.String;constsub=nh.subscribe('/chatter',StringMsg,(msg)=>{ ... });constpub=nh.advertise('/chatter',StringMsg);constAddTwoInts=rosnodejs.require('beginner_tutorials').srv.AddTwoInts;constservice=nh.advertiseService('/add_two_ints',AddTwoInts,(req,res)=>{ ... });constclient=nh.serviceClient('/add_two_ints',AddTwoInts);

Actions (Experimental)

constnh=rosnodejs.nh;constas=newrosnodejs.ActionServer({
nh,type: 'turtle_actionlib/Shape',actionServer: '/turtle_shape'});as.on('goal',function(goal){goal.setAccepted();});as.start();constac=newrosnodejs.ActionClient({
nh,type: 'turtle_actionlib/Shape',actionServer:'/turtle_shape'});ac.sendGoal({edges: 3,radius: 1});

Run the turtlesim example

Start:

roscore
rosrun turtlesim turtlesim_node
rosrun turtle_actionlib shape_server

Then run

node src/examples/turtle.js

or, if you are running an older version of node:

npm run compile
node dist/examples/turtle.js

Catkin-Like

Checkout rosnodejs_examples for a more catkin-inspired take on using rosnodejs.

Inspired By

rosnodejs was inspired by other work that you can learn more about here

About

Client library for writing ROS nodes in JavaScript with nodejs

Topics

Resources

Stars

200 stars

Watchers

24 watching

Forks

Releases

Packages

Used by

Contributors

Languages