ROS实现消息发布器和订阅器——Hello Word

维基百科ROS官方教程cn/ROS/Tutorials - ROS Wiki

1.建立工作区间

catkin/Tutorials/create_a_workspace - ROS Wiki

source /opt/ros/melodic/setup.bash
mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/
catkin_make
source devel/setup.bash

ROS实现消息发布器和订阅器——Hello Word_第1张图片

2.创建功能包catkin_create_pkg

catkin/Tutorials/CreatingPackage - ROS Wiki

cd ~/catkin_ws/src
catkin_create_pkg beginner_tutorials std_msgs rospy roscpp
cd ~/catkin_ws
catkin_make
. ~/catkin_ws/devel/setup.bash

功能包架构

ROS实现消息发布器和订阅器——Hello Word_第2张图片

   3.创建ROS消息和ROS服务

cn/ROS/Tutorials/CreatingMsgAndSrv - ROS Wiki

#创建一个msg
cd ~/catkin_ws/src/beginner_tutorials
mkdir msg
echo "int64 num" > msg/Num.msg
#创建一个srv
roscd beginner_tutorials
mkdir srv
roscp rospy_tutorials AddTwoInts.srv srv/AddTwoInts.srv

4.编写源文件

cn/ROS/Tutorials/WritingPublisherSubscriber(c++) - ROS Wiki

将talker.cpp和listener.cpp放在beginner_tutorials的src里面。

①发布者talker.cpp

#include "ros/ros.h"
#include "std_msgs/String.h"
#include 

int main(int argc, char **argv)
{
  ros::init(argc, argv, "talker");
  ros::NodeHandle n;
 
  ros::Publisher chatter_pub = n.advertise("chatter", 1000    );
  ros::Rate loop_rate(10);

  int count = 0;
  while (ros::ok())

  std_msgs::String msg;

  std::stringstream ss;
  ss << "hello world " << count;
  msg.data = ss.str();

 ROS_INFO("%s", msg.data.c_str());
 chatter_pub.publish(msg);
 ros::spinOnce();

 loop_rate.sleep();
    ++count;
  }


  return 0;
}


编写简单的消息发布器节点和订阅器节点
“节点(Node)” 是ROS中指代连接到ROS网络的可执行文件的术语

发布器节点(“talker”) : 它将不断的在ROS网络中广播消息
cd ~/ada_ws/src/beginner_tutorials
创建src文件夹,存储源代码

②编写listener.cpp

#include "ros/ros.h"
#include "std_msgs/String.h"

void chatterCallback(const std_msgs::String::ConstPtr& msg)

{
  
  ROS_INFO("I heard: [%s]", msg->data.c_str());

}
// %EndTag(CALLBACK)%

int main(int argc, char **argv)
{
 ros::init(argc, argv, "listener");
 ros::NodeHandle n;

 ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);
 ros::spin();

 return 0;
}

    

5.修改CMakeLists.txt为如下:

cmake_minimum_required(VERSION 2.8.3)
project(beginner_tutorials)
 
## Find catkin and any catkin packages
find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs genmsg)
 
## Declare ROS messages and services
add_message_files(FILES Num.msg)
add_service_files(FILES AddTwoInts.srv)
 
## Generate added messages and services
generate_messages(DEPENDENCIES std_msgs)
 
## Declare a catkin package
catkin_package()
 
## Build talker and listener
include_directories(include ${catkin_INCLUDE_DIRS})
 
add_executable(talker src/talker.cpp)
target_link_libraries(talker ${catkin_LIBRARIES})
add_dependencies(talker beginner_tutorials_generate_messages_cpp)
 
add_executable(listener src/listener.cpp)
target_link_libraries(listener ${catkin_LIBRARIES})
add_dependencies(listener beginner_tutorials_generate_messages_cpp)
 

package.xml内容



  beginner_tutorials
  0.0.0
  The beginner_tutorials package

  
  
  
  zxy


  
  
  
  TODO


  
  
  
  


  
  
  
  


  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  catkin
  roscpp
  rospy
  std_msgs
  roscpp
  rospy
  std_msgs
  roscpp
  rospy
  std_msgs


  
  
    

  

 5.编译及运行

cd ~/catkin_ws
catkin_make

6.测试消息发布器和订阅器

cn/ROS/Tutorials/ExaminingPublisherSubscriber - ROS Wiki

打开一个新的终端 输入roscore
新终端2:
cd ~/catkin_ws
source ./devel/setup.bash
rosrun beginner_tutorials talker 
新终端3:
cd ~/catkin_ws 
source ./devel/setup.bash
rosrun beginner_tutorials listener

参考文献

​​​​​​ROS实现消息发布器和订阅器_赤豆几维的博客-CSDN博客

ROS学习记录(二)--消息和服务_Siyuada的博客-CSDN博客

ROS总结——ROS消息和ROS服务_YongqiangGao的博客-CSDN博客_ros 消息

你可能感兴趣的:(ubuntu)