ROS subscription and publishing topics

Table of contents

1. Create a new ROS workspace and create a function package
2. Create a msg message
3. Publish a topic
4. Subscribe to a topic

Preface

Ubuntu18.04
ROS Melodic

1. Create a new ROS workspace and create a function package

mkdir -p catkin_ws/src
cd ~/catkin_ws/src/
catkin_init_workspace
cd ~/catkin_ws/
catkin_make
echo "source ~/catkin_ws/devel/setup.bash">>~/.bashrc
source ~/.bashrc 
cd  ~/catkin_ws/src/
catkin_create_pkg topic roscpp rospy std_msgs

After the execution is completed, the workspace catkin_ws is established and the function package topic is created.
Insert image description here

2. Create a msg message

Create the msg folder in the directory of the topic package and add message members to the msg file

cd ~/catkin_ws/src/topic
mkdir msg
cd msg
gedit example.msg

Insert image description hereFill in the following:

std_msgs/Header header
float32 test1
int32 test2

The header is the message header, including the sequence number, timestamp, and frame_id. Generally, each message has this member, and other members can be taken as needed.

Edit package.xml file

gedit ~/catkin_ws/src/topic/package.xml

Make sure it contains the following two lines and is not commented out. If not, add it:

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

Insert image description here
Modify the CMakeLists.txt file under the topic package:

gedit ~/catkin_ws/src/topic/CMakeLists.txt

Add message_generation to COMPONENTS list
Insert image description here
Add runtime dependency CATKIN_DEPENDS message_runtime
Insert image description here
Add msg file

add_message_files(
  FILES
  example.msg
 )

Insert image description here
Make sure the generate_messages() function is called

generate_messages(
DEPENDENCIES
 std_msgs
  )

Insert image description here
Contains message header file path

include_directories(
  include
  ${
    
    catkin_INCLUDE_DIRS}
)

After adding, there is no need to compile and use rosmsg show example to check whether the addition is successful.
Insert image description here
Use other msg types in msg
. If when defining msg, you want to use the msg message type defined by other packages.
For example, define the message in package1 as follows:

std_msgs/Header header
package2/msg2 test

The type of test is the msg2 message type in the package2 package. You need to ensure that the msg2 message is correctly defined and the header file is generated, and at the same time add package2 to generate_messages in CmakeLists.txt of package1 , for example:

 generate_messages(
   DEPENDENCIES
   std_msgs
   package2
 )

If the reference is a message defined in the current package package1, it can be defined directly without modifying generate_messages,
such as:

std_msgs/Header header
package1/msg1 test

If the msg file is modified and an error occurs during compilation:

The dependencies of the message/service ‘XXX’ have changed.
Please rerun cmake.

The reason is that the MSG file becomes a header file after compilation, but the previous header file still exists, causing the compilation to fail. Use

catkin_make_isolated --force-cmake

Just force overwrite compilation directly.
Use catkin_make_isolated --force-cmake to generate the following two folders after compilation.
Insert image description here
If you use catkin_make to compile and prompt that the message header file cannot be found, you can add several header files under devel_isolated/package1/include/package1. Copy and cover it to devel/package1/include/package1 and then use catkin_make to compile.
If you delete the devel and build folders and recompile, it will prompt that the defined msg header file cannot be found. This is because it has not had time to generate it during compilation. You can add more Just compile it a few times and you're done

3. Publish topics

Enter catkin_ws/src/topic/src to create a new node program

cd ~/catkin_ws/src/topic/src
gedit pub.cpp

A simple publishing node program is as follows, which can be copied directly to pub.cpp

#include <ros/ros.h>
#include <std_msgs/String.h>
#include <sstream>
#include"topic/example.h"
int main(int argc, char **argv)
{
    
    
        ros::init(argc, argv,"pub");
        ros::NodeHandle nd;
        ros::Publisher publish = nd.advertise<topic::example>("/topic/example", 10);
        ros::Rate loop_rate(10);
        while (ros::ok())
        {
    
    
                topic::example example;
                example.test1 = 1.5;
                example.test2 = 2;
                publish.publish(example);
                loop_rate.sleep();
        }

        return 0;
}

Modify the CMakeLists.txt file under the topic package:

gedit ~/catkin_ws/src/topic/CMakeLists.txt

At the end of the file add:

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

then compile

cd ~/catkin_ws
catkin_make
. ~/catkin_ws/devel/setup.bash

run:

roscore
rosrun topic pub

Then execute:

rostopic echo /topic/example

The following message appears indicating that the release was successful.
Insert image description here

4. Subscribe to topics

Enter catkin_ws/src/topic/src to create a new node program

cd ~/catkin_ws/src/topic/src
gedit sub.cpp

A simple publishing node program is as follows, which can be copied directly to sub.cpp

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

void subscriberCallback(const topic::example::ConstPtr& msg)
{
    
    
printf("test1=%f,test2=%d\n",msg->test1,msg->test2);
}
int main(int argc, char **argv)
{
    
    
        ros::Subscriber subscriber;
        ros::init(argc, argv,"sub");
        ros::NodeHandle nd;
        subscriber = nd.subscribe("topic/example", 5, &subscriberCallback);
        ros::Rate loop_rate(10);
        while (ros::ok())
        {
    
    
               loop_rate.sleep();
                ros::spinOnce();
        }

        return 0;
}

Modify the CMakeLists.txt file under the topic package:

gedit ~/catkin_ws/src/topic/CMakeLists.txt

At the end of the file add:

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

then compile

cd ~/catkin_ws
catkin_make
. ~/catkin_ws/devel/setup.bash

run:

rosrun sub sub

You can see that the subscribed topics are printed out.
Insert image description here
The results of running rqt_graph are as follows:
Insert image description here

Open a new terminal:
1. Enter: rostopic list to see how many topics there are at this time.
2. Enter: rostopic echo topic name to view the sent topic messages. (Note: If the message is a custom msg, before entering rostopic echo, execute source devel/setup.bash to add the workspace so that rosmaster can recognize it.)

Guess you like

Origin blog.csdn.net/qq_39523365/article/details/131062117