百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

rosnodejs

> 开发工具
开源

使用 nodejs 在 JavaScript 中编写 ROS 节点的客户端库

200 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

使用 nodejs 在 JavaScript 中编写 ROS 节点的客户端库

rosnodejs

Install

npm install rosnodejs

Start a node

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

Publish/Subscribe

javascript
const nh = rosnodejs.nh;
const sub = nh.subscribe('/chatter', 'std_msgs/String', (msg) => {
  console.log('Got msg on chatter: %j', msg);
});

const pub = nh.advertise('/chatter', 'std_msgs/String');
pub.publish({ data: "hi" });

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

javascript
{
  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)

javascript
const nh = rosnodejs.nh;
const sub = 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
});

const pub = nh.advertise('/chatter', 'std_msgs/String');
pub.publish({ data: "hi" });

Services

javascript
const service = nh.advertiseService('/add_two_ints', 'beginner_tutorials/AddTwoInts', (req, res) => {
  res.sum = req.a + req.b;
  return true;
});

const client = nh.serviceClient('/add_two_ints', 'beginner_tutorials/AddTwoInts');
client.call({a: 1, b: 2});

Params

javascript
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+.
bash
$ 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.
javascript
const rosnodejs = 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.
javascript
const rosnodejs = require('rosnodejs');
rosnodejs.initNode('my_node', { onTheFly: true }).then(() => {
  const stdMsgs = rosnodejs.require('std_msgs');
  ...
}
Pre-Kinetic Kinetic & Later
Node.js >= v6 loadAllPackages(), on the fly catkin, loadAllPackages(), on the fly
Node.js { ... });
const pub = nh.advertise('/chatter', StringMsg);

const AddTwoInts = rosnodejs.require('beginner_tutorials').srv.AddTwoInts; const service = nh.advertiseService('/add_two_ints', AddTwoInts, (req, res) => { ... }); const client = nh.serviceClient('/add_two_ints', AddTwoInts);

## Actions (Experimental)
```js
const nh = rosnodejs.nh;
const as = new rosnodejs.ActionServer({
  nh,
  type: 'turtle_actionlib/Shape',
  actionServer: '/turtle_shape'
});

as.on('goal', function (goal) {
  goal.setAccepted();
});

as.start();

const ac = new rosnodejs.ActionClient({
  nh,
  type: 'turtle_actionlib/Shape',
  actionServer:'/turtle_shape'
});

ac.sendGoal({edges: 3, radius: 1});

Run the turtlesim example

Start:

bash
roscore
rosrun turtlesim turtlesim_node
rosrun turtle_actionlib shape_server

Then run

bash
node src/examples/turtle.js

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

bash
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

  • roscpp & rospy
  • Robots as web services
  • ROSBridge
  • roslibjs

Issues· 17 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

JavaScriptclient-libraryjavascriptnodejsrobot

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类开发工具
定价开源

> 相关工具

V
VS Code
流行的开源代码编辑器
G
Git
分布式版本控制系统
V
Vite
下一代前端构建工具