Code Of Arms
Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.
Quick Reference
Serial Plotter Send Window Indices
Links
- Build instructions are here.
- Sketch for this lab is here.
- Matlab script for the second half of this lab here.
Task Summary
- Build, Calibrate, and Test the Arm Control System (Checkoff 1).
- Investigate the behavior of your arm when using only proportional feedback, K_p, and examine the behavior of difference estimates of the derivative as you change the derivative estimation interval m (Checkoff 2).
- Assume arm acceleration is linearly proportional to motor command (instant thrust model) and construct a state-space description for the associated proportional-feedback control system. Compute the system's natural frequencies using Eigen-analysis, and determine if they are consistent with measurements (First part of Checkoff 3).
- Experimentally investigate the impact of changing proportional feedback gains, derivative feedback gains, and derivative estimation intervals, on arm performance (rest of Checkoff 3).
- Calibrate the proportionality constant in last week's instant thrust model by comparing model natural frequencies to measured arm behavior (under proportional control). Then use our Matlab (or your own) script to compare measured to predicted step responses for combinations of proportional and derivative feedback gains (Checkoff 4).
- Calibrate \gamma_a and \beta_a coefficients for the non-instant thrust model by comparing computed and measured step responses (under PD control). Test the model's range of applicability (Checkoff 5).
- Add integral feedback to your model, and use the model to design a good PID controller. Compare with experiments, and then investigate how the integral term effects steady-state errors when you introduce disturbances. Take screen shots with and without the integral term for the POSTLAB! (Checkoff 6).
In the last lab you modeled and determined control parameters for a motor speed control system, a relatively stationary example, and in this lab you will fly! Well, maybe "fly" is a bit of an exaggeration, though there will be motion. You will build a propeller-thrust-driven rotating arm, and you will control the arm's position by using angle measurements to adjust propeller motor drive. Perhaps a few videos...
The videos below show the results using versions of the propeller arm from previous terms. You can thank the many suggestions from former students for the elegant simplicity of the version you are building this term.
In the videos above (again from earlier term arm designs), we show three propeller arm control systems. In the top video, we show what would happen if we used the same proportional feedback controller we used for speed control. In the bottom left, we show a video of using a PID controller, like the one you will be able to design by the end of this lab. And in the bottom right picture, we show you the kind of controller you will be able to design by the end of the term (after we cover state-space and observers).
As the above abstract block diagram (on the left) suggests, controlling the rotation angle requires three interconnected functions, an actuator, a sensor and a controller. Our angle sensor generates an analog voltage proportional to rotation angle, much more direct than the optical encoder sensor used for speed control. But as indicated by the above figure (on the right), our actuator is far more indirect. The controller sends commands to the motor, motor to thrust, thrust to arm acceleration, arm acceleration to arm velocity, and finally, arm velocity to arm angle. The layers of indirection make the control problem challenging, but not uncommonly so, most systems using force-based actuators are similar.
Like before, you will model your system and then use the model to help determine a good controller, though for the arm angle control, the modeling and controlling is more complicated. So, you will start with a simple proportional feedback controller, measure the arm behavior, and use those measurements to calibrate a second-order difference equation model of the arm. Then you will use your arm model to determine how the controlled-system's natural frequencies change as a function of proportional(P) and delta(D) (or derivative) feedback gains, and use that insight to design a good PD controller. Success! -ish.
It is possible to find an "okay" PD controller using the second-order arm model, a model accurate enough to predict failure for proportional-only control, but not much more. To achieve better performance, you'll need a more sophisticated controller, and to tune it, you will need a more comprehensive arm model. In particular, you will add an integral (or summation) term to your controller (the "I" in PID), and then tune the three gains (proportional, K_p, derivative, K_d, and integral, K_i) using a model that includes a non-instant relation between changes in motor command and the resulting changes in propeller thrust. And to analyze this complicated controller and model system? For that, you will need to make use of the more sophisticated analysis techniques you've just learned.
Please build the propeller-arm control system as described here. Be sure to complete the testing at the end of the assembly guide so that:
- the angle sensor axle is oriented correctly,
- the measured angle displayed by the serial plotter is zero when the arm is horizontal,
- and you've adjusted the nominal motor command so that the associated propeller thrust holds the arm level (that is, horizontal).
Demonstrate your calibrated arm holding itself level (with the low proportional gain given in the assembly guide) and that your serial plotter shows a measured angle of zero
For the second checkoff, we would like you to try some preliminary experiments with proportional feedback, to help you develop an appreciation for the differences between the arm angle and speed control problems. We strongly recommend taking "screen-shots" of the serial plotter (after pausing it) as you run these experiments, and recommend you read through the checkoff questions BEFORE YOU BEGIN. It will help you focus your effort.
Once you have set the nominal command, please use the same setup to experiment with increasing the proportional gain, K_p, using the following approach:
- Use the send window to set an new K_p (type "3 0.6" for example).
- Hold the arm (with your hand) so that the serial plotter shows the arm is about 60 milliradians above horizontal.
- Let go and be prepared to pause the plotter!
- Try again with a new K_p
You should be able to find K_p's in the range \frac{1}{4} < K_p < 2 that produce both stable and unstable oscillating behaviors (like the two figures below). Try to find one that is stable (decays to zero like the first figure below), and one that is unstable (like the second figure below).
Once you have found a pair of K_p's (one producing decaying, and the other growing, oscillations), turn on the serial plotter plots for the motor command and the derivative of the error, and redo your growing oscillation experiment. You should be able to produce a figure like the one below.
The DEdt plot shows a difference approximation to the time derivative of the arm angle error. The error is defined as the desired minus the measured angle,
where
Try increasing m (integers between 1 and 10) using index 0 (EXTRABACK) in the send window. You should be able to set m to produce a figure like the one below.
In this section, we assume that the motor command and arm acceleration are related by a scaling factor, and use that model to determine a second-order difference equation model for propeller arm system with proportional control.
The main structural components of our angle control system are blocked out below.
The connection between the structural components and the abstract control functions are detailed in the figure below.
In the above diagram, the command to the actuator (the motor command) is divided into two parts, C = C_{nom} + c. Note that C_{nom} was set to a fixed value during calibration, one that produced a thrust that exactly cancelled any torques due to gravity when \theta_a = 0 (the horizontal arm case). Therefore, as long as we consider small rotations from horizontal, we can ignore C_{nom} and gravity, and develop models that relate arm acceleration to c alone (note "little c").
Before diving into the deep end (the deep end being modeling the relation between motor command and arm acceleration), let us consider the relationship between acceleration and velocity, followed by the relationship between velocity and angle.
Just like linear acceleration, if the angular acceleration is a constant, \alpha_a[n-1], for the time between the n-1^{th} and the n^{th} sample, then the change in angular velocity, \omega_a, is given by
We can assemble the above pair of difference equations into a two-state state-space description,
As we noted above, if the c part of the motor command is non-zero, then the propeller produces a thrust that is not entirely cancelled by gravity, and that excess thrust will accelerate the arm. The simplest motor-command to arm-acceleration model is to assume linear proportionality. That is,
If we examine the description of the actuator in section 4.1, we can see that we are greatly simplifying a complicated process. And what is that complicated process? Roughly: increasing the motor command increases the duty cycle of a Teensy-generated pulse-width-modulation (PWM) signal, which increases the PWM duty cycle, which increases the average motor voltage produced by an H-bridge driver, which increases propeller speed, which increases thrust, which accelerates the arm. Whew, seems like a lot of steps to model with one proportionality constant. And in addition, we already know the relationship between motor command and propeller speed is not instantaneous, because we modeled that as part of the first lab.
So, the simplest model won't be perfect, models never are, but some are useful. Let us see if this one is. As a first step, let us use the model to eliminate \alpha_a from our state-space description,
If the goal is to use feedback to keep our propeller arm level, it would be natural for the motor command to be proportional to the negative of the distance from level. As we have seen analytically (and somewhere dramatically in the first lecture), proportional control can be unstable, but can it be stabilized? Will our model help us decide?
By proportional control (assuming the desired angle is zero), we mean
Using c[n] = K_p\left(-\theta_a[n]\right) in our state-space description,
What are A_{11}, A_{12}, A_{21}, A_{22} as functions of \Delta T, \gamma_a, and K_p? What do the eigenvalues of
Physical analogies can be misleading when first learning about feedback control, but in the case of the propeller arm with PD control, perhaps the intuitive insight is worth the risk. So, consider the above proportional control experiments on the arm. After you set a gain, you pull on the arm, then let go and then watch the arm oscillate. That sounds a lot like pulling on a spring, then letting it go, and watching it oscillate. Also, spring forces are typically proportional to displacement from equilibrium, with stiffer springs generating larger forces. Analogously, proportional feedback produces a "force" for the arm is proportional to the error in angular position, with larger values of K_p generating larger "forces". So isn't using a larger K_p like switching to a stiffer spring, don't they both result in faster oscillations?
Pull on a spring, and at least in this reality, its oscillation will not grow in time. That is, springs don't go unstable but our arm does. Nevertheless, if one sticks with the spring analogy, then one might be tempted to try "damping" the spring's oscillations by adding a friction term. Friction forces are typically proportion to velocity, the time derivative of position, so perhaps it is not so surprising that one can "damp out" the arm's oscillation by adding a feedback term that is proportional to the position error's time derivative.
From the above analogy, perhaps we can stabilize proportional control with
Try the following experiment, what does it tell you about adding "derivative" feedback?
- Set K_d = 0.1 (type "1 0.1" in the send window) and K_p = 0.5
- Hold the arm (with your hand) so that the serial plotter shows the arm is about 60 milliradians above horizontal and then let go. Be sure to note the behavior.
- Repeat the above two steps with K_p = 1.0, then 1.5, and then 2.0. What happens? What changes if you double K_d?
extraBack
help when using high values for K_d? How? EXTRABACK
from 0 to 50.
STOP HERE FOR WEEK A!!!!
PID (Proportional, Integral, Derivative) controllers are ubiquitous in feedback systems, though for discrete-time systems, it would be more accurate to refer to such controllers as PSD (Proportional, Sum, Delta). An ad-hoc approach to designing a PSD controller is to select a proportional feedback gain, K_p, and then increase the gain of the Delta (or derivative) feedback, K_d, until the step response is sufficiently stable. Finally, add Sum (or integral) feedback, with gain K_i, to eliminate any remaining steady-state error.
So if we can easily run experiments, even automate the running of those experiments, what is the value of modeling? With a model we can get far more information that just the result of a single input-output experiment. In particular, we can compute the closed-loop system's natural frequencies as a function of PID controller gains, K_p, K_d, and K_i,, which will tell us how the system will response to a whole of range of inputs and disturbances. In addition, we can even generate plots of how those natural frequencies move as a function of a parameter, a generalization of the so-called "root-locus" plot. Such plots can tell us what is feasible, like when we used a plot of natural frequency versus proportional gain above to show it was impossible to stabilize the arm with proportional gain alone. Finally, we can use the model to solve a minimax problem with respect to the magnitude of the natural frequencies. That is, we could let the computer search through millions of senarios, to find the gains that minimize the closed-loop system's maximum magnitude natural frequency. We would then be sure that resulting closed-loop system would be fast, and that any errors would decrease rapidly. But we digress. There is much to the story of data and models, some of it with connections to machine learning, and this issue will reemerge many times during the semester.
In the second half of this lab, we will focus on using our understanding of natural frequencies to calibrate progressive more complete models of a propeller arm system with PID control. We have included a state-space based matlab script (rootPlotLab02SS, linked on the top of this page) that you can use to examine initial condition responses, and create parameterized plots of natural frequencies, both to help with model calibration and controller design. We use state-space because: it is easy augment the state-space representation, its system matrix eigenvalues are the natural frequencies, and it is easy to "plug-and-chug" the state-space respresentation to simulate our system's zero-input response, which will allow us to compare model and experiment and improve our model's calibration.
We will start by calibrating \gamma_a in the model for which we assumed that thrust is proportional to motor command, using the natural frequency formula we derived last week. After demonstrating that the instant-thrust model is inaccurate, we will augment the model so that propeller thrust does not change instantly with motor command (because thrust is related to propeller speed, and from the speed control lab we know motor speed does not change instantly with changes in motor command), and calibrate the associated \gamma_a and \beta. Finally, we will make use of the third term in PID, the "integral" or sum term, but now we will have three gains to set, K_p, K_d, and K_i. Fortunately, we will have a calibrated model of the arm system, we can add the integral term to our model, use the model to determine reasonable gains, and then use those gains on the arm.
Last week, you found the smallest K_p gain for which your arm produced a growing oscillations. Redo your experiment for the K_p you found before, and then repeat it with a doubled K_p. During each experiment, be sure to pause the serial plotter quickly, so you can measure the oscillation periods (using the plotter's curser) when the growing oscillation amplitude is still small. Keep in mind that \Delta T = 0.001 seconds, and that the plotter plots every tenth sample (SO MULTIPLY THE NUMBER OF PLOTTED POINTS IN ONE PERIOD BY 10 TO GET NUMBER OF SAMPLES)!
By comparing these measured oscillation periods to the natural frequency formulas you derived last week, you should be able estimate \gamma_a for your model, as well as test its prediction (have a quick look at Week 3b lecture notes and sections 4 and 5 of the
oscillating prelab, or ask for help). You can also compare your responses to simulation produced by our matlab script (you can use the on-line version of matlab, every MIT student has an automatic account, see
here. Using the script is summarized in the figure below (note that zoom
is on line fourteen of the script).
The matlab script implements the simple motor command to acceleration model, the same one you calibrated above using oscillation periods of the destabilized arm. Note that we have implemented a zoom feature (line 14), and show the plots of natural frequency trajectories with and without the zoom set to true. Note that because all our natural frequencies are extremely close to one, the zoomed natural frequency plot is much more useful.
Please keep in mind that you will have to scale your experimental results when comparing to matlab ZIR responses. And note that you can also use the matlab script to analyze the case with PD control with your calibrated value for \gamma_a as shown in the figure below.
Try the following experiment, and compare the results to the matlab simulations above (remember that linearity implies that the step responses below have the same form as the ZIR simulated above).
- Set K_d = 0.1 (type "1 0.1" in the send window) and K_p = 0.5
- Set the step frequency to 0.2 (type "5 0.2" in send window), and then set the step amplitude to 0.05 (type "6 0.05"). Check that the arm moves from just a little below level to a little above.
- Gradually increase K_p, to K_p = 1.0, and then 1.5, and then 2.0. Does the arm go unstable? Are the natural frequencies computed (and ZIR responses) computed by the matlab script consistent with that instability?
Be prepared to describe how you determined \gamma_a from your measured oscillation period and your formula for natural frequency. How well does your calibrated model predict the step responses for PD control. Try a few sets of K_p and K_d values, does your calibrated model correctly predict the stability of your arm. For gains that produce a barely stable step response (very oscillatory), turn on the command plot (CMD) in the plotter and adjust the number of steps back used in the derivative estimation, m, from one to ten (e.g. type "0 2" in the send window to set m to three). Keep the value of m that minimizes the CMD noise WITHOUT worsening stability. Set that m to be the default by changing the zero on line 33 in the arduino sketch
Examine the state-space model's 3 \times 3 \;\; \bf{A} matrix on lines 38, 39 and 40 in the matlab script. Based on the elements of \bf{A}, what are states one, two and three of this model? Use the matlab script to compute natural frequency trajectories as K_p varies from zero to two when K_d=0.1, and as K_d varies from zero to 0.5 when K_p=2. Do you see how to use these trajectory plots to help you design a good controller?
#define offsetExtraBack 0
to your optimized value of m.
Propeller thrust produces arm acceleration, and thrust tracks propeller speed instantly but nonlinearly, being approximately proportional to the square of the instantaneous propeller speed. Propeller speed tracks motor command, but not instantly, and somewhat nonlinearly. Together these observations might suggest we need a very non-linear and non-instant model relating motor command to arm acceleration, but that is not the case. The motor command to arm acceleration is suprisingly linear (fortuitous cancellations no doubt), though definitely NOT instant.
Above we used oscillation period measurements versus proportional control gain (K_p) to calibrate \gamma_a in the following relationship between motor command and arm acceleration,
If c[n] = c[\infty], a constant, and -\frac{1}{\Delta T} < \beta < 0 , then what is \lim_{n \rightarrow \infty} \alpha_a[n]?
Implement the above non-instant model in the matlab script (look for lines in the script labeled *FIX THIS, lines 42,43 and 45) and calibrate the values of \gamma_a and \beta by matching the simulated ZIR responses to measured steps on the arm for various values of K_p and K_d. Keep in mind that the matlab model is linear, so keep your step sizes small (e.g. "6 0.05"), Also, it is easier to match waveforms that are highly oscillatory waverforms.
If you modified the matlab script to include the non-Instant model, then you should be able to produce a plot like the one below.
We can add an integral, or more precisely, a sum feedback term to our controller. The advantage of including an integral term is that any persistent error, no matter how small, will cause the sum to grow steadily. When the sum term is large enough, it will cause an adjustment to the motor command, persumably an adjustment that eliminates the small error.
The motor command with the proportional, delta, and sum feedback can be described by the difference equations
In this section you will analyze our new closed loop system and conduct experiments to understand the model's predictive power.
- Add the integral term to the matlab model for the controller, Ki (line 52, labeled "fix this").
- Use your complete matlab model to design the gains of your controller. What are you going to try to minimize? Note: There are practical tradeoffs to make here. The higher the gains, the more the noise in measurement will effect the output. The lower the proportional gain (and associated K_d), the slower the response and greater the error. This choice is an open ended question. There is no 'correct' answer, but rather, many answers can be justified. Use computation and your model!!
- How is the rejection of transient and steady-state disturbances affected by including the integral (aka sum) term? To see the advantage of using the integral term, try dropping a Lego "U" on the arm as shown in the picture below.