Skip to content

Commit 4320382

Browse files
committed
adding functions to Ros: decodeTypeDefs, getMessageDetails, getTopicType
1 parent cc42fee commit 4320382

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

src/core/Ros.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,3 +270,103 @@ ROSLIB.Ros.prototype.getParams = function(callback) {
270270
callback(result.names);
271271
});
272272
};
273+
274+
/**
275+
* Retrieves a type of ROS topic.
276+
*
277+
* @param callback - function with params:
278+
* * type - String of the topic type
279+
*/
280+
ROSLIB.Ros.prototype.getTopicType = function(topic, callback) {
281+
var topicTypeClient = new ROSLIB.Service({
282+
ros : this,
283+
name : '/rosapi/topic_type',
284+
serviceType : 'rosapi/TopicType'
285+
});
286+
var request = new ROSLIB.ServiceRequest({
287+
topic: topic
288+
});
289+
topicTypeClient.callService(request, function(result) {
290+
callback(result.type);
291+
});
292+
};
293+
294+
/**
295+
* Retrieves a detail of ROS message.
296+
*
297+
* @param callback - function with params:
298+
* * details - Array of the message detail
299+
* @param message - String of a topic type
300+
*/
301+
ROSLIB.Ros.prototype.getMessageDetails = function(message, callback) {
302+
var messageDetailClient = new ROSLIB.Service({
303+
ros : this,
304+
name : '/rosapi/message_details',
305+
serviceType : 'rosapi/MessageDetails'
306+
});
307+
var request = new ROSLIB.ServiceRequest({
308+
type: message
309+
});
310+
messageDetailClient.callService(request, function(result) {
311+
var typedefs = result.typedefs;
312+
callback(result.typedefs);
313+
});
314+
};
315+
316+
/**
317+
* Encode a typedefs into a dictionary like `rosmsg show foo/bar`
318+
* @param type_defs - array of type_def dictionary
319+
*/
320+
ROSLIB.Ros.decodeTypeDefs = function(type_defs) {
321+
var type_def_dict = {};
322+
var the_type = type_defs[0];
323+
return ROSLIB.Ros._decodeTypeDefs(type_defs[0], type_defs);
324+
};
325+
326+
/**
327+
* Internal function of ROSLIB.Ros.decodeTypeDefs.
328+
* It calls itself recursively to resolve type definition
329+
* using hint_defs.
330+
* @param the_type - array of type_def dictionary
331+
* @param hint_defs - array of typedefs
332+
*/
333+
ROSLIB.Ros._decodeTypeDefs = function(the_type, hint_defs) {
334+
var type_def_dict = {};
335+
for (var i = 0; i < the_type.fieldnames.length; i++) {
336+
var array_len = the_type.fieldarraylen[i];
337+
var field_name = the_type.fieldnames[i];
338+
var field_type = the_type.fieldtypes[i];
339+
if (field_type.indexOf("/") === -1) { // check the field_type includes "/" or not
340+
if (array_len == -1) {
341+
type_def_dict[field_name] = field_type;
342+
}
343+
else {
344+
type_def_dict[field_name] = [field_type];
345+
}
346+
}
347+
else {
348+
// lookup the name
349+
var sub_type = false;
350+
for (var j = 0; j < hint_defs.length; j++) {
351+
if (hint_defs[j].type == field_type) {
352+
sub_type = hint_defs[j];
353+
break;
354+
}
355+
}
356+
if (sub_type) {
357+
var sub_type_result = ROSLIB.Ros._decodeTypeDefs(sub_type, hint_defs);
358+
if (array_len == -1) {
359+
type_def_dict[field_name] = sub_type_result;
360+
}
361+
else {
362+
type_def_dict[field_name] = [sub_type_result];
363+
}
364+
}
365+
else {
366+
throw "cannot find " + field_type;
367+
}
368+
//ROSLIB.Ros._decodeTypeDefs(field_type, hint_defs)
369+
}
370+
}
371+
return type_def_dict;
372+
};

0 commit comments

Comments
 (0)