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):
18:37:26 - INFO - LOGGING (log: True) 18:37:26 - INFO - BLOCKS (total: 17, dynamic: 3, static: 14, eventful: 2) 18:37:26 - INFO - GRAPH (nodes: 17, edges: 22, alg. depth: 6, loop depth: 0, runtime: 0.202ms) 18:37:26 - INFO - STARTING -> TRANSIENT (Duration: 30.00s) 18:37:26 - INFO - -------------------- 1% | 0.2s<10.6s | 556.8 it/s 18:37:27 - INFO - ##------------------ 11% | 1.2s<8.0s | 577.6 it/s 18:37:28 - INFO - ####---------------- 20% | 2.1s<9.7s | 577.0 it/s 18:37:29 - INFO - ######-------------- 30% | 3.1s<6.6s | 624.2 it/s 18:37:30 - INFO - ########------------ 40% | 4.1s<7.7s | 504.9 it/s 18:37:31 - INFO - #########----------- 49% | 5.1s<5.3s | 581.5 it/s 18:37:32 - INFO - ###########--------- 58% | 6.1s<3.7s | 596.9 it/s 18:37:32 - INFO - ############-------- 60% | 6.2s<3.3s | 579.7 it/s 18:37:33 - INFO - ##############------ 71% | 7.2s<2.2s | 609.6 it/s 18:37:34 - INFO - ################---- 80% | 8.0s<1.7s | 589.0 it/s 18:37:35 - INFO - #################--- 89% | 9.0s<1.1s | 584.5 it/s 18:37:36 - INFO - ###################- 99% | 10.0s<0.0s | 579.0 it/s 18:37:36 - INFO - #################### 100% | 10.0s<--:-- | 565.9 it/s 18:37:36 - INFO - FINISHED -> TRANSIENT (total steps: 5837, successful: 4938, runtime: 10020.61 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.