完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
|
|
相关推荐
1个回答
|
|
自定义话题消息
定义msg文件 cd ~/catkin_workspace/src/learning_topic mkdir msg nano ~/catkin_workspace/src/learning_topic/msg/Person.msg string name uint8 age uint8 sex uint8 unknown = 0 uint8 male = 1 uint8 female = 2 在package.xml中添加功能包依赖 nano ~/catkin_workspace/src/learning_topic/package.xml 在CMakeLists.txt添加编译选项 ... find_package(... message_generation) ... add_message_files(FILES Person.msg) generate_messages(DEPENDENCIES std_msgs) ... catkin_package(... message_runtime) ... nano ~/catkin_workspace/src/learning_topic/CMakeLists.txt 编译 cd ~/catkin_workspace catkin_make 结果 可在~/catkin_workspace/devel/include/learning_topic下看到生成的Person.h 使用 C++ 编写程序 person_publisher.cpp nano ~/catkin_workspace/src/learning_topic/src/person_publisher.cpp /*********************************************************************** Copyright 2020 GuYueHome (www.guyuehome.com). ***********************************************************************/ /** * 该例程将发布/person_info话题,自定义消息类型learning_topic::Person */ #include #include "learning_topic/Person.h" int main(int argc, char **argv) { // ROS节点初始化 ros::init(argc, argv, "person_publisher"); // 创建节点句柄 ros::NodeHandle n; // 创建一个Publisher,发布名为/person_info的topic,消息类型为learning_topic::Person,队列长度10 ros::Publisher person_info_pub = n.advertise // 设置循环的频率 ros::Rate loop_rate(1); int count = 0; while (ros::ok()) { // 初始化learning_topic::Person类型的消息 learning_topic::Person person_msg; person_msg.name = "Tom"; person_msg.age = 18; person_msg.sex = learning_topic::Person::male; // 发布消息 person_info_pub.publish(person_msg); ROS_INFO("Publish Person Info: name:%s age:%d sex:%d", person_msg.name.c_str(), person_msg.age, person_msg.sex); // 按照循环频率延时 loop_rate.sleep(); } return 0; } person_subscriber.cpp nano ~/catkin_workspace/src/learning_topic/src/person_subscriber.cpp /*********************************************************************** Copyright 2020 GuYueHome (www.guyuehome.com). ***********************************************************************/ /** * 该例程将订阅/person_info话题,自定义消息类型learning_topic::Person */ #include #include "learning_topic/Person.h" // 接收到订阅的消息后,会进入消息回调函数 void personInfoCallback(const learning_topic::Person::ConstPtr& msg) { // 将接收到的消息打印出来 ROS_INFO("Subcribe Person Info: name:%s age:%d sex:%d", msg->name.c_str(), msg->age, msg->sex); } int main(int argc, char **argv) { // 初始化ROS节点 ros::init(argc, argv, "person_subscriber"); // 创建节点句柄 ros::NodeHandle n; // 创建一个Subscriber,订阅名为/person_info的topic,注册回调函数personInfoCallback ros::Subscriber person_info_sub = n.subscribe("/person_info", 10, personInfoCallback); // 循环等待回调函数 ros::spin(); return 0; } 配置CMakeLists.txt nano ~/catkin_workspace/src/learning_topic/CMakeLists.txt 添加 add_executable(person_publisher src/person_publisher.cpp) target_link_libraries(person_publisher ${catkin_LIBRARIES}) add_dependencies(person_publisher ${PROJECT_NAME}_generate_messages_cpp) add_executable(person_subscriber src/person_subscriber.cpp) target_link_libraries(person_subscriber ${catkin_LIBRARIES}) add_dependencies(person_subscriber ${PROJECT_NAME}_generate_messages_cpp) 设置需要编译的代码和生成的可执行文件; 设置链接库; 添加依赖项。 编译并运行 cd ~/catkin_workspace catkin_make # 新建一个终端 source ~/catkin_workspace/devel/setup.bash roscore # 新建一个终端 source ~/catkin_workspace/devel/setup.bash rosrun learning_topic person_publisher # 新建一个终端 source ~/catkin_workspace/devel/setup.bash rosrun learning_topic person_subscriber # 新建一个终端 source ~/catkin_workspace/devel/setup.bash rqt_graph python 编写程序 person_publisher.py nano ~/catkin_workspace/src/learning_topic/scripts/person_publisher.py #!/usr/bin/env python # -*- coding: utf-8 -*- ######################################################################## #### Copyright 2020 GuYueHome (www.guyuehome.com). ### ######################################################################## # 该例程将发布/person_info话题,自定义消息类型learning_topic::Person import rospy from learning_topic.msg import Person def velocity_publisher(): # ROS节点初始化 rospy.init_node('person_publisher', anonymous=True) # 创建一个Publisher,发布名为/person_info的topic,消息类型为learning_topic::Person,队列长度10 person_info_pub = rospy.Publisher('/person_info', Person, queue_size=10) #设置循环的频率 rate = rospy.Rate(10) while not rospy.is_shutdown(): # 初始化learning_topic::Person类型的消息 person_msg = Person() person_msg.name = "Tom"; person_msg.age = 18; person_msg.sex = Person.male; # 发布消息 person_info_pub.publish(person_msg) rospy.loginfo("Publsh person message[%s, %d, %d]", person_msg.name, person_msg.age, person_msg.sex) # 按照循环频率延时 rate.sleep() if __name__ == '__main__': try: velocity_publisher() except rospy.ROSInterruptException: pass nano ~/catkin_workspace/src/learning_topic/scripts/person_subscriber.py #!/usr/bin/env python # -*- coding: utf-8 -*- ######################################################################## #### Copyright 2020 GuYueHome (www.guyuehome.com). ### ######################################################################## # 该例程将订阅/person_info话题,自定义消息类型learning_topic::Person import rospy from learning_topic.msg import Person def personInfoCallback(msg): rospy.loginfo("Subcribe Person Info: name:%s age:%d sex:%d", msg.name, msg.age, msg.sex) def person_subscriber(): # ROS节点初始化 rospy.init_node('person_subscriber', anonymous=True) # 创建一个Subscriber,订阅名为/person_info的topic,注册回调函数personInfoCallback rospy.Subscriber("/person_info", Person, personInfoCallback) # 循环等待回调函数 rospy.spin() if __name__ == '__main__': person_subscriber() 运行 sudo chmod +x ~/catkin_workspace/src/learning_topic/scripts/person_publisher.py sudo chmod +x ~/catkin_workspace/src/learning_topic/scripts/person_subscriber.py # 新建一个终端 source ~/catkin_workspace/devel/setup.bash roscore # 新建一个终端 source ~/catkin_workspace/devel/setup.bash rosrun learning_topic person_publisher.py # 新建一个终端 source ~/catkin_workspace/devel/setup.bash rosrun learning_topic person_subscriber.py # 新建一个终端 source ~/catkin_workspace/devel/setup.bash rqt_graph |
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
2018 浏览 1 评论
synopsys 的design ware:DW_fpv_div,浮点数除法器,默认32位下,想提升覆盖率(TMAX),如果用功能case去提升覆盖率呢?
2536 浏览 1 评论
RK3588 GStreamer调试四路鱼眼摄像头四宫格显示报错
5313 浏览 1 评论
【飞凌嵌入式OK3576-C开发板体验】RKNN神经网络-YOLO图像识别
254 浏览 0 评论
【飞凌嵌入式OK3576-C开发板体验】SSH远程登录网络配置及CAN通讯
1336 浏览 0 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-26 04:02 , Processed in 0.719214 second(s), Total 72, Slave 56 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号