文章目录
Client Library
这个概念和我们理解的API的概念很类似。本质上就是一个提供ROS编程的库。例如:建立node,发布消息,调用服务。它封装了底层的操作流程,方便我们去写代码,去调用。
客户端库有多个版本:roscpp、rospy、roslisp
上面两讲讲了roscpp,这一讲会提到rospy。rospy接口主要包括了Node、Topic、Service、Parameter、Time几个类别。
rospy
rospy-Node 相关
函数
init_node(name) # 注册和初始化node
import rospy
rospy.init_node('my_node') # 没有返回值
MasterProxy
- get_master() #获取master的句柄
bool
- is_shutdown() #返回是否关闭
- on_shutdown(fn) #在node关闭时调用函数fn
str
- get_node_uri() # 返回节点的URI
- get_name() # 返回本节点的全名
- get_namespace() # 返回本节点的名字空间
rospy-Topic 相关
函数
[[str1, str2]] get_published_topics() #返回正在被发布的所有topic名称(str1)和类型(str2)
Message
-
wait_for_message(topic, topic_type, time_out=None) # 等待指定topic的一个msg
-
spin() # 触发topic或service的处理,会阻塞直到关闭
Publisher类
- __init__(self,name,data_class,queue_size=None) # 构造函数
- publish(self,msg) # 成员函数 发布消息
- unregister(self) # 成员函数 停止发布
case:
pub = rospy.Publisher('topic_name','topic_type',queue_size=None) #size=None相当于同步通信,大于零会为异步通信
pub.publish(msg)
Subscriber类
- __init__(self, name, data_class, call_back=None, queue_size=None) # 构造函数
- unregister(self) # 成员函数 停止订阅
rospy-Service 相关
函数
wait_for_service(service,timeout=None) # 阻塞直到服务可用
Service类 --> server相关
_init_(self, name, service_class, handler) # 构造函数 提供服务
shutdown(self) # 成员函数 关闭服务
handler case:
def handler(req):
pass
return res #只传入请求,只传出响应
ServiceProxy类 --> client相关
__init__(self, name, service_class) # 构造函数 服务的请求方
Response
- call(self, *args, **kwds) # 调用服务
- __call__(self, *args, **kwds) # 调用服务
client = ServiceProxy(...) //构造函数
client.call(req)
#或者
resp = client(req)
rospy-Param 相关
函数
XmlRpcLegalValue
- get_param(param_name, default = _unspecified) # 获取参数的值
[str]
- get_param_names() # 获取参数的名称
- set_param(param_name, param_value) # 设置参数的值
- delete_param(param_name) # 删除参数
bool
- has_param( param_name) # 参数是否存在于参数服务器上
str
- search_param() # 搜索参数
rospy-Time 相关
Time类 --> 指时刻
__init__(self, secs=0, nsecs = 0) # 构造函数
Time now() # 静态方法 返回当前时刻的Time对象
case:
rospy.Time(secs = 1, nsecs = 0)
# or
rospy.Time.now()
Duration类 --> 指一段时间
__init__(self, secs = 0, nsecs = 0) # 构造函数 秒和纳秒
注意 : 时间,时刻的加减操作得出结果可能不同,且两个时刻不可以相加减。
函数
Time
- get_rostime() # 当前时刻的Time对象
float
- get_time() # 返回当前时间, 返回float单位秒
- sleep(duration) # 执行挂起
Rate类
__init__(self, frequency) # 构造函数
sleep(self) # 挂起 考虑上一次的rate.sleep() 的时间
Time
- remaining(self) # 成员函数 剩余sleep时间
转载请注明出处。
本文总结于中国大学MOOC《机器人操作系统入门》
链接: link.
图片来自于课程视频截图