Hello Experts,
I am trying to write a function that counts the number of rotations of a motor. The function calculates the number of rotations based on the mechanical angle. The mechanical angle is updated in real time.
rotationCounterMA() is called periodically from a state machine.

This is what i have so far:
Code:
void rotationCounterMA(MOTOR_Vars_t *pMotor, uint16_t *rotationCounter, Databus_Vars_t *bus)
{
    static int rotationComplete = 0;
    const float32_t ROTATION_THRESHOLD = 0.95;
    static float32_t prevMechTheta;
    float32_t currentMechTheta = 0;
    currentMechTheta = floorf(pMotor->ptrFCL->qep.MechTheta * 100) / 100;   //get the mechanical position and truncate up to two decimal places. Ex: 0.1234 will become 0.12
    switch(bus->lockedDIR_MA)
    {
    case CLOCKWISE:
    {
        //Clockwise rotation (0.0 to 1.0)
        // Check for the rotation completion
        if (currentMechTheta < ROTATION_THRESHOLD && rotationComplete)
        {
            // Reset the flag when MechTheta goes below the threshold after completion
            rotationComplete = 0;
        }


        if (currentMechTheta >= ROTATION_THRESHOLD
                && !rotationComplete)
        {
            //Make sure that the motor is rotating
            if(prevMechTheta != currentMechTheta)
            {
                // Increment the counter when MechTheta crosses the threshold for the first time
                (*rotationCounter)++;
                rotationComplete = 1; // Set the flag to avoid multiple increments for the same rotation
            }
        }
        prevMechTheta = currentMechTheta;
    }
    break;
    case COUNTERCLOCKWISE:
    {
        //TODO To be completed..
        
        //Counter Clockwise rotation (1.0 to 0.0)
        
        // Check for counterclockwise rotation completion
/*         if (pMotor->ptrFCL->qep.MechTheta > (1.0 - ROTATION_THRESHOLD)
                && rotationComplete == 1)
        {
            rotationComplete = 0;
        }


        if (pMotor->ptrFCL->qep.MechTheta <= (1.0 - ROTATION_THRESHOLD)
                && rotationComplete == 0)
        {
            (*rotationCounter)++;
            rotationComplete = -1;
        } */
    }
    break;
    }
}
The function for the most part works well, but i found one potential bug:
Assume motor direction as clockwise and Lets say that the starting mechanical angle is 0.6, the moment the angle crosses ROTATION_THRESHOLD , the logic thinks that one rotation is complete, But that is not the case. Only if it crosses 0.6 again is one rotation actually complete.
At the moment there is always one rotation error.
Any idea on how to go about solving this issue?