Skip to content

Conversation

@fj-sanchez
Copy link

@fj-sanchez fj-sanchez commented Nov 11, 2025

Summary by CodeRabbit

  • New Features

    • Automatic purge valve control triggers on brew start/end with a timed purge.
    • PID tuning now includes an additional feed‑forward coefficient alongside Kp, Ki, Kd.
  • Changes

    • BLE scale plugin set trimmed — most plugins disabled; BookooScales remains active.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 11, 2025

Walkthrough

Extended PID control to include a fourth feed‑forward parameter (FF) across BLE, controller, and heater layers; added a PurgeValvePlugin that triggers a short purge after brew events; and commented out most BLE scale plugin includes/apply calls leaving only BookooScalesPlugin active.

Changes

Cohort / File(s) Summary
PID Control Parameter Extension
lib/GaggiMateController/src/GaggiMateController.cpp, lib/GaggiMateController/src/peripherals/Heater.h, lib/GaggiMateController/src/peripherals/Heater.cpp
Heater::setTunings signature changed to accept four floats (Kp, Ki, Kd, FF); feed‑forward enabled in setup; call sites and logs updated to pass and display FF (including autotune result handling).
Bluetooth / NimBLE PID Path
lib/NimBLEComm/src/NimBLEComm.h, lib/NimBLEComm/src/NimBLEClientController.cpp, lib/NimBLEComm/src/NimBLEServerController.cpp
pid_control_callback_t extended to four floats; autotune and PID_CONTROL parsing now parse optional FF (default 0) and invoke callbacks with (Kp, Ki, Kd, FF).
Display Controller Integration
src/display/core/Controller.cpp
Registered PurgeValvePlugin; autotune result callback updated to accept FF and PID string formatting/storage updated to include FF.
Purge Valve Plugin (new)
src/display/plugins/PurgeValvePlugin.h, src/display/plugins/PurgeValvePlugin.cpp
New plugin added: subscribes to controller:brew:start and controller:brew:end, tracks brew state, triggers a GrindProcess purge (~1500 ms), and manages purge lifecycle/state with logging.
BLE Scale Plugin Configuration
src/display/plugins/BLEScalePlugin.cpp
Many scale plugin includes and .apply() calls commented out (Acaia and others); only BookooScalesPlugin::apply() remains active.

Sequence Diagram(s)

sequenceDiagram
    participant BLE as BLE Layer
    participant Controller
    participant Heater
    participant PID as PID Module

    rect rgb(220,240,250)
    Note over BLE,Controller: Prior flow (3 params)
    BLE->>Controller: autotuneResult(Kp,Ki,Kd)
    Controller->>Heater: setTunings(Kp,Ki,Kd)
    Heater->>PID: updateGains(Kp,Ki,Kd)
    end

    rect rgb(240,250,220)
    Note over BLE,Controller: New flow (4 params with FF)
    BLE->>Controller: autotuneResult(Kp,Ki,Kd,FF)
    Controller->>Heater: setTunings(Kp,Ki,Kd,FF)
    Heater->>PID: updateGains(Kp,Ki,Kd,FF)
    end
Loading
sequenceDiagram
    participant EventMgr as Event Manager
    participant Purge as PurgeValvePlugin
    participant Grinder as GrindProcess

    EventMgr->>Purge: controller:brew:start
    Purge->>Purge: _brew_started = true

    EventMgr->>Purge: controller:brew:end
    Purge->>Purge: _brew_finished = true

    loop controller.loop()
        alt purge condition met and not already purging
            Purge->>Grinder: Start purge (1500 ms)
            Purge->>Purge: _is_purging = true
        else purge elapsed
            Purge->>Purge: reset _brew_started/_brew_finished/_is_purging
        end
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Review points:
    • Ensure all pid callback typedefs and registrations consistently use four floats and that defaults (FF=0) are handled.
    • Verify Heater::setTunings applies FF correctly to the PID/feed‑forward implementation and that logs reflect the fourth param.
    • Inspect PurgeValvePlugin timing, event subscription/unsubscription and interaction with GrindProcess for race conditions.
    • Confirm commenting out scale plugins doesn't introduce unused include or build/linker issues.

Possibly related PRs

Poem

🐰 I hopped through code with tiny paws,

Four gains now march with one new cause,
FF whispers forward, steady and bright,
Purge hums once after brew’s last light,
A rabbit cheers — the loop runs right!

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 13.33% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Sagemate purge valve' directly corresponds to the main changes in the pull request, which introduce a new PurgeValvePlugin feature.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 862e12c and 2b129fc.

📒 Files selected for processing (1)
  • lib/GaggiMateController/src/peripherals/Heater.cpp (3 hunks)
🔇 Additional comments (4)
lib/GaggiMateController/src/peripherals/Heater.cpp (4)

