How To model a DC motor using state-space representation in MATLAB

To model a DC motor using state-space representation in MATLAB, follow these steps:


Step 1: Define the DC Motor Dynamics

A DC motor can be modeled using two state variables:

  1. Angular velocity (ω) of the rotor.
  2. Armature current (ia).

Parameters:

  • R = Armature resistance (Ω)
  • L = Armature inductance (H)
  • Kt = Torque constant (N·m/A)
  • Ke = Back-emf constant (V·s/rad)
  • J = Rotor inertia (kg·m²)
  • B = Damping coefficient (N·m·s/rad)

Equations:

  1. Electrical equation:Ldiadt=V−Ria−Keω
  2. Mechanical equation:Jdωdt=Ktia−Bω

Step 2: State-Space Representation

Define the state vector x=[iaω] and input u=V.

State-Space Matrices:

x˙=Ax+Buy=Cx+Du

  • State matrix:A=[−RL−KeLKtJ−BJ]
  • Input matrix:B=[1L0]
  • Output matrix (measuring angular velocity ω):C=[01],D=0

Step 3: Implement in MATLAB

Example Code:

matlab
Copy
% Define DC motor parameters
R = 2.0;    % Ohms
L = 0.5;    % Henries
Kt = 0.1;   % N·m/A
Ke = 0.1;   % V·s/rad (Ke ≈ Kt for SI units)
J = 0.02;   % kg·m²
B = 0.01;   % N·m·s/rad

% State-space matrices
A = [-R/L  -Ke/L;
      Kt/J  -B/J];
B = [1/L; 0];
C = [0 1];   % Output is angular velocity (ω)
D = 0;

% Create state-space model
motor_ss = ss(A, B, C, D);

% Step response
figure;
step(motor_ss);
title('Step Response of DC Motor (Angular Velocity)');
xlabel('Time (s)');
ylabel('ω (rad/s)');

% Simulate with arbitrary input (e.g., PWM voltage)
t = 0:0.01:2;
u = 12 * ones(size(t));  % 12V input
figure;
lsim(motor_ss, u, t);
title('Voltage Input Response');

Step 4: Extend for Angular Position

If the output is angular position (θ), add a third state:

dθdt=ω

Update the state vector to x=[iaωθ], and adjust the matrices:

matlab
Copy
A_extended = [-R/L  -Ke/L  0;
              Kt/J  -B/J   0;
              0      1     0];
B_extended = [1/L; 0; 0];
C_extended = [0 0 1];  % Output is angular position (θ)
D_extended = 0;

motor_ss_pos = ss(A_extended, B_extended, C_extended, D_extended);

Step 5: Validate with Simulink

  1. Build a Simulink model using:
    • State-Space Block: Load your ABCD matrices.
    • Voltage Source: Use a step input or PWM signal.
    • Scope: Monitor angular velocity/position.
  2. Compare results with the MATLAB script.