Page 1 of 1

M99998 + Persistent Tool

PostPosted: Tue Aug 17, 2021 8:03 am
by Parkgrr
Hey folks, I'm writing some code into M99998 constructor macro to read from the .pro and load in the tool that I wrote to it in M99999. The issue is I'm writing to the DRO just fine but the program still thinks it doesn't have a tool in. Here's what I've tried:

int Loadedtool = Convert.ToInt16(exec.Readkey("MG103 Custom","CurrentTool",""));
exec.Setcurrenttool(Loadedtool);
exec.AddStatusmessage("program thinks its"+(exec.Getcurrenttool()));

As well as

string LoadedTool = exec.Readkey("MG103 Custom","CurrentTool","");
AS3.Setfield(Convert.ToDouble(LoadedTool),897);
AS3.Validatefield(897);
exec.AddStatusmessage("program thinks it's "+(exec.Getcurrenttool()));

In the second case I get the correct tool # in the DRO but both return "program thinks it's 0" in the dialog.

The odd thing is that if I run this M99998 manually from the dialog right after UCCNC starts up it works perfectly. It just doesn't seem to want to cooperate during start-up.

Any tips?

Re: M99998 + Persistent Tool

PostPosted: Tue Aug 24, 2021 8:35 am
by dezsoe
Now that 1.2114 is released you can use exec.Setcurrenttool(int toolnumber) in M99998, so your first code works. (The Setcurrenttool did not work in reset state, but from now on it works.)

Code: Select all
int Loadedtool = Convert.ToInt16(exec.Readkey("MG103 Custom","CurrentTool",""));
exec.Setcurrenttool(Loadedtool);
exec.AddStatusmessage("program thinks its "+(exec.Getcurrenttool()));

Re: M99998 + Persistent Tool

PostPosted: Wed Sep 15, 2021 4:16 pm
by Parkgrr
Ok good to know, I'll update and try it!

Re: M99998 + Persistent Tool

PostPosted: Mon Sep 27, 2021 10:47 am
by kig23
Hi Parkgrr,
you can't set the current tool if the reset is active on UCCNC startup. In M99998 you have to create a thread and wait until the reset is pressed and after that you can set the current tool.

Code: Select all
    new Thread(delegate () {
    ChangeCurrTool ();
   }).Start();

#Events

private void ChangeCurrTool ()
{
   while (AS3.GetLED (25))
   {
      Thread.Sleep (100);
   }
   
   try
   {
      exec.Setcurrenttool (Convert.ToInt32 (exec.Readkey ("MyCNC", "CurrentTool", "0")));
   }
   catch ()
   {
      // do something to handle the exception
   }
}


hope this can help you.

Re: M99998 + Persistent Tool

PostPosted: Mon Sep 27, 2021 1:13 pm
by dezsoe
Hi kig23,

Beleive me, he can since 1.2114 as I wrote.

Re: M99998 + Persistent Tool

PostPosted: Tue Sep 28, 2021 8:06 am
by kig23
Hi Dezsoe,
you're right for the test version 1.2114, but i'm referring to the official version of UCCNC.

Re: M99998 + Persistent Tool

PostPosted: Mon Nov 01, 2021 7:27 pm
by Parkgrr
Thanks again for the tip Dezsoe, I updated and Setcurrenttool is now working thank you. The only remaining issue is I cannot do the setcurrenttool AND load that tool's offset in M99998.

int Loadedtool = Convert.ToInt16(exec.Readkey(my profile blah blah));
exec.Code("G49"); // Cancel/delete tool offset
exec.Setcurrenttool(Loadedtool); // Set the current tool to be the new tool
exec.Code("G43 H"+Loadedtool); // Load new tool offset

this code does not work when loaded into M99998 but it does work if I run it immediately after startup.

Any tips?

Re: M99998 + Persistent Tool

PostPosted: Mon Nov 01, 2021 7:35 pm
by dezsoe
Of course, you can't, because the reset is active when the M99998 runs: no exec.Code will be executed. You can run g-code commands only when the reset is off, so you have to do it after the reset is released.

Re: M99998 + Persistent Tool

PostPosted: Mon Nov 01, 2021 8:04 pm
by Parkgrr
Ah of course. Thank you.

This is working, can you think of a reason this would be a bad approach in practice?

int Loadedtool = Convert.ToInt16(exec.Readkey("MG103 Custom","CurrentTool",""));
double Offset = Convert.ToDouble(exec.Readkey("Tooltablevalues","TooloffsetZ"+Loadedtool,""));

exec.Setcurrenttool(Loadedtool); // Set the current tool to be the new tool
AS3.Setfield(Offset,169); // Set field number 169 (tool offset) to value 'Offset'
AS3.Validatefield(169);

Re: M99998 + Persistent Tool

PostPosted: Tue Nov 02, 2021 9:06 am
by dezsoe
It may work, but I prefer running commands on the first release of reset. Save the following macro and set it as a macroloop with autostart. It will stop itself when finished. I left a sample comment line in the macro and a G49/G43 command to set your TLO.

Code: Select all
// ================================================================================================
// Run command(s) on first release of reset
// ================================================================================================

bool ResetNow = exec.GetLED(ResetLED);
string Line;

if (FirstRun && !ResetNow)
{
  while (!exec.GetLED(ResetLED))
    Thread.Sleep(10);
  ResetNow = exec.GetLED(ResetLED);
}

FirstRun = false;

if (!ResetNow)
{
  Thread.Sleep(250);
  exec.Codesync("");
  if (!exec.Ismacrostopped())
  {
    exec.DisableGUI(true);
    while(exec.IsMoving()) Thread.Sleep(5);
    // Execute commands, repeat the next 4 lines for all commands
    Line = "(Your g-code here)";
    exec.AddStatusmessage("Startup: " + Line);
    exec.Code(Line);
    while(exec.IsMoving()) Thread.Sleep(5);
    // Example: set tool length offset using the current tool
    Line = (exec.Getcurrenttool() == 0 ? "G49" : "G43 H" + exec.Getcurrenttool().ToString());
    exec.AddStatusmessage("Startup: " + Line);
    exec.Code(Line);
    while(exec.IsMoving()) Thread.Sleep(5);
    // Finished
    exec.AddStatusmessage("Startup command(s) finished");
    exec.DisableGUI(false);
    Console.Beep(800, 50);
    // Stop this macroloop
    loop = false;
  }
  else
  {
    exec.AddStatusmessage("Waiting stop to clear...");
    Thread.Sleep(500);
  }
}

// ================================================================================================

#Events

// ================================================================================================

const int ResetLED = 25;

static bool FirstRun = true;

// ================================================================================================