@@ -57,6 +57,7 @@ const {
5757 serializeMessage,
5858 deserializeMessage,
5959} = require ( './lib/serialization.js' ) ;
60+ const { spawn } = require ( 'child_process' ) ;
6061
6162/**
6263 * Get the version of the generator that was used for the currently present interfaces.
@@ -89,6 +90,79 @@ async function getCurrentGeneratorVersion() {
8990
9091let _rosVersionChecked = false ;
9192
93+ /**
94+ * Run a ROS2 package executable using 'ros2 run' command.
95+ * @param {string } packageName - The name of the ROS2 package.
96+ * @param {string } executableName - The name of the executable to run.
97+ * @param {string[] } [args=[]] - Additional arguments to pass to the executable.
98+ * @return {Promise<{process: ChildProcess}> } A Promise that resolves with the process.
99+ */
100+ function ros2Run ( packageName , executableName , args = [ ] ) {
101+ return new Promise ( ( resolve , reject ) => {
102+ if ( typeof packageName !== 'string' || ! packageName . trim ( ) ) {
103+ reject ( new Error ( 'Package name must be a non-empty string' ) ) ;
104+ return ;
105+ }
106+
107+ if ( typeof executableName !== 'string' || ! executableName . trim ( ) ) {
108+ reject ( new Error ( 'Executable name must be a non-empty string' ) ) ;
109+ return ;
110+ }
111+
112+ if ( ! Array . isArray ( args ) ) {
113+ reject ( new Error ( 'Arguments must be an array' ) ) ;
114+ return ;
115+ }
116+
117+ const command = 'ros2' ;
118+ const cmdArgs = [ 'run' , packageName , executableName , ...args ] ;
119+ const childProcess = spawn ( command , cmdArgs ) ;
120+
121+ childProcess . on ( 'error' , ( error ) => {
122+ reject ( new Error ( `Failed to start ros2 run: ${ error . message } ` ) ) ;
123+ } ) ;
124+
125+ resolve ( {
126+ process : childProcess ,
127+ } ) ;
128+ } ) ;
129+ }
130+
131+ /**
132+ * Run a ROS2 launch file using 'ros2 launch' command.
133+ * @param {string } packageName - The name of the ROS2 package.
134+ * @param {string } launchFile - The name of the launch file to run.
135+ * @param {string[] } [args=[]] - Additional arguments to pass to the launch file.
136+ * @return {Promise<{process: ChildProcess}> } A Promise that resolves with the process.
137+ */
138+ function ros2Launch ( packageName , launchFile , args = [ ] ) {
139+ return new Promise ( ( resolve , reject ) => {
140+ if ( typeof packageName !== 'string' || ! packageName . trim ( ) ) {
141+ reject ( new Error ( 'Package name must be a non-empty string' ) ) ;
142+ return ;
143+ }
144+ if ( typeof launchFile !== 'string' || ! launchFile . trim ( ) ) {
145+ reject ( new Error ( 'Launch file name must be a non-empty string' ) ) ;
146+ return ;
147+ }
148+ if ( ! Array . isArray ( args ) ) {
149+ reject ( new Error ( 'Arguments must be an array' ) ) ;
150+ return ;
151+ }
152+ const command = 'ros2' ;
153+ const cmdArgs = [ 'launch' , packageName , launchFile , ...args ] ;
154+ const childProcess = spawn ( command , cmdArgs ) ;
155+
156+ childProcess . on ( 'error' , ( error ) => {
157+ reject ( new Error ( `Failed to start ros2 launch: ${ error . message } ` ) ) ;
158+ } ) ;
159+
160+ resolve ( {
161+ process : childProcess ,
162+ } ) ;
163+ } ) ;
164+ }
165+
92166/**
93167 * A module that exposes the rclnodejs interfaces.
94168 * @exports rclnodejs
@@ -444,6 +518,28 @@ let rcl = {
444518 // this will not throw even if the handler is already removed
445519 process . removeListener ( 'SIGINT' , _sigHandler ) ;
446520 } ,
521+
522+ /**
523+ * Run a ROS2 package executable using 'ros2 run' command.
524+ * @param {string } packageName - The name of the ROS2 package.
525+ * @param {string } executableName - The name of the executable to run.
526+ * @param {string[] } [args=[]] - Additional arguments to pass to the executable.
527+ * @return {Promise<{process: ChildProcess}> } A Promise that resolves with the process.
528+ */
529+ ros2Run ( packageName , executableName , args = [ ] ) {
530+ return ros2Run ( packageName , executableName , args ) ;
531+ } ,
532+
533+ /**
534+ * Run a ROS2 launch file using 'ros2 launch' command.
535+ * @param {string } packageName - The name of the ROS2 package.
536+ * @param {string } launchFile - The name of the launch file to run.
537+ * @param {string[] } [args=[]] - Additional arguments to pass to the launch file.
538+ * @return {Promise<{process: ChildProcess}> } A Promise that resolves with the process.
539+ */
540+ ros2Launch ( packageName , launchFile , args = [ ] ) {
541+ return ros2Launch ( packageName , launchFile , args ) ;
542+ } ,
447543} ;
448544
449545const _sigHandler = ( ) => {
0 commit comments