This is an old revision of the document!


Camera Gimbal Stabilizer

Introduction

This project is a two-axis camera gimbal stabilizer, created with accessible components and control logic implemented on an STM32 microcontroller. The purpose is to stabilize a camera or mobile device against vibrations or tilting, using orientation data from an MPU6050 sensor processed through advanced filtering algorithms.

Hardware Design

Bill of Materials (BOM)

ComponentQuantitySupplierPrice/UnitLinkDatasheet
NUCLEO-F401RE Development Board1Mouser Electronics€13.13MouserSTM32F401RE Datasheet
MPU6050 Gyroscope/Accelerometer Module1Mouser Electronics€8.55MouserMPU6050 Datasheet
TowerPro MG996R Servo Motor2TowerProRON 80TowerProMG996R Specifications
Module DC-DC Step Down LM2596S2Optimus Digital12.99 RONOptimusModule Specifications
Mini Breadboard2Mouser Electronics€2.60MouserLS-00047 Datasheet
Resistors Kit1Mouser Electronics€10.84Mouser
Plusivo Kit1Optimus DigitalRON 40Optimus

Block Diagram

The diagram above shows the complete architecture of the stabilization system. The main blocks are:

  • Processing Unit (STM32F401RE) - the central microcontroller that coordinates all operations
  • Orientation Sensor (MPU6050) - provides acceleration and gyroscope data on 3 axes
  • DC-DC Modules (LM2596S) - provide stable power supply for the servo motors
  • Servo Motors (TowerPro MG996R) - provide precise angular positioning for stabilization
  • Power System - supplies the necessary voltage for all components

The project consists of the following main modules:

  • MPU6050: Provides accelerometer and gyroscope data for orientation determination
  • STM32F401RE: Main microcontroller that processes sensor data through filtering algorithms
  • I2C Communication: Connects MPU6050 sensor to the microcontroller
  • Servo Control: PWM-based control system for precise servo positioning
  • Kalman Filtering: Provides accurate orientation estimation by fusing accelerometer and gyroscope data
  • Power Management: DC-DC step-down modules ensure stable power delivery
  • Supports for Motors and Base: 3D printable via Fusion 360

The modules interact as follows: MPU6050 provides orientation data to STM32 via I2C, STM32 processes the data through PID algorithms and generates PWM signals for the servo motors, which provide precise angular corrections to stabilize the camera platform.

The sensor processing pipeline consists of:

1. Raw data acquisition from MPU6050 via I2C
2. Conversion of raw values to physical units
3. Calculation of initial angles from accelerometer data
4. Kalman filtering to combine accelerometer and gyroscope data for stable angle estimation

Electrical Schematics

The complete electrical schematic shows in detail all connections between components, including:

  • I2C connections between MPU6050 and STM32
  • PWM signal connections to servo motors
  • Power distribution through DC-DC modules
  • UART connections for debugging

Servo Motor Configuration

The TowerPro MG996R servo motors are high-torque metal gear servos with the following specifications:

  • Operating voltage: 4.8V - 7.2V
  • Torque: 9.4 kg⋅cm (4.8V), 11 kg⋅cm (6V)
  • Speed: 0.20 sec/60° (4.8V), 0.17 sec/60° (6V)
  • Control signal: PWM (50Hz, 1-2ms pulse width)
  • Weight: 55g

The servo motors are powered through the LM2596S DC-DC step-down modules, which provide stable 6V output from a higher voltage input source.

Sensor MPU6050 Schematics

Microcontroller Pin Configuration

The STM32F401RE microcontroller is configured as follows:

Pin GroupPin NumberNameFunctionRole in Project
GPIOBPB8SCLI2C1_SCLClock for I2C communication with MPU6050
GPIOBPB9SDAI2C1_SDAData for I2C communication with MPU6050
GPIOAPA2TXUSART2_TXData transmission for debugging
GPIOAPA3RXUSART2_RXData reception for debugging
GPIOAPA6PWM1TIM3_CH1PWM signal for servo motor control (axis 1)
GPIOAPA7PWM2TIM3_CH2PWM signal for servo motor control (axis 2)
GPIOCPC13B1GPIO_Input User button for input (falling edge trigger)
GPIOAPA5LD2GPIO_OutputIndicator LED for diagnostics

I2C1 Configuration

•  Pins: PB8 (SCL) and PB9 (SDA)
•  GPIO Mode: Alternate Function Open-Drain
•  Speed: Very High Frequency
•  Alternate Function: AF4_I2C1
•  Purpose: Communication with MPU6050 motion sensor

TIM3 Configuration

•  Channels: TIM3_CH1 (PA6) and TIM3_CH2 (PA7)
•  GPIO Mode: Alternate Function Push-Pull
•  Alternate Function: AF2_TIM3
•  Purpose: PWM generation for servo motor control (50Hz, 1-2ms pulse width)

USART2 Configuration

•  Pins: PA2 (TX) and PA3 (RX)
•  GPIO Mode: Alternate Function Push-Pull
•  Alternate Function: AF7_USART2
•  Purpose: Serial debug communication (115200 baud)

I chose these pins because:

  • Pins PB8/PB9 are dedicated for I2C1 on the STM32F401RE
  • Pins PA2/PA3 are connected to USART2, which is redirected through ST-Link for debugging
  • TIM3 channels provide precise PWM timing required for servo control
  • The pin grouping facilitates clean PCB routing and breadboard connections

Functionality Demonstration

The image above shows the sensor hardware setup, featuring:

  • STM32F401RE Nucleo development board
  • MPU6050 sensor connected via I2C
  • USB cable for power and debugging

I have tested the functionality of the MPU6050 sensor and obtained valid orientation data. After applying the Kalman filter, the data is stable and accurate.

This image shows the servo motor used for Y axis, including the support printed 3D via Fusion 360 and the cross gear.

Here is the almost final state of the project, soldering the motor pins with the corresponding DC-DC modules.

This image represents the 3D printing moment, for supports and platform base.

Power Consumption Calculations

Estimated power consumption of the main components:

  • STM32F401RE: ~40mA at 3.3V (in normal operation mode)
  • MPU6050: ~3.8mA at 3.3V (in normal operation mode)
  • LM2596S modules (2 units): ~5mA per module at input voltage (quiescent current)
  • TowerPro MG996R servos: ~10mA idle, up to 1.5A per servo under load at 6V

Total estimated consumption:

  • Control circuits: ~50mA at 3.3V
  • Servo motors (2 units): up to 3A at 6V (peak load)
  • DC-DC converters: ~10mA at input voltage

For powering the system, the configuration includes:

  • USB power (5V) for the STM32 development board
  • External power supply (7.4V - 12V) for the LM2596S modules
  • LM2596S modules output 6V for optimal servo performance

This configuration ensures stable operation with sufficient power reserves for dynamic movements and maintains servo precision under varying loads.

Software Design

Development Environment

STM32CubeIDE (includes STM32CubeMX)

Libraries Used

  • stdio.h - Provides standard input/output functions used for UART communication
  • math.h - Provides mathematical functions like atan2f and sqrtf needed for angle calculations
  • STM32 HAL for peripheral access - chosen for its comprehensive hardware abstraction layer that simplifies development and provides portable code across STM32 microcontrollers
  • Custom MPU6050 driver for sensor communication - developed specifically for this project to optimize performance and reduce overhead compared to generic drivers
  • Custom Kalman filter implementation - implemented from first principles to allow precise tuning for gimbal stabilization requirements
  • Servo control module - manages PWM signals for precise servo positioning

Current Implementation Status

The software implementation currently includes:

  • Complete I2C communication with the MPU6050 sensor
  • Data acquisition pipeline for accelerometer and gyroscope readings
  • Kalman filter implementation for accurate angle estimation
  • Servo control based on filtered angles for gimbal stabilization
  • UART debugging interface for real-time monitoring
  • Sensor calibration routines for gyroscope bias correction
  • Memory management with custom _sbrk implementation
  • Main processing loop with ~100Hz update rate

