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...
12:43:30 - INFO - LOGGING (log: True)
12:43:30 - INFO - BLOCKS (total: 17, dynamic: 3, static: 14, eventful: 2)
12:43:30 - INFO - GRAPH (nodes: 17, edges: 22, alg. depth: 6, loop depth: 0, runtime: 0.190ms)
12:43:30 - INFO - STARTING -> TRANSIENT (Duration: 30.00s)
12:43:31 - INFO - --------------------   1% | 0.1s<7.4s | 795.8 it/s
12:43:32 - INFO - ###-----------------  16% | 1.1s<19.0s | 1087.2 it/s
12:43:32 - INFO - ####----------------  20% | 1.4s<6.6s | 853.0 it/s
12:43:33 - INFO - ######--------------  32% | 2.4s<4.5s | 739.4 it/s
12:43:33 - INFO - ########------------  40% | 3.0s<5.1s | 745.8 it/s
12:43:34 - INFO - ##########----------  53% | 4.0s<3.8s | 797.6 it/s
12:43:35 - INFO - ############--------  60% | 4.4s<1.1s | 1677.0 it/s
12:43:36 - INFO - ###############-----  76% | 5.4s<1.2s | 956.1 it/s
12:43:36 - INFO - ################----  80% | 5.6s<1.3s | 803.4 it/s
12:43:37 - INFO - ##################--  94% | 6.6s<0.5s | 820.7 it/s
12:43:38 - INFO - #################### 100% | 7.0s<--:-- | 811.8 it/s
12:43:38 - INFO - FINISHED -> TRANSIENT (total steps: 5837, successful: 4938, runtime: 7023.64 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.