585,741 active members*
5,240 visitors online*
Register for free
Login
Results 1 to 13 of 13
  1. #1
    Join Date
    Feb 2008
    Posts
    216

    Estop Watchdog command in Initialization File

    Hi Tom,

    This may be a question you have answered clearly before but I haven’t been able to find it.
    I have a few basic questions, may sound stupid but I’m no programmer.
    1. I am trying to set up the Estop for my machine along with a few other things. You have mentioned before that you need to include a watch dog loop in the initialization file for the Estop to work properly. I saw they watchdog example c code in the Kmotion folder and I think I can get it to work, my question is where in the Initialization file code should I copy & paste this watchdog code? Beginning, end?

    2. In Kmotion, there are the 7 threads when you open the C program screen. If I had 7 C-code files open in each of these threads, are all 7 programs actively running in Kmotion while the system is running? I’m curious because I would like a way to test a segment of code before I copy it into a file that I know works for the machine. I know in KmotionCNC you can assign the buttons a thread, but how would I have a watchdog test code always running in parallel with my initialization file while running kmontioncnc?

    3. Along side the E-stop, I want to include some sanity checks into the initilization program to make sure everything is working before any axis are initialized i.e. Compressed Air is reaching the machine, adequate pressure is maintained,no blockages etc. Also I have a pneumatic lock on the Z axis to stop it from dropping when the machine is de-energized. There is a proximity switch on it and it’s set up in Kflop as an input. Say IObit100 for this example. I see there is a readbit command which looks for a change in the input, but is there a command that says if bit100=high, do this…. Else, do this…
    I tried if (ReadBit(100)=1) do this…. but I get an error.

    I’m worried that if I’m relying just on a change in the bit from high to low, during the next cycle it would miss this change and continue on. There are going to be a few other pressure switches with the same sanity check and I just want to make sure that the code is sound.

    So is the ReadBit(N) command relative to the state from the previous loop or does it always assume the Bit is in a fixed state (high/low?) and checks for the change from that fixed state.

    I hope you can understand what I’m asking, I’m confusing myself the more I type.

    Code:
    #include "KMotionDef.h"
    
    main()
    {
    	for (;;)					// repeat forever
    	{
    		WaitNextTimeSlice();	// execute one loop per time slice
    		
    		// check if Lock is raised, (InputI/O 100 is high)
    		if (ReadBit(100))
    
    		{
    			EnableAxis(2); //Enable the Z axis
    		}
    		else
    		{
    			DisableAxis(2); // Disable the Z axis
    			printf("Z Ballscrew is Air-Locked \n");  // send message to console
    		}
    	}
    }

  2. #2
    Join Date
    May 2006
    Posts
    4045

    Re: Estop Watchdog command in Initialization File

    Hi slimneill,

    Regarding:

    I am trying to set up the Estop for my machine along with a few other things. You have mentioned before that you need to include a watch dog loop in the initialization file for the Estop to work properly. I saw they watchdog example c code in the Kmotion folder and I think I can get it to work, my question is where in the Initialization file code should I copy & paste this watchdog code? Beginning, end?
    Put the watchdog loop inside the "main" function toward the end. That way your program will perform all the initialization and then begin looping watching for EStop and so forth. The beginning and end of the "main" program function is marked by "curly brackets". Non C programmers sometimes don't understand the concept of curly brackets. You might read this:

    Fundamentals of C Programming/Preliminaries - Wikibooks, open books for an open world


    In Kmotion, there are the 7 threads when you open the C program screen. If I had 7 C-code files open in each of these threads, are all 7 programs actively running in Kmotion while the system is running? I’m curious because I would like a way to test a segment of code before I copy it into a file that I know works for the machine. I know in KmotionCNC you can assign the buttons a thread, but how would I have a watchdog test code always running in parallel with my initialization file while running kmontioncnc?
    KFLOP can run up to 7 User programs concurrently. But just because a the C Program Screen shows a C Program in the edit window for a Thread does not mean that it is running in KFLOP. It must be Compiled, Downloaded, and Executed for it to be running. If you want to test some code load it into an unused Thread in KMotion.exe and compile/download/execute it. After it is tested all your functions can be combined into one loop in your initialization program. Here is some more info.


    Along side the E-stop, I want to include some sanity checks into the initilization program to make sure everything is working before any axis are initialized i.e. Compressed Air is reaching the machine, adequate pressure is maintained,no blockages etc. Also I have a pneumatic lock on the Z axis to stop it from dropping when the machine is de-energized. There is a proximity switch on it and it’s set up in Kflop as an input. Say IObit100 for this example. I see there is a readbit command which looks for a change in the input, but is there a command that says if bit100=high, do this…. Else, do this…
    I tried if (ReadBit(100)=1) do this…. but I get an error.

    I’m worried that if I’m relying just on a change in the bit from high to low, during the next cycle it would miss this change and continue on. There are going to be a few other pressure switches with the same sanity check and I just want to make sure that the code is sound.

    So is the ReadBit(N) command relative to the state from the previous loop or does it always assume the Bit is in a fixed state (high/low?) and checks for the change from that fixed state.
    The ReadBit function returns the state or level of the input. Not that it changed.

    In C Language a single equals symbol "=" is an assignment operator. For example

    X=5;

    Copies the value of 5 into X

    The operator to compare two things is a double equals "==". So for example:

    if (ReadBit(100) == 1) ........

    KFLOP should only be disabled or enables once, not continuously over and over which your attached program would do. There are a number of ways to handle this. The Example ExternalButtons.c contains a helper function called "Debounce" that can be used to monitor an input and return exactly one time a 1 or 0 when the input transitions high or low (otherwise it returns -1 for no changes). If your input is noisy or doesn't transition cleanly then you should use that method.

    A simpler method is to check if the axes are already enabled or disabled and then only enable or disable the axis if not already enabled or disabled. Such as:

    Code:
    #include "KMotionDef.h"
    
    main()
    {
    	for (;;)					// repeat forever
    	{
    		WaitNextTimeSlice();	// execute one loop per time slice
    		
    		// check if not enabled and Lock is false, (InputI/O 100 is low)
    		if (!ch2->Enabled && ReadBit(100)==0)
    		{
    			EnableAxis(2); //Enable the Z axis
    		}
    		
    		// check if Enabled and Lock is raised, (InputI/O 100 is high)
    		if (ch2->Enabled && ReadBit(100)==1)
    		{
    			DisableAxis(2); // Disable the Z axis
    			printf("Z Ballscrew is Air-Locked \n");  // send message to console
    		}
    	}
    }
    HTH
    Regards
    TK
    http://dynomotion.com

  3. #3
    Join Date
    Feb 2008
    Posts
    216

    Re: Estop Watchdog command in Initialization File

    Thanks!!!!!!!!!!!!!!!!

    It's a lot more clear now.

  4. #4
    Join Date
    Feb 2008
    Posts
    216

    Re: Estop Watchdog command in Initialization File

    Hi Tom,

    I took the advice you gave earlier and got a bunch of the various estops to work for the most part and it's nice now that i get a different estop message on the console screen so i know what was tripped. My last question is i have the code set up to deactivate a single axis when a estop occurs, what type of code do i need to get all 3 axis to stop all at once?

    I tried to incorporate code like that found in your watchenables_disableall.c file at the top of the program but it just make the system freeze up ( i think it was constantly disabling drives, and not just once)

    What am i doing wrong? My thought was that all i had to do was disable a single drive then there would be a separate loop searching for a single axis that would then disable the rest of them but i can't get that to work either.

    Thanks,
    Dan

  5. #5
    Join Date
    May 2006
    Posts
    4045

    Re: Estop Watchdog command in Initialization File

    Hi Dan,

    I think the flaw in your logic is regarding the automatic re-enabling of the axis. So for example say the X Estop signal becomes active. All the axes are disabled. Fine. But then the code for the other axes immediately say, "Hey I'm disabled but don't have a corresponding EStop Active - so automatically re-enable myself".

    One solution might be to remove the automatic re-enable functionality. After any error all axes are disabled and then the operator must re-initialize the system to re-enable the axes. This seems like the safest approach to me.

    Otherwise if you really need to have things automatically re-enable you might test to make sure all conditions are ok and an axis is disabled only then re-enable all the axes.

    HTH
    Regards
    TK
    http://dynomotion.com

  6. #6
    Join Date
    Feb 2008
    Posts
    216

    Re: Estop Watchdog command in Initialization File

    Thats what i want, i don't want things to re-enable until i do it manually. How do i tweak this code to do just that?

    Thanks,
    Dan

  7. #7
    Join Date
    May 2006
    Posts
    4045

    Re: Estop Watchdog command in Initialization File

    Hi Dan,

    Remove the "if" conditions that enables the axes.

    Regards
    TK
    http://dynomotion.com

  8. #8
    Join Date
    Feb 2008
    Posts
    216

    Re: Estop Watchdog command in Initialization File

    Thanks it's working great now.

  9. #9
    Join Date
    Dec 2003
    Posts
    24220

    Re: Estop Watchdog command in Initialization File

    In commercial machines and according to code, the E-stop is a hard wired function, any watch-dog or charge pump should be part of this hard wired AND'ed string.
    the end result shuts down and removes power from all motorized devices. at the same time the controller is also advised of any such E-stop and stops control functions immediately.
    Al.
    CNC, Mechatronics Integration and Custom Machine Design

    “Logic will get you from A to B. Imagination will take you everywhere.”
    Albert E.

  10. #10
    Join Date
    Feb 2008
    Posts
    216

    Re: Estop Watchdog command in Initialization File

    Hi Al,

    Yes the plan is to have a hard wired Estop that will disconnect the power to the drives and spindle.

    I was looking for other ways to stop machine motion if other faults were found too. This way if a single Axis Viper drives trips or the air shuts off all everything will come to a stop until the problem is fixed. And i can rot cause the fault a lot easier with Fault messages being provided in the console screen.

  11. #11
    Join Date
    Feb 2008
    Posts
    216

    Re: Estop Watchdog command in Initialization File

    Hi Tom,
    I've had a few instances where a random trip has occured because the Input went high for a split second.

    Is there a way to add a timer function or delay to the condition so that the signal has to stay high for a full second for it to trip a warning?

    Thanks in advance.

    Example:

    //check if X axis is not enabled and Spindle Overheat sensor, (InputI/O 22 is high)
    if (!ch0->Enable && ReadBit(22)==1)

    {
    EnableAxis(0); //Enable the X axis to operate
    }

    // check if X axis is Enabled and Overheat sensor is activated, (InputI/O 22 is low)
    if (ch0->Enable && ReadBit(22)==0)
    {
    DisableAxis(0); // Disable the X axis
    DisableAxis(1); // Disable the Y axis
    DisableAxis(2); // Disable the Z axis
    printf("Spindle Over Heat Alarm Activated, Auto Axis Disable All \n"); // send message to console
    }

  12. #12
    Join Date
    May 2006
    Posts
    4045

    Re: Estop Watchdog command in Initialization File

    Hi Dan,

    I usually like to filter out noise in the hardware by adding something like a 0.1uF Ceramic Capacitor on the input.

    But otherwise to do it in software you could periodically check the signal and count how many times it was consistently high or low and make a decision based on that.

    The ExternalButtons.c has an example of this as mechanical switches have a similar issue. It uses a function called Debounce(). Feed any signal into the Debounce function and it will tell you when the signal first transitions to a stable high value (returns 1), the signal first transitions to a stable low value (returns 0), or did neither (returns -1).

    The Debounce function is written to be state driven so it always returns back immediately to allow a single loop to continuously do other things (such as check other buttons or signals). This requires 3 global variables to be created and passed to the function so that it can maintain the state of things for the next call.

    Let me know how much of this makes sense. Here is the ExternalButtons.c Example for reference:

    Code:
    #include "KMotionDef.h"
    
    #define FEEDHOLDBIT 46
    #define CYCLESTARTBIT 47
    #define ESTOP 26
    #define HALTBIT 27
    #define RESTARTBIT 28
    #define ZEROALLBIT 29
    
    // function prototypes for compiler
    int DoPC(int cmd);
    int DoPCFloat(int cmd, float f);
    int Debounce(int n, int *cnt, int *last, int *lastsolid);
    
    // state variables for switch debouncing
    int flast=0,flastsolid=-1,fcount=0;
    int clast=0,clastsolid=-1,ccount=0;
    int elast=0,elastsolid=-1,ecount=0;
    int hlast=0,hlastsolid=-1,hcount=0;
    int rlast=0,rlastsolid=-1,rcount=0;
    int zlast=0,zlastsolid=-1,zcount=0;
    
    main()
    {
        int result;
    
        for (;;) // loop forever
        {
            WaitNextTimeSlice();
            
            // Handle FeedHold/Resume
            result = Debounce(ReadBit(FEEDHOLDBIT),&fcount,&flast,&flastsolid);
            if  (result == 1)
            {
                if (CS0_StoppingState == 0)
                    StopCoordinatedMotion();
                else
                    ResumeCoordinatedMotion();
            }
    
            // Handle Cycle Start
            result = Debounce(ReadBit(CYCLESTARTBIT),&ccount,&clast,&clastsolid);
            if  (result == 1)
            {
                DoPC(PC_COMM_EXECUTE);
            }
    
            // Handle ESTOP
            result = Debounce(ReadBit(ESTOP),&ecount,&elast,&elastsolid);
            if  (result == 1)
            {
                DoPC(PC_COMM_ESTOP);
            }
            
            // Handle HALT
            result = Debounce(ReadBit(HALTBIT),&hcount,&hlast,&hlastsolid);
            if  (result == 1)
            {
                DoPC(PC_COMM_HALT);
            }
            
            // Handle RESTART
            result = Debounce(ReadBit(RESTARTBIT),&rcount,&rlast,&rlastsolid);
            if  (result == 1)
            {
                DoPC(PC_COMM_RESTART);
            }
            
            // Handle ZERO ALL
            result = Debounce(ReadBit(ZEROALLBIT),&zcount,&zlast,&zlastsolid);
            if  (result == 1)
            {
                DoPCFloat(PC_COMM_SET_X,0.0);
                DoPCFloat(PC_COMM_SET_Y,0.0);
                DoPCFloat(PC_COMM_SET_Z,0.0);
            }
        }
    }
    
    // Put a Float as a parameter and pass the command to the App
    int DoPCFloat(int cmd, float f)
    {
        int result;
        persist.UserData[PC_COMM_PERSIST+1] = *(int*)&f;
        return DoPC(cmd);
    }
    
    
    // Pass a command to the PC and wait for it to handshake
    // that it was received by either clearing the command
    // or changing it to a negative error code
    int DoPC(int cmd)
    {
        int result;
        
        persist.UserData[PC_COMM_PERSIST]=cmd;
        
        do
        {
            WaitNextTimeSlice();	
        }while (result=persist.UserData[PC_COMM_PERSIST]>0);
        
        printf("Result = %d\n",result);
    
        return result;
    }
    
    
    
    
    // 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;
    }
    Regards
    TK
    http://dynomotion.com

  13. #13
    Join Date
    Feb 2008
    Posts
    216

    Re: Estop Watchdog command in Initialization File

    Hi Tom,

    We managed to remove the spurious trips by replacing a relay and adding a diode across the coil. But if it flares up again I'll look at the Debounce function. We just used it for our external Feed Hold button with some success as you helped with earlier.

    Thanks,
    Dan

Similar Threads

  1. M & S Code (Configure or Define), in Initialization File: How to do it?
    By jeffserv in forum Dynomotion/Kflop/Kanalog
    Replies: 1
    Last Post: 06-11-2014, 10:28 PM
  2. Watchdog Led
    By bhurts in forum HURCO
    Replies: 13
    Last Post: 06-12-2013, 09:44 AM
  3. Disabling Watchdog A-B 8200
    By Wharfrat256 in forum CNC Machine Related Electronics
    Replies: 0
    Last Post: 07-17-2012, 05:14 PM
  4. SS watchdog timeout
    By Dan73 in forum SmoothStepper Motion Control
    Replies: 2
    Last Post: 05-24-2012, 04:39 AM
  5. Command file to delete files
    By Karl_T in forum CamSoft Products
    Replies: 2
    Last Post: 09-07-2008, 12:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •