rostopic demo

 

发布者

#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-  
import rospy
from hdw_driver.msg import update_file_msg

def talker():
    pub = rospy.Publisher('hdw_update',update_file_msg, queue_size=10)
    rospy.init_node('talker',anonymous=True)
    #rate = rospy.Rate(10) # 10hz

    mi = update_file_msg()
    mi.motor_board_file_name="/opt/xx/motor_board_file_name.rb"
    mi.power_board_file_name="/opt/xx/power_board_file_name.rb"
    mi.sensor_board_file_name="/opt/xx/sensor_board_file_name.rb"
    mi.imu_board_file_name="/opt/xx/imu_board_file_name.rb"

    mi.update_motor_board=True
    mi.update_power_board=True
    mi.update_sensor_board=True
    mi.update_imu_board=True
    pub.publish(mi)
    rospy.signal_shutdown("closed!")
 
if __name__ == '__main__':
    try:
        talker()
    except rospy.ROSInterruptException:
        pass

 

订阅者:

#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-  

import rospy
from hdw_driver.msg import update_file_msg

def callback(data):
    rospy.loginfo(data)
    #回调函数 收到的参数.data是通信的数据 默认通过这样的 def callback(data) 取出data.data数据
    
def listener():

    # In ROS, nodes are uniquely named. If two nodes with the same
    # node are launched, the previous one is kicked off. The
    # anonymous=True flag means that rospy will choose a unique
    # name for our 'listener' node so that multiple listeners can
    # run simultaneously.
    rospy.init_node('listener', anonymous=True)
    
    #启动节点同时为节点命名, 若anoymous为真则节点会自动补充名字,实际名字以 listener_322345等表示
    #若为假,则系统不会补充名字,采用用户命名。但是一次只能有一个同名节点,若后面有一个相同listener
    #名字的节点则后面的节点启动会注销前面的相同节点名。
    
    rospy.Subscriber("hdw_update", update_file_msg, callback)
    
    #启动订阅,订阅主题,及标准字符串格式,同时调用回调函数,当有数据时调用函数,取出数据
    
    # spin() simply keeps python from exiting until this node is stopped
    
    #循环程序
    rospy.spin()

if __name__ == '__main__':
    listener()

 

转载于:https://www.cnblogs.com/sea-stream/p/10523688.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值