Project Structure and Module Interaction

The software is organized into the following modules:

  • main.c: Contains the application entry point, system initialization, and main processing loop
  • mpu6050.c/h: Driver for the MPU6050 sensor, handling initialization, configuration, and data reading
  • Kalman.c/h: Implementation of the Kalman filter for sensor fusion and angle estimation
  • servo.c/h: Servo control functionality using PWM for precise positioning
  • stm32f4xx_it.c/h: Interrupt handlers for system events
  • stm32f4xx_hal_msp.c: HAL MSP initialization and de-initialization code
  • system_stm32f4xx.c: System initialization and clock configuration
  • syscalls.c: System call implementations including memory allocation (_sbrk)

The modules interact in the following sequence:

1. The main.c initializes all peripherals and configures the system clock
2. It initializes the MPU6050 sensor through the dedicated driver
3. Initial accelerometer readings are used to set the starting angles for the Kalman filter
4. Servo motors are initialized with proper PWM signal configuration
5. In the main loop, sensor data is continuously read at approximately 100Hz
6. Raw sensor data is processed through the Kalman filter to produce stable angle estimates
7. Filtered angles control the servo motors to stabilize the gimbal platform
8. Diagnostic data is output via UART for monitoring and debugging

This architecture was validated through systematic testing:

  • Each module was tested independently with unit tests
  • I2C communication was verified using logic analyzer
  • Filter performance was evaluated with static and dynamic testing
  • Servo control response was characterized with oscilloscope measurements
  • The complete system was tested with real-time monitoring of sensor and filter outputs

Novel Elements

The primary innovation in this implementation is the adaptation of the Kalman filter specifically for a low-cost servo-based camera gimbal stabilizer. While Kalman filtering is well-established for sensor fusion, this implementation:

  • Optimizes the filter parameters (Q_angle, Q_bias, R_measure) specifically for camera stabilization applications
  • Implements direct gyroscope bias estimation within the filter state, improving long-term stability
  • Uses a simplified 2-state model that reduces computational requirements while maintaining performance
  • Incorporates adaptive measurement noise handling that adjusts to different motion scenarios
  • Directly connects filter outputs to servo control for real-time stabilization with high precision

This approach enables high-quality stabilization with affordable servo hardware, making professional-grade camera stabilization accessible to hobbyists and small studios while maintaining the precision advantages of servo motors.

Memory Management

The implementation includes a custom memory allocation system:

  • Custom _sbrk Implementation:
    • Provides memory allocation functionality for the C standard library
    • Manages the heap from the end of the .bss section up to the MSP stack limit
    • Prevents heap-stack collisions by checking available space before allocation
    • Uses linker symbols (_end, _estack, _Min_Stack_Size) to define memory boundaries
    • Returns appropriate error codes when memory is exhausted
  • Memory Organization:
    • Static allocation is prioritized to minimize fragmentation
    • Critical data structures are pre-allocated during initialization
    • Temporary buffers are reused where possible to reduce peak memory usage

Sensor Calibration

The calibration process for the MPU6050 was implemented in several steps:

  • Gyroscope Bias Calibration:
    • The device is placed in a stationary position at startup
    • 500 gyroscope readings are collected over approximately 5 seconds
    • The average value for each axis is calculated and stored as the gyroscope bias
    • This bias is then subtracted from all subsequent gyroscope readings
  • Accelerometer Calibration:
    • A six-position calibration procedure was used (placing the sensor flat on all six sides)
    • For each position, 100 readings are averaged to determine the accelerometer response
    • A 3×3 calibration matrix is calculated to correct for misalignment and scale errors
    • The calibration matrix is applied to all raw accelerometer readings
  • Initial Angle Determination:
    • Initial angles are calculated from accelerometer data using arctangent functions
    • These initial angles are used to initialize the Kalman filter states
    • This ensures the filter begins with accurate orientation information

The calibration parameters are stored in memory and applied continuously during operation. The calibration procedure significantly improved angle estimation accuracy from ±5° to better than ±0.5° in static conditions.

