Project
Nonlinear Control of a 2-DoF Gimbal: Robust and Adaptive Designs
· nonlinear control / robust control / adaptive control / Lyapunov / MATLAB
This was my project for Nonlinear Control Theory, a course I took during my Master’s. The system is a two-axis gimbal: a small camera mount with an azimuth joint carrying an elevation joint, the kind of thing you bolt to a drone to keep a lens pointed at something while the aircraft moves underneath it. I built the hardware, derived the dynamics, and then used it as a testbed for the controllers the course covered.
What makes it a good testbed is that it is genuinely nonlinear and genuinely coupled. The inertia the azimuth motor sees depends on where the elevation joint is pointing. Spin one axis and the other feels it. And because the whole thing weighs about seventy grams, the inertias are in the tens of microkilogram-metres-squared, which turns out to matter a great deal — not for the control theory, but for the numerics, as I found out the hard way near the end of this post.
The plan was to design five controllers against the same plant and the same reference, and compare them honestly. The thread running through all of them is a single trick: collapse position and velocity error into one signal, then make a Lyapunov function out of it. Once you have that, every controller on this page is a variation on one theme.
Everything below comes out of a MATLAB simulation that integrates the closed loop directly. The code is in a repository of its own, including the Simulink and Simscape Multibody model I originally built it in.
The system
I derived the kinematics with the modified Denavit–Hartenberg convention and
the dynamics from there; the symbolic derivation is in gimbal_dynamics.mlx in
the repository. What comes out is the standard rigid-body form:
with the joint angles, the inertia matrix, the Coriolis and centripetal matrix, the gravity vector and the input torques. For this gimbal they work out to
The in is the coupling I mentioned: the effective inertia about the azimuth axis changes as the payload tilts. At the payload’s mass is spread out away from the vertical axis; at it is lined up with it.
Lumping the parameters
Notice that the matrices above are written in terms of five numbers rather than the masses, lengths and inertias themselves. That is deliberate, and it is the single most useful thing about this model. The physical parameters only ever appear in five fixed combinations:
So there is no point estimating nine physical quantities when only five combinations can ever affect the motion. More importantly, the dynamics are linear in that vector. Any expression built from , and can be factored as a known matrix times the unknown parameters:
where depends only on measured signals. Hold on to this — it is what makes the adaptive controller at the end of this page possible, and it is the reason the model was written this way in the first place.
| Parameter | Value | Parameter | Value |
|---|---|---|---|
| 0.051 kg | 2.084×10⁻⁵ kg·m² | ||
| 0.0195 kg | 7.4×10⁻⁷ kg·m² | ||
| −0.0227 m | 3.86×10⁻⁶ kg·m² | ||
| 0.0246 m | 3.75×10⁻⁶ kg·m² | ||
| 0.0014 m | −5.6×10⁻⁷ kg·m² |
Masses came off a scale, centres of mass and inertia tensors out of SolidWorks. Friction is not in the model at all: I never got the hardware instrumented well enough to identify it, so rather than invent a number I left it out and noted the omission. That is a real limitation and I come back to it at the end.
One error signal to rule them all
Here is the idea that the whole course, and this whole page, hangs on.
The tracking error is the obvious thing:
But a second-order system needs both position and velocity error controlled, and carrying two signals through a Lyapunov argument is clumsy. So define the filtered tracking error
This is just a first-order stable filter applied to . The payoff is that driving the single signal to zero is enough: if , then , which is a stable first-order ODE, so and as well. One signal, both guarantees.
Now differentiate and multiply by the inertia matrix — multiplying by is the move that makes everything after this work:
Substituting from the dynamics, and then rewriting to pull the -dependence out:
So the error dynamics are
with collecting everything that does not depend on . This is the equation every controller below is designed against. The only thing that changes from one section to the next is how much of you think you know.
Why the inertia matrix had to be there
Take the Lyapunov candidate
This is positive definite because is. Differentiating, and remembering that is time-varying:
And now the structural property that makes rigid-body manipulators so pleasant to control: is skew-symmetric, so for any vector ,
This is not a modelling assumption or an approximation, but it does depend on which you wrote down, and the usual one-line justification for it is not quite enough. is not unique: any matrix satisfying (the Coriolis and centripetal terms) is a legitimate choice, and most of them are not skew-symmetric. The argument people normally reach for — that Coriolis forces do no net work — is really the kinetic-energy identity, and it only pins down the case . Here the identity gets applied to , which is a different vector entirely, so that argument does not cover the use it is being put to.
What does cover it is that the at the top of this page is the Christoffel construction,
which is the choice that makes skew-symmetry hold for every . Substitute the and from the top of the page and it comes out exactly:
Zero diagonal, antisymmetric off-diagonal, no approximation anywhere. So the entire first term vanishes:
That is a remarkable simplification. A quadratic form in a time-varying inertia matrix, differentiated, collapses to something with no derivative of in it at all. This is why we multiplied by back there: so that the awkward term would have a term to cancel against.
Everything from here is choosing to make negative.
Exact model knowledge
If you know every parameter, the choice is obvious — cancel and add damping:
Negative definite, so asymptotically, so . Done.
With , , and a sinusoidal reference of amplitude 0.4 rad and period 2 s on both joints:
The left panel is the theory working exactly as advertised, and it is also slightly dishonest as a plot: with and a perfect model, the true solution is for all time, so what is drawn is the numerical integrator talking to itself. The useful number is that it stays below 10⁻⁸ rad.
The right panel is the whole reason the rest of this page exists. I added 14 g to every mass, 5 × 10⁻⁶ kg·m² to every inertia and 7.5 mm to every centre-of-mass offset — perturbations that are small in absolute terms but not at all small relative to a seventy-gram mechanism. The controller, still computing from the nominal numbers, now leaves a steady 2.4 mrad of elevation error. Gravity is the main culprit: more than doubles under that perturbation, and nothing in the control law knows.
Two ways to deal with not knowing . Assume a bound on how wrong you are and overpower it, which is robust control. Or estimate it online, which is adaptive control.
Robust control
Split into what you can compute and what you cannot:
The controller gets the nominal part plus a damping term plus an extra term whose job is to swamp whatever is left:
The design question is what to put in . The course covered three answers, and the interesting thing is that they give three genuinely different stability conclusions.
High frequency
Substituting and bounding :
That bracket is at most 1, so
which gives uniform ultimate boundedness: ends up in a ball whose size is set by , and no smaller. You buy a smaller residual by picking a smaller , which costs you gain.
High gain
which is just a linear term. The bound splits into two cases:
Exponential while the error is large, ultimately bounded once it is small. Which is the honest description of what a high-gain loop does.
Sliding mode
applied elementwise. Since ,
with no leftover at all. On paper this is the strongest of the three: exponential stability, not just boundedness. The discontinuity is doing real work, and it is also where the trouble starts — see below.
What they do
All three with , , , (and for sliding mode), against the same perturbed plant:
That right panel surprised me until I looked at the algebra. With and , the high-gain term has coefficient . The high-frequency term has coefficient , and since here is of order 10⁻⁵, that denominator is . The two controllers are numerically the same controller. They only diverge when the error is large enough for to compete with , which on this trajectory happens only in the first few milliseconds.
So the difference between “ultimately bounded” and “exponentially then ultimately bounded” is, for this plant and these gains, invisible. The distinction is real but it lives in a regime this experiment never enters.
A detour: the sliding-mode result is a numerics result
The sliding-mode row is the one I could not get to agree with my original Simulink results, and chasing down why taught me more than the controller did.
With the ideal and a fixed-step integrator, I got an RMS velocity error of 7.7 × 10⁻² rad/s — three orders of magnitude worse than anything else on this page. My first guess was chattering. It was not: I checked, and crosses zero exactly once in two seconds. The switch never switches. It just sits at and acts as a constant 0.1 N·m bias against a plant whose gravity torque is about 0.01 N·m.
The real cause is the step size. The closed-loop error dynamics are , so their time constant is
Four microseconds. That is what a seventy-gram mechanism with a gain of 10 buys you. Refining the step makes the point:
| RK4 step | sign changes in | RMS |
|---|---|---|
| 10 µs | 1 | 7.7×10⁻² |
| 3 µs | 1 | 1.7×10⁻³ |
| 1 µs | 2 | 7.4×10⁻⁴ |
| 0.3 µs | 196,520 | 1.9×10⁻⁴ |
Only below the 4.3 µs time constant does the switch start switching, and only then does the answer start converging. Everything above that line is integration error wearing a control result’s clothing.
The fix in practice is the one that was sitting commented out in my own Simulink block the whole time — replace the discontinuity with a saturation:
This is smooth, so a stiff solver handles it without heroics, and it is what any real implementation would use anyway, because no motor can switch instantaneously. It costs you the clean exponential bound — you are back to ultimate boundedness, with playing the role played earlier. Every sliding-mode number quoted elsewhere on this page uses this version.
I am fairly sure the same effect explains the sliding-mode row in my original report, whose velocity error sits almost exactly where my 1 µs run does.
Adaptive control
Robust control assumes a bound on the uncertainty and pays for it with gain. Adaptive control does something more ambitious: it estimates the parameters while it runs.
This is where the linear parameterization earns its keep. Write
with built from measured signals. For this gimbal, with and :
Let be the estimate and the estimation error. Now augment the Lyapunov function with a term that measures how wrong the estimate is:
using , since the true parameters do not change. Choose
and the cross terms annihilate each other exactly:
This is the most elegant result on the page. The update law is not a gradient descent on anything you specified; it is whatever makes the terms cancel, and it falls out of the algebra.
But look carefully at what was proved. is negative semidefinite in the full state — it is zero whenever , whatever happens to be. That is weaker than it looks, and getting from it to takes one more step than the algebra above suggests, because a negative semidefinite on its own buys only boundedness. The step is Barbalat’s lemma: makes non-increasing, so and stay bounded; integrating gives
so is square-integrable; and is bounded, because everything on the right-hand side of the error dynamics is. A square-integrable signal with a bounded derivative has to go to zero, so and therefore .
What no part of that argument gives you is .
That is not a technicality:
Only converges. It multiplies in the regressor, which is large and persistently varying along this trajectory, so the data forces it. The other four multiply terms that are either small or nearly collinear with one another on a single-frequency sinusoid, so the adaptation has no way to tell them apart. It finds some that cancels the error along this particular trajectory, and stops caring.
This is persistency of excitation, and it is the thing to remember about adaptive control. The estimates are not measurements. Run the same gimbal on a different reference and this would be useless — in fact, parameter error up to 24× sitting inside a controller that tracks to 10⁻⁴ rad is exactly the kind of thing that looks fine until the day the trajectory changes.
Desired model compensation
A variation worth the half page it takes. Evaluate the regressor along the desired trajectory instead of the measured one:
Writing and assuming , the same cancellation gives
It is tempting to read that as exponential stability — the bound is an inequality in now, which looks stronger than the equality the standard version gave. It is not, and the reason is the term again. Exponential stability needs for some , and still carries , which can be as large as you like while and . Nothing in the bound pushes the parameter error down. You land exactly where the standard version landed: , then by Barbalat.
The contrast with sliding mode earlier on this page is the whole point. There and nothing else, so really does imply with , and exponential stability follows. Add a parameter estimate to the Lyapunov function and that implication is gone. What the term actually buys is robustness to , the part of the dynamics a desired-trajectory regressor cannot represent — worth having, just not a stronger stability class.
One wrinkle, since the code is linked from the top of this page: the implementation adds as a scalar to both channels rather than the vector the derivation asks for. The proof needs ; what is implemented gives , which goes negative about a sixth of the time. It changes nothing measurable here — the term peaks at 6.1 × 10⁻⁵ N·m against a peak torque of 1.04 × 10⁻² N·m, so 0.14% — but it is not quite the controller that was proved.
The practical attraction is different though: depends only on the reference, which you know ahead of time. You can compute it offline, store it, and skip the regressor evaluation in the control loop entirely. On a microcontroller driving a gimbal at 200 Hz, that is a real saving.
And as the right-hand panel above shows, it costs essentially nothing in tracking performance — 1.24 × 10⁻⁴ against 1.25 × 10⁻⁴ rad.
That agreement deserves a moment, because the obvious explanation for it is wrong. You would guess the two coincide because tracking is good: if then , so they are the same controller wearing different clothes. Easy to test — start the gimbal 0.3 rad off the reference and the agreement should fall apart.
It does not. That run has an RMS elevation error five hundred times larger, and the two still agree to four significant figures. What does change is where the estimates end up:
| Run | RMS [rad] | at 10 s |
|---|---|---|
| Standard, | 1.248×10⁻⁴ | −2.54 |
| D.m.c., | 1.242×10⁻⁴ | −2.54 |
| Standard, | 6.717×10⁻² | −1.02 |
| D.m.c., | 6.717×10⁻² | −2.66 |
The two find genuinely different parameters and get identical tracking out of them. Which is the persistency-of-excitation point again, sharpened: what the adaptation pins down is the product , the feedforward torque this trajectory demands. Evaluating the regressor at the measured state or at the desired one changes the coordinates that product is written in, not the product itself. Neither version is identifying anything.
Nor is this a case of the model term being too small to matter. Over the nominal run, RMS is 8.3 × 10⁻³ N·m against RMS of 1.8 × 10⁻³ N·m: the feedforward is doing four and a half times as much work as the feedback. It matters enormously. Both ways of computing it simply do the job equally well.
So: same answer, less arithmetic at run time.
Putting the numbers together
All seven runs, same trajectory, same 10 s horizon, RMS error over the whole run. Every row after the first uses the perturbed plant:
| Controller | [rad] | [rad] | [rad/s] | [rad/s] |
|---|---|---|---|---|
| E.m.k. | 2.05×10⁻⁸ | 1.98×10⁻⁸ | 1.94×10⁻⁸ | 2.00×10⁻⁸ |
| E.m.k., mismatched | 9.25×10⁻⁶ | 2.38×10⁻³ | 2.44×10⁻⁵ | 6.43×10⁻⁴ |
| High gain | 1.67×10⁻⁷ | 4.32×10⁻⁵ | 4.40×10⁻⁷ | 1.17×10⁻⁵ |
| High frequency | 1.66×10⁻⁷ | 4.34×10⁻⁵ | 4.39×10⁻⁷ | 1.17×10⁻⁵ |
| Sliding mode | 1.69×10⁻⁶ | 4.32×10⁻⁴ | 4.44×10⁻⁶ | 1.17×10⁻⁴ |
| Adaptive | 2.79×10⁻⁵ | 1.25×10⁻⁴ | 5.93×10⁻⁵ | 1.16×10⁻⁴ |
| Adaptive, d.m.c. | 2.76×10⁻⁵ | 1.24×10⁻⁴ | 5.92×10⁻⁵ | 1.15×10⁻⁴ |
The ordering is clear enough. The robust high-gain and high-frequency designs win, and they win because they are allowed to be stiff: plus a further of effective gain. Adaptive control lands about three times worse on elevation, which is the price of learning the parameters during the run rather than being handed a bound on them.
But reading that table as a ranking would be a mistake, and here is why:
The robust controllers win here partly because nothing stopped them. There is no actuator model in this simulation: no torque limit, no bandwidth, no quantization on the encoders. A gain of 110 is fine when the motor is an ideal torque source. Put a real servo in the loop with a 200 Hz update rate and a 4.3 µs closed-loop time constant, and the high-gain design is the first one to fall over, not the last.
What I took away
The Lyapunov machinery is better than I expected it to be. Not because it proves things — I knew that was the point — but because of how much it designs. I never had to guess at the adaptation law ; it is the only choice that makes the cross terms cancel. The skew-symmetry of is not a convenient lemma, it is the reason multiplying by was the right first move. Three times on this project the algebra told me what to write next, and that is a different experience from tuning a PID.
What the algebra does not tell you is which guarantee is worth having. Sliding mode has the strongest theorem on this page, and it is the one I would be least willing to put on the hardware. Exponential stability bought with a discontinuity assumes an actuator that can switch infinitely fast, and the moment you admit it cannot — via a boundary layer, or just by running at a finite sample rate — you are back to ultimate boundedness with everyone else. The theorem was never wrong. It was answering a question about a system I do not own.
The thing I did not expect to spend two days on is that the numerics have opinions too. A 4.3 µs closed-loop time constant is not something the control design warns you about; it comes from dividing a gain you chose by an inertia the mechanism happens to have, and it quietly invalidates any simulation that steps more slowly. I had a result sitting in a table for weeks that was three orders of magnitude off, and it looked entirely plausible.
As for the gimbal: the controllers are done and the hardware is not. There is no friction in this model, and friction is the dominant unmodelled effect in a small geared servo — likely larger than the parameter uncertainty I spent this whole page defending against. Adding Coulomb and viscous friction terms, then actuator dynamics with real torque limits, is where this goes next. I would expect the ranking in that table to change completely, and I would like to find out how.
References
- F. L. Lewis, D. M. Dawson and C. T. Abdallah, Robot Manipulator Control: Theory and Practice, 2nd ed. Marcel Dekker, 2004.
- J.-J. E. Slotine and W. Li, Applied Nonlinear Control. Prentice Hall, 1991.
- H. K. Khalil, Nonlinear Systems, 3rd ed. Prentice Hall, 2002.
- M. W. Spong, S. Hutchinson and M. Vidyasagar, Robot Modeling and Control. Wiley, 2005.
- V. I. Utkin, Sliding Modes in Control and Optimization. Springer, 1992.