Page 1 of 1

Starting a new Thread within a Macro

PostPosted: Fri Mar 17, 2023 10:46 am
by D190979V
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.

Re: Starting a new Thread within a Macro

PostPosted: Fri Mar 17, 2023 11:53 am
by ger21
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.

Re: Starting a new Thread within a Macro

PostPosted: Sun Mar 26, 2023 6:51 pm
by D190979V
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