Skip to content
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

Add getClock to Node object #592

Merged
merged 2 commits into from
Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const validator = require('./lib/validator.js');
const Time = require('./lib/time.js');
const TimeSource = require('./lib/time_source.js');
const { Clock, ROSClock } = require('./lib/clock.js');
const ClockType = require('./lib/clock_type.js');
const Duration = require('./lib/duration.js');
const Context = require('./lib/context.js');

Expand Down Expand Up @@ -99,6 +100,9 @@ let rcl = {
/** {@link ROSClock} class */
ROSClock: ROSClock,

/** {@link ClockType} enum */
ClockType: ClockType,

/** {@link Duration} class */
Duration: Duration,

Expand Down
7 changes: 7 additions & 0 deletions lib/clock.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ class Clock {
this._handle = rclnodejs.createClock(this._clockType);
}

/**
* @ignore
*/
get handle() {
return this._handle;
}

/**
* Get ClockType of this Clock object.
* @return {ClockType} Return the type of the clock.
Expand Down
35 changes: 32 additions & 3 deletions lib/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const Publisher = require('./publisher.js');
const QoS = require('./qos.js');
const Service = require('./service.js');
const Subscription = require('./subscription.js');
const TimeSource = require('./time_source.js');
const Timer = require('./timer.js');

const debug = require('debug')('rclnodejs:node');
Expand Down Expand Up @@ -91,6 +92,13 @@ class Node {
}
}

// Clock that has support for ROS time.
// Note: parameter overrides and parameter event publisher need to be ready at this point
// to be able to declare 'use_sim_time' if it was not declared yet.
this._clock = new Clock.ROSClock();
this._timeSource = new TimeSource(this);
this._timeSource.attachClock(this._clock);

if (options.startParameterServices) {
this._parameterService = new ParameterService(this);
this._parameterService.start();
Expand Down Expand Up @@ -217,9 +225,15 @@ class Node {
* @param {number} period - The number representing period in millisecond.
* @param {function} callback - The callback to be called when timeout.
* @param {Context} context - The context, default is Context.defaultContext().
* @param {Clock} clock - The clock which the timer gets time from.
* @return {Timer} - An instance of Timer.
*/
createTimer(period, callback, context = Context.defaultContext()) {
createTimer(
period,
callback,
context = Context.defaultContext(),
clock = null
) {
if (typeof period !== 'number' || typeof callback !== 'function') {
throw new TypeError('Invalid argument');
}
Expand All @@ -237,7 +251,13 @@ class Node {
);
}

let timerHandle = rclnodejs.createTimer(period, context.handle());
const timerClock = clock || this._clock;

let timerHandle = rclnodejs.createTimer(
timerClock.handle,
context.handle(),
period
);
let timer = new Timer(timerHandle, period, callback);
debug('Finish creating timer, period = %d.', period);
this._timers.push(timer);
Expand Down Expand Up @@ -458,6 +478,7 @@ class Node {
}

this.handle.release();
this._clock = null;
this._timers = [];
this._publishers = [];
this._subscriptions = [];
Expand Down Expand Up @@ -560,6 +581,14 @@ class Node {
return this._logger;
}

/**
* Get the clock used by the node.
* @returns {Clock} - The nodes clock.
*/
getClock() {
return this._clock;
}

/**
* Get the list of published topics discovered by the provided node for the remote node name.
* @param {string} nodeName - The name of the node.
Expand Down Expand Up @@ -1036,7 +1065,7 @@ class Node {
PARAMETER_EVENT_MSG_TYPE
))();

const secondsAndNanos = new Clock.ROSClock().now().secondsAndNanoseconds;
mattrichard marked this conversation as resolved.
Show resolved Hide resolved
const secondsAndNanos = this._clock.now().secondsAndNanoseconds;
parameterEvent.stamp = {
sec: secondsAndNanos.seconds,
nanosec: secondsAndNanos.nanoseconds,
Expand Down
20 changes: 8 additions & 12 deletions src/rcl_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,31 +329,27 @@ NAN_METHOD(TriggerGuardCondition) {
}

NAN_METHOD(CreateTimer) {
int64_t period_ms = Nan::To<int64_t>(info[0]).FromJust();
RclHandle* clock_handle = RclHandle::Unwrap<RclHandle>(
Nan::To<v8::Object>(info[0]).ToLocalChecked());
rcl_clock_t* clock = reinterpret_cast<rcl_clock_t*>(clock_handle->ptr());
RclHandle* context_handle = RclHandle::Unwrap<RclHandle>(
Nan::To<v8::Object>(info[1]).ToLocalChecked());
rcl_context_t* context =
reinterpret_cast<rcl_context_t*>(context_handle->ptr());
int64_t period_ms = Nan::To<int64_t>(info[2]).FromJust();

rcl_timer_t* timer =
reinterpret_cast<rcl_timer_t*>(malloc(sizeof(rcl_timer_t)));
*timer = rcl_get_zero_initialized_timer();
rcl_clock_t* clock =
reinterpret_cast<rcl_clock_t*>(malloc(sizeof(rcl_clock_t)));
rcl_allocator_t allocator = rcl_get_default_allocator();
THROW_ERROR_IF_NOT_EQUAL(RCL_RET_OK,
rcl_clock_init(RCL_STEADY_TIME, clock, &allocator),
rcl_get_error_string().str);

THROW_ERROR_IF_NOT_EQUAL(
RCL_RET_OK,
rcl_timer_init(timer, clock, context, RCL_MS_TO_NS(period_ms), nullptr,
rcl_get_default_allocator()),
rcl_get_error_string().str);

auto js_obj = RclHandle::NewInstance(timer, nullptr, [timer, clock] {
rcl_ret_t ret_clock = rcl_clock_fini(clock);
free(clock);
rcl_ret_t ret_timer = rcl_timer_fini(timer);
return ret_clock || ret_timer;
auto js_obj = RclHandle::NewInstance(timer, clock_handle, [timer] {
return rcl_timer_fini(timer);
});
info.GetReturnValue().Set(js_obj);
}
Expand Down
6 changes: 6 additions & 0 deletions test/test-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,12 @@ describe('rcl node methods testing', function() {
assert.equal(logger.fatal('message fatal'), true);
});

it('node.getClock', function() {
var clock = node.getClock();
assert.ok(clock);
assert.strictEqual(clock.clockType, rclnodejs.ClockType.ROS_TIME);
});

it('node.getNodeNames', function() {
var nodeNames = node.getNodeNames();

Expand Down
3 changes: 3 additions & 0 deletions test/types/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ node.namespace();
// $ExpectType Logging
node.getLogger();

// $ExpectType Clock
node.getClock();

// $ExpectType void
rclnodejs.spin(node);

Expand Down
8 changes: 8 additions & 0 deletions types/node.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// eslint camelcase: ["error", {ignoreImports: true}]

import { Clock } from 'rclnodejs';
import { Logging } from 'rclnodejs';
import { Parameters } from 'rclnodejs';
import { rcl_interfaces as rclinterfaces } from 'rclnodejs';
Expand Down Expand Up @@ -165,6 +166,13 @@ declare module 'rclnodejs' {
*/
getLogger(): Logging;

/**
* Get the clock used by the node.
*
* @returns The nodes clock.
*/
getClock(): Clock;

/**
* Get the nodeOptions provided through the constructor.
*/
Expand Down