Introduction to ROS (3): ROS communication programming (topic programming)

Reference: "ROS Robot Development Practice"
Note: Continue to implement communication programming under the function package learning_communication under the catkin_ws workspace created in the previous section

1. Topic Communication Model

Insert picture description here

Second, the realization of basic topic programming

1. Topic programming process:

1. Create a publisher (talker);
2. Create a subscriber (listener);
3. Add compilation options;
4. Run an executable file;

2. Implement the publisher (Talker)

a. Create the talker.cpp file in the following directory:
Insert picture description here
b. The talker.cpp file code is as follows:

/*
该例程将发布chatter话题,消息类型String
*/

#include <sstream>
#include "ros/ros.h"
#include "std_msgs/String.h"

int main(int argc,char **argv)
{
  //ROS节点初始化
  ros::init(argc,argv,"talker");
  
  //创建节点句柄
  ros::NodeHandle n;
  //创建一个Publisher,发布名为chatter的topic,消息类型为std::msgs::String
  ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000);

  //设置循环的频率
  ros::Rate loop_rate(10); 

  int count = 0;
  while (ros::ok())
  {
    //初始化std::msgs::String类型的消息
    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;
}

c. Implementation logic
(1) Initialize the ROS node;
(2) Register the node information with the ROS Master, including the published topic name and the type of message in the topic;
(3) Circulate the message according to a certain frequency;

3. Implement the subscriber (Listener)

a. Create the listener.cpp file in the following directory:
Insert picture description here
b. The code of the listener.cpp file is as follows:

/*
该例程将订阅chatter话题,消息类型String
*/
#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());
}

int main(int argc, char **argv)
{
  //初始化ROS节点
  ros::init(argc, argv, "listener");

  //创建节点句柄
  ros::NodeHandle n;

  //创建一个Subscriber,订阅名为chatter的topic,注册回调函数chatterCallback
  ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);

  //循环等待回调函数
  ros::spin(); 

  return 0;
}

c. Implementation logic
(1) Initialize the ROS node;
(2) Subscribe to the required topic;
(3) Wait for the topic message in a loop and enter the callback function after receiving the message;
(4) Complete the message processing in the callback function;

4. Add compilation options

Compile method

Compile the code in the CMakeLists.txt file:

(1) Set the code to be compiled and the executable file generated;
(2) Set the link library;
(3) Set the dependency;

Specific operation

Open the CMakeLists.txt file in the learning_communication function package and add the following code:

add_executable(talker src/talker.cpp)
target_link_libraries(talker ${catkin_LIBRARIES})

add_executable(listener src/listener.cpp)
target_link_libraries(listener ${catkin_LIBRARIES})
Generate

Generate an executable file under the catkin_ws workspace:

catkin_make

5. Run the executable file

Note that every time you open a new terminal to add environment variables: (if you need to manually add)

source ~/test/catkin_ws/devel/setup.bash

a. Please
open the terminal of ROS Master :

roscore

b. Start the talker and
open a new terminal:

rosrun learning_communication talker

Insert picture description here
c. Start the listener and
open the terminal:

rosrun learning_communication listener

Insert picture description here
Both sides transmit and receive data in real time

Three, custom topic programming

We can use the message structure defined by ROS, or we can define the topic message ourselves, that is, custom topic programming

1. Define the msg file

Create the msg directory under learning_communication and the Person.msg file under the msg directory.
Insert picture description here
Write:

string name
uint8 sex
uint8 age

uint8 unknown = 0
uint8 male    = 1
uint8 female  = 2

2. Add function package dependency in package.xml

  <build_depend>message_generation</build_depend>
  <exec_depend>message_runtime</exec_depend>

3. Add compilation options in CMakeLists.txt

find_package(catkin REQUIRED COMPONENTS
  roscpp
  rospy
  std_msgs
  message_generation
  )
catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES learning_communication
  CATKIN_DEPENDS roscpp rospy std_msgs message_runtime
#  DEPENDS system_lib
)
add_message_files(FILES Person.msg)
generate_messages(DEPENDENCIES std_msgs)

4. Compile

Generate an executable file under the catkin_ws workspace:

catkin_make

5. View custom messages

rosmsg show Person

Insert picture description here
If the customization is successful, you can use it by including the header file!

Published 26 original articles · praised 0 · visits 1206

Guess you like

Origin blog.csdn.net/weixin_44264994/article/details/105269783