Skip to content
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
8 changes: 5 additions & 3 deletions lib/action/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,12 @@ class ActionServer extends Entity {
let goalHandle;
if (accepted) {
// Stamp time of acceptance
const secondsAndNanos = this._node.getClock().now().secondsAndNanoseconds;
const { seconds, nanoseconds } = this._node
.getClock()
.now().secondsAndNanoseconds;
goalInfo.stamp = {
sec: secondsAndNanos.seconds,
nanosec: secondsAndNanos.nanoseconds,
sec: Number(seconds),
nanosec: Number(nanoseconds),
};

try {
Expand Down
4 changes: 2 additions & 2 deletions lib/clock.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ class Clock {
* @return {Time} Return the current time.
*/
now() {
let time = rclnodejs.clockGetNow(this._handle);
return new Time(time.sec, time.nanosec, this._clockType);
const nowInNanosec = rclnodejs.clockGetNow(this._handle);
return new Time(0n, nowInNanosec, this._clockType);
}
}

Expand Down
57 changes: 22 additions & 35 deletions lib/duration.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
'use strict';

const rclnodejs = require('bindings')('rclnodejs');
const int64 = require('int64-napi');
const S_TO_NS = 10n ** 9n;

/**
* @class - Class representing a Duration in ROS
Expand All @@ -24,51 +24,38 @@ const int64 = require('int64-napi');
class Duration {
/**
* Create a Duration.
* @param {number|string} [seconds=0] - The second part of the duration.
* @param {number|string} [nanoseconds=0] - The nanosecond part of the duration.
* @param {bigint} [seconds=0] - The second part of the duration.
* @param {bigint} [nanoseconds=0] - The nanosecond part of the duration.
*/
constructor(seconds = 0, nanoseconds = 0) {
if (typeof seconds !== 'number' && typeof seconds !== 'string') {
constructor(seconds = 0n, nanoseconds = 0n) {
if (typeof seconds !== 'bigint') {
throw new TypeError('Invalid argument of seconds');
}

if (typeof nanoseconds !== 'number' && typeof nanoseconds !== 'string') {
if (typeof nanoseconds !== 'bigint') {
throw new TypeError('Invalid argument of nanoseconds');
}

let secondInt64 = int64.from(seconds);
let nanoInt64 = int64.from(nanoseconds);

if (typeof seconds === 'string' && seconds.startsWith('-')) {
secondInt64 = int64.negative(secondInt64);
}
if (typeof nanoseconds === 'string' && nanoseconds.startsWith('-')) {
nanoInt64 = int64.negative(nanoInt64);
const total = seconds * S_TO_NS + nanoseconds;
if (total >= 2n ** 63n) {
throw new RangeError(
'Total nanoseconds value is too large to store in C time point.'
);
}
this._nanoseconds = secondInt64.multiply(1e9).add(nanoInt64);
this._handle = rclnodejs.createDuration(this._nanoseconds.toString());

this._nanoseconds = total;
this._handle = rclnodejs.createDuration(this._nanoseconds);
}

/**
* Get the nanosecond part of the Duration.
* @name Duration#get:nanoseconds
* @function
* @return {number|string} - value in nanosecond, if the value is greater than Number.MAX_SAFE_INTEGER (2^53-1), will be presented in string of decimal format.
* @return {bigint} - value in nanosecond.
*/

get nanoseconds() {
let nanoStr = rclnodejs.getDurationNanoseconds(this._handle);
let nano;

if (nanoStr.startsWith('-')) {
nano = int64.negative(int64.from(nanoStr));
} else {
nano = int64.from(nanoStr);
}
if (Number.isFinite(nano.toNumber())) {
return nano.toNumber();
}
return nano.toString();
return rclnodejs.getDurationNanoseconds(this._handle);
}

/**
Expand All @@ -78,7 +65,7 @@ class Duration {
*/
eq(other) {
if (other instanceof Duration) {
return this._nanoseconds.eq(other.nanoseconds);
return this._nanoseconds === other.nanoseconds;
}
throw new TypeError(
`Can't compare duration with object of type: ${other.constructor.name}`
Expand All @@ -92,7 +79,7 @@ class Duration {
*/
ne(other) {
if (other instanceof Duration) {
return this._nanoseconds.ne(other.nanoseconds);
return this._nanoseconds !== other.nanoseconds;
}
throw new TypeError('Invalid argument');
}
Expand All @@ -104,7 +91,7 @@ class Duration {
*/
lt(other) {
if (other instanceof Duration) {
return this._nanoseconds.lt(other.nanoseconds);
return this._nanoseconds < other.nanoseconds;
}
throw new TypeError('Invalid argument');
}
Expand All @@ -116,7 +103,7 @@ class Duration {
*/
lte(other) {
if (other instanceof Duration) {
return this._nanoseconds.lte(other.nanoseconds);
return this._nanoseconds <= other.nanoseconds;
}
throw new TypeError('Invalid argument');
}
Expand All @@ -128,7 +115,7 @@ class Duration {
*/
gt(other) {
if (other instanceof Duration) {
return this._nanoseconds.gt(other.nanoseconds);
return this._nanoseconds > other.nanoseconds;
}
throw new TypeError('Invalid argument');
}
Expand All @@ -140,7 +127,7 @@ class Duration {
*/
gte(other) {
if (other instanceof Duration) {
return this._nanoseconds.gte(other.nanoseconds);
return this._nanoseconds >= other.nanoseconds;
}
throw new TypeError('Invalid argument');
}
Expand Down
6 changes: 3 additions & 3 deletions lib/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -1469,10 +1469,10 @@ class Node extends rclnodejs.ShadowNode {
PARAMETER_EVENT_MSG_TYPE
))();

const secondsAndNanos = this._clock.now().secondsAndNanoseconds;
const { seconds, nanoseconds } = this._clock.now().secondsAndNanoseconds;
parameterEvent.stamp = {
sec: secondsAndNanos.seconds,
nanosec: secondsAndNanos.nanoseconds,
sec: Number(seconds),
nanosec: Number(nanoseconds),
};

parameterEvent.node =
Expand Down
89 changes: 39 additions & 50 deletions lib/time.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
const rclnodejs = require('bindings')('rclnodejs');
const Duration = require('./duration.js');
const ClockType = require('./clock_type.js');
const int64 = require('int64-napi');
const S_TO_NS = 10n ** 9n;

/**
* @class - Class representing a Time in ROS
Expand All @@ -26,42 +26,43 @@ const int64 = require('int64-napi');
class Time {
/**
* Create a Time.
* @param {number|string} [seconds=0] - The second part of the time.
* @param {number|string} [nanoseconds=0] - The nanosecond part of the time.
* @param {bigint} [seconds=0] - The second part of the time.
* @param {bigint} [nanoseconds=0] - The nanosecond part of the time.
* @param {ClockType} [clockType=Clock.ClockType.SYSTEM_TIME] - The clock type.
*/
constructor(seconds = 0, nanoseconds = 0, clockType = ClockType.SYSTEM_TIME) {
if (typeof seconds !== 'number' && typeof seconds !== 'string') {
constructor(
seconds = 0n,
nanoseconds = 0n,
clockType = ClockType.SYSTEM_TIME
) {
if (typeof seconds !== 'bigint') {
throw new TypeError('Invalid argument of seconds');
}

if (typeof nanoseconds !== 'number' && typeof nanoseconds !== 'string') {
if (typeof nanoseconds !== 'bigint') {
throw new TypeError('Invalid argument of nanoseconds');
}

if (typeof clockType !== 'number') {
throw new TypeError('Invalid argument of clockType');
}

if (
int64.lt(seconds, 0) ||
(typeof seconds === 'string' && seconds.startsWith('-'))
) {
if (seconds < 0n) {
throw new RangeError('seconds value must not be negative');
}

if (
int64.lt(nanoseconds, 0) ||
(typeof nanoseconds === 'string' && nanoseconds.startsWith('-'))
) {
if (nanoseconds < 0n) {
throw new RangeError('nanoseconds value must not be negative');
}

this._nanoseconds = int64.from(seconds).multiply(1e9).add(nanoseconds);
this._handle = rclnodejs.createTimePoint(
this._nanoseconds.toString(),
clockType
);
const total = seconds * S_TO_NS + nanoseconds;
if (total >= 2n ** 63n) {
throw new RangeError(
'Total nanoseconds value is too large to store in C time point.'
);
}
this._nanoseconds = total;
this._handle = rclnodejs.createTimePoint(this._nanoseconds, clockType);
this._clockType = clockType;
}

Expand All @@ -80,22 +81,11 @@ class Time {
* Get the nanosecond part of the time.
* @name Time#get:nanoseconds
* @function
* @return {number|string} - value in nanosecond, if the value is greater than Number.MAX_SAFE_INTEGER (2^53-1), will be presented in string of decimal format.
* @return {bigint} - value in nanosecond.
*/

get nanoseconds() {
let str = rclnodejs.getNanoseconds(this._handle);
let nano;

if (str.startsWith('-')) {
nano = int64.negative(int64.from(str));
} else {
nano = int64.from(str);
}
if (Number.isFinite(nano.toNumber())) {
return nano.toNumber();
}
return nano.toString();
return rclnodejs.getNanoseconds(this._handle);
}

/**
Expand All @@ -106,9 +96,11 @@ class Time {
*/

get secondsAndNanoseconds() {
const seconds = int64.from(this._nanoseconds).divide(1e9).toNumber();
const nanoseconds = int64.from(this._nanoseconds).mod(1e9).toNumber();
return { seconds, nanoseconds };
const nanoseconds = this._nanoseconds;
return {
seconds: nanoseconds / S_TO_NS,
nanoseconds: nanoseconds % S_TO_NS,
};
}

/**
Expand All @@ -119,8 +111,8 @@ class Time {
add(other) {
if (other instanceof Duration) {
return new Time(
0,
int64.add(this._nanoseconds, other.nanoseconds).toString(),
0n,
this._nanoseconds + other.nanoseconds,
this._clockType
);
}
Expand All @@ -137,14 +129,11 @@ class Time {
if (other._clockType !== this._clockType) {
throw new TypeError("Can't subtract times with different clock types");
}
return new Duration(
0,
int64.subtract(this._nanoseconds, other._nanoseconds).toString()
);
return new Duration(0n, this._nanoseconds - other._nanoseconds);
} else if (other instanceof Duration) {
return new Time(
0,
int64.subtract(this._nanoseconds, other._nanoseconds).toString(),
0n,
this._nanoseconds - other._nanoseconds,
this._clockType
);
}
Expand All @@ -161,7 +150,7 @@ class Time {
if (other._clockType !== this._clockType) {
throw new TypeError("Can't compare times with different clock types");
}
return this._nanoseconds.eq(other.nanoseconds);
return this._nanoseconds === other.nanoseconds;
}
throw new TypeError('Invalid argument');
}
Expand All @@ -176,7 +165,7 @@ class Time {
if (other._clockType !== this._clockType) {
throw new TypeError("Can't compare times with different clock types");
}
return this._nanoseconds.ne(other.nanoseconds);
return this._nanoseconds !== other.nanoseconds;
}
}

Expand All @@ -190,7 +179,7 @@ class Time {
if (other._clockType !== this._clockType) {
throw new TypeError("Can't compare times with different clock types");
}
return this._nanoseconds.lt(other.nanoseconds);
return this._nanoseconds < other.nanoseconds;
}
throw new TypeError('Invalid argument');
}
Expand All @@ -205,7 +194,7 @@ class Time {
if (other._clockType !== this._clockType) {
throw new TypeError("Can't compare times with different clock types");
}
return this._nanoseconds.lte(other.nanoseconds);
return this._nanoseconds <= other.nanoseconds;
}
throw new TypeError('Invalid argument');
}
Expand All @@ -220,7 +209,7 @@ class Time {
if (other._clockType !== this._clockType) {
throw new TypeError("Can't compare times with different clock types");
}
return this._nanoseconds.gt(other.nanoseconds);
return this._nanoseconds > other.nanoseconds;
}
throw new TypeError('Invalid argument');
}
Expand All @@ -235,7 +224,7 @@ class Time {
if (other._clockType !== this._clockType) {
throw new TypeError("Can't compare times with different clock types");
}
return this._nanoseconds.gte(other.nanoseconds);
return this._nanoseconds >= other.nanoseconds;
}
throw new TypeError('Invalid argument');
}
Expand All @@ -261,7 +250,7 @@ class Time {
* @return {Time} Return the created Time object.
*/
static fromMsg(msg, clockType = ClockType.ROS_TIME) {
return new Time(msg.sec, msg.nanosec, clockType);
return new Time(BigInt(msg.sec), BigInt(msg.nanosec), clockType);
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/time_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class TimeSource {
this._node = node;
this._associatedClocks = [];
this._clockSubscription = undefined;
this._lastTimeSet = new Time(0, 0, ClockType.ROS_TIME);
this._lastTimeSet = new Time(0n, 0n, ClockType.ROS_TIME);
this._isRosTimeActive = false;

if (this._node) {
Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
"tree-kill": "^1.2.2",
"typescript": "^5.7.2"
},
"//": "Pin int64-napi to ^1.0.2",
"dependencies": {
"@rclnodejs/ref-array-di": "^1.2.2",
"@rclnodejs/ref-napi": "^4.0.0",
Expand All @@ -76,7 +75,6 @@
"dtslint": "^4.2.1",
"fs-extra": "^11.2.0",
"json-bigint": "^1.0.0",
"int64-napi": "^1.0.2",
"is-close": "^1.3.3",
"mkdirp": "^3.0.1",
"mz": "^2.7.0",
Expand Down
Loading
Loading