Conversation
Make BemfState fields fully private (11 fields), move BEMF logic from isr_logic.rs and bemf.rs into BemfState methods in state.rs: - sync_config, update, zero_cross_detected, record_zero_cross - update_timing_from_timer, record_zc_timing, reset_for_step - reset_after_commutation, com_timer_delay Add DutyState PWM compare methods: - pwm_compare(tim1_arr) — duty to timer compare value - brake_compare(drag_brake_strength, tim1_arr) — brake output Fix pre-existing ramp bug: ramp_limit used duty.last > 500 instead of average_interval > 500 for RPM-based ramp profile selection (matches C). Add 4 unit tests for ramp profile selection. Extract 8 magic numbers into named constants in constants.rs. Simplify clamp_ceilings using .min() chain. state.rs pub(crate) fields: 37 → 13 (BemfState + DutyState fully private).
There was a problem hiding this comment.
Sorry @kaidokert, you have reached your weekly rate limit of 1500000 diff characters.
Please try again later or upgrade to continue using Sourcery
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughThe PR refactors BEMF (Back-EMF) state management by centralizing comparator sampling, zero-cross detection, and timing logic into a cohesive Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Review rate limit: 4/5 reviews remaining, refill in 12 minutes. Comment |
❌ 2 Tests Failed:
View the full list of 2 ❄️ flaky test(s)
To view more test analytics, go to the Test Analytics Dashboard |
There was a problem hiding this comment.
Code Review
This pull request refactors the BEMF zero-cross detection logic by encapsulating state and behavior within the BemfState struct, moving logic out of the ISR and improving modularity. Two critical bugs were identified in the timing calculations where u32 values are truncated to u16 before division or shifting, which could lead to incorrect timing at low RPM.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@rm32/src/control/state.rs`:
- Around line 430-434: reset_for_step currently resets only counter and
bad_count but must also clear the zero-cross flag; update the method
reset_for_step to set self.zc_found = false so that subsequent
zero_cross_detected() and zero_crosses() logic works correctly after calling
record_zero_cross() and bemf_polling(); locate reset_for_step in the same state
struct and add clearing of zc_found (referenced symbols: reset_for_step,
zc_found, record_zero_cross, bemf_polling, zero_cross_detected, zero_crosses,
MIN_ZC_FOR_ADVANCE).
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 04516634-22b7-4e3f-a304-1048eeeaa3c2
📒 Files selected for processing (5)
.gitignorerm32/src/bemf.rsrm32/src/control/isr_logic.rsrm32/src/control/state.rsrm32/src/control/tests.rs
Two wait_time bugs: record_zero_cross and update_timing_from_timer truncated new_ci to u16 BEFORE dividing by 2. At low RPM (new_ci > 65535), this produces completely wrong wait_time. C code divides the u32 value first, then truncates on assignment to u16. reset_for_step was missing zc_found = false. C code clears zcfound together with bemfcounter after every commutation step. Without this, zero_cross_detected() stays false after the first detection in the startup path, stalling old_routine startup.
Summary
BemfState encapsulation — all 11 fields private, BEMF logic moved from
isr_logic.rs into BemfState methods: sync_config, update, zero_cross_detected,
record_zero_cross, update_timing_from_timer, record_zc_timing, reset_for_step,
reset_after_commutation, com_timer_delay. bemf.rs reduced to tests.
DutyState PWM methods — pwm_compare() and brake_compare() extract inline
duty-to-timer math from ISR.
Ramp bug fix — ramp_limit used duty.last > 500 (wrong) instead of
average_interval > 500 (C parity) for RPM-based ramp profile selection.
4 unit tests added that would have caught this.
Constants — 8 magic numbers extracted to named constants in constants.rs.
clamp_ceilings simplified with .min() chain.
state.rs pub(crate) fields: 37 → 13 (BemfState 11 + DutyState 13 made private).
Test plan
Summary by CodeRabbit
Refactor
Tests