Skip to content

Commit b6ea8e0

Browse files
committed
Merge branch 'devel' of github.com:garaemon/roslibjs into devel
2 parents cc42fee + 2261331 commit b6ea8e0

File tree

3 files changed

+191
-1
lines changed

3 files changed

+191
-1
lines changed

build/roslib.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,101 @@ ROSLIB.Ros.prototype.getParams = function(callback) {
585585
});
586586
};
587587

588+
/**
589+
* Retrieves a type of ROS topic.
590+
*
591+
* @param callback - function with params:
592+
* * type - String of the topic type
593+
*/
594+
ROSLIB.Ros.prototype.getTopicType = function(topic, callback) {
595+
var topicTypeClient = new ROSLIB.Service({
596+
ros : this,
597+
name : '/rosapi/topic_type',
598+
serviceType : 'rosapi/TopicType'
599+
});
600+
var request = new ROSLIB.ServiceRequest({
601+
topic: topic
602+
});
603+
topicTypeClient.callService(request, function(result) {
604+
callback(result.type);
605+
});
606+
};
607+
608+
/**
609+
* Retrieves a detail of ROS message.
610+
*
611+
* @param callback - function with params:
612+
* * details - Array of the message detail
613+
* @param message - String of a topic type
614+
*/
615+
ROSLIB.Ros.prototype.getMessageDetails = function(message, callback) {
616+
var messageDetailClient = new ROSLIB.Service({
617+
ros : this,
618+
name : '/rosapi/message_details',
619+
serviceType : 'rosapi/MessageDetails'
620+
});
621+
var request = new ROSLIB.ServiceRequest({
622+
type: message
623+
});
624+
messageDetailClient.callService(request, function(result) {
625+
callback(result.typedefs);
626+
});
627+
};
628+
629+
/**
630+
* Encode a typedefs into a dictionary like `rosmsg show foo/bar`
631+
* @param type_defs - array of type_def dictionary
632+
*/
633+
ROSLIB.Ros.prototype.decodeTypeDefs = function(type_defs) {
634+
var typeDefDict = {};
635+
var theType = type_defs[0];
636+
637+
// It calls itself recursively to resolve type definition
638+
// using hint_defs.
639+
var decodeTypeDefsRec = function(theType, hint_defs) {
640+
var typeDefDict = {};
641+
for (var i = 0; i < theType.fieldnames.length; i++) {
642+
var arrayLen = theType.fieldarraylen[i];
643+
var fieldName = theType.fieldnames[i];
644+
var fieldType = theType.fieldtypes[i];
645+
if (fieldType.indexOf('/') === -1) { // check the fieldType includes '/' or not
646+
if (arrayLen === -1) {
647+
typeDefDict[fieldName] = fieldType;
648+
}
649+
else {
650+
typeDefDict[fieldName] = [fieldType];
651+
}
652+
}
653+
else {
654+
// lookup the name
655+
var sub_type = false;
656+
for (var j = 0; j < hint_defs.length; j++) {
657+
if (hint_defs[j].type === fieldType) {
658+
sub_type = hint_defs[j];
659+
break;
660+
}
661+
}
662+
if (sub_type) {
663+
var sub_type_result = decodeTypeDefsRec(sub_type, hint_defs);
664+
if (arrayLen === -1) {
665+
typeDefDict[fieldName] = sub_type_result;
666+
}
667+
else {
668+
typeDefDict[fieldName] = [sub_type_result];
669+
}
670+
}
671+
else {
672+
throw 'cannot find ' + fieldType;
673+
}
674+
}
675+
}
676+
return typeDefDict;
677+
}; // end of decodeTypeDefsRec
678+
679+
return decodeTypeDefsRec(type_defs[0], type_defs);
680+
};
681+
682+
588683
/**
589684
* @author Brandon Alexander - [email protected]
590685
*/

0 commit comments

Comments
 (0)