npm install rosnodejs
constrosnodejs=require('rosnodejs');rosnodejs.initNode('/my_node').then(()=>{// do stuff});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.
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"});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});nh.setParam('val',2);nh.getParam('val').then((val)=>{// do stuff});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 buildloadAllPackages()- One-time "build" call available throughrosnodejsfor 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-Kinetic | Kinetic & Later | |
|---|---|---|
| Node.js >= v6 | loadAllPackages(), on the fly | catkin, loadAllPackages(), on the fly |
| Node.js < v6 | on the fly | on the fly |
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);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});Start:
roscore
rosrun turtlesim turtlesim_node
rosrun turtle_actionlib shape_serverThen run
node src/examples/turtle.jsor, if you are running an older version of node:
npm run compile
node dist/examples/turtle.jsCheckout rosnodejs_examples for a more catkin-inspired take on using rosnodejs.
rosnodejs was inspired by other work that you can learn more about here