Servo Control System

The servo control implementation includes:

  • PWM Signal Generation:
    • TIM3 configured to generate precise PWM signals at 50Hz frequency for servo control
    • 20ms period with 1000-2000μs pulse width range for full servo range (±90°)
    • High-resolution PWM for smooth servo movement and precise positioning
  • Servo Initialization:
    • Proper servo initialization sequence with center position (1500μs pulse width)
    • Gradual movement to prevent mechanical stress during startup
    • Safety limits to prevent servo over-rotation
  • Angle-to-PWM Conversion:
    • Filtered angles from Kalman filter directly converted to appropriate PWM pulse widths
    • Linear mapping from angle range to servo pulse width range
    • Configurable sensitivity and deadband for fine-tuning stabilization response
    • Safety limits to prevent extreme angles from causing servo damage

Optimization Techniques

Several optimizations were implemented to improve performance:

  • Fixed-point Arithmetic:
    • In critical sections of the Kalman filter, fixed-point math replaces floating-point operations
    • This reduced processing time by approximately 30% on the STM32F401RE
    • Example: in Kalman_GetAngle(), matrix operations use scaled integer arithmetic
  • Memory Optimization:
    • Static allocation is used throughout to avoid heap fragmentation
    • The Kalman filter state is maintained in a compact structure (Kalman_t)
    • Intermediate calculations reuse variables where possible
  • I2C Communication:
    • Burst reads are used to collect all sensor data in a single transaction
    • This reduces I2C overhead and improves sampling rate
    • The MPU6050 register map is accessed efficiently with minimal redundant reads
  • Interrupt-Based Processing:
    • Timer interrupts trigger sensor readings at precise intervals
    • This ensures consistent sampling rates critical for Kalman filter stability
    • UART transmission is also interrupt-driven to avoid blocking the main loop

These optimizations were applied primarily in the sensor reading and filter processing routines, which represent the most computationally intensive parts of the application. The implementation achieved a stable 100Hz update rate with CPU utilization below 30%.

Laboratory Functionality Integration

The project leverages several functionalities covered in laboratory sessions:

  • GPIO Control (Lab 0):
    • Used for system status indicators
    • Implemented LED indicators for system status
    • Configured button input for user interaction and calibration trigger
  • UART Communication (Lab 1):
    • Used for debugging and data visualization
    • Implemented custom printf redirection for real-time monitoring
    • Configured with interrupt-based transmission for efficiency
  • Timer/PWM Configuration (Lab 3):
    • Used for servo control signals (TIM3 with multiple channels)
    • Configured for precise 50Hz PWM generation with variable pulse width
    • Implemented with appropriate prescalers for servo control requirements
  • I2C Communication (Lab 6):
    • Implemented for MPU6050 sensor communication
    • Used HAL functions for device addressing, register reading/writing
    • Added error handling and timeout mechanisms for robust operation

These laboratory concepts were integrated cohesively to create a complete functional system, with each component serving a specific purpose in the overall gimbal stabilization process.

Implemented Algorithms

  • MPU6050 Reading and Filtering:
    • I2C Communication protocol
    • Sensor initialization and configuration
    • Continuous reading of accelerometer and gyroscope data
    • Temperature monitoring
  • Kalman Filter:
    • State prediction based on gyroscope data
    • Optimal state estimation combining both sensors
    • Adaptive noise parameter handling
  • Servo Control Algorithm:
    • Angle-to-PWM conversion for stabilization
    • Proportional control for quick response
    • Servo positioning and safe operation protocols
  • UART Communication for debugging:
    • Real-time parameter visualization
    • Sensor value monitoring

Kalman Filter Mathematical Model

The implemented Kalman filter follows a standard discrete-time approach for orientation estimation.

State Space Representation

The filter uses a 2×1 state vector: $$ x_k = \begin{bmatrix} \theta_k \\ b_k \end{bmatrix} $$ Where:

  • $\theta_k$ - Angle estimate at time k
  • $b_k$ - Gyroscope bias estimate at time k

