This system utilizes PID (Proportional-Integral-Derivative) control to balance an object on a dynamic platform. A smartphone camera tracks the object’s position in real time and sends data to a microcontroller via UART for servo motor adjustments.
📷 Camera → Image Processing → PID Error Calculation → Servo Control
✅ Real-time object tracking using OpenCV.
✅ Flexible PID control for quick system stabilization.
✅ UART connection with a microcontroller for instant response.
✅ Manual PID Tuning – fine-tune each parameter for optimal performance.
🔹 Note: The machine learning model is only used to verify whether the object is balanced. The system's control is entirely handled by the PID algorithm.
- Windows 10 PC (as the software is primarily developed for Windows 10).
- Smartphone camera for object tracking (wired connection preferred to reduce latency).
- Microcontroller (ESP32, Arduino, STM32, etc.) connected via UART.
- Servo motors for platform adjustment.
Install the required Python libraries:
pip install opencv-python numpy matplotlib joblib pyserialUse IVCam (Android) or DroidCam for video streaming.
from joblib import load
model = load("random_forest_model.joblib")import serial
ser = serial.Serial('COM7', baudrate=115200, timeout=0)python PID.py| Key | Function |
|---|---|
| W/A/S/D | Move tracking window |
| 1-4 | Change setpoint |
| F | Toggle tracking area |
| Q | Exit program |
Fine-tune PID parameters step by step:
🔹 Step 1: Adjust Kp – increase until the system responds quickly without excessive oscillations.
🔹 Step 2: Adjust Kd – add to reduce oscillations and stabilize movements.
🔹 Step 3: Adjust Ki – add only if there is a steady-state error but avoid high values to prevent lag.
🔥 Tuning Tip: If excessive oscillations occur, disable one term (Kd or Ki) and retune from scratch!
Example PID parameters:
PID_params = {
'x': {'kp': 2.5, 'ki': 0.2, 'kd': 45},
'y': {'kp': 2.5, 'ki': 0.2, 'kd': 45}
}The ESP32 firmware performs the following tasks:
- Receives object position data from the PC via UART.
- Calculates error and updates servo angles based on PID parameters.
- Controls two servo motors to maintain balance.
- Initialize ESP32, assign servo pins, and set up Serial communication.
- Receive object position data from the PC via Serial.
- If no object is detected, set error values to -1.
- If valid data is received, update servo angles accordingly.
- Repeat the loop.
ESP32 receives data from the PC in the format:
x_valuex y_value
x_valueandy_valuerepresent the object's coordinates.- If the object is not detected, ESP32 receives
"no".
#include <Servo.h>
#include <Wire.h>
static const int servoPin1 = 16;
static const int servoPin2 = 17;
Servo servo1, servo2;
float x_error = -1, y_error = -1;
unsigned long previousMillis = 0;
void setup() {
Serial.begin(115200);
servo1.attach(servoPin1);
servo2.attach(servoPin2);
servo1.write(55);
servo2.write(55);
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= 1) {
previousMillis = currentMillis;
processData();
servo1.write(x_error);
processData();
servo2.write(y_error);
}
}
void processData() {
if (Serial.available() > 0) {
String input = Serial.readStringUntil('\n');
if (input == "no") {
x_error = -1;
y_error = -1;
} else {
int separatorIndex = input.indexOf('x');
if (separatorIndex != -1) {
x_error = input.substring(0, separatorIndex).toInt();
y_error = input.substring(separatorIndex + 1).toInt();
}
}
}
}- Implement signal filtering to reduce noise from the camera.
- Optimize the PID algorithm for faster response times.
- Improve Serial communication for efficient data handling.
📌 System Images:
📌 System Demo Videos:
- Adjust balance position via keyboard
- Impressive balancing performance
- Upcoming update – Setpoint adjustment via mouse clicks
📜 License 🚀 Open-source project – Free to use for educational & commercial purposes
Created by Trần Trọng Phúc 📧 Contact: trantrongphucttp27@gmail.com
🔥 If you find this project useful, don't forget to ⭐ Star the repo!