Skip to content

fan_service: use floats and avoid inaccuracies due to truncation#652

Open
aalamsi22 wants to merge 2 commits into
facebook:mainfrom
aalamsi22:fan_service_pwm_floats
Open

fan_service: use floats and avoid inaccuracies due to truncation#652
aalamsi22 wants to merge 2 commits into
facebook:mainfrom
aalamsi22:fan_service_pwm_floats

Conversation

@aalamsi22

Copy link
Copy Markdown
Contributor

Summary

There is a bug in the way the fan service treats the pwm value as it moves through the pid algorithm:

  • When the new pwm is calculated, it is always truncated down into an integer. This results in two outcomes:
    1. A calculated pwm delta of of >1% is needed to drive a change in the fan speeds. Behaving like a positive hysteresis.
    2. Negative pwm deltas are affected in the opposite way. A small negative delta can truncate the final pwm by upto a whole integer. E.g. 50 - 0.01 -> 49 .
  • The integer output of the pid is then converted into a float again in calculation of the raw byte value, which will again be converted into an integer and undergo truncation once more.

Solution

Convert all pwm variables into floats and only convert into an integer to get the raw byte value. This will reduce inaccuracy as the value will only be truncated once.

Additional Changes

  • Build PidLogicTest for OSS
  • Adjust test to account for float outputs

Test Plan

Tested fan_service on viper with random incremental pid.

...
Nov 08 04:20:39 ... I1108 04:20:39.006693  6591 ControlLogic.cpp:616] Processing Optics ...
Nov 08 04:20:39 ... I1108 04:20:39.006701  6591 ControlLogic.cpp:374] Optics: Aggregation Type: OPTIC_AGGREGATION_TYPE_INCREMENTAL_PID. Aggregate PWM is 61.777637
Nov 08 04:20:39 ... I1108 04:20:39.006707  6591 ControlLogic.cpp:513] zone1: Components: SMB_BOARD_FRONT_TEMP,osfp_group_1. Aggregation Type: ZONE_TYPE_MAX. Aggregate PWM is 61.777637.
Nov 08 04:20:39 ... I1108 04:20:39.007630  6591 ControlLogic.cpp:445] fan_1: Programmed with PWM 61.777637 (raw value 157)
Nov 08 04:20:39 ... I1108 04:20:39.008550  6591 ControlLogic.cpp:445] fan_2: Programmed with PWM 61.777637 (raw value 157)
Nov 08 04:20:39 ... I1108 04:20:39.009469  6591 ControlLogic.cpp:445] fan_3: Programmed with PWM 61.777637 (raw value 157)
Nov 08 04:20:39 ... I1108 04:20:39.010396  6591 ControlLogic.cpp:445] fan_4: Programmed with PWM 61.777637 (raw value 157)

Passes fan_service_hw_test

...
[       OK ] FanServiceHwTest.LedWriteDidNotFail (13989 ms)
[----------] 5 tests from FanServiceHwTest (56547 ms total)

[----------] Global test environment tear-down
[==========] 5 tests from 1 test suite ran. (56547 ms total)
[  PASSED  ] 5 tests.

Passes fan_service_sw_test

...

[----------] 3 tests from PIDLogicTest
[ RUN      ] PIDLogicTest.Basic
[       OK ] PIDLogicTest.Basic (0 ms)
[ RUN      ] PIDLogicTest.Convergence
[       OK ] PIDLogicTest.Convergence (0 ms)
[ RUN      ] PIDLogicTest.Basic2
[       OK ] PIDLogicTest.Basic2 (0 ms)
[----------] 3 tests from PIDLogicTest (0 ms total)

[----------] 1 test from IncrementalPIDLogicTest
[ RUN      ] IncrementalPIDLogicTest.Basic
[       OK ] IncrementalPIDLogicTest.Basic (0 ms)
[----------] 1 test from IncrementalPIDLogicTest (0 ms total)

[----------] Global test environment tear-down
[==========] 18 tests from 6 test suites ran. (17 ms total)
[  PASSED  ] 18 tests.
@meta-cla meta-cla Bot added the CLA Signed label Nov 11, 2025
@aalamsi22 aalamsi22 changed the title fan_service: use floats and avoid rounding inaccuracies Nov 11, 2025
@meta-codesync

meta-codesync Bot commented Nov 12, 2025

Copy link
Copy Markdown
Contributor

@somasun has imported this pull request. If you are a Meta employee, you can view this in D86831922.

@aalamsi22 aalamsi22 requested review from a team as code owners March 25, 2026 21:28
@facebook-github-tools

Copy link
Copy Markdown

@aalamsi22 has updated the pull request. You must reimport the pull request before landing.

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

1 participant