I have been working on the follow line exercise and am able to make a mask of the red line ahead using OpenCV, then using the bounding rectangle command I create a rectangle around the red strip and then find its mid point. or I pass a line through the centre of the rectangle and consider the slope of that line with respect to the forward vertical as an error and am trying to use PID to minimise that error to navigate the robot on the turns. Is my approach correct?
if not please guide me to the right approach. if it is, I am stuck at this part, I’m not able to figure out how to derive angular velocity output using the equations, and where to learn it from.
The focus looks good. First you have to segment the red line using (as you said) some OpenCV library. With this you get a binary image where the white pixels are the red line and the black pixels are the background.
An approximation can be to get the center of the rectangle containing the line. With the center, you calculate the distance (difference) between the central point of the image and the center of the rectangle. That difference is your error.
Another approximation can be, having the image segmented, to get 2 rows of that image, one at the top of the line and one at the bottom. With these two points you can tell if you are on a straight line (aligned points) or if you are on a curve (misaligned points). But even if you take this route, notice how the objective is the same, to know the difference between where is the point of the line with respect to the center of the image. A high difference means that you are very far from the center and therefore you are on a curve so you have to correct (turn left or right).
I hope these steps will help you to keep solving the exercise. Tell us the results.
Once you have calculated the error, if you have a control P, the correction is:
correction = kp * error
If you only use a P control, the car may oscillate on the line. For this reason, it is better to use a PD control. In a PD control, the correction is:
Correction = kp * error + kd * (error - previous_error)
kp, kd are calculated experimentally.
I hope it helped you.