rclnodejs
Version:
ROS2.0 JavaScript client with Node.js
65 lines (54 loc) • 1.7 kB
JavaScript
// Subscriber for repro test - runs in separate process
;
const rclnodejs = require('./index.js');
async function main() {
await rclnodejs.init();
const node = new rclnodejs.Node('test_subscriber_node');
let msgCount = 0;
let lastTs = null;
const hzSamples = [];
let startTime = null;
node.createSubscription(
'std_msgs/msg/Float64MultiArray',
'/test_hz_topic',
(msg) => {
const now = Date.now();
msgCount++;
if (!startTime) startTime = now;
if (lastTs) {
const hz = 1000 / (now - lastTs);
hzSamples.push(hz);
if (msgCount % 50 === 0) {
const recentSamples = hzSamples.slice(-50);
const recentAvg =
recentSamples.reduce((a, b) => a + b, 0) / recentSamples.length;
console.log(
`msg#${msgCount} recent avg Hz: ${recentAvg.toFixed(2)}`
);
}
}
lastTs = now;
}
);
node.spin();
setTimeout(() => {
if (hzSamples.length > 0) {
const avgHz =
hzSamples.reduce((a, b) => a + b, 0) / hzSamples.length;
const minHz = Math.min(...hzSamples);
const maxHz = Math.max(...hzSamples);
const elapsed = (Date.now() - startTime) / 1000;
console.log(`\n--- Results ---`);
console.log(`Received: ${msgCount} messages in ${elapsed.toFixed(1)}s`);
console.log(`Avg Hz: ${avgHz.toFixed(2)}`);
console.log(`Min Hz: ${minHz.toFixed(2)}`);
console.log(`Max Hz: ${maxHz.toFixed(2)}`);
} else {
console.log('No messages received!');
}
node.stop();
rclnodejs.shutdown();
process.exit(0);
}, 12000);
}
main().catch(console.error);