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.
- 3 (or more) ESP32
- 2 (or more) robot structures
- 2 (or more) 9V-battery (or another supply)
- 4 (or more) DC motors
- 2 (or more) L298N H-Bridge modules
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: Vision System
The vision system is made of a camera and an ESP connected to a Linux computer. The camera must be aiming to the field (which might be white to make it easier to detect other colors).
Step 2: Detecting Colors
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:
Step 3: How to Find the Range of Each Color
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".
Step 4: Position and Angle Detection
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.
Step 5: Vision Code
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:
Step 6: Robot Structure
Step 7: How the Robot Works
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.
Step 8: Robot's Code
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
Step 9: Results
The robots are finally ready to play and you can check the results watching the video.