Class: Rate

Rate(hz, timer)

A timer that runs at a regular frequency (hz). A client calls Rate#sleep() to block until the end of the current cycle. This makes Rate useful for looping at a regular frequency (hz). Rate#sleep() avoids blocking the JS event-loop by returning a Promise that the caller should block on, e.g. use 'await rate.sleep()'. Note that Rate.sleep() does not prevent rclnodejs from invoking callbacks such as a subscription or client if the entity's node is spinning. Thus if your intent is to use rate to synchronize when callbacks are invoked then use a spinOnce() just after rate.sleep() as in the example below. Rate runs within it's own private rcl context. This enables it to be available immediately after construction. That is, unlike Timer, Rate does not require a spin or spinOnce to be active.
Source:
Example
async function run() {
  await rclnodejs.init();
  const node = rclnodejs.createNode('mynode');
  const rate = await node.createRate(1); // 1 hz
  while (true) {
    doSomeStuff();
    await rate.sleep();
    rclnodejs.spinOnce(node);
  }
}

Members

frequency

Get the frequency in hertz (hz) of this timer.
Source:

Methods

cancel() → {undefined}

Permanently stops the timing behavior.
Source:
Returns:
Type
undefined

isCanceled() → {boolean}

Determine if this rate has been cancelled.
Source:
Returns:
- True when cancel() has been called; False otherwise.
Type
boolean

(async) sleep() → {Promise}

Returns a Promise that when waited on, will block the sender until the end of the current timer cycle. If the Rate has been cancelled, calling this method will result in an error.
Source:
Returns:
- Waiting on the promise will delay the sender (not the Node event-loop) until the end of the current timer cycle.
Type
Promise
Example
(async () => {
  await rate.sleep();
})();