@@ -49,6 +49,7 @@ const {
4949 getActionServerNamesAndTypesByNode,
5050 getActionNamesAndTypes,
5151} = require ( './lib/action/graph.js' ) ;
52+ const Lifecycle = require ( './lib/lifecycle.js' ) ;
5253
5354function inherits ( target , source ) {
5455 const properties = Object . getOwnPropertyNames ( source . prototype ) ;
@@ -123,6 +124,9 @@ let rcl = {
123124 /** {@link IntegerRange } class */
124125 IntegerRange : IntegerRange ,
125126
127+ /** Lifecycle namespace */
128+ lifecycle : Lifecycle ,
129+
126130 /** {@link Logging } class */
127131 logging : logging ,
128132
@@ -192,33 +196,26 @@ let rcl = {
192196 context = Context . defaultContext ( ) ,
193197 options = NodeOptions . defaultOptions
194198 ) {
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+ } ,
204201
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 ) ;
222219 } ,
223220
224221 /**
@@ -442,3 +439,30 @@ const TimeSource = require('./lib/time_source.js');
442439rcl . TimeSource = TimeSource ;
443440
444441inherits ( 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