ros入门教程(六)-- 客户端库(上)

Client Library

这个概念和我们理解的API的概念很类似。本质上就是一个提供ROS编程的库。例如:建立node,发布消息,调用服务。它封装了底层的操作流程,方便我们去写代码,去调用。

客户端库有多个版本:roscpp、rospy、roslisp

roscpp

ros::init()

解析传入的ROS参数, 为当前node命名。在使用roscpp之前都必须调用ros::init函数。
:: 双冒号表示作用域的限定符,它在此表示init函数是在ros这个命名空间之中的。

ros::NodeHandle Class

可用来创建topic、services、parameters,并调用公共接口。这是ros里的一个类,里面常用的成员函数如下:
发布和订阅

//创建话题的publisher,返回一个publiser的对象
ros::Publisher advertise(const string &topic, uint32_t queue_size);

//&topic : 指定topic名称,确定往哪个topic发布消息
//queue_size : 指定发送的消息队列长度
//用法:
ros::NodeHandle nh; //先声明一个Nodehandle
ros::Publisher pub = nh.advertise(); //返回了publisher的对象pub
pub.publish(msg) //发布消息
//创建话题的subscriber
ros::Subscriber subscribe(const string &topic, uint32_t queue_size, void(*)(M));
//M:回调函数的指针,当收到msg后,就会由回调函数处理msg

服务端和客户端

//创建服务的server
ros::ServiceServer advertiseService(const string &service, bool(*srv_func)(Mreq &, Mres&));
//创建服务的clent
ros::ServiceClient serviceClient(const string &service_name, bool persistent=false);

参数相关

//查询某个参数的值
bool getParam(const string &key, void &val);
//给参数赋值
bool setparam(const string &key, void val);
ros::master Namespace

通过该函数来从master查询消息。master不是类,是一个命名空间,两者区别于在调用时没有对象的概念,如:ros::master::check();即可直接调用该函数。常用函数如下:

bool check(); //检查master是否启动
const string& getHost(); //返回master所处的hostname
bool getNodes(V_string &nodes); //从master返回已知的node名称列表
bool getTopics(V_TopicInfo &topics); //返回所有正在被发布的topic列表
bool getURL(); //返回到master的URL地址,如http://host:port/
unit32_t getPort(); //返回master运行在的端口
ros::this_node Namespace

通过该函数从该进程的节点查询函数消息。

ros::service Namespace

可用来查询服务的消息。

//调用一个PRC服务
bool call(const string &service, Service &service);
//创建一个服务的client
ServiceClient createClient(const string& service_name, bool persistent=false, const M_string &header_values=M_string());
//确认服务可调用
bool exists(const string &service_name, bool print_failure_reason);
//等待一个服务,直到它可调用
bool waitForService (const string &service_name, int32_t timeout);
ros::param

可用来直接查询参数服务器的消息,而且不需要ros::NodeHandle。

ros::names Namespace

包含处理ROS图资源名称的函数。

string append(const std::string &left, const std::string &string); //追加名称
string clean(const string &name); //清除图资源名称:删去双斜线、结尾斜线
const M_string &getRemappings(); //返回重映射remapping
string remap(const string &name); //对名称重映射
string resolve(const string &name, bool remap=true); //解析出名称的全名
bool validate(const string &name, string &error); //验证名称

转载请注明出处。
本文总结于中国大学MOOC《机器人操作系统入门》
链接: link.
图片来自于课程视频截图

发布了43 篇原创文章 · 获赞 20 · 访问量 1473

猜你喜欢

转载自blog.csdn.net/Chen_2018k/article/details/104334367