23-23: LGTM!

Enabling feed-forward is consistent with the PR's objective to integrate the FF parameter into PID control.


62-68: LGTM!

The updated setTunings method correctly integrates the FF parameter into the PID control logic, including proper change detection and logging.


115-115: Verify FF scaling is intentional.

The setTunings call correctly includes all 4 parameters. However, Kp, Ki, and Kd are scaled by 1000.0f while FF is not. Please verify this is intentional and aligns with the expected units/ranges for the FF parameter.


117-118: LGTM!

The autotune completion log correctly includes all four PID parameters (Kp, Ki, Kd, Kff) with consistent formatting and scaling.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
lib/GaggiMateController/src/peripherals/Heater.cpp (1)

107-113: Missing FF parameter in error callback.

Line 111 calls pid_callback with only 3 parameters (0, 0, 0), but based on the PR changes, the callback signature now requires 4 parameters including FF.

Apply this diff:

         if (temperature > MAX_AUTOTUNE_TEMP) {
             output = 0.0f;
             autotuning = false;
             softPwm(TUNER_OUTPUT_SPAN);
-            pid_callback(0, 0, 0);
+            pid_callback(0, 0, 0, 0);
             return;
         }
🧹 Nitpick comments (3)
lib/NimBLEComm/src/NimBLEServerController.cpp (1)

122-129: Consider including FF parameter for protocol consistency.

While the current implementation is backward-compatible (the client defaults FF to 0), the autotune result sends only three parameters (Kp, Ki, Kd), creating an asymmetry with the extended four-parameter PID control protocol. For consistency and clarity, consider either:

  1. Updating the signature to sendAutotuneResult(float Kp, float Ki, float Kd, float FF = 0.0f) and explicitly sending four values
  2. Documenting that autotune does not tune the feed-forward term
src/display/plugins/PurgeValvePlugin.cpp (2)

10-21: Add null pointer validation for controller parameter.

The setup method should validate that the controller pointer is not null before storing it to prevent potential null pointer dereferences in the loop method.

