Football Table Robot: Camera-Controlled AI Opponent

This project aims to develop a robot that can play table football against a human. It uses a camera as a sensor to detect the ball's and players' positions on the table. Based on the detected positions, the robot sends signals to motors, which control the movements of the robot players. You can find the full code here

.

1. Setup and Libraries

captura = cv2.VideoCapture(2) # Camera setup
data = np.zeros((muestras, 2)) # Data collection for ball/player positions
jugadores = np.zeros([20,4]) # Matrix for player positions

2. Player and Ball Detection

The camera captures the playing field, converting images to the HSV color space to track the positions of the ball and players.

Technologies: Kalman Filter predicts the ball's trajectory.

kfObj = KalmanFilter() # Initialize Kalman filter
hsv = cv2.cvtColor(imagen, cv2.COLOR_BGR2HSV) # Convert image to HSV
verde_bajos = np.array([74,55,67], dtype=np.uint8) # Ball color range

3. Control System: Motor Commands

Based on the detected positions of the ball and players, the system sends signals to control the robot’s motors through ROS publishers.

PubArquero = rospy.Publisher("/Arquero", Quaternion, queue_size=20)
PubDefensas = rospy.Publisher("/Defensas", Twist, queue_size=20)
PubMedios = rospy.Publisher("/Medios", Quaternion, queue_size=20)
PubDelanteros = rospy.Publisher("/Delanteros", Quaternion, queue_size=20)

ROS Messaging: Quaternions and Twist messages are used to represent positions and movements.

4. Prediction and Player Movement

The Kalman filter predicts the future position of the ball, allowing the robot to react faster and more accurately. The detected positions of the players are used to move them to block or hit the ball.

predictedCoords = kfObj.Estimate(x1, y1)
cv2.circle(imagen, (predictedCoords[0], predictedCoords[1]), 10, [0,255,255], 2)

5. Visualization

The system visualizes the game by drawing rectangles around the detected players and circles around the ball, displaying both real-time and predicted positions.

cv2.putText(imagen, "x=" + str(x1), (x1, y1 + 20), font, sletter, (255, 255, 255), 2, cv2.LINE_AA)
cv2.imshow("original", imagen)

Technologies Summary