Conversation
MainState had e_rpm, average_interval, last_average_interval, and commutation_intervals as loose scalars duplicating TimingState. Trim TimingState to only the 4 main-loop-owned fields (ISR timing lives in SharedComm), derive Default, and adopt as MainState::timing. Also removes dead TimingState fields: commutation_interval, zero_crosses, e_com_time (all in SharedComm), and polling_mode_changeover (hardcoded constant in isr_logic.rs).
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 (3)
📝 WalkthroughWalkthroughThis change consolidates timing-related fields in Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 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: 3/5 reviews remaining, refill in 14 minutes and 14 seconds. 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 MainState structure by encapsulating timing-related fields into a new TimingState struct and removing fields that are now managed by the ISR in SharedComm. While the refactoring improves code organization, two significant issues were identified: the commutation_intervals array in MainState is currently unpopulated in production because the ISR cannot access it, which will result in incorrect telemetry and timing calculations. Additionally, the desync reset logic for average_interval is ineffective as it is immediately overwritten by calculations derived from the un-reset commutation_intervals buffer.
| let sum: u32 = self | ||
| .timing | ||
| .commutation_intervals | ||
| .iter() | ||
| .map(|&v| v as u32) | ||
| .sum(); |
There was a problem hiding this comment.
The commutation_intervals array in MainState is currently unpopulated in the actual firmware path. While the test harness (harness.rs) manually updates this array, the firmware's ISR logic (isr_logic.rs) only has access to SharedState, which does not contain this array. Consequently, sum will always be zero in production, causing e_com_time, e_rpm telemetry, and average_interval to be calculated incorrectly.
Consider either moving the intervals array to SharedState so the ISR can update it, or simplifying the calculation to use the singular shared.commutation_interval() (e.g., let e_com_time = (shared.commutation_interval() * 3) as i32;) if per-step averaging is not strictly required.
References
- Modules without direct HAL access should communicate with the ISR via shared state to ensure data consistency and proper hardware interaction, as the ISR is the primary interface for hardware-level updates.
| // Check before zeroing zero_crosses (C has this after, which is a bug) | ||
| if zc > 100 { | ||
| self.average_interval = DESYNC_RESET_INTERVAL; | ||
| self.timing.average_interval = DESYNC_RESET_INTERVAL; |
There was a problem hiding this comment.
Resetting average_interval here is ineffective because it is recalculated at the beginning of every tick() (line 212) based on e_com_time, which in turn is derived from the commutation_intervals array. To make this reset persistent across ticks, you must also reset the underlying intervals array.
self.timing.average_interval = DESYNC_RESET_INTERVAL;
self.timing.commutation_intervals.fill(DESYNC_RESET_INTERVAL as u16);References
- When updating or resetting control state, all internal buffers and terms contributing to that state must be cleared to prevent stale data from causing transient calculation errors.
Summary
TimingStateto 4 main-loop-owned fields (e_rpm,average_interval,last_average_interval,commutation_intervals)commutation_interval,zero_crosses,e_com_time) and a hardcoded constant (polling_mode_changeover)MainState::timing: TimingStatereplacing 4 loose scalar fieldsTest plan
Summary by CodeRabbit