### Complete Publisher Example Source: https://wiki.ros.org/rosnodejs/overview/Publishers%20and%20Subscribers A full example showing node initialization and periodic message publishing. ```javascript const std_msgs = rosnodejs.require('std_msgs'); rosnodejs.initNode('my_node') .then((nh) => { const pub = nh.advertise('/my_topic', std_msgs.msg.String); const msg = new std_msgs.msg.String(); msg.data = 'Hello, World'; setInterval(() => { pub.publish(msg); }, 1000); }); ``` -------------------------------- ### Create a Publisher Source: https://wiki.ros.org/rosnodejs/overview/Publishers%20and%20Subscribers Demonstrates creating a publisher using a string for the message type or the message class itself. ```javascript const nh = rosnodejs.nh; const pub = nh.advertise('/topic', 'std_msgs/String'); ``` ```javascript const std_msgs = rosnodejs.require('std_msgs'); const nh = rosnodejs.nh; const pub = nh.advertise('/topic', std_msgs.msg.String); ``` -------------------------------- ### Create and Call a Service Client Source: https://wiki.ros.org/rosnodejs/overview/services Initialize a service client via NodeHandle and invoke it by passing a request object. The call returns a promise that resolves with the service response. ```javascript let serviceClient = nh.serviceClient('/add_two_ints', AddTwoInts); ``` ```javascript let req = new AddTwoInts.Request(); req.a = 1; req.b = 2; serviceClient.call(req) .then((resp) => { console.log(resp); }); ``` -------------------------------- ### Create a subscriber with a message class Source: https://wiki.ros.org/rosnodejs/overview/Publishers%20and%20Subscribers Initializes a subscriber using the message class directly instead of a string. ```javascript const std_msgs = rosnodejs.require('std_msgs'); const nh = rosnodejs.nh; const sub = nh.subscribe('/topic', std_msgs.msg.String, () => {}); ``` -------------------------------- ### Initialize a subscriber with options Source: https://wiki.ros.org/rosnodejs/overview/Publishers%20and%20Subscribers Configures a subscriber with an optional object to define queue size and throttling behavior. ```javascript const options = {...}; const sub = nh.subscribe('/topic', std_msgs.msg.String, () => {}, options); ``` -------------------------------- ### Configure Publisher Options Source: https://wiki.ros.org/rosnodejs/overview/Publishers%20and%20Subscribers Pass an optional configuration object to customize publisher behavior like latching, queue size, and throttling. ```javascript const options = {...}; nh.advertise('/topic', std_msgs.msg.String, options); ``` -------------------------------- ### Create a subscriber with a string message type Source: https://wiki.ros.org/rosnodejs/overview/Publishers%20and%20Subscribers Initializes a subscriber using a string to specify the message type. ```javascript const callback = function(msg) { console.log(msg.data); } const nh = rosnodejs.nh; const sub = nh.subscribe('/topic', 'std_msgs/String', callback); ``` -------------------------------- ### Initialize Node for Dynamic Messages Source: https://wiki.ros.org/rosnodejs/overview/messages Enable on-the-fly message creation by setting the onTheFly option during node initialization. ```javascript rosnodejs.initNode('my_node', { onTheFly: true }); ``` -------------------------------- ### Initialize node with promise handling Source: https://wiki.ros.org/rosnodejs/overview/Initialization%20and%20Shutdown Initialization patterns using the promise returned by initNode to access the node handle. ```javascript rosnodejs.initNode('my_node') .then((nodeHandle) => { // do stuff }); ``` ```javascript rosnodejs.initNode('my_node') .then(() => { let nh = rosnodejs.nh; // do stuff }); ``` -------------------------------- ### Initialize Message Objects Source: https://wiki.ros.org/rosnodejs/overview/messages Create message instances and assign values to fields, either directly or via constructor arguments. ```javascript const msg = new geometry_msgs.msg.PoseStamped(); msg.header.frame_id = 'base'; ``` ```javascript const msg = new std_msgs.msg.String({ data: 'Hello' }); ``` -------------------------------- ### nh.serviceClient Source: https://wiki.ros.org/rosnodejs/overview/services Creates a client to call an existing ROS service. ```APIDOC ## nh.serviceClient(serviceName, serviceType) ### Description Creates a ServiceClient instance to interact with a specific service. ### Method NodeHandle.serviceClient ### Parameters - **serviceName** (string) - Required - The name of the service to call. - **serviceType** (object) - Required - The service definition module. ``` -------------------------------- ### Initialize a ROS node Source: https://wiki.ros.org/rosnodejs/overview/Initialization%20and%20Shutdown Basic initialization of a ROS node using the specified node name. ```javascript rosnodejs.initNode('my_node') ``` -------------------------------- ### Advertise a Service with NodeHandle Source: https://wiki.ros.org/rosnodejs/overview/services Use advertiseService to register a service callback. The callback receives the request and must populate the response object, returning true on success. ```javascript const AddTwoInts = rosnodejs.require('ros_tutorials/AddTwoInts'); rosnodejs.initNode('/test_node') .then((nh) => { const addTwoInts = (req, resp) => { resp.sum = req.a + req.b; return true; }; let service = nh.advertiseService('/add_two_ints', AddTwoInts, addTwoInts); }); ``` -------------------------------- ### nh.waitForService Source: https://wiki.ros.org/rosnodejs/overview/services Waits for a service to become available. ```APIDOC ## nh.waitForService(serviceName, [timeout]) ### Description Blocks or waits until the specified service is available. Returns a promise that resolves with a boolean indicating availability. ### Method NodeHandle.waitForService ### Parameters - **serviceName** (string) - Required - The name of the service to wait for. - **timeout** (number) - Optional - Maximum time to wait in milliseconds. ``` -------------------------------- ### nh.getParam(paramName) Source: https://wiki.ros.org/rosnodejs/overview/params Fetches a value from the parameter server. Returns a Promise that resolves with the parameter value. ```APIDOC ## nh.getParam(paramName) ### Description Fetches a value from the parameter server. The returned Promise resolves with the value of the parameter if it exists. ### Parameters - **paramName** (string) - Required - The name of the parameter to retrieve. ``` -------------------------------- ### nh.advertiseService Source: https://wiki.ros.org/rosnodejs/overview/services Advertises a service through the NodeHandle to allow other nodes to call it. ```APIDOC ## nh.advertiseService(serviceName, serviceType, callback) ### Description Advertises a service so that it can be called by other nodes. The callback function receives the request and response objects. ### Method NodeHandle.advertiseService ### Parameters - **serviceName** (string) - Required - The name of the service to advertise. - **serviceType** (object) - Required - The service definition module. - **callback** (function) - Required - A function with signature (req, resp) that returns true on success. ``` -------------------------------- ### Load Pre-generated Messages Source: https://wiki.ros.org/rosnodejs/overview/messages Generate message code for pre-Kinetic ROS environments. ```javascript rosnodejs.loadAllPackages(); ``` -------------------------------- ### Check parameter existence Source: https://wiki.ros.org/rosnodejs/overview/params Verifies if a parameter exists on the server, returning a Promise that resolves with a boolean. ```javascript nh.hasParam(paramName) .then((exists) => { console.log('Has param ? ' + exists); }); ``` -------------------------------- ### nh.setParam(paramName, paramValue) Source: https://wiki.ros.org/rosnodejs/overview/params Sets a value on the parameter server. Supports strings, numbers, booleans, lists, and objects. ```APIDOC ## nh.setParam(paramName, paramValue) ### Description Sets a value on the parameter server. ### Parameters - **paramName** (string) - Required - The name of the parameter to set. - **paramValue** (any) - Required - The value to assign to the parameter. ``` -------------------------------- ### Wait for Service Availability Source: https://wiki.ros.org/rosnodejs/overview/services Use waitForService to block until a service is available. An optional timeout in milliseconds can be provided, with the promise resolving to a boolean indicating availability. ```javascript nh.waitForService('/add_two_ints') .then(() => { // call it, presumably }); ``` ```javascript nh.waitForService('/add_two_ints', 1000) .then((available) => { if (available) { // as you were } }); ``` -------------------------------- ### Set parameter values Source: https://wiki.ros.org/rosnodejs/overview/params Sets a value on the parameter server. Supports various data types including strings, numbers, lists, and objects. ```javascript nh.setParam(paramName, paramValue); ``` ```javascript nh.setParam('a_string', 'baz'); nh.setParam('~private_int', 2); nh.setParam('list_of_floats', [1.1, 2.2, 3.3, 4.4]); nh.setParam('gains', {'p': 1, 'i': 2, 'd': 3}); ``` -------------------------------- ### Fetch a parameter value Source: https://wiki.ros.org/rosnodejs/overview/params Retrieves a value from the parameter server, returning a Promise that resolves with the parameter value. ```javascript nh.getParam(paramName) .then((paramValue) => { // a whole new parameter-y world }); ``` -------------------------------- ### nh.hasParam(paramName) Source: https://wiki.ros.org/rosnodejs/overview/params Checks if the parameter server contains the specified parameter. ```APIDOC ## nh.hasParam(paramName) ### Description Check if the parameter server has the specified parameter. Returns a Promise that resolves with a boolean indicating existence. ### Parameters - **paramName** (string) - Required - The name of the parameter to check. ``` -------------------------------- ### Publish Messages Source: https://wiki.ros.org/rosnodejs/overview/Publishers%20and%20Subscribers Publish messages using an instance of the message object, a generic object, or with a throttle override. ```javascript const pub = nh.advertise('/my_topic', 'std_msgs/String'); ... const msg = new std_msgs.msg.String(); msg.data = 'Hello!'; pub.publish(msg) ``` ```javascript pub.publish({ data: 'Hello!' }); ``` ```javascript pub.publish(msg, -1); ``` -------------------------------- ### Require Generated Messages Source: https://wiki.ros.org/rosnodejs/overview/messages Load standard message or service types from catkin-generated packages. ```javascript const std_msgs = rosnodejs.require('std_msgs'); const StringMsg = std_msgs.msg.String; ``` ```javascript const SetBool = rosnodejs.require('std_srvs').srv.SetBool; ``` -------------------------------- ### serviceClient.call Source: https://wiki.ros.org/rosnodejs/overview/services Invokes the service call with a request object. ```APIDOC ## serviceClient.call(request) ### Description Calls the service and returns a promise that resolves with the response. ### Method ServiceClient.call ### Parameters - **request** (object) - Required - The request message instance. ``` -------------------------------- ### Register and remove shutdown hooks Source: https://wiki.ros.org/rosnodejs/overview/Initialization%20and%20Shutdown Managing shutdown event listeners for the ROS node. The function passed to removeListener must match the registered listener. ```javascript rosnodejs.on('shutdown', function() { }); rosnodejs.once('shutdown', function() { }); rosnodejs.removeListener('shutdown', function() { }); ``` -------------------------------- ### nh.deleteParam(paramName) Source: https://wiki.ros.org/rosnodejs/overview/params Removes a parameter from the parameter server. ```APIDOC ## nh.deleteParam(paramName) ### Description Removes a parameter from the parameter server. ### Parameters - **paramName** (string) - Required - The name of the parameter to delete. ``` -------------------------------- ### Delete a parameter Source: https://wiki.ros.org/rosnodejs/overview/params Removes a specified parameter from the parameter server. ```javascript nh.deleteParam(paramName); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.