12. PID Control

12.1. Purpose

This activity guides students through tuning of PID control parameters in order to adjust system response (e.g., rise time, overshoot, etc.) to desired values.

12.2. Hardware and Tools

  • Not Applicable

12.3. Description

In Laboratory 3 a simplistic motor control was added cause the motor to drive at a desired speed. While the method used works relatively well with fixed speeds, desired speed changes would be slow to take effect. To this end, this activity uses a simulation to hopefully provide fast and intuitive feedback on the effects of adding PID control to the wheel control.

A quick overview of PID control components:

12.3.1. Proportional Control

The “P” portion of PID control uses the current system error to calculate a control signal. Suppose that a system is desired to have output \(x_D(t)\) at time \(t\) but currently has output \(x_A(t)\). The error between these two values is the current, or instantaneous, error: \(e_P(t) = x_D(t)-x_A(t)\). The P control produces a control signal (\(y_P(t)\), e.g., PWM Duty Cycle) that is proportional to the error:

\[y_P(t) = k_P e_P(t)\]

where \(k_P\) is the proportional gain constant. From this equation: increasing \(k_P\) will cause a stronger control signal; likewise causing the system to react more strongly to instantaneous errors.

This PID control component is used primarily to cause the system to react quickly to a changing desired output, \(x_D(t)\), but may cause the system to behave poorly if too high; such as to cause or make worse response oscillations, drive the system unstable, and/or cause the system to inappropriately react to noisy conditions.

12.3.2. Integral Control

The “I” portion of PID control uses the history of the system error to calculate a control signal. In a continuous-time system, the history may be calculated as the integral of the instantaneous error, \(e_P(t)\), over time:

\[e_I(t) = \int_0^\tau e_P(\tau) \mathrm{d}\tau\]

where \(e_I(t)\) is the integral error, or the history of the instantaneous error at time \(t\). Multiplying this error by the integral gain constant, \(k_I\) produces the I component of the control signal:

\[y_I(t) = k_I e_I(t) = k_I \int_0^\tau e_P(\tau) \mathrm{d}\tau\]

As \(e_I(t)\) is the integration of the error over time, persistent errors in the system response (e.g., steady state errors) will cause \(e_I(t)\) to grow large. Therefore, the integral control component will be more impactful with a persistent error.

This PID control component is used primarily to cause the system to remove steady state errors (a persistent error) but may cause the system to behave poorly is the gain is too high; such as indirectly causing oscillations.

12.3.3. Derivative Control

The “D” portion of PID control uses the derivative of the system error to calculate a control signal:

\[e_D(t) = \frac{\mathrm{d}}{\mathrm{d}t} e_P(t) = \frac{\mathrm{d}}{\mathrm{d}t}x_D(t) - \frac{\mathrm{d}}{\mathrm{d}t}x_A(t)\]

where \(e_D(t)\) is the derivative error. This is effectively the rate-of-change of the instantaneous error. For example, if considering the desired output as a constant position, then the derivative error is effectively the negative of the system velocity. Multiplying this error by the derivative gain constant gives the control signal:

\[y_D(t) = k_D e_D(t) = k_D \frac{\mathrm{d}}{\mathrm{d}t} e_P(t)\]

As \(e_D(t)\) is the derivative of the error (or possibly the “velocity” of system output if the desired output is constant), \(y_D(t)\) will be large if the system output is changing rapidly (assuming desired output is near constant, again). This effectively adds artificial “damping” to the system; where the derivative control acts to slow the system.


Combining all of the PID components gives the continuous time control equation:

\[y(t) = y_P(t)+y_I(t)+y_D(t) = k_p e_p(t) + k_i \int_0^\tau e_P(\tau) \mathrm{d}\tau + k_D \frac{\mathrm{d}}{\mathrm{d}t} e_P(t)\]

To use in a discrete-time system, where the equation is evaluated periodically instead of in a continuous fashion, both the derivative and integral terms must be approximated. This may be done via several different methods, with a simple approach being:

\[y(k) = k_p e_p(k) + k_i \sum_{n=0}^k e_P(n) \Delta t + k_D \frac{e_P(k) - e_P(k-1)}{\Delta t}\]

where the time, \(t\), is replaced by the sample number, \(k\), which is essentially the iteration number of the control loop (starting at 0). Each successive sample, \(k\), or iteration takes place after a fixed time step, \(\Delta t\), from the previous sample, \(k-1\). For example: the system control loop for Activity 10 runs at approximately 100 Hz, or with \(\Delta t = 10~\mathrm{ms}\). The sample taken at \(t = 10~\mathrm{ms}\) would be sample \(k=1\) and the sample taken at \(t = 20~\mathrm{ms}\) would be sample \(k=2\). For most practical applications, \(\Delta t\) is a fixed size and is therefore constant; allowing it to be lumped into the gain constants as well and removed from the equation:

\[y(k) = k_p e_p(k) + k_i^\prime \sum_{n=0}^k e_P(n) + k_D^\prime \left(e_P(k) - e_P(k-1)\right)\]

12.4. Instructions

This activity DOES NOT use the RSLK or Launchpad Board. Instead, we will use a web based simulation tool: EmCon Car PID Simulator. This is a javascript based tool: browser extensions such as noscript might break it!

