The objective of this project is to create robots that are able to play soccer. To make that work, we made a system that detects the robots and the ball by checking theirs colors and finds theirs respective positions and angles. These informations are sent to the robots, which decide what to do, according to theirs codes. For this project we made 2 robots, one for each team, but it can be made with more than that, if you want to.
Using Python and the OpenCV library, we can detect each robot position by checking theirs colors. To install OpenCV, you can check out this link.
After getting OpenCV, we can start thinking of a logic to detect the robots. We are using an yellow ball, a blue and green circle for the first robot and a blue and pink circle for the other one. To detect each color, we have to mask the image captured by the camera, by defining a range of colors in HSV. For that, we use two dictionaries, one for the lower and one for upper colors codes. When you mask the image, everything will turn into black, except the pixels that are between the range you selected. Then you can look for countours in the masked image.
Here's an example of how to detect the color yellow:
This was by far the hardest part. Finding the best colors for each robot and for the ball took a long time, since we had to adapt the range a lot, mainly because of the back light. The ideal situation is to make this project on a room that doesn't have much light changing, so you don't have to change the colors range.
To make it easier to find the range for each color, we used this code: https://github.com/alkasm/colorfilters. With this, we could easily change the hue, saturation and value (HSV) to find the range that masks everything except the color we want, all we had to do was to save the image captured by the camera and select it. To save the image, we made a short code, called "photo.py".
Now that we know how to mask an image to find a color, we need to find its position. After finding the contour of your color, the position can be easily found with some OpenCV commands, which are shown on "detect_yellow_position.py".
Well, that's all for the ball... But we also need to know where the robots are facing at, so the logic for the robots is a little harder than that.
To check the robots angles, we use 2 colors for each, so we can make a line connecting the middle of the first color to the middle of the second one. With this line and some trigonometry, we can get the angle.
To get all information about the robot, first we need to mask the image captured with the camera, using the range of the two colors of the robot. Then, we can get the position of the robot. After that, we need to make two different masks, one for each color, and find theirs centers. Next step is to calculate the angle, and then you will get all information you need.
To send the information to the robots, we use ESPNow protocol. Each robot has an ESP that receives the DATA and controls it. There's also an ESP connected to the computer, that is responsible to send the DATA to the others. Since it's connected to the computer via USB, we can send the DATA from the computer to the ESP master via serial communication.
Connecting all these steps, we get the code below as result for the vision code:
Each robot has an ESP, that is responsible for receiving the information from the ESP Master and for controlling the H Bridge module. This module controls the motors, and it's powered directly from the the battery. Since it has 3 power pins and one of them is regulated to 5V, we use it to power the ESP. We also use a switch to easily turn the robot on and off.
As mentioned before, each robot has an ESP to control its movements. The code for these ESPs are made using Arduino IDE. If you don't have it, download it here and install ESP32 boards in Arduino IDE following this link. The code is almost the same for every robot, except some defined variables. You might need to change the following lines:
#define team "Green"
#define pos "Blue"
#define adteam "Pink"
#define ball "Yellow"
#define quant_robots 2
#define quant_team 2
#define xgoal 10
#define ygoal 225
The robots are finally ready to play and you can check the results watching the video.