MEGR 3171  |  UNC Charlotte Mechatronics 2
Dr. Roger Tipton
Mechanical Engineering & Engineering Science
Back to Course Hub
Week 15 — Modules 4 & 5: Control & System Integration

Digital Control & System Integration

Discretizing continuous controllers, implementing interrupt-driven control loops, designing finite state machines, and integrating all course concepts into a complete mechatronic system.

Learning Objectives

1. Discretization of Continuous Controllers

A continuous PID must be converted to a discrete-time difference equation for implementation on a microcontroller. The choice of method affects stability and frequency response of the digital implementation.

Discretization Methods (s to z transform)
Euler Forward: s ≈ (z-1)/T_s [simple but unstable for large T_s] Euler Backward: s ≈ (z-1)/(T_s·z) [always stable but distorts frequency] Tustin (Bilinear): s ≈ (2/T_s)(z-1)/(z+1) [best frequency preservation] Discrete PID (Tustin, velocity form): u[k] = u[k-1] + K_p(e[k]-e[k-1]) + K_i(T_s/2)(e[k]+e[k-1]) + K_d(2/T_s)(e[k]-2e[k-1]+e[k-2]) [with filter]
Sampling Period Selection Rule of thumb: T_s ≤ T_d/10 (sample at least 10× per derivative time constant). For bandwidth-limited systems: T_s ≤ 0.1/ω_c. Tustin introduces frequency warping; pre-warp critical frequencies if the Bode shape matters.

2. Interrupt-Driven Control Loops

The control loop must execute at a precise, fixed rate. Using delay() in Arduino is not acceptable for real control because it blocks all other code and the rate varies with computation time. Timer interrupts enforce precise execution.

Arduino Timer Interrupt (Conceptual)
// Configure Timer1 for 100 Hz interrupt (10 ms period) void setup() { noInterrupts(); TCCR1A = 0; TCCR1B = 0; OCR1A = 156; // 16 MHz / 1024 / 100 Hz - 1 TCCR1B |= (1<

3. Finite State Machines

A finite state machine (FSM) models a system with a discrete set of states, transitions triggered by events, and actions associated with states or transitions.

Moore Machine

Output depends only on current state. Simpler to analyze. State transition diagram shows outputs labeled on states.

Mealy Machine

Output depends on current state AND input. More compact (fewer states). Outputs labeled on transitions.

Implementation

Use an enum for states, a switch-case for transitions. Update state at fixed intervals or on event. Document all transitions explicitly.

Application

Control mode switching (idle → startup → run → fault), sequence control, communication protocols, user interface logic.

4. Communication Protocols Comparison

ProtocolTopologySpeedWiresBest Use
UARTPoint-to-pointUp to 1 Mbps2 (TX, RX)Debugging, GPS, PC communication
I2CMulti-drop bus100 kHz / 400 kHz / 3.4 MHz2 (SDA, SCL)Short-range sensors, displays (IMUs, EEPROMs)
SPIMaster-slave, full-duplexUp to 50 MHz4 (MOSI, MISO, SCK, CS)High-speed DACs, ADCs, SD cards
CAN busMulti-node bus, differentialUp to 1 Mbps2 (CAN-H, CAN-L)Automotive, industrial, noisy environments

5. Complete System Design Process

  1. Requirements Definition: Performance specs, environmental constraints, cost, safety, reliability targets
  2. System Architecture: Block diagram of complete signal chain from physical quantity to output
  3. Sensor and Actuator Selection: Range, accuracy, bandwidth, interface compatibility
  4. Signal Conditioning Design: Amplification, filtering, impedance matching
  5. Controller Design: Plant model, stability analysis, gain selection, anti-windup
  6. Embedded Implementation: Interrupt structure, state machine, communication
  7. Integration and Test: Subsystem testing, then system-level verification against requirements
  8. Documentation: Calibration records, uncertainty analysis, test data, user guide

Practice Problems

Problem 1 — Discrete PID A continuous PI controller has Kp = 3.0 and Ki = 0.5 s&sup-¹. Discretize the integral term using the Tustin method with Ts = 0.1 s. Write the discrete difference equation for the integral output.

Tustin discretization of integrator 1/s → T_s(z+1)/(2(z-1)):

Integral output: I[k] = I[k-1] + (K_i · T_s/2)(e[k] + e[k-1])

= I[k-1] + (0.5 × 0.1/2)(e[k] + e[k-1]) = I[k-1] + 0.025(e[k] + e[k-1])

Total: u[k] = K_p·e[k] + I[k] = 3.0·e[k] + I[k-1] + 0.025(e[k] + e[k-1])

I[k] = I[k-1] + 0.025(e[k] + e[k-1]); u[k] = 3.0·e[k] + I[k]
Problem 2 — Protocol Selection You are designing an IMU data logger that must read a 6-axis IMU at 1 kHz and transmit data to a laptop for logging. Which protocol connects the IMU to the microcontroller, and which connects the microcontroller to the laptop? Justify.

IMU to microcontroller: SPI is preferred at 1 kHz because it has higher speed than I2C and full-duplex operation. Most modern IMU ICs (e.g., ICM-42688) support SPI at speeds up to 24 MHz, easily handling 1 kHz reads of 12 bytes.

Microcontroller to laptop: UART (USB-UART bridge). Standard for debug and logging. At 115200 baud this transmits ~11,000 bytes/s, which comfortably handles 12 bytes per read × 1000 Hz = 12,000 bytes/s — marginal. Use 921600 baud or pack multiple reads per transmission.

IMU to MCU: SPI (high speed, low latency). MCU to laptop: UART at high baud rate (921600) or USB CDC.