Skip to content

Conversation

@derekhe
Copy link

@derekhe derekhe commented Oct 5, 2025

Add 5s and 10s average of ampere to help calibrate ampere sensor.

Summary by CodeRabbit

  • New Features
    • Display rolling amperage averages over 5s and 10s for Motors and Battery, updating in real time.
  • UI
    • Added new rows/elements in Motors and Power sections to show 5s/10s average amperage values.
  • Localization
    • Added English and Simplified Chinese translations for new amperage average labels and values.

Copilot AI review requested due to automatic review settings October 5, 2025 09:17
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 5, 2025

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

Walkthrough

Adds i18n keys for 5s/10s amperage averages, updates motors and power tabs to track rolling amperage histories and compute/display 5s/10s averages, and extends corresponding HTML templates with new rows/elements for these metrics. No removals; existing refresh cycles now update new UI fields.

Changes

Cohort / File(s) Summary
Localization: EN and ZH
locales/en/messages.json, locales/zh_CN/messages.json
Added translation keys: motorsAmperageAverage5s/10s, motorsAmperageAverage5sValue/10sValue, powerBatteryAmperageAverage5s/10s.
Motors logic/UI wiring
src/js/tabs/motors.js
Introduced 5s/10s amperage histories and sizes; on refresh, push/trim histories, compute averages, update UI elements for motors averages.
Power logic/UI wiring
src/js/tabs/power.js
Added 5s/10s amperage histories and sizes; on refresh, maintain rolling windows, compute averages, update battery average UI elements.
Motors UI markup
src/tabs/motors.html
Added display elements for motors amperage 5s/10s averages with new i18n labels and value cells.
Power UI markup
src/tabs/power.html
Added two rows in battery section for 5s/10s amperage averages with i18n labels and value cells.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor User
  participant Telemetry as Telemetry Stream
  participant MotorsTab as Motors Tab JS
  participant PowerTab as Power Tab JS
  participant DOM as UI Elements

  User->>Telemetry: Start data feed
  Telemetry-->>MotorsTab: amperage
  Telemetry-->>PowerTab: amperage

  rect rgba(230,246,255,0.5)
    note over MotorsTab,PowerTab: On each refresh tick
    MotorsTab->>MotorsTab: push amperage to 5s/10s histories<br/>trim to sizes (20/40)
    MotorsTab->>MotorsTab: compute avg5s, avg10s
    MotorsTab->>DOM: update motors avg5s/avg10s elements
    PowerTab->>PowerTab: push amperage to 5s/10s histories<br/>trim to sizes (25/50)
    PowerTab->>PowerTab: compute avg5s, avg10s
    PowerTab->>DOM: update battery avg5s/avg10s elements
  end

  note right of DOM: Labels from i18n (EN/zh_CN)
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Pre-merge checks and finishing touches

❌ Failed checks (2 warnings)
Check name Status Explanation Resolution
Description Check ⚠️ Warning The PR description consists solely of a one-line summary and does not remove the template text or include the structured sections (such as detailed change explanations, motivation, and testing instructions) required by the repository template. This omission leaves out critical context like specific file modifications, related issue references, and CI verification steps. Accordingly, the description fails to meet the repository’s pull request description guidelines. Please remove the template placeholder text and expand the description to include the structured sections mandated by the template—detailed summary of changes, motivation, testing instructions, and related issue references—while clearly outlining the specific file modifications and any CI verification steps. Adhering to the repository’s PR template will provide reviewers with the context needed to evaluate the changes effectively.
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (1 passed)
Check name Status Explanation
Title Check ✅ Passed The title succinctly summarizes the primary change—adding 5-second and 10-second amperage averages to support sensor calibration—and accurately reflects the functionality introduced by the changeset.
✨ 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: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cea9bbc and 6943b61.

