Hi @min.wang.1997.01.13!
Starting foremost with your inquiry regarding the idea of combining publishers and subcribers, our goal for this exercise is to conduct a control scheme for our robot to essentially be able to stop once it detects a wall. Now, for this, we will need the inclusion of a subscriber and a publisher.
Going foremost into the subscriber, its primary purpose if for us to let the robot acquire information about the distance that has been detected in its frontmost sensor. After this, the question then is what will we do with this information? Based on the premise of the problem, our goal is to have the robot traverse in a circular manner then later on shifting to linear and eventually stopping depending on the distance recorded by the sensor. To do this, we then implement a publisher which will then publish the velocities into the /cmd_vel topic which our robot essentially acquires its movement from.
So overall, our node essentially acquires information (subscriber) and processes that information in order to come up with a command or behavior relating to our desired motion for the robot (publisher).
Now, in terms of the Twist message, you may want to read on the idea of Degrees of Freedom (DOF). Based on my understanding of the linear x, y, and z components, these pertain to essentially the x, y, and z axes of a 3-D plane. So relative to the robot, which in this case would be a turtlebot3, its linear/forward motion may then be described as one that traverses along the x-axis (See image acquired from Making a Mobile Robot #2 - Concept Design URDF | Articulated Robotics)
For the angular components, these then describe the rotations of the robot with respect to that axis. Now, from this premise, the reason why we designated the angular velocity to be that of the cmd.angular.z component is because our robot may only rotate with respect to the z axis (See more about Pitch, Yaw, and Roll); and for the numbers, positive pertains to a counter-clockwise motion as this is how we measure positive angles and negative for clockwise motion.
Lastly, for the laser_callback function, you can see this information when you run an echo on the topic /scan. You will be able to see that it outputs a syntax that begins with “range:” which is then followed by all sensor readings. Now, if you wish to know more about the laser positions, I may also suggest that you refer to the documentation of the turtlebot3. But the idea is there essentially are 360 points scattered around the robot. So given our knowledge that 359 pertains to the frontmost laser, you can also do a little experimentation to determine how the remaining sensors are distributed throughout the turtlebot3!
Hope this helps.
Regards,
Christian