how to implement a security switch launching a program / gcode

Moderators: TomKerekes, dynomotion

gui_marchioro
Posts: 33
Joined: Sun Aug 21, 2022 11:22 pm

Re: how to implement a security switch launching a program / gcode

Post by gui_marchioro » Mon Nov 07, 2022 1:14 pm

Hi,

I think I am missing something else in this configuration.

Now the Cycle Start program is indeed configured to "Exec/wait", but still it is executing some lines before Halt.



Here is the program that is executed when Cycle Start is called:

Code: Select all

#include "Homing.c"
#include "InitConfig.c"
#include "MillChanger.h"
#include "KMotionDef.h"

#define TMP 10 // which spare persist to use to transfer data
#include "KflopToKMotionCNCFunctions.c"

// Program to be executed when the Cycle Start button is pressed
// DoPC(PC_COMM_EXECUTE);
void main()
{
    if (GetInitExecuted() == 0 || GetHomingExecuted() == 0 || GetIsExecutingHoming() == 1)
    {
        HaltAndWarn("Execute the Init and Homing function before run a program");
    }
    else if (ReadBit(MAGAZINE_OPENED_INPUT))
    {
        HaltAndWarn("Close the tool magazine before run a program");
    }
}

void HaltAndWarn(char *message)
{
    DoPC(PC_COMM_HALT);
    MsgBoxNoWait(message, MB_ICONEXCLAMATION);
    DoPC(PC_COMM_RESTART);
}
Sincerely,
Guilherme

User avatar
TomKerekes
Posts: 2118
Joined: Mon Dec 04, 2017 1:49 am

Re: how to implement a security switch launching a program / gcode

Post by TomKerekes » Wed Nov 09, 2022 12:55 am

Hi Guilherme,

There was a bug regarding this. GCode execution was beginning before the Cycle Start Action was complete.

Here is a patch for Version 4.35h.

KMotionCNC.exe


Download and copy to the C:\KMotion435h\KMotion\Release folder.

The following code seems to now work now for me:

Code: Select all

#include "Homing.c"
#include "InitConfig.c"
#include "MillChanger.h"
#include "KMotionDef.h"

#define TMP 10 // which spare persist to use to transfer data
#include "KflopToKMotionCNCFunctions.c"

// Program to be executed when the Cycle Start button is pressed
// DoPC(PC_COMM_EXECUTE);
void main()
{
    if (GetInitExecuted() == 0 || GetHomingExecuted() == 0 || GetIsExecutingHoming() == 1)
    {
        HaltAndWarn("Execute the Init and Homing function before run a program");
    }
    else if (ReadBit(MAGAZINE_OPENED_INPUT))
    {
        HaltAndWarn("Close the tool magazine before run a program");
    }
}

void HaltAndWarn(char *message)
{
    DoPC(PC_COMM_HALT);
    MsgBox(message, MB_ICONEXCLAMATION);
}
HTH
Regards,

Tom Kerekes
Dynomotion, Inc.

gui_marchioro
Posts: 33
Joined: Sun Aug 21, 2022 11:22 pm

Re: how to implement a security switch launching a program / gcode

Post by gui_marchioro » Wed Nov 09, 2022 2:43 pm

Hello Tom,

Nice. Thanks for the reply.

I have pasted in the specific folder but when I try to execute KMotionCNC I get a "Missing entry point" error.
error.png
Any thoughts on how to solve this problem?

Sincerely,
Guilherme

User avatar
TomKerekes
Posts: 2118
Joined: Mon Dec 04, 2017 1:49 am

Re: how to implement a security switch launching a program / gcode

Post by TomKerekes » Wed Nov 09, 2022 4:32 pm

Hi Guilherme,

It must be used with Version 4.35h
Regards,

Tom Kerekes
Dynomotion, Inc.

gui_marchioro
Posts: 33
Joined: Sun Aug 21, 2022 11:22 pm

Re: how to implement a security switch launching a program / gcode

Post by gui_marchioro » Sun Jan 22, 2023 5:49 pm

Hello,

I finally tried this, and it indeed solved the problem. Now before executing a G code program it execute my C program.

It work as expected when clicking in the button Cycle Start, but when executing the command DoPC(PC_COMM_EXECUTE) the execution halts and the UI stops to respond with this new KMotionCNC.exe version.



The programs involved are:
Program that execute when Cycle Start button is clicked

Code: Select all

