# PMSM (Permanent Magnet Synchronous Machine)


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

### Back-EMF (Electromotive Force)

The back-EMF is the voltage induced by the rotating permanent magnet
flux.

**Peak phase back-EMF:**

*E*<sub>*p*</sub> = *ω*<sub>*e*</sub> ⋅ *ψ*<sub>*m*</sub>

where: - *ω*<sub>*e*</sub> = electrical angular velocity \[rad/s\] -
*ψ*<sub>*m*</sub> = permanent magnet flux linkage \[Wb\]

**Relationship to mechanical speed:**

$$\omega_e = p \cdot \omega_m = p \cdot \frac{2\pi N}{60}$$

where *p* is the number of pole pairs and *N* is mechanical speed in
rpm.

**Physical significance:** Back-EMF represents the voltage that must be
overcome to drive current through the motor at a given speed. Higher
flux and higher speed → higher back-EMF → reduced available voltage for
current production (important at high speeds).

## Executive Summary

**Purpose:** Control and simulate AC permanent magnet motors

**What it does:** Analyze PMSM (Permanent Magnet Synchronous Motor) in
dq-frame.

### Why It Matters

A control engineer designs field-oriented control (FOC) algorithms and
needs steady-state current/torque relationships.

### Quick Start

**Inputs:** Motor parameters (inductances, flux linkage, resistance),
operating speed, applied voltages

**Outputs:** dq-axis currents, torque, back-EMF, motor profile
(constant-torque and flux-weakening regions)

## How It Fits Into the Motor Design Workflow

**Upstream (depends on):** Uses winding concepts from 01_winding.ipynb,

**Downstream (used by):** control system design

## PMSM (Permanent Magnet Synchronous Machine)

A dq-frame model for AC synchronous machines with permanent magnet
excitation.

### dq-Frame Theory

The dq-frame (synchronously rotating reference frame) is the foundation
of AC motor control.

**Key concepts:**

- **d-axis (Direct)**: Aligned with the permanent magnet flux direction
- **q-axis (Quadrature)**: 90° ahead of the d-axis in the direction of
  rotation
- **Benefits**: Transforms AC quantities into DC-like quantities at
  steady-state, enabling intuitive control

**Voltage equations in dq-frame:**

$$V_d = R_s i_d + \frac{d\psi_d}{dt} - \omega_e \psi_q$$

$$V_q = R_s i_q + \frac{d\psi_q}{dt} + \omega_e \psi_d$$

**Flux linkages:**

*ψ*<sub>*d*</sub> = *L*<sub>*d*</sub>*i*<sub>*d*</sub> + *ψ*<sub>*m*</sub>

*ψ*<sub>*q*</sub> = *L*<sub>*q*</sub>*i*<sub>*q*</sub>

In steady-state (constant speed), the derivatives vanish, yielding the
simplified equations used here.

### Inductance and Saliency

**Non-salient (SPM) vs. Salient (IPM) machines:**

- **Surface-Permanent Magnet (SPM)**: Magnets mounted on rotor surface →
  *L*<sub>*d*</sub> ≈ *L*<sub>*q*</sub>
  - Torque produced primarily by interaction between PM flux and q-axis
    current
  - Limited flux-weakening capability
- **Interior-Permanent Magnet (IPM)**: Magnets buried in rotor poles →
  *L*<sub>*d*</sub> \> *L*<sub>*q*</sub>
  - Torque has both PM (excitation) and reluctance (saliency) components
  - Better flux-weakening capability due to saliency

**Torque breakdown:**

$$T_e = \underbrace{\frac{3}{2}p\psi_m i_q}\_{\text{Excitation torque}} + \underbrace{\frac{3}{2}p(L_d - L_q)i_d i_q}\_{\text{Reluctance torque}}$$

For maximum torque at low speed: set *i*<sub>*d*</sub> = 0 (all current
goes to *i*<sub>*q*</sub>)

At high speed (flux-weakening): apply negative *i*<sub>*d*</sub> to
reduce back-EMF and maintain voltage margin

### Flux-Weakening Control

At high speeds, the back-EMF becomes large, limiting the available
voltage for current production.

**Voltage constraint:**

$$|\mathbf{V}| = \sqrt{(R_s i_d - \omega_e L_q i_q)^2 + (R_s i_q + \omega_e(L_d i_d + \psi_m))^2} \leq V_b$$

**Flux-weakening strategy:**

Apply negative *i*<sub>*d*</sub> to weaken the total flux
(*ψ*<sub>*d*</sub> = *L*<sub>*d*</sub>*i*<sub>*d*</sub> + *ψ*<sub>*m*</sub>),
reducing back-EMF and extending the speed range.

**Operating regions:**

1.  **Constant-Torque Region** (low speed, *i*<sub>*d*</sub> = 0):
    Voltage allows rated current → constant torque output
2.  **Flux-Weakening Region** (high speed, *i*<sub>*d*</sub> \< 0):
    Reduced voltage → power limited to rated value

