Introductory knowledge of ros

Ros's introductory tutorial link:

  1. linux ros installation
  2. Preschool small case - the keyboard controls the movement of the little turtle
  3. Create workspaces and feature packages
  4. publisher publisher
  5. subscriber subscriber

Basic knowledge of ros

Different linux systems install different ros versions, linux18.04 installs melodic, linux20.04 installs noetic.

roscore: the manager that all nodes run

rosrun runs the instructions of a certain function package and a certain node, and adds the name of a certain function package and the name of the corresponding compiled executable file after rosrun. The executable file and the node name should be the same

rqt_graph: A dynamic graph showing the current system operation, including the nodes currently running and how the messages between nodes are transmitted

rostopic echo [topic_name]: Command to view messages published on a topic. Among them, topic_name represents the name of the topic to be viewed, for example: /lane_data, /object_data

rospack list: Query all the package files currently installed. When the newly compiled function package cannot be automatically completed by rosrun, you can use rospack list to update it

In ros, packages, nodes and topics are three very important concepts. The relationship between them is as follows:

  • Function package (packages)
    function package is the basic unit of ros organization code, which puts together a set of related files (such as nodes, configuration files, compilation scripts, etc.). The advantage of using a function package is that different ros nodes and other related files can be combined for easier management and use.
  • Node (nodes)
    The node is the basic unit of the ros runtime, and each node is an executable file used to perform specific tasks. For example, nodes can read sensor data, control robot movement, or provide certain services.
  • Topics Topics
    are used to implement communication between nodes . Nodes can publish (publish) messages to topics, and can also subscribe (subscribe) to messages from topics. Through topics, nodes can communicate and coordinate with each other to implement complex applications.

Definition and usage of topic message:

  • The role of the topic message definition: to determine the type of message transfer between nodes, such as the message of yolo, including the type of target, center point, height and width, confidence, etc.
  • In a function package, create a msg folder. In this folder, create a msg file that saves the custom data type

In a non-ros cmakelist.txt, in addition to adding some necessary header files and libraries in ros, add ${catkin_LIBRARIES} to the TARGET_LINK_LIBRARIES of the generated executable file

Publisher's Implementation Steps

  1. Initialize the ROS node, ros::init(argc, argv, "node name"); ros::NodeHandle node;
    Note: node is the handle used to access the ROS system
  2. Create a ROS publisher object: ros::Publisher chatter_pub = node.advertise<std_msgs::String>("chatter", 1000);
    Note: "chatter" is the topic name, 1000 is the number of messages used to cache unsent messages upper limit.
  3. Set the publishing frequency: ros::Rate loop_rate(10);
    Note: 10 means the number of messages published by the publisher per second.
  4. Publish message: chatter_pub.publish(msg); loop_rate.sleep();
    Note: The loop_rate.sleep() method is used to make the program pause for the corresponding time according to the publishing frequency we set.

Subscriber Implementation Steps

  1. Initialize the ROS node, ros::init(argc, argv, "node name"); ros::NodeHandle node;
    Note: node is the handle used to access the ROS system
  2. Define a subscriber object, specify the type of message to subscribe and its topic name: ros::Subscriber sub = n.subscribe<std_msgs::String>("topic_name", 1000, callback_function); Note: <std_msgs::String
    > Indicates the type of message to be subscribed; "topic_name" is the name of the subscription topic; 1000 is the length of the message queue; callback_function is a callback function, which will be automatically called to process the message whenever a new message is subscribed.
  3. Write a callback function callback_function to process the received message.
    void callback_function(const std_msgs::String::ConstPtr& msg)
    {
    ROS_INFO("I heard: [%s]", msg->data.c_str());
    }
注释:msg是收到的消息,可以使用msg->data来获取消息内容。
  1. Use the ros::spin() function to keep the ROS node running while processing incoming new messages: ros::spin();

ROS subscriber: When the ROS node starts, the subscriber will start to receive messages of type std_msgs::String from the topic topic_name, and process them through the callback function callback_function. Whenever a new message arrives, the callback function will be called automatically for processing, thus realizing the function of ROS subscriber.

catkin_init_workspace: Create a workspace
catkin_make: Compile the workspace
catkin_make clean: Clean up the workspace
catkin_create_pkg: Create a function package

When running the ros node, if it is found that the function package and node cannot be automatically completed:

  • Case 1: No environment variable is set. You need to enter source devel/setup.bash in the terminal (the modification of ~/.bashrc requires running source ~/.bashrc)
  • Situation 2: After setting, you may need to manually complete it once (maybe the computer is a little unresponsive, so you need to teach him manually); using the rospack list command seems to be able to update the function pack
  • Case 3: Check whether roscore is running: Use the roscore command to enable the ros core, and then open the terminal window to run the command. If roscore is not running, nothing will be displayed

Guess you like

Origin blog.csdn.net/qq_42178122/article/details/130772312