DC/BLDC position control D-term (p_pid_kd_filter) implemented inconsistently vs. FOC — no reset-on-disable, no averaged-dt handling for low-resolution position feedback
p_pid_kd_filter is a single shared mc_configuration field used to low-pass filter the derivative (D) term of the position-control PID loop. It is exposed identically in VESC Tool for both FOC and DC/BLDC motor types, implying equivalent behavior. However, the two backend implementations diverge significantly:
foc_run_pid_control_pos() in motor/foc_math.c
run_pid_control_pos() in motor/mcpwm.c (used for MOTOR_TYPE_DC/MOTOR_TYPE_BLDC)
This means identical p_pid_kd_filter (and related PID) values do not produce comparable D-term behavior across motor types, and the DC/BLDC path has a state-leak bug the FOC path already guards against.
Code comparison
FOC implementation clears all D-term/I-term state whenever position control is inactive: foc_math.c:394-402
FOC also averages dt across ticks where the position hasn't changed (to avoid derivative spikes at low encoder/hall resolution and low speed), and only updates the filter once a real position change is observed: foc_math.c:437-451
The DC/BLDC implementation does neither of these: mcpwm.c:1246-1270
static void run_pid_control_pos(float dt, float pos_now) {
static float i_term = 0;
static float prev_error = 0;
...
if (control_mode != CONTROL_MODE_POS) {
i_term = 0;
prev_error = 0;
return; // <-- d_filter is NOT reset here
}
...
d_term = (error - prev_error) * (conf->p_pid_kd / dt); // <-- raw dt every tick, no averaging over unchanged error
// Filter D
static float d_filter = 0.0; // <-- persists across enable/disable cycles
UTILS_LP_FAST(d_filter, d_term, conf->p_pid_kd_filter);
d_term = d_filter;
...
}
Specific defects
No filter-state reset when position control is disabled. i_term and prev_error are reset when control_mode != CONTROL_MODE_POS, but the static d_filter variable is never cleared. FOC explicitly clears the equivalent motor->m_pos_d_filter in the same disable branch foc_math.c:394-402 . Re-enabling position control on the DC/BLDC path can therefore apply a stale filtered D-term left over from a previous session/motion.
No averaged-dt handling for low-resolution position feedback. At low speed with coarse position resolution (e.g. hall A/B sensing, which has far lower angular resolution than an encoder), several control ticks can execute with error == prev_error (no new position sample yet). FOC handles this explicitly by accumulating dt and skipping the derivative computation until the position actually changes foc_math.c:437-447 . The DC/BLDC path instead computes d_term = (error - prev_error) * (conf->p_pid_kd / dt) every tick unconditionally — when error == prev_error this correctly evaluates to 0 for the raw d_term, but because dt here is the raw per-tick loop period (not accumulated), if/when the position does change after several stale ticks, the derivative spike computed against the last tick's dt (rather than the true elapsed time since the last real position update) will be different — and generally noisier — than what FOC computes, since FOC's kd / motor->m_pos_dt_int divides by the true elapsed time since the last observed change.
Impact
Since p_pid_kd_filter (and p_pid_kd) are shared, motor-type-agnostic fields in VESC Tool's UI, users tuning position control on DC/BLDC + hall sensor hardware (a common combination for low-cost linear actuator / steering-style applications) get behavior inconsistent with what the UI/documentation implies is a shared, portable PID implementation, and can be bitten by an unexpected D-term kick on re-enabling position control due to the unreset filter state.
Suggested fix
In run_pid_control_pos() (motor/mcpwm.c), reset the static d_filter to 0.0 alongside i_term/prev_error in the control_mode != CONTROL_MODE_POS early-return branch.
Consider porting FOC's averaged-dt-on-unchanged-error logic to the DC/BLDC path, or at minimum documenting in VESC Tool that p_pid_kd_filter/p_pid_kd behavior differs meaningfully between FOC and DC/BLDC motor types due to differing derivative-term implementations.
Environment
Custom hw_410-based hardware (STM32F405/F407, VESC 4.x-class board), DC motor with A/B hall sensor, MOTOR_TYPE_DC, CONTROL_MODE_POS (position control) for a steering/trim state machine application.