Skip to content

Add optional dq voltage vector limiter (limit_voltage_vector) - #599

Open
Barre-Beavogui wants to merge 2 commits into
simplefoc:devfrom
Barre-Beavogui:feat/dq-voltage-vector-limit
Open

Barre-Beavogui wants to merge 2 commits into
simplefoc:devfrom
Barre-Beavogui:feat/dq-voltage-vector-limit

Conversation

@Barre-Beavogui

@Barre-Beavogui Barre-Beavogui commented Nov 25, 2026 •

Copy link
Copy Markdown

Description

Adds an opt-in FOCMotor::limit_voltage_vector flag (default false, so existing behaviour is unchanged). When enabled, loopFOC() scales voltage.d and voltage.q together right before setPhaseVoltage() whenever the vector magnitude exceeds voltage_limit, instead of relying only on the independent d/q limits. The direction of the voltage vector is preserved. sqrtf is used instead of _sqrt because the fast approximation could slightly exceed the limit.

Example from the issue: with voltage_limit = 6 V, Vd = Vq = 6 V request about 8.5 V in total; with the flag enabled they are scaled to about 4.24 V each, giving a 6 V vector.

Fixes #561

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Host-side test (g++, stubbed Arduino.h, fake sensor and driver) running the real BLDCMotor::loopFOC() code in voltage torque-control mode with voltage_limit = 6 V:

case limit_voltage_vector Vq / Vd requested Vq / Vd after loopFOC()
over the limit false 6 / 6 (|v| = 8.485 V) 6.000 / 6.000 (unchanged)
over the limit true 6 / 6 4.243 / 4.243 (|v| = 6.000 V, direction preserved)
below the limit false / true 2 / 1 2.000 / 1.000 (unchanged)

FOCMotor.cpp, BLDCMotor.cpp, StepperMotor.cpp and HybridStepperMotor.cpp also compile with the change.

Test Configuration/Setup:

  • Hardware: none, host-side test only. Not yet tested on a physical motor/driver.
  • IDE: g++ on Linux with a stubbed Arduino.h (not Arduino/Platformio)
  • MCU package version (stm32duino/arduino-esp32/..): n/a

Open question (see #561): proportional scaling as implemented here, or a d-axis-priority limiter like MathWorks' DQ Limiter block?

…r is set

Refs simplefoc#561. Scales Vd and Vq together when the vector magnitude exceeds voltage_limit. Opt-in, disabled by default.
@Barre-Beavogui
Barre-Beavogui marked this pull request as ready for review November 25, 2026 23:49
@simplefoc simplefoc deleted a comment from candas-agent Nov 27, 2026
@Candas1

Candas1 commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Thanks, nice and minimal. A few suggestions:

  • Enum instead of bool, e.g. none / proportional / d_priority, as in the MathWorks DQ Limiter. d-priority is what field weakening needs if implemented in the future. An enum avoids an API change when it's added later.
  • foc_current: the limiter also clips the current PID outputs, but their integrals are still clamped to voltage_limit, so they wind up. Setting PID_current_q.limit (and d) to the vector limit each loop would fix that. But we should test it
  • sqrtf is expensive on MCUs without an FPU (e.g. F103, AVR). An inverse square root with one Newton step never overestimates, so it stays under the limit, and it removes the division.
// fast inverse square root, one Newton step: ~0.18 % error, never above 1/sqrt(x)
__attribute__((weak)) float _invSqrtApprox(float number) {
  if (number <= 0.0f) return 0.0f;
  union {
    float    f;
    uint32_t i;
  } y = { .f = number };
  y.i = 0x5f375a86 - ( y.i >> 1 );          // y ≈ 1/sqrt(x)
  y.f *= 1.5f - 0.5f * number * y.f * y.f;  // Newton step
  return y.f;
}

__attribute__((weak)) float _sqrtApprox(float number) {//low in fat
  return number * _invSqrtApprox(number);
}

I could make a PR for this better _sqrtApprox and _invSqrtApprox after testing it.

@askuric

askuric commented Nov 27, 2026

Copy link
Copy Markdown
Member

@Candas1 we already habe the inverse square root approx in the library. :)

// square root approximation function using

@candas-agent

Copy link
Copy Markdown

Ok excuse me It's been a while 😂

@Candas1

Candas1 commented Nov 27, 2026 via email •

Copy link
Copy Markdown
Collaborator

@Candas1

Candas1 commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator

Sorry I am on my computer now, let me clarify:

  • sqrtf is expensive
  • simplefoc has _sqrtApprox, it's an adaptation of the fast inverse square root approximation with an additional multiplication to compute the fast square root, it seems it can be a 3.4% off and slightly exceed the limit
  • an additional newton step will make it more accurate and not exceed the limit (but slightly slower), it's one additional line ( y.f = 1.5f - 0.5f * number * y.f * y.f; // Newton step* )
  • the invert sqrt that is needed for the limit is part of the sqrtapprox already so the functions can be split and sqrtapprox only needs an additional multiplication.

To be honest this was suggested by Claude so I would trust the limit/speed/accurary only if I test and compare the results.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants