Skip to content

Commit df09ca4

Browse files
authored
merge typescript branch (#545)
merge typescript branch
1 parent f0d44a6 commit df09ca4

34 files changed

+4778
-21
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@ npm-debug.log
1414
tags
1515
cpplint.py
1616
generated
17+
types/interface.d.ts
1718
dist
19+
.vscode
20+
.project

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ branches:
77
only:
88
- develop
99
- master
10-
- actionlib
1110

1211
before_install:
1312
- sudo docker pull ubuntu:bionic

README.md

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# rclnodejs - ROS Client Library for JavaScript language[![npm](https://img.shields.io/npm/v/rclnodejs.svg)](https://www.npmjs.com/package/rclnodejs)[![Coverage Status](https://coveralls.io/repos/github/RobotWebTools/rclnodejs/badge.svg?branch=develop)](https://coveralls.io/github/RobotWebTools/rclnodejs?branch=develop)[![npm](https://img.shields.io/npm/dm/rclnodejs)](https://www.npmjs.com/package/rclnodejs)[![GitHub license](https://img.shields.io/github/license/RobotWebTools/rclnodejs.svg)](https://github.com/RobotWebTools/rclnodejs/blob/develop/LICENSE)[![node](https://img.shields.io/node/v/rclnodejs.svg)](https://nodejs.org/en/download/releases/)[![dependencies Status](https://david-dm.org/RobotWebTools/rclnodejs/status.svg)](https://david-dm.org/RobotWebTools/rclnodejs)
1+
# rclnodejs - ROS2 Client Library for JavaScript [![npm](https://img.shields.io/npm/v/rclnodejs.svg)](https://www.npmjs.com/package/rclnodejs)[![Coverage Status](https://coveralls.io/repos/github/RobotWebTools/rclnodejs/badge.svg?branch=develop)](https://coveralls.io/github/RobotWebTools/rclnodejs?branch=develop)[![npm](https://img.shields.io/npm/dm/rclnodejs)](https://www.npmjs.com/package/rclnodejs)[![GitHub license](https://img.shields.io/github/license/RobotWebTools/rclnodejs.svg)](https://github.com/RobotWebTools/rclnodejs/blob/develop/LICENSE)[![node](https://img.shields.io/node/v/rclnodejs.svg)](https://nodejs.org/en/download/releases/)[![dependencies Status](https://david-dm.org/RobotWebTools/rclnodejs/status.svg)](https://david-dm.org/RobotWebTools/rclnodejs)
22

33
Branch | Linux Build | macOS Build | Windows Build |
44
------------ | :-------------: | :-------------: | :-------------: |
@@ -94,9 +94,9 @@ npm install
9494
set PATH=<path\to\python 2.x>;%PATH%
9595
```
9696

97-
## Run Unit Test
97+
## Run Unit Tests
9898

99-
[mocha](https://www.npmjs.com/package/mocha) is a javascript test framework for node.js, simply run the following command to run the unit test under `test` folder:
99+
The test suite is implemented using the [mocha](https://www.npmjs.com/package/mocha) JavaScript test framework for node.js. Run the unit tests:
100100

101101
```javascript
102102
npm run test
@@ -129,19 +129,58 @@ rclnodejs.init().then(() => {
129129

130130
rclnodejs.spin(node);
131131
});
132-
```
132+
```
133133

134-
There are also several useful examples under the `example` folder, which will show you how to use some important features, including `timer/subscription/publisher/client/service/time/node graph`. You are encouraged to try these examples to understand them.
134+
Browse the JavaScript programs in the `example` folder to learn how to create and work with `timers, subscriptions, publishers, clients, services, timers,` and `nodes`.
135135

136136
## API Specification
137137

138-
The API spec is generated by `jsdoc`, you can manually run `npm run docs` to create them by yourself, or just use the existing documents under `docs` folder.
138+
The API spec is generated by `jsdoc`. To create a local copy run `npm run docs`. Alternatively you can use the prebuilt api documentation found in the `docs` folder or view the [on-line version](http://robotwebtools.org/rclnodejs/docs/index.html) in your browser.
139+
140+
141+
## TypeScript Support
142+
Type declaration files (*.d.ts) are included to support developers that wish to use rclnodejs in TypeScript projects.
143+
144+
In your node project, in addition to installing the rclnodejs package, you will need to install the TypeScript compiler and node typings.
145+
```
146+
npm install typescript @types/node -D
147+
```
148+
149+
Your tsconfig.json file should include the following compiler options:
150+
```json
151+
{
152+
"compilerOptions": {
153+
"module": "commonjs",
154+
"moduleResolution": "node",
155+
"target": "es6",
156+
// your additional options here
157+
}
158+
}
159+
```
160+
161+
Here's a simple example implemented in TypeScript.
162+
```
163+
import * as rclnodejs from 'rclnodejs';
164+
rclnodejs.init().then(() => {
165+
const node = rclnodejs.createNode('publisher_example_node');
166+
const publisher = node.createPublisher('std_msgs/msg/String', 'topic');
167+
publisher.publish(`Hello ROS 2.0 from rclnodejs`);
168+
rclnodejs.spin(node);
169+
});
170+
```
171+
172+
The benefits of using TypeScript become evident when working with more complex messages. The ROS2 messages are defined in the interfaces.d.ts module. This module is updated as part of the generate_messages process. Here's a trivial example of working with a String msg.
173+
```
174+
const msg: rclnodejs.std_msgs.msg.String = {
175+
data: 'hello ROS2 from rclnodejs'
176+
}
177+
```
178+
Smart TypeScript tools such as Visual Studio Code and the CodeMix plugin for Eclipse will help you learn the rclnodejs api and identify issues while coding rather than at runtime.
139179

140-
To visit the on-line version, please navigate to http://robotwebtools.org/rclnodejs/docs/index.html in your browser.
141180

142181
## Experimental - Deprecated
143182

144-
* [actionlib](https://github.com/RobotWebTools/rclnodejs/blob/develop/tutorials/actionlib.md) - as the [rcl](https://github.com/ros2/rcl) library has implemented the action related functions, we are going to drop this one and we don't garantee the current actionlib can work with [rclcpp](https://github.com/ros2/rclcpp).
183+
* [actionlib](https://github.com/RobotWebTools/rclnodejs/blob/develop/tutorials/actionlib.md) - as the [rcl](https://github.com/ros2/rcl) library has implemented the action related functions, we are going to drop this one and we don't guarantee the current actionlib can work with [rclcpp](https://github.com/ros2/rclcpp).
145184

146185
## Troubleshooting
147186

index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const Node = require('./lib/node.js');
2424
const path = require('path');
2525
const QoS = require('./lib/qos.js');
2626
const rclnodejs = require('bindings')('rclnodejs');
27+
const tsdGenerator = require('./rostsd_gen/index.js');
2728
const validator = require('./lib/validator.js');
2829
const ActionLib = require('ros2-actionlibjs');
2930
const Time = require('./lib/time.js');
@@ -236,10 +237,11 @@ let rcl = {
236237
regenerateAll() {
237238
// This will trigger to regererate all the JS structs used for messages and services,
238239
// to overwrite the existing ones although they have been created.
239-
debug('Begin to regenerate JavaScript code from ROS IDL files.');
240+
debug('Begin regeneration of JavaScript code from ROS IDL files.');
240241
return new Promise((resolve, reject) => {
241242
generator.generateAll(true).then(() => {
242-
debug('Finish regenerating.');
243+
tsdGenerator.generateAll(); // create interfaces.d.ts
244+
debug('Finish regeneration.');
243245
resolve();
244246
}).catch((e) => {
245247
reject(e);

0 commit comments

Comments
 (0)