I need some serious help here, so, I have been using the Hall effect sensors to count gear teeth and get an RPS for the wheels. It works pretty well. I was told to check the sensor every millisecond, so I set an interrupt to fire every millisecond and do a little math.
Code:
// this function will be called when a timer 2 interrupt occurs
static unsigned char lastHighL = 0; //The last value seen was a high
static unsigned char lastLowL = 0; //The last value seen was a low
static unsigned char lastHighR = 0; //The last value seen was a high
static unsigned char lastLowR = 0; //The last value seen was a low
msClock++; // increment the millisecond clock
if(msClock > 0 && msClock % 26 ==0)
return;
//Count the changed between highs and lows
if(Get_Analog_Value(rc_ana_in01) > 900 && lastLowL)
{
lastHighL=1;
lastLowL=0;
leftTicks++;
}
else if(Get_Analog_Value(rc_ana_in01) < 100 && lastHighL)
{
lastHighL=0;
lastLowL=1;
}
else if(!lastHighL && !lastLowL && Get_Analog_Value(rc_ana_in01) > 900)
{
lastHighL=1;
lastLowL=0;
}
else if(!lastHighL && !lastLowL && Get_Analog_Value(rc_ana_in01) < 100)
{
lastHighL=0;
lastLowL=1;
}
if(Get_Analog_Value(rc_ana_in02) > 900 && lastLowR)
{
lastHighR=1;
lastLowR=0;
rightTicks++;
}
else if(Get_Analog_Value(rc_ana_in02) < 100 && lastHighR)
{
lastHighR=0;
lastLowR=1;
}
else if(!lastHighR && !lastLowR && Get_Analog_Value(rc_ana_in02) > 900)
{
lastHighR=1;
lastLowR=0;
}
else if(!lastHighR && !lastLowR && Get_Analog_Value(rc_ana_in02) < 100)
{
lastHighR=0;
lastLowR=1;
}
if(leftTicks == GEAR_TEETH)
{
leftTicks=0;
leftRevo++;
}
if(rightTicks == GEAR_TEETH)
{
rightTicks=0;
rightRevo++;
}
//Every second, increment secClock
if(msClock%1000 == 0 && msClock>0)
{
secClock++;
}
//Calculate RPS every 4 seconds
if(secClock>0 && secClock % 4 ==0)
{
leftRPS=leftRevo/secClock;
rightRPS=rightRevo/secClock;
}
This is what I have going right now in that interrupt, most of it is just incrementing variables and such, so it shouldn't be too much, though I am worried that the division is taking too long. Is there anything I can do to optimze this? Or am I overlooking something? I told the interrupt to return immediately after starting if the number of milliseconds is divisible by 26, so every 26ms, return without doing any processing (I'm concerned that all that code may take more than .2 ms, and I think the 1ms check should be more than enough to skip once without too many detrimental effects, though I could be wrong). I then plugged it into the dashboard to try and get more details, as the manual suggests, and it tells me "User violation: Invalid CONTROL or CURRENT_MODE byte". Any clue what that means? I sure have no clue....... Any help would be wonderful, or ideas, or better ideas. :'( :'( :'( :'( I'm going nuts, and I have an angry teacher glaring at me and breathing down my neck...... That would be grand if any of you can help