> 0 being the 0th Mechanical degree and 1.0 being the 360th mechanical degree.
So 0 and 100 are the same thing in your scaled result.
Having two values for the same thing might be a problem.
Like when is it 0, and when is it 100?
Oh, and I wouldn't bother dividing by 100 again in your code. Just keep it as an integer in the range 0 to 100.
> How do i remove the first rotation bug? Any idea?
Consider two initial positions
0 - some arbitrary direction at the zero point
50 - a point at 180' to the zero point.
All these examples constitute the completion of a rotation.
clockwise
Code:
previous initial current
90 0 10
40 50 60
counter-clockwise
Code:
previous initial current
10 0 90
60 50 40
Then I might try something like this.
Code:
#define FLOAT_TO_INT_ANGLE 100
bool clockwise_crossed(int initial, int previous, int current) {
if ( initial < previous ) initial += FLOAT_TO_INT_ANGLE;
if ( current < previous ) current += FLOAT_TO_INT_ANGLE;
return ( previous < initial && initial <= current );
}
typedef struct {
int initial;
int previous;
int count;
} rotate_state_t;
void rotationCounterMA(MOTOR_Vars_t *pMotor, rotate_state_t *state, Databus_Vars_t *bus)
{
int current = pMotor->ptrFCL->qep.MechTheta * FLOAT_TO_INT_ANGLE;
switch(bus->lockedDIR_MA)
{
case CLOCKWISE:
if ( clockwise_crossed(state->initial, state->previous, current) ) {
state->count++;
}
state->previous = current;
break;
}
}