Skip to content

Conversation

@uLipe
Copy link

@uLipe uLipe commented Oct 23, 2025

The SetPWM functions of all the drivers perform a constrain operation that normalizes the phase voltage and DC link voltage using a division operation, which may take several cycles even on processors that feature a FPU on their hardware.

This waste of cycles (which represent a precious amount of microseconds) can be drastically reduced when replacing the division by reciprocal multiplication, since the power supply voltage value will not change in the runtime it it is valid to replace for example:

 dc_a = _constrain(Ua / voltage_power_supply, 0.0f , 1.0f );

To :

 dc_a = _constrain(Ua * inverse_power_supply, 0.0f , 1.0f );

where:

inverse_power_supply = 1.0f / voltage_power_supply;

The inverse_power_supply is calculated during the driver initialization avoiding the division to be done in the SetPWM function which is a hot code path, that is it, it gets called very often in the FoC main loop. I took the care to make this generic by introducing this new member in the base class and doing the inverse calculation in all the drivers available.

Probably this optimization can be reused in more hot code paths, I will check where this can be potentially applied.

@uLipe uLipe changed the base branch from master to dev October 23, 2025 15:28
…l multiplications

To reduce time of execution of the SetPWM member function.

Signed-off-by: Felipe Neves <[email protected]>
@uLipe uLipe force-pushed the feature/reduce_division branch from 1f0bba3 to 52a9629 Compare October 23, 2025 15:30
@uLipe uLipe marked this pull request as draft October 23, 2025 15:30
@uLipe uLipe marked this pull request as ready for review October 23, 2025 15:36
@uLipe
Copy link
Author

uLipe commented Oct 23, 2025

@askuric @runger1101001 please have a look on this, I think there may be other points of the code that can benefit of this optimization.

@runger1101001
Copy link
Member

runger1101001 commented Oct 23, 2025

Yeah, it’s true, but the fix has to be more complicated unfortunately.

this is because the assumption that voltage_power_supply remains constant after initialization is not correct.

A more complete code for a ESC will include VBUS voltage monitoring and adjust the power supply voltage to match the real world conditions.

It’s also unfortunate that the current code performs multiple scalings of the voltage, because the motor class deals with voltages, while the driver.setPwm() converts to 0-1 duty cycles and the hardware drivers deal in integer counter values.
I would prefer to see a solution where we skip the intermediate 0-1 representation and go directly to integer values in timer counts.

@uLipe
Copy link
Author

uLipe commented Oct 23, 2025

@runger1101001 thank you for the extra context on that issue, in this case I will craft a better solution that covers the fluctuation of the power_supply_voltage.

For now let's close this one, and I will also check way to getting rig of the 0-1 scaling in favor of proper compare values in fixed point.

@uLipe uLipe closed this Oct 23, 2025
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.

2 participants