1、rospy.init_node(name, argv, anonymous,…)
2、pub = rospy.Publisher(“name”,data_class,latch=, queue_size=)
- 作用:
创建发布者对象 - 参数:
- name: 话题名称
- data_class: 发布的数据类型
- latch: 默认为False,当设置为True时,发布方会保留最后一条消息,当有新的订阅方订阅时,发送给订阅方,且只发送一次
- 使用:
同C++
3、rospy.spin()
- 作用:
用于处理回调函数 - 注意:
只有spin函数,没有spinOnce()函数
4、时间函数
import rospy
def doMsg(event):
rospy.loginfo("调用回调函数的时刻:%.2f",event.current_real.to_sec())
if __name__ == "__main__":
rospy.init_node("hello_time_p")
begin = rospy.Time.now()
rospy.loginfo("当前时刻:%.2f",begin.to_sec())
rospy.loginfo("当前时刻:%d",begin.secs)
t1 = rospy.Time(12.3)
t2 = rospy.Time(12,234567)
rospy.loginfo("t1=%.2f",t1.to_sec())
rospy.loginfo("t2=%.2f",t2.to_sec())
t3 = rospy.Time.from_sec(22.4444)
rospy.loginfo("t3=%.2f",t3.to_sec())
rospy.loginfo("休眠前——————————————————————")
du1 = rospy.Duration(4)
rospy.sleep(du1)
rospy.loginfo("休眠后——————————————————————")
du2 = rospy.Duration(5)
t4 = begin + du2
rospy.loginfo("结束时刻:%.2f",t4.to_sec())
du_sum = du1 + du2
rospy.loginfo("持续时间相加:%.2f",du_sum.to_sec())
'''
@param period: desired period between callbacks
@type period: rospy.Duration
@param callback: callback to be called
@type callback: function taking rospy.TimerEvent
@param oneshot: if True, fire only once, otherwise fire continuously until shutdown is called [default: False]
@type oneshot: bool
'''
timer = rospy.Timer(rospy.Duration(2),doMsg,True)
rate = rospy.Rate(1)
while not rospy.is_shutdown():
rate.sleep()
rospy.loginfo("设置运行频率")
5、节点相关函数
- rospy.is_shutdown():
- 作用: 判断节点是否关闭
- 返回结果: bool值,关闭:True; 未关闭:False
- rospy.on_shutdown(callback):
- 作用: 关闭节点的时候,执行回调函数
- callback: 回调函数
- rospy.signal_shutdown(“节点已关闭”):
6、日志函数
rospy.logdebug("hello,debug")
rospy.loginfo("hello,info")
rospy.logwarn("hello,warn")
rospy.logerr("hello,error")
rospy.logfatal("hello,fatal")