Skip to content

Commit

Permalink
merge typescript branch (#545)
Browse files Browse the repository at this point in the history
merge typescript branch
  • Loading branch information
wayneparrott authored Dec 13, 2019
1 parent f0d44a6 commit df09ca4
Show file tree
Hide file tree
Showing 34 changed files with 4,778 additions and 21 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ npm-debug.log
tags
cpplint.py
generated
types/interface.d.ts
dist
.vscode
.project
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ branches:
only:
- develop
- master
- actionlib

before_install:
- sudo docker pull ubuntu:bionic
Expand Down
55 changes: 47 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 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)
# 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)

Branch | Linux Build | macOS Build | Windows Build |
------------ | :-------------: | :-------------: | :-------------: |
Expand Down Expand Up @@ -94,9 +94,9 @@ npm install
set PATH=<path\to\python 2.x>;%PATH%
```

## Run Unit Test
## Run Unit Tests

[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:
The test suite is implemented using the [mocha](https://www.npmjs.com/package/mocha) JavaScript test framework for node.js. Run the unit tests:

```javascript
npm run test
Expand Down Expand Up @@ -129,19 +129,58 @@ rclnodejs.init().then(() => {

rclnodejs.spin(node);
});
```
```

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.
Browse the JavaScript programs in the `example` folder to learn how to create and work with `timers, subscriptions, publishers, clients, services, timers,` and `nodes`.

## API Specification

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.
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.


## TypeScript Support
Type declaration files (*.d.ts) are included to support developers that wish to use rclnodejs in TypeScript projects.

In your node project, in addition to installing the rclnodejs package, you will need to install the TypeScript compiler and node typings.
```
npm install typescript @types/node -D
```

Your tsconfig.json file should include the following compiler options:
```json
{
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"target": "es6",
// your additional options here
}
}
```

Here's a simple example implemented in TypeScript.
```
import * as rclnodejs from 'rclnodejs';
rclnodejs.init().then(() => {
const node = rclnodejs.createNode('publisher_example_node');
const publisher = node.createPublisher('std_msgs/msg/String', 'topic');
publisher.publish(`Hello ROS 2.0 from rclnodejs`);
rclnodejs.spin(node);
});
```

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.
```
const msg: rclnodejs.std_msgs.msg.String = {
data: 'hello ROS2 from rclnodejs'
}
```
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.

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

## Experimental - Deprecated

* [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).
* [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).

## Troubleshooting

Expand Down
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const Node = require('./lib/node.js');
const path = require('path');
const QoS = require('./lib/qos.js');
const rclnodejs = require('bindings')('rclnodejs');
const tsdGenerator = require('./rostsd_gen/index.js');
const validator = require('./lib/validator.js');
const ActionLib = require('ros2-actionlibjs');
const Time = require('./lib/time.js');
Expand Down Expand Up @@ -236,10 +237,11 @@ let rcl = {
regenerateAll() {
// This will trigger to regererate all the JS structs used for messages and services,
// to overwrite the existing ones although they have been created.
debug('Begin to regenerate JavaScript code from ROS IDL files.');
debug('Begin regeneration of JavaScript code from ROS IDL files.');
return new Promise((resolve, reject) => {
generator.generateAll(true).then(() => {
debug('Finish regenerating.');
tsdGenerator.generateAll(); // create interfaces.d.ts
debug('Finish regeneration.');
resolve();
}).catch((e) => {
reject(e);
Expand Down
Loading

0 comments on commit df09ca4

Please sign in to comment.