Sensor modes
FOC sensor mode is configured viamc_foc_sensor_mode in datatypes.h. The default is FOC_SENSOR_MODE_SENSORLESS.
FOC_SENSOR_MODE_SENSORLESS
FOC_SENSOR_MODE_SENSORLESS
The flux observer estimates rotor position from stator voltages and currents. No position sensor is required. The motor coasts through open-loop at low speeds until the observer locks in, controlled by
MCCONF_FOC_OPENLOOP_RPM (default: 1500 ERPM) and the open-loop hysteresis/timing parameters.FOC_SENSOR_MODE_ENCODER
FOC_SENSOR_MODE_ENCODER
Uses an incremental (ABI) encoder for rotor position. Requires encoder offset calibration. Ratio and inversion are set by
MCCONF_FOC_ENCODER_RATIO (default: 7.0) and MCCONF_FOC_ENCODER_INVERTED (default: false).FOC_SENSOR_MODE_ENCODER_AB
FOC_SENSOR_MODE_ENCODER_AB
Encoder AB mode — uses only the A and B signals (no index pulse). Useful when the index channel is unavailable.
FOC_SENSOR_MODE_HALL
FOC_SENSOR_MODE_HALL
Hall effect sensors provide 60-degree resolution commutation. Sensor angles are stored in the FOC hall table (
MCCONF_FOC_HALL_TAB_0 through MCCONF_FOC_HALL_TAB_7). Hall interpolation is disabled below MCCONF_FOC_HALL_INTERP_ERPM (default: 500 ERPM) to avoid noise at low speeds.FOC_SENSOR_MODE_HFI
FOC_SENSOR_MODE_HFI
High-Frequency Injection. A high-frequency voltage is superimposed on the motor to detect rotor position from inductance anisotropy. Intended for salient-pole motors (e.g., IPM motors). See the HFI section below.
FOC_SENSOR_MODE_HFI_START
FOC_SENSOR_MODE_HFI_START
Sensorless mode that uses HFI only during startup and low-speed operation to resolve initial rotor position, then hands off to the sensorless observer. Avoids open-loop coasting on startup.
FOC_SENSOR_MODE_HFI_V2
FOC_SENSOR_MODE_HFI_V2
Revised HFI algorithm (V2) with a correction gain (
MCCONF_FOC_HFI_GAIN, default: 0.3) and max error truncation (MCCONF_FOC_HFI_MAX_ERR, default: 0.30).FOC_SENSOR_MODE_HFI_V3
FOC_SENSOR_MODE_HFI_V3
HFI V3 — an incremental variation of the V2 algorithm.
FOC_SENSOR_MODE_HFI_V4
FOC_SENSOR_MODE_HFI_V4
HFI V4 — further refined HFI variant.
FOC_SENSOR_MODE_HFI_V5
FOC_SENSOR_MODE_HFI_V5
HFI V5 — latest HFI variant with additional hysteresis control via
MCCONF_FOC_HFI_HYST (default: 0.0).HFI (High-Frequency Injection)
HFI detects rotor position at standstill and low speeds by injecting a high-frequency test voltage and measuring the resulting current response. It requires a motor with significant inductance anisotropy (Ld ≠ Lq).HFI key parameters
HFI ambiguity resolution mode
Because inductance anisotropy is 180° ambiguous, HFI requires a separate step to resolve the correct pole. The ambiguity mode is set bymc_foc_hfi_amb_mode:
Ambiguity current and threshold are set by
MCCONF_FOC_HFI_AMB_CURRENT (default: 60.0 A) and MCCONF_FOC_HFI_AMB_TRES (default: 15).
Position observer
For sensorless operation, the flux observer estimates rotor position. The observer type is selected bymc_foc_observer_type:
FOC_OBSERVER_MXLEMMING_LAMBDA_COMP. Observer gain is set by MCCONF_FOC_OBSERVER_GAIN (default: 9×10⁷; suggested starting value: 600 / L). At low duty cycles the gain is scaled down by MCCONF_FOC_OBSERVER_GAIN_SLOW (default: 0.05).
Hybrid sensor / sensorless
When using Hall or encoder feedback, you can configure the firmware to blend in the observer at higher speeds:
Between
MCCONF_FOC_SL_ERPM_START and MCCONF_FOC_SL_ERPM the two sources are blended linearly.
Open-loop startup (sensorless)
In sensorless mode the motor runs open-loop at low speeds before the observer has locked in. Key parameters:Current controller
The FOC current controller (inner PI loop) operates on the d and q axes:Current controller decoupling
FOC_CC_DECOUPLING_DISABLED. Cross-coupling and BEMF feedforward can improve dynamic response at high speeds.
Motor parameters
The observer and current controller use these motor-specific parameters:PLL speed estimator
A phase-locked loop (PLL) tracks the rotor angle from the observer output:Field weakening
Field weakening extends operating speed above the rated RPM by injecting negative d-axis current:MTPA (Maximum Torque Per Amp)
MTPA exploits d/q inductance asymmetry to maximize torque output at a given current. It is configured byMCCONF_FOC_MTPA_MODE and requires MCCONF_FOC_MOTOR_LD_LQ_DIFF to be non-zero.
FOC functions (mcpwm_foc)
Themcpwm_foc module implements the FOC algorithm. You typically call it through mc_interface, but the direct API is available:
Sample modes
FOC sampling behavior is configurable:FOC_CONTROL_SAMPLE_MODE_V0 and FOC_CURRENT_SAMPLE_MODE_LONGEST_ZERO.