Imagine balancing a stick on your finger. Now imagine doing it automatically with a robot. This project does exactly thatโusing advanced control theory, custom electronics, and real-time software to keep an inverted pendulum perfectly balanced.
IMG_2700.mp4
Think of balancing a broomstick on your palm. You can see where the stick is (the angle), but you can't directly feel how fast it's moving. Yet your brain somehow figures it out and moves your hand to keep it balanced.
This project replicates that process:
- Sensors measure the pendulum's angle (like your eyes seeing the stick)
- A "smart guesser" (called an observer) figures out how fast it's moving (like your brain estimating speed)
- A controller calculates the right motor command to keep it balanced (like your hand moving)
- Custom circuits translate these commands into actual motor movements
The magic happens 1000 times per secondโfast enough that the pendulum never falls!
This is a complete mechatronics system combining:
- โ Control Theory: State-feedback control with full-state observer
- โ Control System Design: PID tuning (Ziegler-Nichols, Cohen-Coon), root locus analysis, Bode plots, stability margins
- โ Hardware Design: Custom analogue and digital circuits for sensors and actuators
- โ Power Management: Efficient power supplies and protection circuits
- โ Signal Processing: Filtering and conditioning for clean sensor data
- โ PCB Design: Complete printed circuit board design workflow
- โ Testing Infrastructure: Comprehensive functional and environmental tests
Mechanism Overview. A rotary base (joint 1) moves the pendulum link (joint 2). Sensors measure angles; the controller estimates velocities and commands the motor.
The pendulum has two joints:
- Joint 1 (base): Can rotate horizontally
- Joint 2 (pendulum): The link that needs to stay upright
We can measure the angles (
Instead of measuring everything, we:
- Predict what the velocities should be (using a mathematical model)
- Compare our predictions to the actual measured angles
- Correct our predictions based on the difference
- Control the motor using these corrected estimates
This happens continuously in a feedback loop running at 1 kHz (1000 times per second).
Encoders measure joint angles with high precision:
- Type: Incremental optical encoders (2048 pulses/revolution)
- Interface: Quadrature decoding via LS7366R ICs
- Signal Conditioning: Noise filtering and level shifting
- Communication: SPI interface to microcontroller
๐ Details: See hardware/circuits/sensor_interface.md
DC Motor drives the base to balance the pendulum:
- Power: 24V DC, 2A continuous (5A peak)
- Driver: DRV8871 H-bridge with integrated current sensing
- Control: 20 kHz PWM for smooth operation
- Protection: Overcurrent and thermal shutdown
๐ Details: See hardware/circuits/actuator_drive.md
Efficient power distribution for all subsystems:
- 24V Input: Main power supply (wall adapter or battery)
- 3.3V Regulator: For microcontroller and digital logic (85% efficiency)
- 5V Regulator: For encoder power (88% efficiency)
- Protection: Overvoltage, undervoltage, and overcurrent protection
๐ Details: See hardware/circuits/power_management.md
Clean, accurate sensor data through:
- Anti-aliasing Filters: Remove high-frequency noise before ADC
- Current Sensing: Precise motor current measurement (1.2 mA resolution)
- Voltage Monitoring: System voltage tracking for diagnostics
- Digital Filtering: Software filters for angle rate estimation
๐ Details: See hardware/circuits/signal_processing.md
Complete printed circuit board design:
- 4-Layer Stackup: Signal, ground, power planes
- Component Selection: Detailed analysis and alternatives
- Layout Guidelines: Thermal management, EMI reduction
- Manufacturing: Gerber files, BOM, assembly notes
๐ Details:
Comprehensive validation of all system components:
- โ Sensor accuracy and noise levels
- โ Actuator response time and characteristics
- โ Control loop stability
- โ Observer performance
- โ Signal processing accuracy
- โ Power management
๐ Details: See tests/functional_tests.m
Reliability testing under various conditions:
- ๐ก๏ธ Temperature: Operation from -10ยฐC to 60ยฐC
- ๐ณ Vibration: Resistance to 10-1000 Hz mechanical vibration
- โฑ๏ธ Extended Operation: 1-hour continuous operation test
- โก Power Variation: Behavior with 20-28V input voltage
- ๐ Thermal Cycling: Rapid temperature change stress test
๐ Details: See tests/environmental_tests.m
Run Tests:
% Functional tests
run('tests/functional_tests.m')
% Environmental tests
run('tests/environmental_tests.m')
Control System Implementation. Observer estimates unmeasured states; controller applies state-feedback using estimates.
After tuning the observer speed:
- 20ร scaling: System stabilized but showed oscillation
- 15ร scaling: Smooth behavior with minimal control effort
Hardware Results. Joint angles (top) and motor voltage (bottom) with observer poles at 15ร the controller poles.
The pendulum dynamics are linearized about the upright position:
where the state vector is:
and only angles are measured:
If all states were available, the control law would be:
The gain matrix
These poles provide:
- Fast response (real parts around -10 to -18)
- Good damping (complex poles with damping ratio ~0.707)
- Stability (all poles in left half-plane)
Since velocities are not measured, we use an observer:
The correction term
The estimation error
By making
The actual control law uses the state estimate:
The separation principle guarantees that if both
- System Identification: Linearize pendulum model and identify parameters
-
Controller Design: Compute
$K$ via pole placement -
Observer Design: Compute
$L$ with poles 15ร faster than controller - Control System Analysis: PID tuning, root locus, Bode plots, stability margins
- Simulink Model: Implement observer and controller blocks
- Real-Time Execution: Run at 1 kHz on STM32F4 microcontroller
- Sensors: Optical encoders with quadrature decoding
- Actuator: 24V DC motor with H-bridge driver
- Processing: STM32F407VGT6 microcontroller (ARM Cortex-M4)
- Power: Multi-rail power supply with protection circuits
- PCB: Custom 4-layer board with optimized layout
Upright-Pendulum-main/
โโโ README.md # This file
โโโ LICENSE # MIT License
โ
โโโ scripts/
โ โโโ script_phase.m # Controller and observer design
โ โโโ actual_graph.m # Hardware data visualization
โ โโโ control_design_tools.m # Control system design and analysis
โ โโโ pid_tuning_demo.m # PID tuning method comparison
โ
โโโ models/
โ โโโ model1.slx # Simulink control model
โ โโโ model1.1.slx # Updated Simulink model
โ
โโโ data/
โ โโโ qm_actual-15.mat # Hardware test data (angles, 15ร observer)
โ โโโ qm_actual-20.mat # Hardware test data (angles, 20ร observer)
โ โโโ um_actual-15.mat # Hardware test data (input, 15ร observer)
โ โโโ um_actual-20.mat # Hardware test data (input, 20ร observer)
โ
โโโ hardware/
โ โโโ circuits/
โ โ โโโ sensor_interface.md # Encoder circuit design
โ โ โโโ actuator_drive.md # Motor driver circuits
โ โ โโโ power_management.md # Power supply design
โ โ โโโ signal_processing.md # Signal conditioning
โ โโโ pcb/
โ โโโ design_workflow.md # PCB design process
โ โโโ component_selection.md # Component analysis
โ
โโโ tests/
โ โโโ README.md # Test documentation
โ โโโ functional_tests.m # Functional validation
โ โโโ environmental_tests.m # Environmental testing
โ
โโโ docs/
โโโ PROJECT_STRUCTURE.md # Project organization documentation
- MATLAB (R2018b or later) with:
- Control System Toolbox
- Simulink
- Signal Processing Toolbox (for tests)
- Hardware (optional, for physical implementation):
- Inverted pendulum rig
- STM32F4 development board or custom PCB
- Encoders, motor, power supply
The project includes control system design and analysis tools for controller development and performance evaluation.
PID Tuning Methods:
- Ziegler-Nichols ultimate gain method for PID parameter selection
- Cohen-Coon tuning for first-order plus dead time systems
- Automatic parameter calculation from system step response
Frequency Domain Analysis:
- Root locus plots for gain selection and stability analysis
- Bode plots showing magnitude and phase response
- Gain and phase margin calculation for stability assessment
Time Domain Analysis:
- Step response with performance metrics including rise time, settling time, and overshoot
- Open-loop versus closed-loop comparison
- Steady-state error analysis
Usage:
% Run comprehensive control design analysis
run('scripts/control_design_tools.m')
% Compare PID tuning methods
run('scripts/pid_tuning_demo.m')The scripts generate plots showing root locus, Bode plots, step responses, and stability margins, providing a complete view of the control system's performance characteristics.
-
Design Controller and Observer:
run('scripts/script_phase.m') -
Simulate in Simulink:
- Open
models/model1.slxormodels/model1.1.slx - Run simulation
- Analyze results
- Open
-
Visualize Hardware Data:
run('scripts/actual_graph.m') -
Run Tests:
run('tests/functional_tests.m') run('tests/environmental_tests.m')
- Faster observers track better but amplify sensor noise
- Slower observers are more robust but lag behind
- Sweet spot: Observer poles ~15ร faster than controller poles
- Motor voltage limited to ยฑ24V
- Unmodeled friction affects performance
- Current limiting protects hardware
- 1 kHz sampling rate: Matches control loop frequency
- Anti-aliasing filters: Prevent high-frequency noise aliasing
- Digital filters: Smooth angle rate estimates
- State-Space Representation: Modern control theory foundation
- Pole Placement: Direct eigenvalue assignment method
- Observers: State estimation from partial measurements
- Separation Principle: Independent controller/observer design
- Sensor Interfaces: Encoder decoding, signal conditioning
- Motor Control: PWM, H-bridge drivers, current sensing
- Power Management: Voltage regulation, protection circuits
- PCB Design: Layout, thermal management, EMI reduction
State Feedback โ Control law
Full-State Observer โ Estimator
Pole Placement โ Design method choosing eigenvalues of
Separation Principle โ Allows independent design of controller (
Quadrature Decoder โ Circuit that converts encoder A/B signals into position counts
H-Bridge โ Motor driver circuit allowing bidirectional current flow
PWM โ Pulse-width modulation for efficient motor control
MIT License โ see LICENSE file for details.
This project demonstrates the integration of:
- Control systems theory
- Embedded systems design
- Analog and digital circuit design
- PCB layout and manufacturing
- System testing and validation
A complete mechatronics system from theory to hardware! ๐ฏ