(5) Topic and message data type

------------------------------------

Topic communication

1. Topic communication is an asynchronous communication mechanism that supports one-to-many.
2. Generally, a topic has a publisher and a receiver.
3. The publisher publishes messages to the topic.
4. Subscribers subscribe to messages from the topic.
5. One A topic can have multiple topic subscribers.
6. The asynchronous communication mechanism is:
① The publisher only publishes the topic, and doesn’t care how many subscribers there are, or whether the subscribers have successfully subscribed to the message.
② Subscribers also Just subscribe to the topic, it will not care who publishes the topic

Operation instructions for the ros topic

  • rostopic echo /(topic): is to print topic information to the current terminal
  • rostopic hz /(topic): is the publication frequency of the viewing topic (s / Hz)
  • rostopic info /(topic): Print the subscribers and publishers of this topic on the terminal, and the message type of the topic
  • rostopic list: List all current topics
  • rostopic pub /(topic) (message structure):
  • ①After entering the topic, press the Tab key to automatically complete the message structure.
    ② Enter information into the topic on the command line.
    ③ Add -r after pub: cyclic transmission mode
    ④ The number after -r : cyclic transmission rate (rostopic hz can be viewed)
  • rostopic bw /(topic): View the bandwidth of the topic
  • rostopic find /(topic): Find topics according to the message type
  • rostopic type /(topic): View the message data type of the topic

Note: When the topic has information, the topic will be executed; when the topic has no information, the topic will not be executed and will be stuck

How to publish topics in the terminal

1. Take C++ programming as an example.
2. There are 4 steps:
① Use functions to create a topic publisher

  • ros::Publisher pub: Initialize the publisher (may be initialized in the .h file)
  • nh: node handle (you need to create it yourself)
  • .advertise: fixed collocation
  • std_msgs::String: the message data type of the topic to be published
  • topic_name: topic name
  • queue size: message queue length

② Create message data (define a structure)

  • std_msgs::Float32 voltage_msgs;

③ Message assignment (assign value to the message data)
④ Publish the message (publish the message data)
Insert picture description here
Insert picture description here

How to subscribe to topics

1. Create a subscriber (check whether there is a new topic published)

  • Cmd_vel_Sub: Initialize the subscriber (may be initialized in the .h file)
  • n: node handle
  • .subscribe: fixed collocation
  • ackermann_cmd: the topic name of the subscribed topic
  • 100: message queue length
  • &turn_on_robot::Cmd_Vel_Callback: custom callback function name (consistent with the one created) (pointer is used here)
    Insert picture description here

2. Create a callback function (process the message data of the new topic)

  • turn_on_robot::Cmd_Vel_Callback: the name of the callback function
  • ackermann_msgs::AckermannDriveStamped: message data type (it must be consistent with the message data type of the topic published by the publisher)
  • &akm_ctl: A variable (pointer) used to receive the data type of the message published by the publisher
    Insert picture description here

------------------------------------

ros message data type

1. The message data type is like a structure, which contains many variables of the same type, such as the example of the callback function just now: (It can be seen that the variables in the program correspond to the members of the message data type when we view the terminal)
Insert picture description here

2. Regardless of whether it is subscribing to or publishing a topic, there are clear requirements for a message data type of the topic.
3. Some commonly used message data types have been defined in ros (such as radar and speed topics, they are all well defined Some of the message data types)

ros message command

  • rosmsg list: List all message data types in the current system
  • rosmsg show (ackermann_megs/AckermannDriveStamped): View the specific content of a certain message data type
  • rostopic type /topic_name (topic name) | rosmsg show: You can directly view the specific structure of the message data type based on this topic

Custom message data type

Has a fixed operation

------------------------------------

Guess you like

Origin blog.csdn.net/m0_46278925/article/details/114841242