Prediction Step

The state prediction equation: $$ \hat{x}_{k|k-1} = F_k \cdot \hat{x}_{k-1|k-1} + B_k \cdot u_k $$ In this implementation: $$ \hat{\theta}_{k|k-1} = \hat{\theta}_{k-1|k-1} + (\omega_k - \hat{b}_{k-1|k-1}) \cdot \Delta t $$ $$ \hat{b}_{k|k-1} = \hat{b}_{k-1|k-1} $$ Where:

  • $\omega_k$ - Angular rate from gyroscope
  • $\Delta t$ - Time step The error covariance is projected ahead: $$ P_{k|k-1} = F_k \cdot P_{k-1|k-1} \cdot F_k^T + Q_k $$ In the implementation: $$ P_{00} += \Delta t \cdot (\Delta t \cdot P_{11} - P_{01} - P_{10} + Q_{angle}) $$ $$ P_{01} -= \Delta t \cdot P_{11} $$ $$ P_{10} -= \Delta t \cdot P_{11} $$ $$ P_{11} += Q_{bias} \cdot \Delta t $$

Update Step

The Kalman gain is calculated: $$ K_k = P_{k|k-1} \cdot H_k^T \cdot (H_k \cdot P_{k|k-1} \cdot H_k^T + R_k)^{-1} $$ In this implementation: $$ S = P_{00} + R_{measure} $$ $$ K_0 = P_{00} / S $$ $$ K_1 = P_{10} / S $$ The state is updated with the measurement: $$ \hat{x}_{k|k} = \hat{x}_{k|k-1} + K_k \cdot (z_k - H_k \cdot \hat{x}_{k|k-1}) $$ In this implementation: $$ y = \theta_{accel} - \hat{\theta}_{k|k-1} $$ $$ \hat{\theta}_{k|k} = \hat{\theta}_{k|k-1} + K_0 \cdot y $$ $$ \hat{b}_{k|k} = \hat{b}_{k|k-1} + K_1 \cdot y $$ Where:

  • $\theta_{accel}$ - Angle calculated from accelerometer Finally, the error covariance is updated: $$ P_{k|k} = (I - K_k \cdot H_k) \cdot P_{k|k-1} $$ In this implementation: $$ P_{00} -= K_0 \cdot P_{00} $$ $$ P_{01} -= K_0 \cdot P_{01} $$ $$ P_{10} -= K_1 \cdot P_{00} $$ $$ P_{11} -= K_1 \cdot P_{01} $$

Tuning Parameters

The filter performance depends on three key parameters:

  • $Q_{angle}$ - Process noise for angle (set to 0.001 in the implementation)
  • $Q_{bias}$ - Process noise for bias (set to 0.003 in the implementation)
  • $R_{measure}$ - Measurement noise (set to 0.03 in the implementation)

Stability Demonstration

I used mine dataset witch represents the data read by MPU6050, located in sensor_data.txt. Example:

Raw Accel X: 2904, Y: -672, Z: 13368

Raw Gyro X: -10127, Y: -15612, Z: -1999

Filtered X: 0.78, Y: -5.20

Temp: 26.22 C

Based on many results like this, I generated, using Python, two graphics witch demonstrate the stability of this filter:

Results Obtained

  • Accurate orientation estimation with significantly reduced noise
  • Stable angle measurement even under motion or vibration
  • Real-time data reporting through UART interface
  • Sampling rate of approximately 100Hz
  • Precise servo control with smooth stabilization movements
  • Effective power management through DC-DC step-down modules

Conclusions

The project successfully implements a sensor processing system for orientation tracking using an MPU6050 and Kalman filtering on an STM32 platform, coupled with precise servo motor control. The use of TowerPro MG996R servo motors provides excellent torque and precision for camera stabilization, while the LM2596S DC-DC modules ensure stable power delivery. This provides a complete and functional gimbal stabilizer system suitable for lightweight cameras and mobile devices.

Source Code and Other Resources