📒 Files selected for processing (1)
  • src/js/tabs/motors.js (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/js/tabs/motors.js

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

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds 5-second and 10-second rolling averages for amperage readings to help calibrate ampere sensors. The implementation provides more stable amperage readings by calculating moving averages over recent measurements.

Key changes:

  • Added amperage averaging logic with configurable history buffers
  • Extended UI to display 5s and 10s average amperage values in both power and motors tabs
  • Added localization support for the new average amperage labels

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/tabs/power.html Added table rows for displaying 5s and 10s amperage averages
src/tabs/motors.html Added span elements for displaying amperage averages in motors tab
src/js/tabs/power.js Implemented amperage averaging logic and UI updates for power tab
src/js/tabs/motors.js Implemented amperage averaging logic and UI updates for motors tab
locales/zh_CN/messages.json Added Chinese translations for new amperage average labels
locales/en/messages.json Added English translations for new amperage average labels

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines 11 to +17
const power = {
supported: false,
analyticsChanges: {},
amperageHistory5s: [], // Store last 5 seconds of amperage readings for average calculation
amperageHistorySize5s: 25, // Number of readings to keep for 5 seconds average (5Hz * 5s = 25)
amperageHistory10s: [], // Store last 10 seconds of amperage readings for average calculation
amperageHistorySize10s: 50, // Number of readings to keep for 10 seconds average (5Hz * 10s = 50)
Copy link

Copilot AI Oct 5, 2025

Choose a reason for hiding this comment

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

The comment assumes a 5Hz update rate, but this assumption should be documented or made configurable since update rates may vary depending on hardware or configuration.

Suggested change
const power = {
supported: false,
analyticsChanges: {},
amperageHistory5s: [], // Store last 5 seconds of amperage readings for average calculation
amperageHistorySize5s: 25, // Number of readings to keep for 5 seconds average (5Hz * 5s = 25)
amperageHistory10s: [], // Store last 10 seconds of amperage readings for average calculation
amperageHistorySize10s: 50, // Number of readings to keep for 10 seconds average (5Hz * 10s = 50)
// Configurable update rate in Hz (readings per second). Change this value if hardware/configuration differs.
const updateRateHz = 5;
const power = {
supported: false,
analyticsChanges: {},
amperageHistory5s: [], // Store last 5 seconds of amperage readings for average calculation
amperageHistorySize5s: updateRateHz * 5, // Number of readings to keep for 5 seconds average (updateRateHz * 5s)
amperageHistory10s: [], // Store last 10 seconds of amperage readings for average calculation
amperageHistorySize10s: updateRateHz * 10, // Number of readings to keep for 10 seconds average (updateRateHz * 10s)

Copilot uses AI. Check for mistakes.
Comment on lines +37 to +39
amperageHistorySize5s: 20, // Number of readings to keep for 5 seconds average (4Hz * 5s = 20)
amperageHistory10s: [], // Store last 10 seconds of amperage readings for average calculation
amperageHistorySize10s: 40, // Number of readings to keep for 10 seconds average (4Hz * 10s = 40)
Copy link

Copilot AI Oct 5, 2025

Choose a reason for hiding this comment

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

The motors tab uses different update rate assumptions (4Hz) compared to the power tab (5Hz). This inconsistency could lead to different averaging behaviors between tabs and should be standardized.

Suggested change
amperageHistorySize5s: 20, // Number of readings to keep for 5 seconds average (4Hz * 5s = 20)
amperageHistory10s: [], // Store last 10 seconds of amperage readings for average calculation
amperageHistorySize10s: 40, // Number of readings to keep for 10 seconds average (4Hz * 10s = 40)
amperageHistorySize5s: 25, // Number of readings to keep for 5 seconds average (5Hz * 5s = 25)
amperageHistory10s: [], // Store last 10 seconds of amperage readings for average calculation
amperageHistorySize10s: 50, // Number of readings to keep for 10 seconds average (5Hz * 10s = 50)

Copilot uses AI. Check for mistakes.
const amperageAverage5s = amperageSum5s / TABS.motors.amperageHistory5s.length;

// 10 seconds average
TABS.motors.amperageHistory10s.push(currentAmperage);
Copy link

Copilot AI Oct 5, 2025

Choose a reason for hiding this comment

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

Using TABS.motors instead of self is inconsistent with the pattern used in the power tab and makes the code less maintainable. Consider using self for consistency.

Copilot uses AI. Check for mistakes.
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: 0

🧹 Nitpick comments (1)
src/js/tabs/motors.js (1)

652-678: Consider using self for consistency.

The amperage averaging implementation is correct and follows sound logic. However, the code uses TABS.motors.amperageHistory5s and TABS.motors.amperageHistory10s instead of self.amperageHistory5s and self.amperageHistory10s. While functionally equivalent, this is inconsistent with the rest of the file, which predominantly uses self to reference instance properties.

Apply this diff to align with the file's conventions:

 // 5 seconds average
-TABS.motors.amperageHistory5s.push(currentAmperage);
-if (TABS.motors.amperageHistory5s.length > TABS.motors.amperageHistorySize5s) {
-    TABS.motors.amperageHistory5s.shift();
+self.amperageHistory5s.push(currentAmperage);
+if (self.amperageHistory5s.length > self.amperageHistorySize5s) {
+    self.amperageHistory5s.shift();
 }
-const amperageSum5s = TABS.motors.amperageHistory5s.reduce((sum, value) => sum + value, 0);
-const amperageAverage5s = amperageSum5s / TABS.motors.amperageHistory5s.length;
+const amperageSum5s = self.amperageHistory5s.reduce((sum, value) => sum + value, 0);
+const amperageAverage5s = amperageSum5s / self.amperageHistory5s.length;

 // 10 seconds average
-TABS.motors.amperageHistory10s.push(currentAmperage);
-if (TABS.motors.amperageHistory10s.length > TABS.motors.amperageHistorySize10s) {
-    TABS.motors.amperageHistory10s.shift();
+self.amperageHistory10s.push(currentAmperage);
+if (self.amperageHistory10s.length > self.amperageHistorySize10s) {
+    self.amperageHistory10s.shift();
 }
-const amperageSum10s = TABS.motors.amperageHistory10s.reduce((sum, value) => sum + value, 0);
-const amperageAverage10s = amperageSum10s / TABS.motors.amperageHistory10s.length;
+const amperageSum10s = self.amperageHistory10s.reduce((sum, value) => sum + value, 0);
+const amperageAverage10s = amperageSum10s / self.amperageHistory10s.length;

Optional: Add defensive null check and extract duplicate logic.

Consider adding a guard for FC.ANALOG.amperage to prevent potential NaN values:

const currentAmperage = FC.ANALOG.amperage ?? 0;

Additionally, the 5s and 10s averaging logic is duplicated. You could optionally extract this into a helper function to reduce duplication:

function updateAmperageAverage(history, maxSize, currentValue) {
    history.push(currentValue);
    if (history.length > maxSize) {
        history.shift();
    }
    const sum = history.reduce((acc, val) => acc + val, 0);
    return sum / history.length;
}
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e65dc56 and cea9bbc.

📒 Files selected for processing (6)
  • locales/en/messages.json (3 hunks)
  • locales/zh_CN/messages.json (3 hunks)
  • src/js/tabs/motors.js (4 hunks)
  • src/js/tabs/power.js (3 hunks)
  • src/tabs/motors.html (1 hunks)
  • src/tabs/power.html (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
src/js/tabs/power.js (2)
src/js/fc.js (1)
  • FC (132-1006)
src/js/localization.js (1)
  • i18n (7-7)
src/js/tabs/motors.js (2)
src/js/gui.js (1)
  • TABS (8-8)
src/js/localization.js (1)
  • i18n (7-7)
🔇 Additional comments (10)
src/tabs/power.html (1)

126-133: Battery average rows look good
The new 5 s/10 s rows fit the existing template structure and line up with the IDs consumed in power.js. 👍

src/tabs/motors.html (1)

282-283: Motors power panel update looks solid
Nice addition—labels/classes match the JS bindings for the new rolling averages.

locales/zh_CN/messages.json (2)

2242-2262: Thanks for keeping zh_CN in sync
The added keys cover the new amperage averages and follow the existing wording style.

Also applies to: 2242-2262


4015-4020: Battery average labels translated consistently
Good to see the power tab labels localized alongside the motors entries.

src/js/tabs/power.js (3)

14-17: State additions make sense
Persisting separate 5 s/10 s histories right on the tab state keeps the logic tidy. Good call.


23-26: Nice reset hook
Resetting both histories during tab init prevents stale averages when revisiting the tab.


235-369: Average computation reads clearly
The rolling buffer + reduce pattern is straightforward and pairs cleanly with the new UI rows. 👍

src/js/tabs/motors.js (3)

36-39: LGTM!

The amperage history size calculations are correct. The 4Hz sampling rate (250ms interval at line 682) multiplied by the time windows yields the correct array sizes: 20 samples for 5 seconds and 40 samples for 10 seconds.


84-86: LGTM!

Properly resetting the amperage history arrays on initialization ensures a clean state.


474-475: LGTM!

Caching the UI element references follows the established pattern in this file and improves performance by avoiding repeated DOM queries.

@sonarqubecloud
Copy link

sonarqubecloud bot commented Oct 5, 2025

Quality Gate Failed Quality Gate failed

Failed conditions
23.7% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

@github-actions
Copy link
Contributor

github-actions bot commented Oct 5, 2025

@derekhe derekhe closed this Oct 5, 2025
@derekhe derekhe deleted the feature/average-of-ampere branch October 5, 2025 11:28
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.

1 participant