Skip to content

Commit 33ce31e

Browse files
authored
Create npmjs-readme.md
1 parent 8869a79 commit 33ce31e

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

scripts/npmjs-readme.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
`rclnodejs` is a Node.js client for Robot Operating System (ROS) v2.0. It provides extremely simple & easy API for ROS 2.0 programming, the following examples shows how to create a ROS 2.0 node and then publish a string message in only 6 lines of code.
2+
3+
``` JavaScript
4+
const rclnodejs = require('rclnodejs');
5+
rclnodejs.init().then(() => {
6+
const node = rclnodejs.createNode('publisher_example_node');
7+
const publisher = node.createPublisher('std_msgs/msg/String', 'topic');
8+
publisher.publish(`Hello ROS 2.0 from rclnodejs`);
9+
rclnodejs.spin(node);
10+
});
11+
```
12+
13+
# Install ROS 2.0
14+
Befire install rclnodejs, make sure ROS 2.0 is installed first. Read and follow the [Installation Guide](https://github.com/ros2/ros2/wiki/Installation) to install ROS 2.0
15+
16+
# Install rclnodejs
17+
After ROS 2.0 is installed, run the following command
18+
19+
```
20+
npm i --save rclnodejs
21+
```
22+
23+
# More Examples
24+
25+
Use complex message
26+
27+
```JavaScript
28+
const publisher = node.createPublisher('sensor_msgs/msg/JointState', 'topic');
29+
publisher.publish({
30+
header: {
31+
stamp: {
32+
sec: 123456,
33+
nanosec: 789,
34+
},
35+
frame_id: 'main frame',
36+
},
37+
name: ['Tom', 'Jerry'],
38+
position: [1, 2],
39+
velocity: [2, 3],
40+
effort: [4, 5, 6],
41+
});
42+
43+
```
44+
45+
Create a subscription
46+
47+
``` JavaScript
48+
const rclnodejs = require('../index.js');
49+
50+
// Create a ROS node and then print out the string message received from publishers
51+
rclnodejs.init().then(() => {
52+
const node = rclnodejs.createNode('subscription_example_node');
53+
54+
node.createSubscription('std_msgs/msg/String', 'topic', (msg) => {
55+
console.log(`Received message: ${typeof msg}`, msg);
56+
});
57+
58+
rclnodejs.spin(node);
59+
});
60+
```
61+
62+
63+
64+
Create a service
65+
66+
```JavaScript
67+
node.createService('example_interfaces/srv/AddTwoInts', 'add_two_ints', (request, response) => {
68+
console.log(`Incoming request: ${typeof request}`, request);
69+
let result = response.template;
70+
result.sum = request.a + request.b;
71+
console.log(`Sending response: ${typeof result}`, result, '\n--');
72+
response.send(result);
73+
});
74+
75+
```
76+
77+
Send a request in a client
78+
79+
```JavaScript
80+
const client = node.createClient('example_interfaces/srv/AddTwoInts', 'add_two_ints');
81+
const request = {
82+
a: Math.floor(Math.random() * 100),
83+
b: Math.floor(Math.random() * 100),
84+
};
85+
86+
console.log(`Sending: ${typeof request}`, request);
87+
client.sendRequest(request, (response) => {
88+
console.log(`Result: ${typeof response}`, response);
89+
});
90+
91+
```
92+
93+
# License
94+
Apache License Version 2.0
95+

0 commit comments

Comments
 (0)