DRONE CAT-MOUSE
In this exercise we have to use a drone to follow another. Our drone will be the "cat", which is the black drone, and the drone to follow the "mouse", represented by the red drone.
We will use colour filter and controllers P and PD to move the cat through all the axis.

We are going to follow it with a color filter, as we did in the first exercise follow line. Following the same steps, we only have to change the colour to filter. We have to know that the image of the mouse drone isn't red as it seems. We can check it with a function on opencv, that will let us to take a picture and see the real colour.
Once we have the ranges low and high of our colour, we have to obtain the mask, and get the contours with me mask. This will let us choose the biggest contour, which corresponds to the center part of the mouse.
The contours will give us the center of the drone, so we will be able to know where the drone is in reference to the center of our screen. After apply the same methods as we did here, we obtain this result:


Where the black point is the middle point obtained with thew contours
Since the drone is able to fly, we have three axis to adjust.
The first axis I'm going to adjust will be the x. Since the robot is holonomic, we don't have to adjust the speed to go there, we can turn to that point. So, using a PD controller, we will be able to adjust it to do a quick and precise turn.
The PD controller is:
u= -Kp * error + Kd * de
​​
Where Kp is the constant we have to adjust for the proportional controller, the error is the difference between where the center of the drone is (obtained previously filtering the image) and the middle of the image (where we want tit to be), Kd is the derivative constant, de, since we can't apply derivatives, is the difference between the last error and the actual one. The u value obtained is the yaw rate to apply.
​
The next part is adjust the y axis, which correspond to the distance between the mouse and the cat.
With the colour filter we are able to know the are of the mask, which corresponds to the area on the mouse. Depending of the size of this area, we will have to decide if we will have to move towards the mouse, get away from it or if we stay in a right distance.
To do this, we can apply a P controller as we have done in the z axis.
​
One problem that I have found, is that the change of velocity is very rough most of the time, because it can be very close in one iteration, so it have to move proportionally, and in the next iteration be in a good range so it has to stop.
To control this, I have done a case-based control, that increases the speed little by little until obtain a value close to 0, that will let the drone be have 0 speed with no sudden movements.
​
You can see the result of both controllers in this video:
Finally, I'm going to adjust the z axis, which corresponds to the altitude of the drone. As we have done with x and the width of the image, I'm going to do the same with the height, because now the target is go up or down depending where is the mouse.
So, following the same steps, we can do a P controller, which will be added to the current position, so will let us go up or down depending where the drone is. Thus, the P controller will be:
u= altitude + (-Kp * error)
Where the altitude is the current position in z axis and the second part the P controller.
​
You can see how the cat follows the mouse with the three parts working:
IMPROVEMENTS
As we did with F1, we can add a recovery mode in the case that the drone doesn't recognize any contours in the image. In this case, will turn until find the mouse, as you can see in this video:
There are 4 levels to test our cat, so here are the four videos: