Ok, I think I figured out a software filter to help me look for persistent signals and ignore narrow spikes with a modification of the ExternalButtons.c example. It seems to work, but I wanted to take a second and see if you think there are reasons not to go this route. There is the delay in the response of my limit switches, but I don't plan on traveling faster than 120IPM. I haven't done the math yet, but the response seems to be almost instantaneous and I've got some play in my switches for the extra travel.
The code I used is below:
Code:
#include "KMotionDef.h"
#define INXLIMITBIT 6 //Bit connected to Limit Switch
#define OUTXLIMITBIT 48 //Bit monitored by Kflop for Limit Activation
// function prototypes for compiler
int Debounce(int n, int *cnt, int *last, int *lastsolid);
// state variables for switch debouncing
int xlast=0,xlastsolid=-1,xcount=0;
main()
{
int result;
for (;;) // loop forever
{
WaitNextTimeSlice();
// Handle Limit X
result = Debounce(ReadBit(INXLIMITBIT),&xcount,&xlast,&xlastsolid);
if (result == 1)
{
SetBit(OUTXLIMITBIT);
}
else if (result == 0)
{
ClearBit(OUTXLIMITBIT);
}
}
}
// Debounce a bit
//
// return 1 one time when first debounced high
// return 0 one time when first debounced low
// return -1 otherwise
#define DBTIME 300
int Debounce(int n, int *cnt, int *last, int *lastsolid)
{
int v = -1;
if (n == *last) // same as last time?
{
if (*cnt == DBTIME-1)
{
if (n != *lastsolid)
{
v = *lastsolid = n; // return debounced value
}
}
if (*cnt < DBTIME) (*cnt)++;
}
else
{
*cnt = 0; // reset count
}
*last = n;
return v;
}
During testing I ran into some odd behavior from the control loop. After completing a Move command from the Console it would oscillate quickly around Zero. Like shown below. Which value could I adjust to fix this? I tried playing around with 'Freq' in the 2nd Order Low Pass Filter, but it didn't seem to make a difference after Downloading to the Kflop.