-For stallable, there's a much more efficent way to implement this that dosen't require For loops (those are always the enemy of realtime embedded controls) - using a racheting error counter. Basically, you have a single number which is the number of iterations of good or bad data. You sample the data at a constant frequency and perform the delta comparison each time. If the data is good, you decrement the error counter by some number (for 100hz systems, 3 or 4 seems about right). If the data is bad, you increment the counter. To sample the state, you return a gt check on the counter vs a constant. To reset the state, you just reset the counter to 0.
psudocode:
Quote:
double val_last;
char counter;
#define VAL_NOISE_THRESH_LT 0.1
#define STALL_CNT_GT 10
bool is_stalled(double ana_val, bool reset)
{
if(abs(val_last - ana_val) < VAL_NOISE_THRESH_LT))
{
counter++;
}
else
{
counter -= 3;
}
counter = max(counter,0);
counter = min(counter,(VAL_NOISE_THRESH_LT + 2));
if(reset)
{
counter=0;
}
val_last = ana_val;
return(counter > STALL_CNT_GT);
}
|
You could also replace the entire limit switch class with an inlined function for efficiency. Also, I believe you want the comparison to be 0== instead of 1== due to the pull-up resistor (the logic line would idle high and be pulled low by pressing the switch, a 0== compare would return 1 when the switch is pressed).