-
Notifications
You must be signed in to change notification settings - Fork 85
Update the README for npmjs #1233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2,6 +2,8 @@ | |||||
|
|
||||||
| `rclnodejs` is a Node.js client for the Robot Operating System (ROS 2). It provides a simple and easy JavaScript API for ROS 2 programming. TypeScript declarations are included to support use of rclnodejs in TypeScript projects. | ||||||
|
|
||||||
| \* rclnodejs development and maintenance is limited to all active ROS 2 LTS releases and the Rolling development branch | ||||||
|
|
||||||
| Here's an example for how to create a ROS 2 node that publishes a string message in a few lines of JavaScript. | ||||||
|
|
||||||
| ```JavaScript | ||||||
|
|
@@ -14,163 +16,66 @@ rclnodejs.init().then(() => { | |||||
| }); | ||||||
| ``` | ||||||
|
|
||||||
| ## Prerequisites | ||||||
|
|
||||||
| **Node.js** | ||||||
|
|
||||||
| - [Node.js](https://nodejs.org/en/) version >= 16.13.0. | ||||||
| ## Installation | ||||||
|
|
||||||
| **ROS 2 SDK** | ||||||
| ### Prerequisites | ||||||
|
|
||||||
| - See the ROS 2 SDK [Installation Guide](https://docs.ros.org/en/kilted/Installation.html) for details. | ||||||
| - **DON'T FORGET TO [SOURCE THE ROS 2 STARTUP FILES](https://docs.ros.org/en/kilted/Tutorials/Beginner-CLI-Tools/Configuring-ROS2-Environment.htmls)** | ||||||
| - [Node.js](https://nodejs.org/en/) version >= 16.13.0 | ||||||
| - [ROS 2 SDK](https://docs.ros.org/en/jazzy/Installation.html) - **Don't forget to [source the setup file](https://docs.ros.org/en/jazzy/Tutorials/Beginner-CLI-Tools/Configuring-ROS2-Environment.html#source-the-setup-files)** | ||||||
|
|
||||||
| ## Install rclnodejs | ||||||
|
|
||||||
| Install the rclnodejs version that is compatible with your installed version of ROS 2 (see table below). | ||||||
|
|
||||||
| Run the following command for the most current version of rclnodejs | ||||||
| ### Install rclnodejs | ||||||
|
|
||||||
| ```bash | ||||||
| npm i rclnodejs | ||||||
| ``` | ||||||
|
|
||||||
| or to install a specific version of rclnodejs use | ||||||
|
|
||||||
| ```bash | ||||||
| npm i rclnodejs@x.y.z | ||||||
| ``` | ||||||
|
|
||||||
| #### RCLNODEJS - ROS 2 Version Compatibility | ||||||
|
|
||||||
| | RCLNODEJS Version | Compatible ROS 2 LTS | | ||||||
| | :----------------------------------------------------------------------------------------: | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | | ||||||
| | latest version (currently [v1.5.0](https://github.com/RobotWebTools/rclnodejs/tree/1.5.0)) | [Kilted](https://github.com/RobotWebTools/rclnodejs/tree/kilted)<br>[Jazzy](https://github.com/RobotWebTools/rclnodejs/tree/jazzy)<br>[Humble](https://github.com/RobotWebTools/rclnodejs/tree/humble-hawksbill) | | ||||||
| - **Note:** to install rclnodejs from GitHub: add `"rclnodejs":"RobotWebTools/rclnodejs#<branch>"` to your `package.json` dependency section. | ||||||
|
|
||||||
| ## Documentation | ||||||
|
|
||||||
| API [documentation](https://robotwebtools.github.io/rclnodejs/docs/index.html) is available online. | ||||||
|
|
||||||
| ## JavaScript Examples | ||||||
|
|
||||||
| The source for the following examples and many others can be found [here](https://github.com/RobotWebTools/rclnodejs/tree/develop/example). | ||||||
|
|
||||||
| Use complex message | ||||||
|
|
||||||
| ```JavaScript | ||||||
| const publisher = node.createPublisher('sensor_msgs/msg/JointState', 'topic'); | ||||||
| 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], | ||||||
| }); | ||||||
|
|
||||||
| ``` | ||||||
|
|
||||||
| Create a subscription | ||||||
|
|
||||||
| ```JavaScript | ||||||
| const rclnodejs = require('../index.js'); | ||||||
|
|
||||||
| // Create a ROS node and then print out the string message received from publishers | ||||||
| rclnodejs.init().then(() => { | ||||||
| const node = rclnodejs.createNode('subscription_example_node'); | ||||||
|
|
||||||
| node.createSubscription('std_msgs/msg/String', 'topic', (msg) => { | ||||||
| console.log(`Received message: ${typeof msg}`, msg); | ||||||
| }); | ||||||
|
|
||||||
| rclnodejs.spin(node); | ||||||
| }); | ||||||
| ``` | ||||||
|
|
||||||
| Create a service | ||||||
|
|
||||||
| ```JavaScript | ||||||
| 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); | ||||||
| }); | ||||||
|
|
||||||
| ``` | ||||||
|
|
||||||
| Send a request in a client | ||||||
|
|
||||||
| ```JavaScript | ||||||
| const client = node.createClient('example_interfaces/srv/AddTwoInts', 'add_two_ints'); | ||||||
| const request = { | ||||||
| a: Math.floor(Math.random() * 100), | ||||||
| b: Math.floor(Math.random() * 100), | ||||||
| }; | ||||||
|
|
||||||
| console.log(`Sending: ${typeof request}`, request); | ||||||
| client.sendRequest(request, (response) => { | ||||||
| console.log(`Result: ${typeof response}`, response); | ||||||
| }); | ||||||
|
|
||||||
| ``` | ||||||
|
|
||||||
| Check out more [examples](https://github.com/RobotWebTools/rclnodejs/tree/develop/example). | ||||||
| Try the [examples](https://github.com/RobotWebTools/rclnodejs/tree/develop/example) to get started. | ||||||
|
|
||||||
| ## Using rclnodejs with TypeScript | ||||||
|
|
||||||
| In your node project install the rclnodejs package as described above. You will also need the TypeScript compiler and node typings installed. | ||||||
|
|
||||||
| ``` | ||||||
| npm install typescript @types/node -D | ||||||
| ``` | ||||||
|
|
||||||
| In your project's tsconfig.json file include the following compiler options: | ||||||
| TypeScript declaration files are included in the `types/` folder. Configure your `tsconfig.json`: | ||||||
|
|
||||||
| ```jsonc | ||||||
| { | ||||||
| "compilerOptions": { | ||||||
| "module": "commonjs", | ||||||
| "moduleResolution": "node", | ||||||
| "target": "es6", | ||||||
| ... | ||||||
| } | ||||||
| "target": "es2020", | ||||||
| }, | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| Here's an earlier JavaScript example reimplemented in TypeScript. | ||||||
| TypeScript example: | ||||||
|
|
||||||
| ```typescript | ||||||
| import * as rclnodejs from 'rclnodejs'; | ||||||
| rclnodejs.init().then(() => { | ||||||
| const node = rclnodejs.createNode('publisher_example_node'); | ||||||
| const node = new rclnodejs.Node('publisher_example_node'); | ||||||
|
||||||
| const node = new rclnodejs.Node('publisher_example_node'); | |
| const node = rclnodejs.createNode('publisher_example_node'); |
Copilot
AI
Aug 26, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The TypeScript example uses node.spin() while the JavaScript example uses rclnodejs.spin(node). This API inconsistency could confuse users. Both examples should demonstrate the same API pattern for consistency.
| node.spin(); | |
| rclnodejs.spin(node); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The backslash before the asterisk (
\*) creates escaped markdown. This should be just*to render as a proper bullet point, or use-for consistency with other list items in the document.