#include "KMotionDef.h"
#include "Homing.c"
#include "InitConfig.c"
#include "Magazine.h"
#include "MillChanger.h"

#define TMP 10 // which spare persist to use to transfer data
#include "KflopToKMotionCNCFunctions.c"

// Program to be executed when the Cycle Start button is pressed
// DoPC(PC_COMM_EXECUTE);
void main()
{
    if (GetInitExecuted() == 0 || GetHomingExecuted() == 0 || GetIsExecutingHoming() == 1)
    {
        HaltAndWarn("Execute the Init and Homing function before run a program");
    }
    else if (ReadBit(MAGAZINE_OPENED_INPUT))
    {
        HaltAndWarn("Close the tool magazine before run a program");
    }
}

void HaltAndWarn(char *message)
{
    DoPC(PC_COMM_HALT);
    Delay_sec(1);
    MsgBoxNoWait(message, MB_ICONEXCLAMATION);
    Delay_sec(1);
    DoPC(PC_COMM_RESTART);
    Delay_sec(1);
    MDI("M30");
}
Program that execute when button Execute is clicked

Code: Select all

#include "KMotionDef.h"
#ifndef TMP
#define TMP 10 // which spare persist to use to transfer data
#include "KflopToKMotionCNCFunctions.c"
#endif

main()
{
    DoPC(PC_COMM_EXECUTE);
}
Is there a new patch that solves this?

Sincerely,
Guilherme

User avatar
TomKerekes
Posts: 2118
Joined: Mon Dec 04, 2017 1:49 am

Re: how to implement a security switch launching a program / gcode

Post by TomKerekes » Mon Jan 23, 2023 5:21 pm

Hi Guilherme,

The code you posted is different from what I suggested. But anyway the main issue is that the KFLOP to PC commands only allow one command at a time to be made. When one command is given it must complete before the next. In the case where a button issues a PC_COMM_EXECUTE and this attempts to do a Halt or Message Box the commands will fail.

One solution would be to have the Execute button do the safety checks itself, display any message, and only issue the PC_COMM_EXECUTE if everything is ok. There is a small chance that the IO changed after the Execute button made the check but before the Cycle Start actually begins, a flag (Virtual Bit) can be used to inform the Cycle Start program that the checks have already been made. Below is an example. I couldn't fully test because you didn't post some files and I don't have your hardware.

Execute Button Code:

Code: Select all

#include "KMotionDef.h"
#include "Homing.c"
#include "InitConfig.c"
#include "Magazine.h"
#include "MillChanger.h"

#ifndef TMP
#define TMP 10					// which spare persist to use to transfer data
#include "KflopToKMotionCNCFunctions.c"
#endif

#define OK_TO_RUN_BIT 48

main()
{
	if (GetInitExecuted() == 0 || GetHomingExecuted() == 0 || GetIsExecutingHoming() == 1)
	{
		HaltAndWarn("Execute the Init and Homing function before run a program");
	}
	else if (ReadBit(MAGAZINE_OPENED_INPUT))
	{
		HaltAndWarn("Close the tool magazine before run a program");
	}
	else
	{
		SetBit(OK_TO_RUN_BIT);
		DoPC(PC_COMM_EXECUTE);
		ClearBit(OK_TO_RUN_BIT);
	}
}
Cycles Start Code:

Code: Select all

#include "KMotionDef.h"
#include "Homing.c"
#include "InitConfig.c"
#include "Magazine.h"
#include "MillChanger.h"

#define TMP 10					// which spare persist to use to transfer data
#include "KflopToKMotionCNCFunctions.c"

#define OK_TO_RUN_BIT 48

// Program to be executed when the Cycle Start button is pressed
// DoPC(PC_COMM_EXECUTE);
void main()
{
	if (!ReadBit(OK_TO_RUN_BIT))
	{
		if (GetInitExecuted() == 0 || GetHomingExecuted() == 0 || GetIsExecutingHoming() == 1)
		{
			HaltAndWarn("Execute the Init and Homing function before run a program");
		}
		else if (ReadBit(MAGAZINE_OPENED_INPUT))
		{
			HaltAndWarn("Close the tool magazine before run a program");
		}
	}
	ClearBit(OK_TO_RUN_BIT);
}

void HaltAndWarn(char *message)
{
	DoPC(PC_COMM_HALT);
	MsgBox(message, MB_ICONEXCLAMATION);
}
Regards,

Tom Kerekes
Dynomotion, Inc.

Post Reply