Skip to content
Tutorials Center
My learning

Topics

Lesson 35 of 36 ยท Projects and Practice

Project: DC Motor Position Control from Model to Code

Model a DC motor, linearize it, design cascaded PID and LQR position controllers, add friction and saturation, test against requirements and prepare fixed-point code. Python stand-ins included.

A DC motor with a gearbox positions a robot joint, a camera pan or a valve. The task is classic: move to an angle quickly, without overshoot, against friction and a load disturbance, within the voltage limit. This project follows the whole Simulink workflow: physical model, linear analysis, controller design, nonlinear verification, tests and code generation. Simulink and its toolboxes do not run here, so Python stand-ins (our own code) let you check the numbers; build the same thing in Simulink using the steps.

Requirements

  1. REQ-1: Step of 90 degrees reaches within 2 percent in less than 0.5 s with less than 5 percent overshoot.
  2. REQ-2: The motor voltage shall stay within +/-12 V.
  3. REQ-3: A constant load torque of 0.1 N m shall give a steady-state error below 0.5 degrees.
  4. REQ-4: The controller shall run at 1 kHz on a microcontroller without a floating-point unit.

The motor model

Electrical and mechanical equations:

TEXT
L di/dt = V - R i - Ke w
J dw/dt = Kt i - b w - Tload - Tfriction(w)
dtheta/dt = w

with armature resistance R, inductance L, back-EMF constant Ke, torque constant Kt (equal in SI units), inertia J, viscous friction b. Typical small motor values: R = 1.0 ohm, L = 0.5 mH, Ke = Kt = 0.05, J = 2e-5 kg m^2, b = 1e-5.

In Simulink build it from basic blocks: Integrators for i, w, theta, Gains for constants, Sum blocks; or use Simscape Electrical blocks (DC Motor, H-Bridge) if available. A third option is the DC Motor block from Simscape. Create a subsystem Motor with input V and Tload, outputs theta, w, i. Add friction with a Coulomb and Viscous Friction block for later.

Python Stand-in: motor response to a 6 V step

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The speed climbs to about V/Ke minus a small drop (the no-load speed is 120 rad/s for 6 V) in a few hundredths of a second. The current spike at the start (6 V across 1 ohm is 6 A) is why the voltage limit and the current limit matter in practice.

Step 1: reduce and linearize

Because L/R (0.5 ms) is much smaller than the mechanical time constant, many designs ignore the inductance and use the second-order model theta/V = Kt / (s (J R s + Kt Ke + b R)). In Simulink: put Linear Analysis Points at the controller output (input perturbation) and at theta (output measurement), run the Model Linearizer and compare Bode plots of the full model and the reduced one (linearization lesson). If the plots agree up to the closed-loop bandwidth, the reduced model is good enough for design.

Step 2: position loop with a PD or cascaded PID

A common structure is cascaded loops: an inner speed (or current) loop and an outer position loop. Cascade design:

  1. Tune the inner loop to be fast (say 100 rad/s bandwidth) with a PI on speed.
  2. Tune the outer P (or PD) on position with a bandwidth about five times lower than the inner loop.

Using the PID Tuner twice (once per loop, tuning the inner loop first, then the outer loop with the inner loop closed). Add saturation +/-12 V and anti-windup on the inner loop (PID lesson).

Python Stand-in: cascaded position/speed controller with a 12 V limit

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Compare the printed overshoot, time and final value with REQ-1 to REQ-3. You will probably need to adjust the gains: this is the tuning loop that the PID Tuner automates on the linearized model. Raise kp_pos for speed, ki_w for load rejection and check the voltage limit.

Step 3: LQR alternative

For the reduced model, the state is [theta, w] (plus the integral of position error for load rejection). Use lqi or augment with the integral state and lqr (LQR lesson). In Simulink: a State-Space block for the model, a Gain block with -K, an Integrator for the position error. Compare the voltage use and response with the cascaded PID: LQR gives smooth voltage; the PID is easier to tune on hardware and to explain.

Step 4: friction, saturation and nonlinear verification

Add to the full model: Coulomb friction (a few mN m), a backlash block in the gearbox, a Saturation on voltage, quantization of the encoder (counts per revolution through a Quantizer block) and the 1 kHz zero-order hold with a computation delay (a Unit Delay). Re-run the step test. Typical findings: a small steady-state error or limit cycle from friction and backlash (the integral wind-up hunting), noise on the speed estimate from the quantized encoder. Possible fixes: filter the speed estimate, add a small dead zone or reduce ki.

Step 5: tests and requirements

  • Test harness for the controller with a step of 90 degrees and a Test Sequence applying the 0.1 N m load at 0.6 s; assessments for overshoot (max(theta) < 94.5), settling time (after(0.5, sec) -> abs(theta - 90) < 1.8), voltage (abs(V) <= 12) and load error.
  • Parameter iterations: motor parameters +/-20 percent, load 0 to 0.15 N m, battery voltage 10 to 14 V. This is a robustness sweep in the Test Manager.
  • Model coverage for the controller subsystem; Design Verifier to check that no overflow occurs in the fixed-point version.

Step 6: fixed-point and code

REQ-4 says no floating-point unit. Steps (fixed-point lesson):

  1. Run range collection over all test cases.
  2. Use fixdt(1,32,16) for the integrator and position error, fixdt(1,16,8) for speed.
  3. Set rounding to Nearest for filters and Floor where cheaper, and saturate on overflow.
  4. Compare fixed-point against floating-point results (tolerance 0.1 degree).
  5. Generate code with ert.tlc, base rate 1 ms, and verify with SIL and PIL (deployment lesson).

The stand-in below estimates the quantization effect of a fixed-point speed gain.

Python Stand-in: effect of fixed-point gain resolution on steady-state error

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With a gain this small, 4 fractional bits would turn 0.05 into 0.0625 (25 percent error), so you give small gains more fractional bits or scale the signals so that the gains are near 1.

Extensions

  1. Trajectory generation: a trapezoidal velocity profile (a Stateflow chart plus an integrator) instead of a raw step.
  2. Disturbance observer for load estimation and feedforward.
  3. Sensorless velocity with an observer from current and voltage.
  4. Hardware: deploy to an STM32 or Arduino with a support package, drive an H-bridge with PWM and compare real and simulated step responses; use External mode for tuning.
  5. Two-axis robot arm with coupled dynamics (Simscape Multibody).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why tune the inner loop before the outer loop in a cascade?

    The outer loop design assumes a well-behaved inner loop.
  2. 2. What limits the voltage in the design?

    REQ-2 needs an output limit, and anti-windup keeps the integrator sane when it is active.
  3. 3. Which effects can cause a limit cycle near the target in the nonlinear model?

    Stick-slip and backlash interact with the integrator and can make the position hunt.
  4. 4. Why give small gains more fraction bits in fixed point?

    The relative quantization error of a gain grows as the gain gets smaller compared with the resolution.

Summary

  • Model the motor with its electrical and mechanical equations, reduce it for design, and confirm the reduction on the Bode plot.
  • Use a cascaded loop (speed inside, position outside) or LQR with integral action; add saturation and anti-windup.
  • Verify on the nonlinear model with friction, backlash, quantization and delay; sweep parameters in the Test Manager.
  • For an FPU-less target pick fixed-point types from collected ranges, compare with floating point and verify the generated code with SIL and PIL.

Next: Practice: troubleshooting and design review