I saw the servo tutorial from Nicholas and the MPU tutorial from Josh and I immediately had the idea to combine these two. I build a little platform that could move in two directions (X and Y). On the platform was the mpu registering the pitch and roll of the platform. The arduino processes these data and moves the servos in a direction so that the platform stays horizontal.
It is not going very fast but it works.
To demonstrate how it is processing the data I added 5 leds. 4 red) in a square and 1 (green) in the middle. when the platform is horizontal only the green led lights. Otherwise there are several other leds that indicate in which direction the platform is tilted. The variable "norm" gives the amount of degrees the platform may be from the exact horizontal position.
In the sketch are lines as: servox=servox+1 and:
servox=servox-1
The addition or substraction is depending on the way the servos are mounted and need some experimenting. Together with the above mentioned lines, the lines just before them (if (servox<179) and if (servox>0)) need to be replaced too.
EXPANDED TECHNICAL DETAILS
Automatic Leveling and Stabilization
This project is a 1-axis or 2-axis "Self-Leveling" platform designed to keep sensors or cameras horizontal regardless of the base's tilt.
- IMU Orientation Correction: Uses an MPU-6050 accelerometer/gyroscope. The Arduino captures the "Roll" and "Pitch" angles and applies a Complimentary Filter to remove sensor vibration noise.
- Servo Compensation Drive: The Arduino outputs a corrected angle to high-torque MG996R servos. If the base tilts 10 degrees to the left, the Arduino moves the servo 10 degrees to the right, maintaining a perfect horizontal plane.
Performance
- PID Loop Implementation: (Advanced version) Uses a Proportional-Integral-Derivative algorithm to ensure the platform reacts smoothly and without overshooting during rapid tilts.