ROS知识(20)----使用Master_API查询Master管理的节点话题服务内容
在一些应用中会需要获取master的uri地址,发布的话题,订阅的话题,发布的服务,节点的信息等等。这些功能我们通常可一通过rosnode list, rosnode info, rostopic list, rostopic info, rosservice list和 rosservice info 等命令查询,而这些功能是怎么做到的呢? 这就需要用到Master_API进行查询,ROS官方使用了python来实现(在ros_comm包中),这里我们使用c++来实现这些查询的功能。
1.实现
参考[1]API,功能描述细节不累赘了,下面为实现的代码:
#include "ros/ros.h"
#include "std_msgs/String.h"
#include <ros/master.h>
#include <iostream> /*
* Get the URI of the master.
*/
bool getUri(std::string&uri) {
XmlRpc::XmlRpcValue args, result, payload;
args[] = ros::this_node::getName(); if (!ros::master::execute("getUri", args, result, payload, true)) {
std::cout << "Failed!" << std::endl;
return false;
}
std::cout << "----------Master URI----------" << std::endl;
uri = std::string(payload);
std::cout << std::string(payload) << std::endl;
} /*
* Get list of topics that can be subscribed to.
* This does not return topics that have no publishers.
* See getSystemState() to get more comprehensive list.
* */
bool getPublishedTopics(const std::string& subgraph,
ros::master::V_TopicInfo& topics) {
XmlRpc::XmlRpcValue args, result, payload;
args[] = ros::this_node::getName();
args[] = subgraph;
if (!ros::master::execute("getPublishedTopics", args, result, payload,
true)) {
std::cout << "Failed!" << std::endl;
return false;
}
topics.clear();
std::cout << "----------PublishedTopics----------" << std::endl;
std::cout << "published_topic_name \t message_name" << std::endl;
for (int i = ; i < payload.size(); ++i) {
topics.push_back(
ros::master::TopicInfo(std::string(payload[i][]),
std::string(payload[i][])));
std::string v1 = std::string(payload[i][]);
std::string v2 = std::string(payload[i][]);
std::cout << v1.c_str() << "\t" << v2.c_str() << std::endl;
} return true;
} /*
* Retrieve list topic names and their types.
* */
bool getTopicTypes(ros::master::V_TopicInfo& topics) {
XmlRpc::XmlRpcValue args, result, payload;
args[] = ros::this_node::getName(); if (!ros::master::execute("getTopicTypes", args, result, payload, true)) {
std::cout << "Failed!" << std::endl;
return false;
}
topics.clear();
std::cout << "----------TopicTypes----------" << std::endl;
std::cout << "topic_name\t message_name" << std::endl;
for (int i = ; i < payload.size(); ++i) {
topics.push_back(
ros::master::TopicInfo(std::string(payload[i][]),
std::string(payload[i][])));
std::string v1 = std::string(payload[i][]);
std::string v2 = std::string(payload[i][]);
std::cout << v1.c_str() << "\t" << v2.c_str() << std::endl;
} return true;
} /*
* Get the XML-RPC URI of the node with the associated name/caller_id.
* This API is for looking information about publishers and subscribers.
* Use lookupService instead to lookup ROS-RPC URIs.
*/
bool lookupNode(const std::string& node, std::string&uri) {
XmlRpc::XmlRpcValue args, result, payload;
args[] = ros::this_node::getName();
args[] = node;
if (!ros::master::execute("lookupNode", args, result, payload, true)) {
std::cout << "Failed!" << std::endl;
return false;
}
std::cout << "----------LookupedNode----------" << std::endl;
uri = std::string(payload);
std::cout << node << ":" << std::string(payload) << std::endl;
} /*
* Lookup all provider of a particular service.
*/
bool lookupService(const std::string& service, std::string&uri) {
XmlRpc::XmlRpcValue args, result, payload;
args[] = ros::this_node::getName();
args[] = service;
if (!ros::master::execute("lookupService", args, result, payload, true)) {
std::cout << "Failed!" << std::endl;
return false;
}
std::cout << "----------LookupedService----------" << std::endl;
uri = std::string(payload);
std::cout << service << ":" << std::string(payload) << std::endl;
} /*
* Retrieve list representation of system state
* (i.e. publishers, subscribers, and services).
* */
bool getSystemState() {
XmlRpc::XmlRpcValue args, result, payload;
args[] = ros::this_node::getName();
if (!ros::master::execute("getSystemState", args, result, payload, true)) {
std::cout << "Failed!" << std::endl;
return false;
}
std::cout << "----------SystemState----------" << std::endl; //publishers
int t = ;
std::cout << "Published Topics:" << std::endl;
for (int j = ; j < payload[t].size(); ++j) {
//topics
std::cout << " *" << std::string(payload[t][j][]) << ":"
<< std::endl;
for (int k = ; k < payload[t][j][].size(); ++k) {
//publisher
std::cout << " *" << std::string(payload[t][j][][k])
<< std::endl;
}
}
t = ;
std::cout << "Subscribed Topics:" << std::endl;
for (int j = ; j < payload[t].size(); ++j) {
//topics
std::cout << " *" << std::string(payload[t][j][]) << ":"
<< std::endl;
for (int k = ; k < payload[t][j][].size(); ++k) {
//publisher
std::cout << " *" << std::string(payload[t][j][][k])
<< std::endl;
}
} t = ;
std::cout << "Services:" << std::endl;
for (int j = ; j < payload[t].size(); ++j) {
//topics
std::cout << " *" << std::string(payload[t][j][]) << ":"
<< std::endl;
for (int k = ; k < payload[t][j][].size(); ++k) {
//publisher
std::cout << " *" << std::string(payload[t][j][][k])
<< std::endl;
}
}
return true;
} int main(int argc, char **argv) {
ros::init(argc, argv, "listener");
ros::NodeHandle n; //get master uri
std::string host_uri;
getUri(host_uri); //get published topics
std::string subgraph;
ros::master::V_TopicInfo published_topics;
getPublishedTopics(subgraph, published_topics); //get topic types
ros::master::V_TopicInfo topics;
getTopicTypes(topics); //get node uri
std::string node, node_uri;
node = "/talker";
lookupNode(node, node_uri); //get service uri
std::string service, service_uri;
service = "/talker";
lookupService(service, service_uri); //get all published topics,subscribed topics and services
getSystemState(); ros::spin();
return ;
}
查询的结果如下:
----------Master URI----------
http://192.168.1.21:11311/
----------PublishedTopics----------
published_topic_name message_name
/rosout rosgraph_msgs/Log
/rosout_agg rosgraph_msgs/Log
/talker/message std_msgs/String
----------TopicTypes----------
topic_name message_name
/rosout rosgraph_msgs/Log
/rosout_agg rosgraph_msgs/Log
/talker/message std_msgs/String
----------LookupedNode----------
/talker:http://192.168.1.21:44625/
Failed!
----------SystemState----------
Published Topics:
*/rosout:
*/talker
*/listener
*/rosout_agg:
*/rosout
*/talker/message:
*/talker
Subscribed Topics:
*/rosout:
*/rosout
Services:
*/talker/set_logger_level:
*/talker
*/listener/set_logger_level:
*/listener
*/rosout/get_loggers:
*/rosout
*/talker/get_loggers:
*/talker
*/rosout/set_logger_level:
*/rosout
*/listener/get_loggers:
*/listener
2.参考资料
[1]. Master_API
ROS知识(20)----使用Master_API查询Master管理的节点话题服务内容的更多相关文章
- ROS知识(5)----消息与服务的示例
ROS中已经定义了较多的标准类型的消息,你可以用在这些标准类型的消息上再自定义自己的消息类型.这个在复杂数据传输很有用,例如节点和服务器进行交互时,就可能用到传输多个参数到服务器,并返回相应的结果.为 ...
- ROS知识(2)----理解ROS系统结构
学习新事物,方法高于技术本身,如果没有把握"BIG PICTURE"的话很难理解进去.通过以下几点进行理解ROS: ROS实际上不是操作系统,他只是一个通信的框架,一个代码管理的架 ...
- C#开发微信门户及应用(20)-微信企业号的菜单管理
前面几篇陆续介绍了很多微信企业号的相关操作,企业号和公众号一样都可以自定义菜单,因此他们也可以通过API进行菜单的创建.获取列表.删除的操作,因此本篇继续探讨这个主体,介绍企业号的菜单管理操作. 菜单 ...
- RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.0 版新增查询引擎管理
欲了解V3.0版本的相关内容可查看下面的链接地址. RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.0 版本发布 RDIFramework.NET — 基于.NET的快速信 ...
- ORACLE表空间查询和管理【转】
红色是自由指定的~~--查询表空间SELECT D.TABLESPACE_NAME, SPACE "SUM_SPACE(M)", SPACE - NVL(F ...
- [敏杰开发]知识路书——图形化文献管理大师 Beta版发布喽!!!
[敏杰开发]知识路书--图形化文献管理大师 Beta版发布喽!!! 一.总览 项目名称:知识路书 发布形式:网页应用 发布地址:http://roadmap.imcoming.top 二.运行环境与使 ...
- MySQL中间件之ProxySQL(6):管理后端节点
返回ProxySQL系列文章:http://www.cnblogs.com/f-ck-need-u/p/7586194.html 1.配置后端节点前的说明 为了让ProxySQL能够找到后端的MySQ ...
- ProxySQL(6):管理后端节点
文章转载自:https://www.cnblogs.com/f-ck-need-u/p/9286922.html 配置后端节点前的说明 为了让ProxySQL能够找到后端的MySQL节点,需要将后端的 ...
- SharePoint 2013管理中心里【管理服务器上的服务】不见了
打开管理中心,准备配置Managed Metadata Service,发现"管理服务器上的服务"不见了 那我自己拼url直接访问:http://xxxx/_admin/Serve ...
随机推荐
- 洛谷P2886牛继电器
传送门啦 倍增 $ Floyd $ 注意结构体里二维数组不能开到 $ 2000 $ #include <iostream> #include <cstdio> #include ...
- [经典算法题]寻找数组中第K大的数的方法总结
[经典算法题]寻找数组中第K大的数的方法总结 责任编辑:admin 日期:2012-11-26 字体:[大 中 小] 打印复制链接我要评论 今天看算法分析是,看到一个这样的问题,就是在一堆数据 ...
- Oracle学习笔记:wm_concat函数合并字段
在Oracle中使用wm_concat(column)可以实现字段的分组合并,逗号分隔. 例如,现有表temp_cwh_test: -- 创建临时表 create table temp_cwh_tes ...
- MySQL学习笔记:创建整年日期
见识到另外一种创意,惊讶! 1.创建小数据表 0-9 # 创建小数据表 DROP TABLE IF EXISTS aa_numbers_small; CREATE TABLE aa_numbers_s ...
- Vue-Socket.io
github地址:https://github.com/MetinSeylan/Vue-Socket.io 安装: npm install vue-socket.io -S 注册: import Vu ...
- sublime text配置fcitx输入法
sublime text配置fcitx输入法 环境:Fedora20 输入法:fcitx sublime text:version 3 安装fcitx输入法 这个看前面教程 编译sublime-imf ...
- TeamViewer的下载地址,低调低调
https://github.com/cary-zhou/TeamViewer13-Crack
- hdu 4664 划线(SG)
N个平面,每个平面有ni个点 两个人玩游戏,划线,他们可以划任意一个平面的两个点,有以下要求:两个人划得线不能交叉,不要划已经划过的线,如果一个平面被划了一个空心的三角形,那么这个平面就不能继续划线了 ...
- sql server2014 企业版 百度云下载
sql server2014 企业版 百度云下载 链接: https://pan.baidu.com/s/1j7a6RWwpvSzG-sF7Dnexfw 提取码: 关注公众号[GitHubCN]回复获 ...
- codeforces 354 D. Transferring Pyramid
D. Transferring Pyramid time limit per test 3 seconds memory limit per test 256 megabytes input stan ...