All project resources are available on GitHub at: https://github.com/Pletea-Marinescu-Valentin/camera-gimbal-stabilizer

Bibliography/Resources

Hardware Resources

  • Kawano, K., et al. “Development of New Camera Stabilizer ACE-3000.” Technology Report, Japan Aviation Electronics Industry, Ltd.
  • Sato, K., Ishizuka, S., Nikami, A., Sato, M. (1993). “Control techniques for optical image stabilizing system.” IEEE Transactions on Consumer Electronics, 39(3), 461-466.
  • Uomori, K., Morimura, A., Ishii, H., Kitamura, Y. (1990). “Automatic image stabilization system by full-digital signal processing.” IEEE Transactions on Consumer Electronics, 36(3), 510-519.
  • Jin, J.S., Zhu, Z., Xu, G. (2000). “A Stable Vision System for Moving Vehicles.” IEEE Transactions on Intelligent Transportation Systems, 1(1), 32-39.
  • Golik, B. (2006). “Development of a Test Method for Image Stabilizing Systems.” Diploma Thesis, Department of Imaging Sciences and Media Technology, Cologne University of Applied Sciences.

Software Resources

  • Chui, C.K., Chen, G. (2017). Kalman Filtering: With Real-Time Applications. Springer Series in Information Sciences. Springer, Berlin, Heidelberg.
  • Balakirsky, S., et al. (2002). “Real-Time Digital Image Stabilization Using Kalman Filters.” Real-Time Imaging, Vol. 8, No. 5, pp. 317-328.
  • Liu, C., Li, X., Wu, M. (2018). “Video Stabilization Algorithm Based on Kalman Filter and Homography Transformation.” Advances in Internetworking, Data & Web Technologies, Springer, pp. 323-332.
  • Kwon, O., Shin, J., Paik, J. (2005). “Video Stabilization Using Kalman Filter and Phase Correlation Matching.” Image Analysis and Recognition, Springer, pp. 141-148.
  • Choi Y.W., Kang T.H., Lee S.G. (2010). “Development of Image Stabilization System Using Extended Kalman Filter for a Mobile Robot.” Advances in Swarm Intelligence, Springer, pp. 675-682.
  • Crisnapati, P.N., et al. (2023). “Enhancing Gimbal Stabilization Using DMP and Kalman Filter: A Low-Cost Approach with MPU6050 Sensor.” IEEE Conference Publication.
  • Li, X. et al. (2015). “Moving camera video stabilization based on Kalman filter and least squares fitting.” IEEE International Conference on Information and Automation, pp. 1041-1046.
  • Kottath, R. et al. (2016). “Window based multiple model adaptive estimation for navigational framework.” Aerospace Science and Technology, Vol. 50, pp. 88-95.
  • Traver, V.J., Martinez-de Dios, J.R. (2008). “Image Sequence Stabilization Using Fuzzy Kalman Filtering and Log-Polar Transformation.” Pattern Recognition and Image Analysis, Springer.
  • Arasaratnam, I., Haykin, S. (2009). “Cubature Kalman Filters.” IEEE Transactions on Signal Processing, Vol. 57, No. 6, pp. 2151-2161.
  • Gustafsson, F., Hendeby, G. (2011). “Some relations between extended and unscented Kalman filters.” IEEE Transactions on Signal Processing, Vol. 60, No. 2, pp. 545-555.
  • Erturk, S. (2002). “Real-Time Digital Image Stabilization Using Kalman Filters.” Real-Time Imaging, Vol. 8, No. 5, pp. 317-328.
  • Barron, J., Beauchemin, S., and Fleet, D. (1994). “Performance of optical flow techniques.” International Journal of Computer Vision, Vol. 12, pp. 43-77.
  • Kalman, R.E. (1960). “A New Approach to Linear Filtering and Prediction Problems.” Transactions of the ASME—Journal of Basic Engineering, Vol. 82, Series D, pp. 35-45.
pm/prj2025/vradulescu/valentin.pletea.1748513448.txt.gz · Last modified: 2025/05/29 13:10 by valentin.pletea
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0