Closed Amene-Gafsi closed 5 months ago
Once the position is predicted, we can transmit this data to the Arduino to accordingly control the motor. This can be achieved with a simple piece of code in Python, placed within the tolerance condition of the main function:
import serial
import time
# Initialize the serial connection
ser = serial.Serial('COM3', 9600) # Adjust 'COM3' to your Arduino's COM port
time.sleep(2)
ser.write(f"{last_predicted_position},{total_time}\n".encode())
ser.close() # Close the serial connection when done
For the Arduino side, to receive and process the variables, the following simple code can be used:
void setup() {
Serial.begin(9600); // Start serial communication at 9600 bps
}
void loop() {
if (Serial.available()) {
String data = Serial.readStringUntil('\n'); // Read the data until newline
int separator = data.indexOf(',');
long last_predicted_position = data.substring(0, separator).toInt();
long total_time = data.substring(separator + 1).toInt();
Serial.print("Received last position: ");
Serial.print(last_predicted_position);
Serial.print(", Total time: ");
Serial.println(total_time);
}
}
The objective of this algorithm is to track and predict the trajectory of a ball on a playing field, along with detecting paddle positions in real-time. The main method orchestrates this by initially calibrating the camera to accurately detect markers within the environment, enhancing the precision of object tracking.
Here's a breakdown of how the algorithm functions:
1. Camera Calibration: The camera is calibrated using ArUco markers. This step helps to establish a frame of reference and is essential for accurately positioning the ball and paddles within the captured video frames. 2. Frame Processing: Each video frame is processed to detect and track the ball and paddles. This involves applying a Gaussian blur for noise reduction, converting the frame to the HSV color space, and then using color thresholding to create masks that isolate the ball and paddles based on their colors. Contours are extracted from these masks to determine the positions and sizes of the ball and paddles. 3. Position and Movement Analysis: The positions of the ball are continuously recorded with timestamps. Using this data, the algorithm calculates the velocity of the ball in both horizontal and vertical directions. 4. Prediction of Ball Trajectory: With the calculated velocities, the algorithm predicts where the ball will ultimately end up on the y-axis of the playing field. This prediction is based on the physical properties of the playing field and the dynamics of motion influenced by gravity. 5. Real-Time Updates and Predictions: As the game progresses, the algorithm constantly updates its predictions about the ball's final position on the y-axis, helping in strategizing the paddle positions to effectively interact with the ball.
The algorithm operates in a loop within the main method, capturing frames in real-time, processing these frames to track and analyze movements, and predicting future positions.