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 ...
随机推荐
- HTML5 之图片上传预处理
在开发 H5 应用的时候碰到一个问题,应用只需要一张小的缩略图,而用户用手机上传的确是一张大图,手机摄像机拍的图片好几 M,这可要浪费很多流量. 像我这么为用户着想的程序员,绝对不会让这种事情发生的, ...
- AngularJS中ng-class使用方法
转自:https://blog.csdn.net/jumtre/article/details/50802136 其他博文ng-class使用方法:https://blog.csdn.net/sina ...
- 洛谷P3378堆
传送门啦 #include <iostream> #include <cstdio> #include <cstring> #include <algorit ...
- Flask form
用户登录 #!/usr/bin/env python # -*- coding:utf- -*- from flask import Flask, render_template, request, ...
- 详解MySQL大表优化方案
单表优化 除非单表数据未来会一直不断上涨,否则不要一开始就考虑拆分,拆分会带来逻辑.部署.运维的各种复杂度,一般以整型值为主的表在千万级以下,字符串为主的表在五百万以下是没有太大问题的.而事实上很多时 ...
- 每位架构师都应该熟知的 10 个 SOA 设计模式
这 10 个 SOA 设计模式是如此之重要,其应用是如此之广泛,以至于它们都有些显而易见了. 1. 服务无关 服务无关实现对多种业务通用的逻辑.将服务无关的逻辑分离成离散的服务以方便服务的重用和整合. ...
- (二) solr 索引数据导入:xml格式
xml 是最常用的数据索引格式,不仅可以索引数据,还可以对文档与字段进行增强,从而改变它们的重要程度. 下面就是具体的实现方式: schema.xml的字段配置部分如下: <field name ...
- Linux学习笔记:crontab定时任务
通过crontab 命令,我们可以在固定的间隔时间执行指定的系统指令或 shell script脚本.时间间隔的单位可以是分钟.小时.日.月.周及以上的任意组合.这个命令非常适合周期性的日志分析或数据 ...
- MySQL学习笔记:调用存储过程或函数报1418错误
问题 MySQL开启bin-log后,调用存储过程或者函数以及触发器时,会出现错误号为1418的错误: ERROR 1418 (HY000): This function has none of DE ...
- 关于jedis2.4以上版本的连接池配置,及工具类
jedis.propertise 注意以前版本的maxAcitve和maxWait有所改变,JVM根据系统环境变量ServerType中的值 取不同的配置,实现多环境(测试环境.生产环境)集成. re ...