Yet another iteration of nothing
Random header image... Refresh for more!

KFLOP C Programming Dissection – My Init.c E-Stop Code

#include “KMotionDef.h”

int elast=0,elastsolid=-1,ecount=0; // for debouncing estop pushbutton
int Debounce(int n, int *cnt, int *last, int *lastsolid);
int DoPC(int cmd);

#define ESTOP 168 // set to the external estop input bit
#define TMP 10 // which spare persist to use to transfer data
#include “C:\KMotion433\C Programs\KflopToKMotionCNCFunctions.c”

int main()
{
double T0, LastX=0, LastY=0, LastZ=0, Tau;
int result;
int BitA,Change1=0,Change2=0, DiffX2;
int PosNoWrap, NewPos, Pos=0, wraps;
int InMotion=FALSE,Axis,LastAxis=-1;
double LastChangeTime=0,Target,Factor=0;

// Main program starts here; includes ESTOP, MPGSmooth subroutines
for (;;) //Main program, cycle forever
{
WaitNextTimeSlice();

// Handle ESTOP interrupts
result = Debounce(ReadBit(ESTOP),&ecount,&elast,&elastsolid);
if (result == 0)
{
DoPC(PC_COMM_ESTOP);
printf(“Local ESTOP Active!\n”);
MsgBox(“Local ESTOP ACTIVE!”,MB_OK|MB_ICONEXCLAMATION);
}
}

// 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;
}