Apply this diff:

 void PurgeValvePlugin::setup(Controller *controller, PluginManager *pluginManager) {
+    if (controller == nullptr || pluginManager == nullptr) {
+        ESP_LOGE(LOG_TAG, "Invalid controller or pluginManager in PurgeValvePlugin::setup");
+        return;
+    }
     _controller = controller;

23-28: Address commented code and consider semantic clarity.

  1. Commented code (Line 25): The commented sendAltControl(true) call should either be removed if no longer needed or uncommented if it's required for the purge valve operation. Leaving it commented creates ambiguity about the intended behavior.

  2. Semantic mismatch (Line 26): Using GrindProcess for a purge valve operation is semantically confusing, as "Grind" implies coffee grinding rather than valve purging. Consider either:

    • Creating a dedicated PurgeProcess class for clarity
    • Adding a comment explaining why GrindProcess is reused
    • Using the alt relay control (as suggested by the commented line) instead of process-based control

If the alt relay approach is correct, consider:

     if (!_is_purging && _brew_started && _brew_finished) {
-        // _controller->getClientController()->sendAltControl(true);
-        this->_controller->startProcess(new GrindProcess(ProcessTarget::TIME, PURGE_TIME_MS, 0.0, 0.0));
+        _controller->getClientController().sendAltControl(true);
         _is_purging = true;
     }
 
-    if (_is_purging && millis() > _last_brew_finished + PURGE_TIME_MS) {
+    if (_is_purging && millis() > _last_brew_finished + PURGE_TIME_MS) {
+        _controller->getClientController().sendAltControl(false);
         _is_purging = false;
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ecb0937 and 7ea7bf8.

📒 Files selected for processing (10)
  • lib/GaggiMateController/src/GaggiMateController.cpp (1 hunks)
  • lib/GaggiMateController/src/peripherals/Heater.cpp (3 hunks)
  • lib/GaggiMateController/src/peripherals/Heater.h (1 hunks)
  • lib/NimBLEComm/src/NimBLEClientController.cpp (1 hunks)
  • lib/NimBLEComm/src/NimBLEComm.h (1 hunks)
  • lib/NimBLEComm/src/NimBLEServerController.cpp (1 hunks)
  • src/display/core/Controller.cpp (3 hunks)
  • src/display/plugins/BLEScalePlugin.cpp (2 hunks)
  • src/display/plugins/PurgeValvePlugin.cpp (1 hunks)
  • src/display/plugins/PurgeValvePlugin.h (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (6)
lib/GaggiMateController/src/peripherals/Heater.h (1)
lib/GaggiMateController/src/peripherals/Heater.cpp (2)
  • setTunings (68-74)
  • setTunings (68-68)
src/display/core/Controller.cpp (1)
src/display/core/Controller.h (1)
  • pluginManager (134-134)
src/display/plugins/PurgeValvePlugin.h (4)
src/display/core/Plugin.h (1)
  • Plugin (6-12)
src/display/plugins/PurgeValvePlugin.cpp (4)
  • setup (10-21)
  • setup (10-10)
  • loop (23-35)
  • loop (23-23)
src/display/core/Controller.cpp (4)
  • setup (33-92)
  • setup (33-33)
  • loop (250-333)
  • loop (250-250)
src/display/core/Controller.h (1)
  • pluginManager (134-134)
src/display/plugins/PurgeValvePlugin.cpp (2)
src/display/core/Controller.cpp (4)
  • setup (33-92)
  • setup (33-33)
  • loop (250-333)
  • loop (250-250)
src/display/core/Controller.h (1)
  • pluginManager (134-134)
lib/NimBLEComm/src/NimBLEServerController.cpp (1)
lib/NimBLEComm/src/NimBLEComm.cpp (2)
  • get_token (3-23)
  • get_token (3-3)
lib/NimBLEComm/src/NimBLEClientController.cpp (1)
lib/NimBLEComm/src/NimBLEComm.cpp (2)
  • get_token (3-23)
  • get_token (3-3)
🪛 Clang (14.0.6)
src/display/plugins/PurgeValvePlugin.h

[error] 4-4: 'display/core/Plugin.h' file not found

(clang-diagnostic-error)

🔇 Additional comments (13)
lib/GaggiMateController/src/peripherals/Heater.h (1)

26-26: LGTM! Feed-forward parameter correctly added to setTunings.

The signature extension to four parameters (Kp, Ki, Kd, FF) is consistent with the implementation in Heater.cpp and the updated call sites.

lib/NimBLEComm/src/NimBLEClientController.cpp (1)

288-289: LGTM! Backward-compatible FF parameter parsing.

The use of get_token with a default value of "0" ensures backward compatibility when the autotune result contains only three parameters.

lib/NimBLEComm/src/NimBLEComm.h (1)

36-36: LGTM! Callback signature correctly extended.

The type alias update to include the FF parameter is consistent with the extended PID control flow throughout the codebase.

lib/NimBLEComm/src/NimBLEServerController.cpp (1)

239-242: LGTM! PID control parsing correctly extended.

The parsing of the FF parameter with a default value ensures backward compatibility, and the callback invocation matches the updated signature.

lib/GaggiMateController/src/GaggiMateController.cpp (1)

134-134: LGTM! PID control callback correctly extended.

The lambda properly accepts four parameters and forwards them to setTunings, maintaining consistency with the extended PID control flow.

src/display/core/Controller.cpp (3)

24-24: LGTM! PurgeValvePlugin properly included.


72-72: LGTM! PurgeValvePlugin correctly registered.

The registration follows the established pattern for other plugins in the system.


165-169: LGTM! Autotune callback correctly extended to include FF.

The callback signature, logging, and PID string formatting all properly handle the fourth parameter. The buffer size is adequate for the formatted output.

src/display/plugins/BLEScalePlugin.cpp (1)

6-16: Change is verified as intentional code cleanup.

The commit message "Remove unused scales" confirms this is deliberate. A codebase search found no references to the removed scale plugins (Acaia, Decent, Difluid, Eclair, Eureka, FelicitaScale, Timemore, Varia, WeighMyBrew, myscale) anywhere outside this file, confirming they were genuinely unused code. BookooScalesPlugin remains active.

This is legitimate cleanup of stale code, not a breaking change affecting users.

lib/GaggiMateController/src/peripherals/Heater.cpp (3)

23-23: LGTM: Feed-forward activation aligns with new FF parameter.

Enabling feed-forward control is appropriate now that the FF parameter is fully supported throughout the PID control stack.


68-74: LGTM: setTunings implementation correctly handles FF parameter.

The new 4-parameter signature properly checks all gains (including FF), updates the controller, resets the PID, and logs the new values.


121-121: LGTM: Correct FF parameter passed to setTunings.

The setTunings call correctly includes the FF parameter from the autotuner.

src/display/plugins/PurgeValvePlugin.h (1)

1-8: Header structure looks good.

The header guard, includes, and constant definition follow standard conventions. The static analysis error about Plugin.h not being found is a false positive from the sandbox environment.

Note: The PURGE_TIME_MS constant is set to 1500ms (1.5 seconds). Verify this duration is appropriate for your purge valve hardware and requirements.

@sonarqubecloud
Copy link

@jniebuhr
Copy link
Owner

Why are all the scales disabled?
This cannot be merged like this as it changes existing behavior in a way that would not be wanted for a lot of machines, these options can be used as additional behavior

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