ros入门实例,ros基本概念

  ros入门实例,ros基本概念

  古月居活性氧簇(活性氧物种的缩写)入门21讲第十二讲话题消息的定义与使用Person.msg字符串姓名性别年龄未知=0男性=1女性=2 c

  person _ publisher。CPP #包括ro/ro。h #包含“学习主题/人”。h int main(int argc,charwzdbksargv){ ros:init(argc,argv, person _ publisher );ROS:节点句柄n;ROS:Publisher Person _ info _ pub=n . advertise elearning _ topic:Person(/Person _ info ,10);ROS:Rate循环速率(1);while(ROS:ok()){ learning _ topic:Person Person _ msg;person _ msg . name= Tom person _ msg . age=10 person _ msg。性别=学习_话题:人:男;个人信息发布。发布(person _ msg);ROS_INFO(发布人信息:姓名:%s年龄:%d性别:%d ,person_msg.name.c_str(),person_msg.age,person_msg。性);循环速率。睡眠();}返回0;}个人_订户

  #包括ro/ro。h #包含“学习主题/人”。h void personInfoCallback(const learning _ topic:Person:ConstPtrmsg){ ROS _ INFO(订阅者个人信息:姓名:%s年龄:%d性别:%d ,msg-name.c_str(),msg-年龄,消息-性别);}int main(int argc,charwzdbksargv){ ros:init(argc,argv, person _ subscriber );ROS:节点句柄n;ROS:Subscriber person _ info _ sub=n . subscribe(/person _ info ,10,personInfoCallback);ROS:spin();返回0;} vim src/learning _ topic/package。可扩展置标语言在package.xml中添加

  build _ depend message _ generation id _ depend exec _ depend message _ runtime/exec _ depend vim src/learning _ topic/cmakelists。文本文件(textfile)在CMakeLists.txt中找到相应位置添加

  查找包(柳絮必需组件geometry _ msgs ROS CPP rospy STD _ msgs turtle sim message _ generation)add _ message _ FILES(FILES person。msg)生成消息(依赖标准消息)柳絮_包(# INCLUDE _ DIRS INCLUDE #图书馆学习_主题#这里直接把注释去掉并添加消息_运行时柳絮_ DEPENDS geometry _ msgs ROS CPP STD _ msgs turtle sim message _ runtime # DEPENDS system _ lib)add _ executable(person _ publisher src/person _ publisher。CPP)target _ link _ LIBRARIES(person _ publisher $ {柳絮_ LIBRARIES })add _ dependencies(person _ publisher $ { PROJECT _ NAME } _ generate _ messages _ CPP)add _ executable(person _ subscriber src/person _ subscriber。CPP)target _ link _ LIBRARIES(person _ subscriber $ {柳絮_ LIBRARIES })add _ dependencies(

  CD ~/柔荑_ws柔荑_makesource devel/setup.bash运行

  roscorerosrun学习_话题人物_发布者rosrun学习_话题人物_订阅者Python

  person_publisher.py #!/usr/bin/python从learning _ topic导入rospy。msg导入person def person _ publisher():rospy。init _ node( person _ publisher ,anonymous=True)person _ info _ pub=rospy .Publisher(/person_info ,person,queue_size=10) rate=rospy .rate(1)Person _ msg=Person()Person _ msg。name= Tom Person _ msg。年龄=10人_消息。性=人。男性;而不是罗斯贝。is _ shut down():person _ info _ pub。发布(person _ msg)罗斯比。日志信息(发布人员信息[% s,%d,%d],人员消息名称,人员消息年龄,人员消息。性)率。sleep()if _ _ name _ _= _ _ main _ _ :try:person _ publisher()除了罗斯贝.ROSInterruptException():传递person_subscriber.py

  #!/usr/bin/python从learning _ topic导入rospy。消息导入人def personInfoCallback(msg):rospy。日志信息(订阅人信息[%s,%d,%d],消息名称,消息年龄,消息。sex)def person _ subscriber():rospy。init _ node( person _ subscriber ,anonymous=True) rospy .订户(/person_info ,person,personInfoCallback)rospy。spin()if _ _ name _ _= _ _ main _ _ :Person _ subscriber()运行(注意需要增加执行权限)

  roscorerosrun学习_话题人物_发布者。pyrosrun学习_话题人物_订阅者。巴拉圭

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: