ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
The lines of code you refer to act something like a very simple proportional controller. Consider the first line that sets the angular velocity. The transform.getOrigin()
call represents a vector from turtle2
to turtle1
expressed in the turtle2
frame. By grabbing just the x and y components of this vector and feeding them into atan2
, we are computing a signed angle between the turtle2
x-axis and the vector pointing to turtle1
-- when this angle is zero, turtle2
is pointing straight towards turtle2
. We multiply this by a positive gain (randomly chosen to be 4.0) to turn this into a proportional controller for the orientation of turtle2
. If we only used this line, this controller would always work to rotate turtle2
to point towards turtle1
.
The second line is a proportional controller for the linear velocity of turtle1
. Here we compute the distance and then multiply that by an arbitrary gain to set the linear velocity. As turtle2
gets closer to turtle1
, the velocity tends to zero.
Note, this system is not really the type of system that you should expect your classic PID-style controller to work well. It's nonlinear, has multiple inputs and outputs, and is nonholonomic (not to mention the fact that the actual implementation of the turtlesim responds to a single command input for a finite amount of time and then automatically zeros the command). The system is often called a differential drive or a unicycle model and there is a ton of research on stable position and trajectory tracking controllers. This simple implementation is just a rough way of getting turtle 2 to follow turtle 1.