------------------------------------------------------------------------

### PMSMParams

``` python
def PMSMParams(
    p:int, Ld:float, Lq:float, psi_m:float, Rs:float=0.0, J:float=0.0
):
```

*Container for PMSM parameters.*

### Back-EMF

The back-EMF is proportional to speed and PM flux:
*E* = *ω*<sub>*e*</sub> ⋅ *ψ*<sub>*m*</sub>

------------------------------------------------------------------------

### back_emf

``` python
def back_emf(
    omega_e:float, psi_m:float
)->float:
```

*Peak phase back-EMF.*

``` python
emf = back_emf(1000, 0.15)
assert emf == 150.0
print(f"✓ Back-EMF test passed")
```

    ✓ Back-EMF test passed

### Electromagnetic Torque

The instantaneous torque has two components:

$$T_e = \frac{3}{2} p \[\psi_m i_q + (L_d - L_q) i_d i_q\]$$

**Component breakdown:**

1.  **Excitation (PM) torque:** $T\_{exc} = \frac{3}{2} p \psi_m i_q$
    - Produced by interaction of PM flux with q-axis current
    - Present in all PMSM topologies
    - Maximized at *i*<sub>*d*</sub> = 0 for a given current magnitude
2.  **Reluctance (Saliency) torque:**
    $T\_{rel} = \frac{3}{2} p (L_d - L_q) i_d i_q$
    - Produced by saliency (magnetic asymmetry)
    - Only non-zero in IPM machines where
      *L*<sub>*d*</sub> \> *L*<sub>*q*</sub>
    - Requires specific *i*<sub>*d*</sub> and *i*<sub>*q*</sub> signs to
      add constructively

**Control implications:** - **Low speed (constant-torque region):** Use
*i*<sub>*d*</sub> = 0, vary *i*<sub>*q*</sub> for torque control -
**High speed (flux-weakening region):** Use *i*<sub>*d*</sub> \< 0 to
reduce back-EMF and maintain power output

------------------------------------------------------------------------

### torque

``` python
def torque(
    params:PMSMParams, id:float, iq:float
)->float:
```

*Electromagnetic torque.*

``` python
params = PMSMParams(p=5, Ld=78e-6, Lq=78e-6, psi_m=0.15)
T = torque(params, 0, 100)
assert T > 0
print(f"✓ PMSM torque test passed")
```

    ✓ PMSM torque test passed

### Steady-State dq-Frame Currents

Solve the 2×2 linear system for steady-state dq currents from applied
voltages.

**Steady-state voltage equations (neglecting transients):**

*V*<sub>*d*</sub> = *R*<sub>*s*</sub>*i*<sub>*d*</sub> − *ω*<sub>*e*</sub>*L*<sub>*q*</sub>*i*<sub>*q*</sub>

*V*<sub>*q*</sub> = *R*<sub>*s*</sub>*i*<sub>*q*</sub> + *ω*<sub>*e*</sub>(*L*<sub>*d*</sub>*i*<sub>*d*</sub> + *ψ*<sub>*m*</sub>)

**Physical interpretation:** - **d-axis:** Resistive voltage drop plus
cross-coupling from q-axis current - **q-axis:** Resistive drop plus
back-EMF and cross-coupling from d-axis current

**Matrix form:**

$$\begin{bmatrix} V_d \\V_q - \omega_e\psi_m \end{bmatrix} = \begin{bmatrix} R_s & -\omega_e L_q \\\omega_e L_d & R_s \end{bmatrix} \begin{bmatrix} i_d \\i_q \end{bmatrix}$$

Solving this system gives the steady-state currents needed to produce a
given voltage vector.

------------------------------------------------------------------------

### dq_currents

``` python
def dq_currents(
    params:PMSMParams, omega_e:float, Vd:float, Vq:float
)->tuple[float, float]:
```

*Steady-state dq currents.*

``` python
params = PMSMParams(p=5, Ld=78e-6, Lq=78e-6, psi_m=0.15, Rs=0.1)
id, iq = dq_currents(params, 500, 2.0, 10.0)
assert not np.isnan(id) and not np.isnan(iq)
print(f"✓ DQ-frame currents test passed")
```

    ✓ DQ-frame currents test passed

### SPM Motor Profile

Surface-mount PMSM with constant-torque and flux-weakening regions.

------------------------------------------------------------------------

### SPM

``` python
def SPM(
    phi_m:float, ld:float, pp:int, Vb:float, Ib:float
):
```

*Surface-Mount PMSM motor profile.*

``` python
spm = SPM(phi_m=0.087, ld=78e-6, pp=5, Vb=12, Ib=50)
spm.validate()
spm.motor_profile()
assert len(spm.torque) == len(spm.speed)
print(f"✓ SPM motor profile test passed")
```

    ✓ SPM motor profile test passed

## Tests

PMSM dq-model validated against analytical expressions.
