Starting a new Thread within a Macro

This is where you talk about Macros, show examples of your macro scripting and SHARE handy segments of script code as examples.

Starting a new Thread within a Macro

Postby D190979V » Fri Mar 17, 2023 10:46 am

Hello,
I did some searches before, but have not found any "thread" that adresses this topic,
maybe because there are too many results if searching for "Thread". So I hope for the
older members that this is not asked again and again.

I have a constant surface speed (lathe) macro running in a macroloop to adjust
Spindle speed during the moves on the fly.
But it would be nice to have it not always on and that it can be called like any other
code from MDI or within a G-Code file.

If there is a call for a code execution within a macro,
the macro blocks and it´s execution is halted until the code
is finished, so the following does not work:
Code: Select all
exec.Code("G1 X5");
while(exec.IsMoving()==true)
{
//Adjust Speed
}

The while loop starts after the finished move.

I also tested the following and this seems to work:
Code: Select all
Thread thread = new Thread(() =>
{
   exec.Code(gCodeString);
});
thread.Start();
//Wait for move to start
while(exec.IsMoving()==false)
{
   Thread.Sleep(threadSleep);
}
//Adjust Speed during Movement
while(exec.IsMoving()==true)
{
   //Adjust Speed here
}


So my question is: Is it ok to do so, or may this result in some issues I´m not aware?

Best regards
Dominik

P.S.: As far as I know there is no comprehensive manual for macros, e.g. what can be done or should not be done,
if so, please point me to the location.
D190979V
 
Posts: 5
Joined: Wed Mar 15, 2023 12:16 pm

Re: Starting a new Thread within a Macro

Postby ger21 » Fri Mar 17, 2023 11:53 am

Create a user LED that you can use to enable or disable the code in your macroloop.
Then create a macro to toggle the LED on/off. You can call it from an M code or a button.
Have your macroloop look at the state of the LED, and only do what it does if the LED is on.
Gerry
UCCNC 2022 Screenset - http://www.thecncwoodworker.com/2022.html
ger21
 
Posts: 2714
Joined: Sat Sep 03, 2016 2:17 am

Re: Starting a new Thread within a Macro

Postby D190979V » Sun Mar 26, 2023 6:51 pm

Hello ger21,
thanks for that suggestion, I tried it and it worked quite fine.
However finally I struggled with the max. override of 300%, this
is too small to make deeper cuts on the lathe. Of course the spindle
speed could be set somewhere larger and the cut would start below
100%, but this approach made it difficult to keep it human
read- and operate-able so I changed the approach and placed the
spindle speed changes within the actual macros for generating
the code for the specific turning operations.
That made it much more predictable what happens when…

Best regards
D190979V
 
Posts: 5
Joined: Wed Mar 15, 2023 12:16 pm


Return to Macros

Who is online

Users browsing this forum: No registered users and 8 guests