Lab 2: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
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 difference interval m (Checkoff 2).
- Investigate, experimentally, relationships between proportional and derivative feedback gains and arm performance. Pay particular attention to determining when step responses show the impact of nonlinearity.
- Use a combination of proprotional and derivative feedback (by setting non-zero values of K_p and K_d ) to stabilize the arm, and briefly investigate the impact of these gains on disturbance rejection (by dropping a load onto the arm (checkoff 3).
- REPLACE THE ARM-TO-ANGLE CONNECTOR, SEE UPDATED ASSSEMBLY GUIDE!
- Relate difference equation natural frequencies to measured arm behavior, and use the relation to estimate \gamma_a (Checkoff 4).
- Extract \gamma_a and \beta coefficients from the measured dropped arm response of a PD controller by comparing to a matlab model with non-instant 1st-order difference equation model of the command-to-angular-acceleration relation.
- 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 the steady-state error when you introduce disturbances (checkoff 6). Take screen shots with and without the integral term for the POSTLAB!
Introduction
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...
Arm Wrestling
In the videos above, 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).
The Key Challenge


As the abstract block diagram (left panel above) suggests, controlling the rotation angle requires three interconnected functions, an actuator, a sensor and a controller. The angle sensor generates an analog voltage proportional to rotation angle, which is much more direct than the optical encoder sensor used for speed control. But the actuator is more complicated (right panel above). The controller sends commands to the motor and the resulting motor speed generates thrust on the rotating arm. The thrust accelerates the arm. The acceleration induces a change in arm velocity. And the arm velocity changes the arm angle. The layers of indirection make the control problem challenging, but not uncommonly so, most systems using force-based actuators are similar.
Control Design Approach
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.
Assemble and Test
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. Use the low proportional gain described in the assembly guide to demonstrate the arm holding itself level. Try blowing on the arm. How well does the arm hold its horizontal position? Set K_p = 0 and adjust the arm position by turning the potentiometer (the same one you adjusted in lab1) to adjust the nominal motor command (and therefore the propeller thrust). Try to adjust the potentiometer so that the arm is rotated to approximately -0.75 radians (-45 degrees, so BELOW level).
How well does the arm hold this position? Now adjust the potentiometer so that the arm is approximately +0.75 radians (+45 degrees, so ABOVE level). How well does the arm hold this position? What fraction of the gravity force on the arm acts to rotate the arm in each case? What is different about the two arm position? Hint: think about how the gravity force changes when you perturbn the arm angle away from +0.75 radians versus how that force changes when you perturb away from -0.75 radians.
Experiment with Proportional Gain
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.
Determine how the response of the system changes with K_p by using the following procedure:
- Use the send window to set 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.
- Be prepared to stop the plotter -- then let go of the arm!
- Stop the plotter, then stop the arm by grabbing the end of the chop stick or by pulling the large power connector.
- Repeat 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). Take screen shots for these two behaviors. You will need them for the checkoff.


Examining Cmd and DEdt/10
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.

Show the staff member your screen shots for the largest value of K_p for which your system was stable (the oscillations decayed). Show the staff member your screen shots for the smallest value of K_p for which your system was unstable. What is the relation between the angle, command, and DEdt signals? Do the peaks in these waveforms line up? How do the waveforms change as m gets bigger? Note that on line 19 in the sketch,
#define offsetExtraBack 2
sets an offset of two for EXTRABACK. When you type "0 2" in the send window, you are setting EXTRABACK to four, and therefore setting m=5 . If you want to set m = 1 , type "0 -2" in the send window.
Disturbing Observations
As we saw in lecture this past week, adding derivative feedback helped stabilize the path following robot, so for this last checkoff of part A, let us try stabilizing the arm by adding derivative feedback.
Try the following after reuploading the sketch:
- Use the send window to set extraback = 5 (type "0 3" in the send window, to add 3 to offsetExtraBack, which is 2), K_d = 0.2 (type "1 0.2" in the send window) and K_p = 0.5 (type "3 0.5" in the send window). The arm should be level, if not, adjust the potentiometer to level it.
- Set the step frequency to 0.1 (type "5 0.1" in send window), and then set the step amplitude to 0.2 (type "6 0.2").
- Note the step response, and then try setting K_p = 0.75, then K_p = 1.0, 1.25, 1.5, ... Note how the step response changes with increasing K_p. What is the largest K_p you can use before the system becomes unstable?
- Set K_p = 1.0 and set the input amplitude negative (type "6 -0.1"). What is different? We will make use of sine waves later.
Now try setting the step amplitude to 0.35, K_d=0.02, K_p=0.3, and increase K_p until your falling step response looks like the plot below (your K_p will be somewhere between 0.4 to 0.8). What do you observe about the difference between the rising and falling step responses (You will probably need some staff help to answer this one!)?

