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:
Motor Parameters
We define realistic parameters for a small DC motor:
Source Signals
Define the speed setpoint with step changes and load torque disturbances using subsequent step signals with the StepSource .
System Definition
Now we construct the multi-domain system with separate electrical and mechanical subsystems:
The connections implement the coupled electrical-mechanical dynamics with feedback control:
Simulation Setup and Execution
We initialize the simulation with the RKCK54 solver (Runge-Kutta Cash-Karp 5th order with adaptive step size):
00:43:44 - INFO - LOGGING (log: True) 00:43:44 - INFO - BLOCKS (total: 17, dynamic: 3, static: 14, eventful: 2) 00:43:44 - INFO - GRAPH (nodes: 17, edges: 22, alg. depth: 6, loop depth: 0, runtime: 0.171ms) 00:43:44 - INFO - STARTING -> TRANSIENT (Duration: 30.00s) 00:43:44 - INFO - -------------------- 1% | 0.2s<10.1s | 583.2 it/s 00:43:45 - INFO - ##------------------ 11% | 1.2s<7.5s | 599.6 it/s 00:43:46 - INFO - ####---------------- 20% | 2.1s<7.5s | 751.1 it/s 00:43:47 - INFO - ######-------------- 30% | 3.1s<5.4s | 617.6 it/s 00:43:48 - INFO - ########------------ 40% | 3.9s<6.3s | 606.4 it/s 00:43:49 - INFO - ##########---------- 50% | 4.9s<02:46 | 601.2 it/s 00:43:50 - INFO - ###########--------- 59% | 5.9s<3.7s | 599.0 it/s 00:43:50 - INFO - ############-------- 60% | 6.0s<3.4s | 565.4 it/s 00:43:51 - INFO - ##############------ 70% | 7.0s<3.5s | 571.9 it/s 00:43:51 - INFO - ################---- 80% | 7.8s<1.3s | 780.0 it/s 00:43:52 - INFO - ##################-- 90% | 8.8s<0.7s | 665.6 it/s 00:43:53 - INFO - #################### 100% | 9.6s<--:-- | 648.4 it/s 00:43:53 - INFO - FINISHED -> TRANSIENT (total steps: 5837, successful: 4938, runtime: 9646.72 ms)
Results
Let's plot the speed tracking and electrical signals:
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.