This example is interactive. Click the play button on any cell to execute it, or run all cells in sequence.

DC Motor Speed Control

This example demonstrates multi-domain modeling of a DC motor with PID speed control. The system combines electrical and mechanical dynamics with anti-windup control to handle voltage saturation.

You can also find this example as a single file in the GitHub repository.

The DC motor control system features:

- Electrical subsystem: Armature circuit with resistance, inductance, and back-EMF - Mechanical subsystem: Rotor dynamics with inertia and viscous friction - Anti-windup PID: Prevents integrator windup during voltage saturation - Load disturbances: Tests disturbance rejection capability

DC Motor Physics

A DC motor is a multi-domain electromechanical system where electrical energy is converted to mechanical rotation. The system consists of two coupled subsystems:

Electrical Subsystem (Armature Circuit)

The armature circuit dynamics are governed by Kirchhoff's voltage law:

MATHDISPLAY0ENDMATH

where:

  • MATHINLINE3ENDMATH is the applied voltage (control input)
  • MATHINLINE4ENDMATH is the armature resistance
  • MATHINLINE5ENDMATH is the armature inductance
  • MATHINLINE6ENDMATH is the armature current
  • MATHINLINE7ENDMATH is the back-EMF (electromotive force)

The back-EMF is proportional to the rotor angular velocity:

MATHDISPLAY1ENDMATH

where MATHINLINE8ENDMATH is the back-EMF constant and MATHINLINE9ENDMATH is the angular velocity. Rearranging for the current derivative:

MATHDISPLAY2ENDMATH

Mechanical Subsystem (Rotor Dynamics)

The rotor dynamics are described by Newton's second law for rotation:

MATHDISPLAY0ENDMATH

where:

  • MATHINLINE4ENDMATH is the rotor moment of inertia
  • MATHINLINE5ENDMATH is the electromagnetic torque
  • MATHINLINE6ENDMATH is the viscous friction torque
  • MATHINLINE7ENDMATH is the external load torque

The electromagnetic torque is proportional to the armature current:

MATHDISPLAY1ENDMATH

where MATHINLINE8ENDMATH is the torque constant. The friction torque is proportional to angular velocity:

MATHDISPLAY2ENDMATH

where MATHINLINE9ENDMATH is the viscous friction coefficient. The complete mechanical equation becomes:

MATHDISPLAY3ENDMATH

Electromechanical Coupling

The two subsystems are bidirectionally coupled:

  • Electrical → Mechanical: Current MATHINLINE0ENDMATH produces torque MATHINLINE1ENDMATH
  • Mechanical → Electrical: Angular velocity MATHINLINE2ENDMATH produces back-EMF MATHINLINE3ENDMATH

This coupling creates natural feedback: as the motor speeds up, the back-EMF increases, which opposes the applied voltage and reduces current. At steady-state with no load, the motor reaches an equilibrium where the back-EMF nearly balances the applied voltage.

PID Speed Control

To control the motor speed, we use a PID (Proportional-Integral-Derivative) controller. The control law is:

MATHDISPLAY0ENDMATH

where MATHINLINE2ENDMATH is the speed error.

Anti-Windup Mechanism

Since the applied voltage is physically limited (MATHINLINE3ENDMATH), the integrator can "wind up" during saturation, causing overshoot. The anti-windup mechanism modifies the integral term:

MATHDISPLAY1ENDMATH

where MATHINLINE4ENDMATH is the anti-windup gain. When saturation occurs, the feedback term prevents further integration.

Import and Setup

First let's import the required classes and blocks:

Python
Loading...

Motor Parameters

We define realistic parameters for a small DC motor:

Python
Loading...

Source Signals

Define the speed setpoint with step changes and load torque disturbances using subsequent step signals with the StepSource .

Python
Loading...

System Definition

Now we construct the multi-domain system with separate electrical and mechanical subsystems:

Python
Loading...

The connections implement the coupled electrical-mechanical dynamics with feedback control:

Python
Loading...

Simulation Setup and Execution

We initialize the simulation with the RKCK54 solver (Runge-Kutta Cash-Karp 5th order with adaptive step size):

Python
Loading...
18:44:13 - INFO - LOGGING (log: True)
18:44:13 - INFO - BLOCKS (total: 17, dynamic: 3, static: 14, eventful: 2)
18:44:13 - INFO - GRAPH (nodes: 17, edges: 22, alg. depth: 6, loop depth: 0, runtime: 0.148ms)
18:44:13 - INFO - STARTING -> TRANSIENT (Duration: 30.00s)
18:44:13 - INFO - --------------------   1% | 0.2s<9.0s | 666.2 it/s
18:44:14 - INFO - ##------------------  12% | 1.2s<6.4s | 751.4 it/s
18:44:15 - INFO - ####----------------  20% | 1.9s<7.8s | 719.3 it/s
18:44:16 - INFO - ######--------------  31% | 2.9s<5.6s | 668.3 it/s
18:44:17 - INFO - ########------------  40% | 3.5s<6.3s | 607.3 it/s
18:44:18 - INFO - ##########----------  50% | 4.5s<35.7s | 619.7 it/s
18:44:19 - INFO - ############--------  60% | 5.5s<3.2s | 597.6 it/s
18:44:20 - INFO - ##############------  70% | 6.5s<2.6s | 617.6 it/s
18:44:21 - INFO - ################----  80% | 7.4s<1.7s | 620.9 it/s
18:44:22 - INFO - #################---  89% | 8.4s<0.9s | 583.9 it/s
18:44:23 - INFO - #################### 100% | 9.3s<--:-- | 600.9 it/s
18:44:23 - INFO - FINISHED -> TRANSIENT (total steps: 5837, successful: 4938, runtime: 9328.40 ms)

Results

Let's plot the speed tracking and electrical signals:

Python
Loading...
Output
Output

This example demonstrates PathSim's capability to model multi-domain systems with bidirectional coupling:

- Multi-domain physics: Electrical and mechanical subsystems with natural coupling through back-EMF and electromagnetic torque - Advanced control: :class: controller handles actuator saturation gracefully - Disturbance rejection: Controller compensates for external load torques - Realistic dynamics: Captures transient and steady-state behavior of real DC motors

The model accurately represents the energy conversion and control challenges in electromechanical systems, making it useful for motor controller design and analysis.