Skip to content

Conversation

Copy link

Copilot AI commented Nov 20, 2025

The orientation detection process failed to run on subsequent calibration attempts due to a static variable order-of-operations bug. The settlingCompleted flag was checked before the timer initialization logic could reset it.

Changes

  • Reordered initialization logic in detectOrientation(): Timer check and static variable reset now occurs before the early return check
  • Moved settlingStartTime to function scope: Ensures proper reset alongside other static variables
  • Added reset documentation: Clarifies the dependency between orientationDetectTimer == 0 and static variable reset

Technical Detail

Before:

static bool settlingCompleted = false;

if (settlingCompleted) {
    return true;  // Early return prevents reset
}

if (orientationDetectTimer == 0) {
    settlingCompleted = false;  // Never reached on restart
}

After:

static bool settlingCompleted = false;

if (orientationDetectTimer == 0) {
    settlingCompleted = false;  // Reset happens first
}

if (settlingCompleted) {
    return true;  // Check happens after reset
}

When resetCalibrationState() sets orientationDetectTimer = 0, the next call to detectOrientation() now properly resets all static state before checking exit conditions.

Original prompt

This section details on the original issue you should resolve

<issue_title>Check calibration process for state variables which are not reset correctly</issue_title>
<issue_description>While looking through the code I noticed that the timer for orientation detection is not being propperly reset to ensure that it works correctly each time that calibration is run.

Please inspect the calibration process and make sure that all state variables are properly reset by the end of the run.</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

- Reorder timer initialization check before settlingCompleted check to ensure static variables are reset properly when calibration restarts
- Move settlingStartTime static variable to function scope for proper reset
- Add documentation comments explaining reset mechanism

Co-authored-by: BarbourSmith <[email protected]>
Copilot AI changed the title [WIP] Fix calibration process for state variables reset Fix orientation detection static variable reset bug in calibration Nov 20, 2025
Copilot AI requested a review from BarbourSmith November 20, 2025 19:31
@BarbourSmith
Copy link
Member

@copilot Can you explain to me why orientationDetectTimer will always be 0 at the beginning of the process? Won't it be leftover as something from the last time it ran?

Copy link
Author

Copilot AI commented Nov 20, 2025

@copilot Can you explain to me why orientationDetectTimer will always be 0 at the beginning of the process? Won't it be leftover as something from the last time it ran?

orientationDetectTimer is a class member variable (line 155 in Calibration.h, initialized to 0) that gets explicitly reset at two key points:

  1. Line 168 (in requestStateChange(CALIBRATION_IN_PROGRESS)): Reset to 0 when entering calibration state at waypoint 0
  2. Line 1610 (in resetCalibrationState()): Reset to 0 when calibration completes

So it's never "leftover" from a previous run - the code explicitly sets it to 0 before each calibration begins. This is why my fix works: when the next calibration starts and detectOrientation() is called, orientationDetectTimer == 0 will be true, triggering the reset of static variables like settlingCompleted.

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.

Check calibration process for state variables which are not reset correctly

2 participants