@@ -49,6 +49,7 @@ const {
49
49
getActionServerNamesAndTypesByNode,
50
50
getActionNamesAndTypes,
51
51
} = require ( './lib/action/graph.js' ) ;
52
+ const Lifecycle = require ( './lib/lifecycle.js' ) ;
52
53
53
54
function inherits ( target , source ) {
54
55
const properties = Object . getOwnPropertyNames ( source . prototype ) ;
@@ -123,6 +124,9 @@ let rcl = {
123
124
/** {@link IntegerRange } class */
124
125
IntegerRange : IntegerRange ,
125
126
127
+ /** Lifecycle namespace */
128
+ lifecycle : Lifecycle ,
129
+
126
130
/** {@link Logging } class */
127
131
logging : logging ,
128
132
@@ -192,33 +196,26 @@ let rcl = {
192
196
context = Context . defaultContext ( ) ,
193
197
options = NodeOptions . defaultOptions
194
198
) {
195
- if ( typeof nodeName !== 'string' || typeof namespace !== 'string' ) {
196
- throw new TypeError ( 'Invalid argument.' ) ;
197
- }
198
-
199
- if ( ! this . _contextToNodeArrayMap . has ( context ) ) {
200
- throw new Error (
201
- 'Invalid context. Must call rclnodejs(context) before using the context'
202
- ) ;
203
- }
199
+ return _createNode ( nodeName , namespace , context , options , rclnodejs . ShadowNode ) ;
200
+ } ,
204
201
205
- const handle = rclnodejs . createNode ( nodeName , namespace , context . handle ) ;
206
- const node = new rclnodejs . ShadowNode ( ) ;
207
- node . handle = handle ;
208
- Object . defineProperty ( node , 'handle' , {
209
- configurable : false ,
210
- writable : false ,
211
- } ) ; // make read-only
212
- node . context = context ;
213
- node . init ( nodeName , namespace , context , options ) ;
214
- debug (
215
- 'Finish initializing node, name = %s and namespace = %s.' ,
216
- nodeName ,
217
- namespace
218
- ) ;
219
-
220
- this . _contextToNodeArrayMap . get ( context ) . push ( node ) ;
221
- return node ;
202
+ /**
203
+ * Create a managed Node that implements a well-defined life-cycle state
204
+ * model using the { @link https://github.com/ros2/rcl/tree/master/rcl_lifecycle|ros2 client library (rcl) lifecyle api}.
205
+ * @param { string } nodeName - The name used to register in ROS.
206
+ * @param { string } [namespace=''] - The namespace used in ROS.
207
+ * @param { Context } [context=Context.defaultContext()] - The context to create the node in.
208
+ * @param { NodeOptions } [options=NodeOptions.defaultOptions] - The options to configure the new node behavior.
209
+ * @return { LifecycleNode } A new instance of the specified node.
210
+ * @throws { Error } If the given context is not registered.
211
+ */
212
+ createLifecycleNode (
213
+ nodeName ,
214
+ namespace = '' ,
215
+ context = Context . defaultContext ( ) ,
216
+ options = NodeOptions . defaultOptions
217
+ ) {
218
+ return _createNode ( nodeName , namespace , context , options , Lifecycle . LifecycleNode ) ;
222
219
} ,
223
220
224
221
/**
@@ -442,3 +439,30 @@ const TimeSource = require('./lib/time_source.js');
442
439
rcl . TimeSource = TimeSource ;
443
440
444
441
inherits ( rclnodejs . ShadowNode , Node ) ;
442
+
443
+ function _createNode (
444
+ nodeName ,
445
+ namespace = '' ,
446
+ context = Context . defaultContext ( ) ,
447
+ options = NodeOptions . defaultOptions ,
448
+ nodeClass
449
+ ) {
450
+ if ( typeof nodeName !== 'string' || typeof namespace !== 'string' ) {
451
+ throw new TypeError ( 'Invalid argument.' ) ;
452
+ }
453
+
454
+ if ( ! rcl . _contextToNodeArrayMap . has ( context ) ) {
455
+ throw new Error ( 'Invalid context. Must call rclnodejs(context) before using the context' ) ;
456
+ }
457
+
458
+ let handle = rclnodejs . createNode ( nodeName , namespace , context . handle ) ;
459
+ let node = new nodeClass ( ) ;
460
+ node . handle = handle ;
461
+ Object . defineProperty ( node , 'handle' , { configurable : false , writable : false } ) ; // make read-only
462
+ node . context = context ;
463
+ node . init ( nodeName , namespace , context , options ) ;
464
+ debug ( 'Finish initializing node, name = %s and namespace = %s.' , nodeName , namespace ) ;
465
+
466
+ rcl . _contextToNodeArrayMap . get ( context ) . push ( node ) ;
467
+ return node ;
468
+ }
0 commit comments