Now lets try disturbing the arm, by dropping a Lego "U" on the arm as shown in the picture below.
- Set K_d = 0.2, K_p = 0.5, and the step amplitude to zero (type "6 0.0" in the send window).
- Try dropping the Lego "U" on your arm and note how the arm angle changes.
- Try setting K_p = 0.75, redrop the "U", and note how much the arm angle changes. Repeat with K_p = 1.0, 1.25, 1.5.

Show your experiments to a staff member. What do you observe about the arm step response as you increase K_p? For the large step, barely stable experiment, what did you observe about the difference between the rising and falling step responses? Can you explain it? (You will probably need some staff help to answer this one!) What do you observe about the disturbance response as you increase K_p?
STOP HERE FOR WEEK A!!!! NOTE THAT PART B, BELOW, IS BEING REVISED!!
REPLACE THE ARM-TO-ANGLE CONNECTOR, SEE UPDATED ASSSEMBLY GUIDE!

Simple Model, Simple Control
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 Structural and Functional Blocks
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").
From angular acceleration to angle
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,
A simple model relating motor command to arm acceleration
As we noted above, if the c part of the motor command is non-zero, then the propeller will produce a thrust that is not entirely cancelled by gravity, and that excess thrust will accelerate the arm. So perhaps we can model the relationship between c and \alpha_a as
If we examine the description of the actuator in section 4.1, we can see that we are using a scalar to model a complicated process. That process starts with a change in motor command that gets converted to a change in a motor-driving H-bridge-buffered PWM signal, which in turn changes the speed of a thrust-generating propeller, resulting in a change in arm acceleration. Seems like a lot to expect from one number. And in addition, we already know that there is a non-instant relation between motor command and propeller speed, 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,
Proportional Control
By proportional control, we mean that
Assuming the desired angle is zero (a level arm), c[n] = K_p\left(-\theta_a[n]\right) in our state-space description,
Checkoff 4
Determine an analytic formula for the natural frequencies of the 2^{nd}-order difference equation describing the proportional feedback system above, as a function of \gamma_a and K_p, and then plot them as a root locus in the complex plane. What can you say about the plot, and the stability of the system, without knowing precise values for \gamma_a and K_p?
In Checkoff 2, you found the smallest K_p gain for which your arm produced a growing oscillations when started with a 60 milliradian offset from level. Redo the experiment to find the smallest K_p that produces growing oscillations, but use a starting offset of 150 milliradians, and then repeat your experiment using twice that K_p (we will refer to these gains as K_{p0} and 2K_{p0}). You will likely find that using a larger initial offset gives you a smaller K_{p0} than last week, so when you use 2K_{p0}, the arm should not be too unstable. 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 there are one hundred plotted points per second).
By comparing your measured oscillation periods to your natural frequency formulas, you should be able estimate \gamma_a for your model, as well as check its consistency. It may be useful to review sections 4 and 5 of oscillating prelab, and take note of the approximation for the angle of a complex number z = a + jb when a = 1 and |b| << 1. That is,
Show your formula for the natural frequencies. What do they tell you about the stability of the proportionally controlled system? Show the plots of the propeller arm under proportional control for K_p = K_{p0} and K_p = 2K_{p0}. Determine the two periods of the oscillation, and their period ratio. Is the ratio consistent with your model? You can estimate \gamma_a based on the oscillation period when K_p=K_{p0}, and test your estimate by using it predict the oscillation period when K_p = 2K_{p0}. Or you can estimate \gamma_a based on the oscillation period when K_p=2K_{p0}, and test your estimate by using it predict the oscillation period when K_p = K_{p0}. Do you think averaging your estimates would be better? If so, how would you test that?
Better Controller, Better Model.
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.
We can add a feedback term that is proportional an approximation of the derivative (the difference in a pair of angle measurements, i.e. the n^{th} minus the (n-m)^{th} sample, divided by the time between measurements, i.e. (m \Delta T). We have already seen experimentally that adding derivative feedback stabilizes the arm (when we made K_d nonzero), but there are other consequences. Arm angle derivatives typically change more rapidly than arm angle, so motor commands produced by proportional plus derivative (PD) controllers change more rapidly, rapidly enough to invalidate our model which assumed arm acceleration changes instantly with motor command. If we want to model PD control, we will need a more accurate model.
For a 2 \times 2 matrix, eigenvalues are easily computed by hand, but a system that models both derivative feedback AND a non-instant command to acceleration model will be larger, and its eigenvalues too time-consuming to compute by hand. So we've included a matlab script to do the computation, which we describe below.
PD Difference equation
We define the error in angular position for the arm as
For the case m=1, we can write the vector difference equation as
The matrix in the above system (the m=1 case) is 3 \times 3, so it has three eigenvalues, and therefore there are three natural frequencies. Suppose and Suppose m = 5, how many natural frequencies would there be?
A Computational Tool
For the rest of this lab, we will focus on using our understanding of natural frequencies to calibrate progressively more complete models of a propeller arm system with PID control. We have included a vector difference-equation based matlab script (rootPlotLab02SS, linked on the top of this page) that you can use to examine initial condition responses and parameterized plots of natural frequencies. The former is most helpful when calibrating the model and the latter more helpful during controller design.
In the above check-off, you estimated \gamma_a for your model, as well as tested its prediction. You can also compare your responses to simulation produced by the matlab script (you can use the on-line version of matlab, every MIT student has an automatic account, see here).
The matlab script can analyze the propeller arm system using P or PD control, assuming arm acceleration is proportional to motor command, the same one you just calibrated using oscillation periods of the destabilized arm. Note that we have implemented a zoom feature (line 14 matlab script), and in the first example below we show the matlab-generated plots for a proportional control case 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.
You can use the matlab script, unmodified, to analyze your arm with either P or PD control, though you have to provide values for \gamma_a and \beta (we describe \beta below, for now assume \beta = 0). For example, below we show a NOT REALISTIC example in which \gamma = 10, \beta=0 and K_p varies from 0.1 to 5.0.

Below we show a PD example, again for unrealistic \gamma = 10, and \beta=0.

Please keep in mind that you will have to scale your experimental results when comparing step responses to matlab ZIR responses (The matlab script assumes the initial arm angle is one, and assumes all other states are initially zero).
Non-Instant Motor Command to Arm Acceleration Model
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 assuming the relationship between motor command and arm acceleration was given by
In the equations for the model of the previous section, arm acceleration does not appear explicitly as a variable because we used the instant model and some algebra to eliminate it. But if we want to model the fact that arm angular acceleration does not instantly track motor command, we need to introduce it as a state variable, and describe its evolution with an associated difference equation. One possible first-order model of arm angular acceleration is
In the above equation, if the motor command is a constant, i.e. c[n] = c[\infty], and -\frac{1}{\Delta T} < \beta < 0 , then what is \lim_{n \rightarrow \infty} \alpha_a[n]? And if the the motor command is NOT a constant, but \beta = -\frac{1}{\Delta T}, what is the relationship between \alpha_a[n] and c[n].
For the case m=1, we can write the vector difference equation for a system that includes the non-instant model, as in
Assuming that \theta_d[n] = 0 for all n, so the motor command is given by
beta
for \beta, dT
for \Delta T, (note that dT has a CAPITAL T), G
for \gamma, Kp
for K_p and Kd
for K_d.
Implementing and Calibrating the Non-instant model.
Implement the above non-instant model in the matlab script (look for lines in the script labeled *FIX THIS, lines 42,43,44, and 45). If you correctly modified the matlab script to include the non-Instant model, then you should be able to produce a plot like the one below (try this example to test your matlab, it should match!)

Once you have implemented the non-instant model, you will use it to calibrate the values of \gamma_a and \beta by matching the simulated zero input responses (ZIR) to measurements of the arm for various values of K_p and K_d (we have some suggestions below). Before you begin that process, try double \gamma_a a few times in the above example, then reset \gamma_a and try doubling \beta a few times.
- How does the ZIR change with changing \gamma_a.
- How does the ZIR change with changing \beta.
We suggest trying (and taking screenshots of) the following experiments on your propeller arm, and then running matlab simulations for various values of \gamma_a and \beta until you can match simulation and experiment. To generate a ZIR experimentally, lift one end of the arm (that is, rotate the arm upwards 200 milliradians or so), and then let go.
- Set K_d = 0.1 (type "1 0.1" in the send window) and K_p = 0.5
- Hold the arm approximate 0.2 radians above horizontal and then let go. Halt the plotter (be quick, so you capture the initial response) and record the result (take a screenshot).
- Gradually increase K_p, to K_p = 1.0, and then 1.5, and then 2.0, and retry your lift-and-let-go arm experiment. Are the natural frequencies and ZIR responses computed by the matlab script consistent with your experiments? If not, adjust \gamma_aand \beta and try again.
A good strategy for finding \gamma_a and \beta is to start with the \gamma_a you found when just using proportional control, and the try values for \beta in the range [-200,-1].
Describe your estimates of \gamma_a and \beta. Show us your changes to the model, and explain how they implement the "non-instant" motor model. What properties of the ZIR are most affected by changes in \gamma? What properties are most affected by \beta? Show us the ZIR responses that you measured and the corresponding responses from the model. How similar are the measured and modeled oscillation frequencies and the rate of oscillation amplitude decay (or growth if your model or experiment is unstable).
Better Controls, Models, and Manipulations
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 poles (another name for 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 poles 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 poles. 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 pole. We would then be sure that resulting closed-loop system would be fast, and that any errors would decrease rapidly. 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.
But we digress. 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
If you are paying very very very close attention, and compared how the error sum is formed and used on lines 130 through 144 in the Teensy sketch, you might notice an inconsistency between what is in the sketch and sum equation in the matlab model. We will examine this issue in the postlab.
Implementing and Testing the PID Controller.
In this section you will analyze our new closed loop system and conduct experiments to understand the model's predictive power. Start by adding the integral term to the matlab model for the controller, Ki (line 52, labeled "fix this"). You can check your formula for A45 below (use Ki for K_i.
Your matlab implementation should produce plots like the ones below when using the command in the red box at the bottom of the figure.

Try the following experiments:
- Compare the matlab model response to your arm's response when K_i > 0. You can not compare to matlab using the lift-arm-and-drop approach, when there is an integral, or more precisely, an error sum term. Once you lift up the arm, the error sum starts growing and will be very large before you let go of the arm. In the matlab simulation, the initial condition for the error sum term is zero. A better approach is to monitor the response of your system to input step changes, and exploit the linearity of the system. To do this, set the input's amplitude to 0.1 (type 6 0.1 in the send window) and its period to 0.1 (5 0.1).
- Once you are satisfied that you have a reasonable match between your arm and the matlab simulation, 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, particularly the K_d gain, the more noise in measurement will effect the output. The lower the proportional gain, and the higher that derivative gain, the slower the step response and the greater the error. 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.
Checkoff 6:
-
Demonstrate disturbance rejection as follows. Find values for
Kp
andKd
(withKi=0
) so that the measured step response converges within a second. Then increaseKi
to decrease the disturbance caused by dropping the LEGO U on the propeller arm. What values ofKp
,Kd
,Ki
work best? -
Use your matlab model to find values for K_p and K_d that yield good step responses when K_i = 10.
-
Compare measured results with and without the integral (sum) term.
-
In this week's postlab, we will develop a model for the system's responses to disturbances. Measure (and take screenshots of) the result of dropping the LEGO U on the propeller arm for
Ki
for use in your postlab. Be sure to try gains for which the response to dropping the Lego U results in oscillations.
-