The last program of the subject!. This time we have to deal with a flight problem, its a simple program in which we play the game cat-maouse with a copuple of drones. This exercise is similar to the first one we did in this subject, we have to follow the red drone mouse that can move on it's surrond instead of an static red line. Let's do it!
Filtering the image
So first thing is to be able to find the mouse on the cat camera. As the last time im using the same process to filter the image, but this time to find the range of high and lows of the mask i made a simple program to help me to find the correct values, as you can see in the next image:
In the tracking window are the best values that i find to filter the red ball.
So moving the bars i get the High and Low HSV values more eficiently.
Note: the drone actually is blue!, so have this in mind to make the mask. You can see the real colour of the drone if you execute in my_solution.py the command cv2.imwrite(), which makes a picture. With that picture you can make the perfect filter. The drone in reality looks something like this:
With this i was able to filter perfctly the drone, and also can get the middle point of it so later i can calculate the cat movements with that point.
Drone MovementNow is time for the cat to start moving. This time we have to control 3 dimensions: x (right-left), y (near-far) and z (up-down).
Before anything, to make the drone start moving to any side we have to first make it takeoff. I made this with a global variable which if it is true the drone has to make a
drone.takeoff() and inside the if i change the value to false so it only makes one takeoff. Also i have to add that i made a takeoff of 0.1, why so low? well instead of making it fly really high and start looking for the mouse i decide that it will be more efficient if the cat start whith a low altitud so from the beginning it can have the mouse controlled.
Drone recovery
In case the cat is looking to other side in which there is no mouse it will start turning around until it see the mouse.
It will start looking for the mouse after some iterations in which it didn't see it, this is to avoid the case in which in one instant it doen't found the cat and starts turning (enters in the recovery mode) when it's by it's side and there was no need to enter in the recovery mode.
X Edge
Now that is resolved the takeoff and the recovery part let's start with x edge. So to control this i use basically the PD control i made to the
follow line problem, i adjust the Kp and Kd to have a more efficient move but the idea is the same: get the position of the mouse in the image and calculate the difference between the center in x of the mouse and the center of the cat image, knowing this error we can apply the PD controller to move.
This move i apply only in the fourth argument of drone.set_cdm_mix(vx, vy, z, yaw_rate) instead of the first one. I made this to avoid the cat start moving like a crab and be able to turn "it's neck" towards the mouse.
Y Edge
The next thing i did was to controll how near or far was the cat from the mouse, this is controlled by the y edge, where a positive number makes the drone go backward and a negative number forward. I decide that i was going to controll this with a P controller but the problem was that the drone shakes it head strongly when it starts the move (when we set a bigger velocity from the one we had) so for some instant it loses the drone and also destabilizes, so to get a correct movement i have to move in that direction slowly so it doesn't make sharp movements. But i wanted to mkae it go a more faster so it could follow the mouse at a good distance.
(This is what i mean when i said that the drone shackes its head)
What i made? I save the actual velocity of the drone, if the P controller wants to set a velocity which has a big difference from the actual velocity i dont set that velocity, i increase more slowly the actual velocity to avoid abrupt changes. With this i get a better behaviour and allows the cat go faster. The same thing is apply when the drone wants to stop (becouse the distance to the mouse is the correct) the velocity decreases slowly so the cat wont past from 1 to 0 in one instant (if the actual velocity is low i set directly to 0).
And how i know if the mouse is far away or really near? with the area that the camera filters, if the area is really big it means that the mouse is too close, instead if it is a small dot on th horizont it means it is really far away.
Here i let you a couple of examples of the two movements (x and y) explained before:
The final x movement using "the neck" of the drone:
Z Edge
The last thing we have to control is the altitude of the dron in reference to the mouse. For this i don't use any velocity, i use the position (that's why i use the drone.set_cdm_mix(vx, vy, z, yaw_rate) function).
What i make is to get the error between our cat and the altitude in the image of the mouse. Once i get this i multiplied it by a constant, the result is what i have to sum to the actual position in z to get the same height as the mouse. If the mouse is below the cat we would sum a negative number and if it is higher we will sum a positive number, (if the position we want to command is a negative position i set it to 0, we can't go under the ground).
Here are the cat's reation to the differents types of mouses we can try our code:
The last video i had to filmed with my movile phone becouse i have some troubles with mi laptop
Hope this has help you, see you in the next projects!
Comments
Post a Comment