Hi Dan,
I also realised that this soft limits code doesn't work when i'm doing a tool change (M6 C Program) or when homing the machine (Homing C Program). How do I keep this program active when other C programs are being initiated?
I'm not sure how you are running the code. If you are running it in some Thread and then run some other program (Tool change or home) in the same Thread then the new program will kill and overwrite the Limits program. Normally you would add the Limits checking to a forever loop at the end of your Initialization program.
I see that there are 2 Soft Limit files in the example C code Folder. Does the Rev 2 file include the back up approach for a simple soft limit working envelope?
Yes the idea is to only disable if in the Limit and also moving in the direction of the limit. That is an older example there is now a better variable available called ch->DirectionOfMotion
Here is a program that might work (untested ) It should only disable if moving inward within either of your zones. That should allow you to re-initialize and remain enabled even if you are within a zone as long as you don't move the wrong direction.
Actually I only now realize that this assumes the machine coordinates are still correct after a re-initialization. Is this true? Also before Homing we don't know where we are or where the zones are so we can't really do any protection. How are you handling this now?
Code:
#include "KMotionDef.h"
#define XRes 19190.2062692 //Counts/inch
#define YRes 22854.4476 //Counts/inch
#define ZRes 28853.846 //Counts/inch
#define XMIN_Head 40
#define YMIN_Head 15
#define XMAX_Head 54
#define YMAX_Head 34
#define XMIN_Tail 60
#define YMIN_Tail 20
#define XMAX_Tail 70
#define YMAX_Tail 34
int X1=XMIN_Head*XRes;
int X2=XMAX_Head*XRes;
int X3=XMIN_Tail*XRes;
int X4=XMAX_Tail*XRes;
int Y1=YMIN_Head*YRes;
int Y2=YMAX_Head*YRes;
int Y3=YMIN_Tail*YRes;
int Y4=YMAX_Tail*YRes;
void HandleZone(double minx, double maxx, double miny, double maxy);
BOOL CheckInRange(double x, double min, double max, int *dir);
main()
{
for (;;) // loop forever
{
WaitNextTimeSlice();
HandleZone(X1,X2,Y1,Y2); // disable if moving in a bad direction in Head Zone
HandleZone(X3,X4,Y3,Y4); // disable if moving in a bad direction in Tail Zone
}
}
// Disable all if we are within a Zone and either x or y are moving in a bad direction
void HandleZone(double minx, double maxx, double miny, double maxy)
{
int dirx, diry;
if (CheckInRange(ch0->Dest, minx, maxx, &dirx) &&
CheckInRange(ch1->Dest, miny, maxy, &diry) &&
(ch0->DirectionOfMotion == dirx || ch1->DirectionOfMotion == diry))
{
DisableAxis(0);
DisableAxis(1);
DisableAxis(2);
}
}
// check if within range and if so return direction that
// would move further inward from the closest edge
// returns true if within range
BOOL CheckInRange(double x, double min, double max, int *dir)
{
if (x>min && x<max) // within range?
{
if (x-min < max-x) // closer to min?
*dir = 1; // yes, positive would go further inward
else
*dir = -1; // no, negative would move further inward
return TRUE;
}
return FALSE;
}