Online Circuit Simulator
Servo position control simulation: geared motor, potentiometer and PD
We typically respond within one business day.
A servo is a geared motor that turns its output shaft to the angle it is told. Here a small 6 V motor drives the shaft through a 100 to 1 gear, a potentiometer on the shaft reports the angle to an ATmega328P, and the program drives the motor through an H-bridge 1000 times a second. The circuit, the motor, the gear, the potentiometer and the chip are simulated together.
Recorded run of the default program: blue, the shaft angle; grey, the target (0, then +60 degrees at 0.1 s and -45 degrees at 0.6 s); orange, the drive.
What is being simulated?
The motor has resistance, inductance and back voltage; the arm on the shaft has inertia, and the gear has some friction. Through the gear the arm adds only 1/100^2 of its inertia at the motor. The potentiometer gives 0.5 V at -135 degrees and 4.5 V at +135 degrees, about 3 ADC codes per degree.
| Part | Value |
|---|---|
| Supply | 6 V |
| Motor R, L, k | 4 ohm, 0.5 mH, 0.004 V s/rad |
| Gear | 100:1 |
| Arm inertia | 2e-4 kg m2 |
| Gear friction (output) | 10 mN m |
| Potentiometer | 270 degrees, 0.5 to 4.5 V |
With proportional control only, the shaft behaves as a second-order system: with 4 duty steps per code the damping ratio is 0.34, and the formula exp(-zeta pi / sqrt(1 - zeta^2)) gives 32 % overshoot for a small step; the simulated program gives 34 % without friction and 22 % with the gear friction.
What does the default program do?
Every millisecond it reads the potentiometer and sets the drive to 8 duty steps per code of error minus 240 per code of movement since the last step: a PD controller. The D term brakes the arm before it arrives. In the recorded run the 60 degree step peaks at 60.3 degrees and stays within 1 degree after 0.16 s. One code of movement in a millisecond is worth 240 duty steps, so the drive jumps while the arm moves; the motor and the gear smooth it out.
Default program in AVR assembly
Default program in C
Lessons and challenges
- P only. Set KD to 0 and give a 5 degree step. Measure the overshoot and the time of the peak and compare them with the second-order formulas.
- More gain. Double KP: the overshoot grows to about 49 %, and the formula predicts 46 %.
- PD. Put KD back. Check: overshoot under 1 degree on the 60 degree step, within 1 degree by 0.2 s.
- Friction. Raise the gear friction and look for a dead band: the arm stops short of the target where KP times the error can no longer move it.
- Resolution. One ADC code is 0.33 degrees. Find the smallest step the servo answers to.
- Challenge: take the target from a hobby servo pulse (1 to 2 ms on D2) instead of the fixed steps.
How were the numbers checked?
The motor and gear model was checked against its steady speed with friction and its time constant. The proportional runs were compared with the second-order overshoot and peak-time formulas, and the assembly and C programs give the same drive.
Get your project started
We typically respond within one business day.