This simulator has two physics modes, both of which have control routines to control the independent wheel speed only with no consideration of travel direction. The two physics modes may be selected by checking/unchecking the Frictionless Motors box:

  • Unchecked: The simulation will ROUGHLY emulate the car’s actual behavior, where a PWM Duty Cycle corresponds approximately to a wheel speed. Only an integral control scheme (I) is implemented. This same I control scheme will be implemented in Lab 4.

  • Checked: The car’s wheels are assumed frictionless, with the PWM Duty Cycle controlling the motor force generated. This force is proportional to acceleration, not speed (F=ma). Forcing is slow to ramp up to the desired value the accentuate control issues for learning purposes. This is not realistic to the actual car’s behavior! A full PID control is implemented in this mode.

This activity will use both of these modes. Warning: both modes have a random error applied to the wheels. This error changes each time the simulation is run!

The simulation produces three plots (top to bottom):

  1. A plot of the applied PWM duty cycle to each wheel,

  2. A plot of the control components from the PID control which comprise the applied PWM duty cycle to each wheel (i.e, \(y(k)\) is the duty cycle). Note that after the desired duty cycle is calculated, the value is capped between -100% and 100%.

  3. A plot of the resulting wheel speeds.

For all questions asked: record your answers on paper or a computer document for submission.

All questions asking for measurements of rise time, overshoot, etc. are with respect to the WHEEL SPEED. You may estimate these by considering one wheel only for your answers (e.g., left).

12.4.1. Part 1: Frictionless Motors: Unchecked

  1. For the default configuration of the simulation, what is the rise time, \(t_r\), settling time and \(t_s\)? Note that steady state error changes each run. Refer to the lecture slides for definitions of \(t_r\) and \(t_s\) (among other quantities that will be asked about). The default configuration is:

    • Frictionless Motors: Unchecked

    • \(k_I = 0\)

    • Desired Speed: 50 mm/s

  2. Change the Desired Speed to 100 mm/s and repeat the previous step. Afterwards, set Desired Speed back to 50 mm/s.

  3. Considering the resulting system responses, how would you characterize the system? As 0th, 1st, or 2nd order?

  4. The system currently has no control other than the PWM being set to a value that, without the noted wheel errors, would produce the desired speed. These wheel errors manifest as steady state errors in the response. Integral Control may be used to remove these errors. Add integral control by setting the integral gain constant to a small value: \(k_i = 0.001\) and run the simulation. You will note that very little changes. Increase \(k_i\) until the steady state error is consistently removed within 3 seconds. What value did works well for this?

  5. Now increase \(k_i\) further to see the detrimental effects on the system response: namely, a large \(k_i\) will work but will cause significant oscillations. Increase \(k_i\) until the car no longer stabilizes. Again, report your \(k_i\) value.

For Lab 4, a similar process will be used to find a reasonable value for \(k_i\); though omitting step 5 (and possibly step 4).

12.4.2. Part 2: Frictionless Motors: Checked

  1. [Re]set the default configuration but with frictionless motors mode activated:

    • Frictionless Motors: Checked

    • \(k_P = 1\)

    • \(k_I = 0\)

    • \(k_D = 0\)

    • Desired Speed: 50 mm/s

  2. Run this default configuration and estimate \(t_r\), \(t_s\), percent overshoot, and oscillation frequency.

    • You may rnbeed to extend the simulation time to properly estimate \(t_s\).

    • Note that percent overshoot is defined as:

      \[\mathrm{\%~Overshoot} = \frac{\mathrm{peak~value}-\mathrm{steady~state~value}}{\mathrm{steady~state~value}} \times 100 \%\]
  3. It should be obvious that this is a 2nd order response. What type of 2nd order response is this (refer to lecture)?

  4. As seen, the system response is very slow. Increase \(k_P\) until \(t_r \approx 0.5~\mathrm{s}\). Report \(k_P\) value along with the other system parameters requested from the previous step. What parameters got worse, which got better? Your value for \(k_P\) should be a value less than 10. Any value larger than that is much too large!.

    • The Simulation Time may be changed to make it easier to measure smaller \(t_r\) values. SloMo Simulation may also be used to increase the time resolution.

    • Note that as the motor forcing is limited to 100 % duty cycle, raising the \(k_P\) past the value that you found will likely not resulting in significant changes to the system response.

  5. Oscillations should clearly be visible with the modified \(k_P\), causing the settling time, \(t_s\), to be long. Add in derivative control by setting \(k_D = 1\) and observing the result. Increase \(k_D\) until \(t_s \approx 3~\mathrm{s}\). Report your gain value. For the gain value you reported, did you still see overshoot? If so, what was it?

  • If you did still see overshoot, further increase \(k_D\) until no overshoot exists and the \(t_s\) requirement is still met. What is your new value?

  1. Take the \(k_D\) found from the previous step and multiply it by 10. What happened to the response and how would you characterize it now?

  2. Revert your \(k_D\) back to a good value (divide by 10). Use \(k_I\) to remove the steady state errors; following the process from part 1. Report your final value.

  3. Take a screenshot of your final simulation window with response shown and add to your answers.

  4. Submit your answers to Gradescope.