|
| 1 | +// |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +'use strict'; |
| 15 | + |
| 16 | +const rclnodejs = require('bindings')('rclnodejs'); |
| 17 | +const Context = require('./context.js'); |
| 18 | +const NodeOptions = require('./node_options.js'); |
| 19 | + |
| 20 | +const NOP_FN = () => {}; |
| 21 | + |
| 22 | +/** |
| 23 | + * A timer that runs at a regular frequency (hz). |
| 24 | + * |
| 25 | + * A client calls Rate#sleep() to block until the end of the current cycle. |
| 26 | + * This makes Rate useful for looping at a regular frequency (hz). Rate#sleep() |
| 27 | + * avoids blocking the JS event-loop by returning a Promise that the caller |
| 28 | + * should block on, e.g. use 'await rate.sleep()'. |
| 29 | + * |
| 30 | + * Note that Rate.sleep() does not prevent rclnodejs from invoking callbacks |
| 31 | + * such as a subscription or client if the entity's node is spinning. Thus |
| 32 | + * if your intent is to use rate to synchronize when callbacks are invoked |
| 33 | + * then use a spinOnce() just after rate.sleep() as in the example below. |
| 34 | + * |
| 35 | + * Rate runs within it's own private rcl context. This enables it to be |
| 36 | + * available immediately after construction. That is, unlike Timer, Rate |
| 37 | + * does not require a spin or spinOnce to be active. |
| 38 | + * |
| 39 | + * @example |
| 40 | + * async function run() { |
| 41 | + * await rclnodejs.init(); |
| 42 | + * const node = rclnodejs.createNode('mynode'); |
| 43 | + * const rate = node.createRate(1); // 1 hz |
| 44 | + * while (true) { |
| 45 | + * doSomeStuff(); |
| 46 | + * await rate.sleep(); |
| 47 | + * rclnodejs.spinOnce(node); |
| 48 | + * } |
| 49 | + * } |
| 50 | + */ |
| 51 | +class Rate { |
| 52 | + /** |
| 53 | + * Create a new instance. |
| 54 | + * @hideconstructor |
| 55 | + * @param {number} hz - The frequency (hz) between (0.0,1000] hz, |
| 56 | + * @param {Timer} timer - The internal timer used by this instance. |
| 57 | + * default = 1 hz |
| 58 | + */ |
| 59 | + constructor(hz, timer) { |
| 60 | + this._hz = hz; |
| 61 | + this._timer = timer; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Get the frequency in hertz (hz) of this timer. |
| 66 | + * |
| 67 | + * @returns {number} - hertz |
| 68 | + */ |
| 69 | + get frequency() { |
| 70 | + return this._hz; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Returns a Promise that when waited on, will block the sender |
| 75 | + * until the end of the current timer cycle. |
| 76 | + * |
| 77 | + * If the Rate has been cancelled, calling this method will |
| 78 | + * result in an error. |
| 79 | + * |
| 80 | + * @example |
| 81 | + * (async () => { |
| 82 | + * await rate.sleep(); |
| 83 | + * })(); |
| 84 | + * |
| 85 | + * @returns {Promise} - Waiting on the promise will delay the sender |
| 86 | + * (not the Node event-loop) until the end of the current timer cycle. |
| 87 | + */ |
| 88 | + async sleep() { |
| 89 | + if (this.isCanceled()) { |
| 90 | + throw new Error('Rate has been cancelled.'); |
| 91 | + } |
| 92 | + |
| 93 | + return new Promise(resolve => { |
| 94 | + this._timer.callback = () => { |
| 95 | + this._timer.callback = NOP_FN; |
| 96 | + resolve(); |
| 97 | + }; |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Permanently stops the timing behavior. |
| 103 | + * |
| 104 | + * @returns {undefined} |
| 105 | + */ |
| 106 | + cancel() { |
| 107 | + this._timer.cancel(); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Determine if this rate has been cancelled. |
| 112 | + * |
| 113 | + * @returns {boolean} - True when cancel() has been called; False otherwise. |
| 114 | + */ |
| 115 | + isCanceled() { |
| 116 | + return this._timer.isCanceled(); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +/** |
| 121 | + * Internal class that creates Timer instances in a common private rcl context |
| 122 | + * for use with Rate. The private rcl context ensures that Rate timers do not |
| 123 | + * deadlock waiting for spinOnce/spin on the main rcl context. |
| 124 | + */ |
| 125 | +class RateTimerServer { |
| 126 | + /** |
| 127 | + * Create a new instance. |
| 128 | + * |
| 129 | + * @constructor |
| 130 | + * @param {Node} parentNode - The parent node for which this server |
| 131 | + * supplies timers to. |
| 132 | + */ |
| 133 | + constructor(parentNode) { |
| 134 | + this._context = new Context(); |
| 135 | + |
| 136 | + // init rcl environment |
| 137 | + rclnodejs.init(this._context.handle()); |
| 138 | + |
| 139 | + // create hidden node |
| 140 | + const nodeName = `_${parentNode.name()}_rate_timer_server`; |
| 141 | + const nodeNamespace = parentNode.namespace(); |
| 142 | + this._node = new rclnodejs.ShadowNode(); |
| 143 | + this._node.handle = rclnodejs.createNode( |
| 144 | + nodeName, |
| 145 | + nodeNamespace, |
| 146 | + this._context.handle() |
| 147 | + ); |
| 148 | + |
| 149 | + const options = new NodeOptions(); |
| 150 | + options.startParameterServices = false; |
| 151 | + options.parameterOverrides = parentNode.getParameters(); |
| 152 | + options.automaticallyDeclareParametersFromOverrides = true; |
| 153 | + |
| 154 | + this._node.init(nodeName, nodeNamespace, this._context, options); |
| 155 | + |
| 156 | + // spin node |
| 157 | + this._node.startSpinning(this._context.handle(), 10); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Create a new timer instance with callback set to NOP. |
| 162 | + * |
| 163 | + * @param {number} period - The period in milliseconds |
| 164 | + * @returns {Timer} - The new timer instance. |
| 165 | + */ |
| 166 | + createTimer(period) { |
| 167 | + const timer = this._node.createTimer(period, () => {}, this._context); |
| 168 | + return timer; |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Permanently cancel all timers produced by this server and discontinue |
| 173 | + * the ability to create new Timers. |
| 174 | + * |
| 175 | + * The private rcl context is shutdown in the process and may not be |
| 176 | + * restarted. |
| 177 | + * |
| 178 | + * @returns {undefined} |
| 179 | + */ |
| 180 | + shutdown() { |
| 181 | + this._node.destroy(); |
| 182 | + this._context.shutdown(); |
| 183 | + } |
| 184 | +} |
| 185 | +// module.exports = {Rate, RateTimerServer}; |
| 186 | +module.exports = { Rate, RateTimerServer }; |
0 commit comments