Skip to content

Commit 2b8ca58

Browse files
committed
Add types to ParameterService
Since ROS2 Jazzy, parameter services support a service type of rcl_interfaces/srv/GetParameterTypes, which can get a list of parameter types associated with the requested parameters. This patch implements: 1. Add service /node_name/get_parameters, which is used to get the parameter types. 2. Add unit test, Get_parameter_types, into test-parameter-service.js. Meanwhile, this patch fixes the timeout issue when executing: ros2 param list node_name because of absence of /node_name/get_parameters. Fix: #1024
1 parent 91f59a4 commit 2b8ca58

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

lib/node.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,26 @@ class Node extends rclnodejs.ShadowNode {
12731273
return params;
12741274
}
12751275

1276+
/**
1277+
* Get the types of given parameters.
1278+
*
1279+
* Return the types of given parameters.
1280+
*
1281+
* @param {string[]} [names] - The names of the declared parameters.
1282+
* @return {Uint8Array} - The types.
1283+
*/
1284+
getParameterTypes(names = []) {
1285+
let types = [];
1286+
1287+
for (const name of names) {
1288+
const descriptor = this._parameterDescriptors.get(name);
1289+
if (descriptor) {
1290+
types.push(descriptor.type);
1291+
}
1292+
}
1293+
return types;
1294+
}
1295+
12761296
/**
12771297
* Get the names of all declared parameters.
12781298
*

lib/parameter_service.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,16 @@ class ParameterService {
110110
}
111111
);
112112

113+
// Create GetParameterTypes service.
114+
const getParameterTypesServiceName = nodeName + '/get_parameter_types';
115+
this._node.createService(
116+
'rcl_interfaces/srv/GetParameterTypes',
117+
getParameterTypesServiceName,
118+
(request, response) => {
119+
this._handleGetParameterTypes(request, response);
120+
}
121+
);
122+
113123
// create SetParametersAtomically service
114124
const setParametersAtomicallyServiceName =
115125
nodeName + '/set_parameters_atomically';
@@ -266,6 +276,23 @@ class ParameterService {
266276
response.send(msg);
267277
}
268278

279+
/**
280+
* Get a list of parameter types.
281+
*
282+
* request.names identifies the parameter types to get.
283+
*
284+
* @param {GetParameterTypes_Request} request - The client request.
285+
* @param {GetParameterTypes_Response} response - The service response with
286+
* Uint8Array.
287+
* @return {undefined} -
288+
*/
289+
_handleGetParameterTypes(request, response) {
290+
const types = this._node.getParameterTypes(request.names);
291+
const msg = response.template;
292+
msg.types = types;
293+
response.send(msg);
294+
}
295+
269296
/**
270297
* Update a list of parameters atomically.
271298
*

test/test-parameter-service.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,4 +367,34 @@ describe('Parameter_server tests', function () {
367367
`Expected 3 parameter-events, received ${eventCount} events.`
368368
);
369369
});
370+
371+
it('Get_parameter_types', async function () {
372+
const client = clientNode.createClient(
373+
'rcl_interfaces/srv/GetParameterTypes',
374+
'test_node/get_parameter_types'
375+
);
376+
await client.waitForService();
377+
378+
const ParamTypes = rclnodejs.require('rcl_interfaces/msg/ParameterType');
379+
const request = new (rclnodejs.require(
380+
'rcl_interfaces/srv/GetParameterTypes'
381+
).Request)();
382+
request.names = ['p1', 'p2', 'A.p3'];
383+
let success = false;
384+
385+
client.sendRequest(request, (response) => {
386+
assert.deepEqual(response.types.length, 3);
387+
assert.deepEqual(
388+
response.types,
389+
Uint8Array.from([
390+
ParamTypes.PARAMETER_STRING,
391+
ParamTypes.PARAMETER_INTEGER,
392+
ParamTypes.PARAMETER_BOOL,
393+
])
394+
);
395+
success = true;
396+
});
397+
await assertUtils.createDelay(STD_WAIT);
398+
assert.ok(success);
399+
});
370400
});

0 commit comments

Comments
 (0)