Variables in macros

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

Re: Variables in macros

Postby ger21 » Fri Nov 04, 2016 5:07 pm

Correct, but I'm still not really sure what he's trying to do.
Gerry
UCCNC 2022 Screenset - http://www.thecncwoodworker.com/2022.html
ger21
 
Posts: 2714
Joined: Sat Sep 03, 2016 2:17 am

Re: Variables in macros

Postby Battwell » Fri Nov 04, 2016 5:19 pm

pasted whole m6 code below- ive added this by ****probing section.
{ and } are indented so you can see where im trying to skip the code

// Battwells m6 and probing
//Tool positions definition
int headdownport = 3; //hsd
int headdownpin = 6; //hsd
int headnumber = 1; //hsd

int Newtool = exec.Getnewtool();
int Currenttool = exec.Getcurrenttool();
//iVars[1]=1;
double probeyes = exec.Getvar (1);

double[] ToolX = new double[11];
double[] ToolY = new double[11];
//ToolX[1] = -150; // Tool1 X position
//ToolY[1] = -1858; // Tool1 Y position
double ToolXchgpos = -200; // Toolchange X position
double ToolYchgpos = -1550; // Toolchange Y position // HSD
double probeheight = -208.7625;// hsd // use m312 to get exact position of probe with no tool
double probeX = 4; // HSD
double probeY = -1586;



// change settings for perske head ********(tool numbers above 80)
if (Newtool >79)
{
headnumber = 2; // perske
probeX = -1; //perske
probeY = -1872; //perske
ToolYchgpos = -1872; // Toolchange Y position // PERSKE
probeheight = -146.0625;// perske // use m312 to get exact position of probe with no tool
headdownport = 3;// perske
headdownpin = 7;// perske
}
// ***end of set head 2 perske ***

// Probing settings//
double Zmin = -200;
double Zmin2 = -3;
double Feedrate = 600;
double Feedrate2 = 50;
double SafeZ2 = -50;
double retractheight = 10;
double retractheight2 = 2;
double SafeZ = -10;



// ***set tool field number***

int Boxnumber = 195;// fieldnumber for tools 1-20 (field 196)
if (Newtool >20)
{
Boxnumber = 921-21;// fieldnumber for tools 21-96. minus 21 so tool increment is correct
}

// ***end of set tool field number***

if(Newtool == -1) // If new tool number is -1 means a missing T code, so we need to stop here...
{
MessageBox.Show("no tool number!");

exec.Stop();

return;
}


if(Newtool <1 || Newtool >96) // Tool number is out of range, so we need to stop here...
{
MessageBox.Show(" tool number out of range !");
exec.Stop();
return;
}

if(Newtool == Currenttool) // Same tool was selected, so do nothing, stop here...
{
// MessageBox.Show(" same tool !");

return;
}

//return;

if(!exec.GetLED(56)||!exec.GetLED(57)||!exec.GetLED(58)) // If machine was not homed then it is unsafe to move in machine coordinates, stop here...
{
MessageBox.Show("The machine was not yet homed, do homeing before executing a tool change!");
exec.Stop();
return;
}

while(exec.IsMoving()){}

// Stop spindle if running and Move Z up

exec.Stopspin();
exec.Code("G00 G53 Z"+ SafeZ); // Move Z up
while(exec.IsMoving()){}

// Move to old tool position on XY plane

exec.Code("G00 G53 X" + ToolXchgpos + " Y" + ToolYchgpos);
while(exec.IsMoving()){}
MessageBox.Show(" Insert tool " +Newtool + " then press ok ");

//********probing************

//M31 probing macro
if (probeyes >0)
{ // skip probe if #1=0 *********************


if(!exec.GetLED(56)||!exec.GetLED(57)||!exec.GetLED(58)) // If machine was not homed then it is unsafe to move in machine coordinates, stop here...
{
MessageBox.Show("MACHINE NOT HOMED, home the machine before tool change!");
exec.Stop();
return;
}

while(exec.IsMoving()){}
exec.Wait(200);
if(exec.GetLED(37)) // if probe signal is on - probe stuck! stop here...
{
MessageBox.Show("The probe signal isnt ready-check probe!");
exec.Stop();
return;
}

//HEAD DOWN
exec.Setoutpin(headdownport,headdownpin);


double Xoriginalpos = exec.GetXmachpos(); // Get the current X machine coordinates
double Yoriginalpos = exec.GetYmachpos(); // Get the current Y machine coordinates
double Zoriginalpos = exec.GetZmachpos(); // Get the current Z machine coordinates





exec.Code("G00 G53 X" + probeX +" Y" + probeY + "Z-5");// // Move to the probe sensor position in XY
while(exec.IsMoving()){}

//********switch to incremental *********
exec.Code("G91");

exec.Code("G31 Z" + Zmin + "F" + Feedrate); // Move to the probe sensor position in z
while(exec.IsMoving()){}

exec.Wait(400);
Console.Beep();




double Zoriginalposprobed = exec.GetZmachpos(); // Get the current Z machine coordinates

//MessageBox.Show ("position is now " + Zoriginalposprobed);

double probedpos= Zoriginalposprobed - Zoriginalpos;

//MessageBox.Show ("Length difference is" + probedpos);





double backoff = exec.GetZmachpos() + retractheight2;

exec.Code("G00 G53 Z" + backoff); // Move few mm?? above probe plate
while(exec.IsMoving()){}
exec.Wait(100);

if(exec.GetLED(37)) // if probe signal is on- probe stuck! stop here...
{
MessageBox.Show("The probe signal isnt ready-check probe!");
exec.Stop();
return;
}


exec.Code("G31 Z" + Zmin2 + "F" + Feedrate2); // slow probe
while(exec.IsMoving()){}

exec.Wait(100);
Console.Beep();

//********switch to absolute *********
exec.Code("G90");


if(!exec.Ismacrostopped()) // If tool change was not interrupted with a stop only then validate
{

Zoriginalposprobed = exec.GetZmachpos(); // Get the current Z machine coordinates

//MessageBox.Show ("position is now " + Zoriginalposprobed);

probedpos= Zoriginalposprobed - Zoriginalpos;

//MessageBox.Show ("Length difference is " + probedpos);


double tooloffset= Zoriginalposprobed - probeheight;
//MessageBox.Show ("tool length is " + tooloffset);


//exec.Code("G44 H1"); // Load tool offset one, note the tool length is defined in the tools menu
//while(exec.IsMoving()){}
//exec.Wait(500);

double Zup = exec.GetZmachpos() + retractheight;
if(Zup > SafeZ)
{
Zup = SafeZ;
}

//HEAD UP
//exec.Clroutpin(headdownport,headdownpin);
//exec.Wait(100);
// end of head up

exec.Code("G00 G53 Z-5");// // Move z to -5 from home switch
while(exec.IsMoving()){}
exec.Wait(100);
exec.Code("G00 G53 x-200");// // Move x to -5 from home switch
while(exec.IsMoving()){}
exec.Wait(100);





// **** Set tool table ****


int fieldtoset= (Boxnumber + Newtool);// different field numbers now with extra tools added in this version

AS3.Setfield(tooloffset, fieldtoset);
while(exec.IsMoving()){}
exec.Callbutton(167);// save all settings
exec.Wait(100);

//exec.Code("G43 H"+ Newtool); // Load new tool offset
AS3.Setfield(tooloffset, 169);//set tooloffset z

exec.Wait(200);
AS3.Validatefield(169); // this updates all settings with new z offset.

exec.Wait(100);
while(exec.IsMoving()){}
} //end of no probe loop *****************



if(!exec.Ismacrostopped()) // If tool change was not interrupted with a stop only then validate new tool number
{
exec.Code("G43 H"+ Newtool); // Load new tool offset
exec.Wait(100);
exec.Setcurrenttool(Newtool); //Set the current tool -> the new tool
exec.Wait(100);
// MessageBox.Show("Tool change done.");
exec.AddStatusmessage("Tool change OK - Now Tool "+ Newtool);

}
else
{
exec.StopWithDeccel();
MessageBox.Show("Tool change was interrupted by user!");
}
}
Uc300eth on router and mill.
UK uccnc powered machine sales. https://cncrouter.uk/atc-cnc-routers.htm
Automateanything/duzzit cnc/mercury cnc
Battwell
 
Posts: 867
Joined: Sun Sep 25, 2016 7:39 pm
Location: South Wales. Uk

Re: Variables in macros

Postby Battwell » Fri Nov 04, 2016 5:21 pm

ha- the indents dont show after pasting on here.
the section has *** by it at start and end to make it easier to find
Uc300eth on router and mill.
UK uccnc powered machine sales. https://cncrouter.uk/atc-cnc-routers.htm
Automateanything/duzzit cnc/mercury cnc
Battwell
 
Posts: 867
Joined: Sun Sep 25, 2016 7:39 pm
Location: South Wales. Uk

Re: Variables in macros

Postby Battwell » Fri Nov 04, 2016 7:49 pm

correct- but it doesnt run the code after the end of no probe code. so got me stumped. (maybe its my hangover)
(if probeyes is 0)

it runs fine all the way through if probeyes=1 (running the whole lot)
il have another look tomorrow.
ps. the system beep and status messages made a nice improvement- :-)
Uc300eth on router and mill.
UK uccnc powered machine sales. https://cncrouter.uk/atc-cnc-routers.htm
Automateanything/duzzit cnc/mercury cnc
Battwell
 
Posts: 867
Joined: Sun Sep 25, 2016 7:39 pm
Location: South Wales. Uk

Re: Variables in macros

Postby cncdrive » Fri Nov 04, 2016 8:44 pm

Battwell wrote:ok. couldnt get syntax right for old ivars version so i installed 2028.

full syntax that works is
double myvar=exec. Getvar(1); // for variable #1

then used
if(myvar>0)
{

skip this code

}

which works.
except the macro just seems to stop and end after the }
is there a goto function i can use instead ? if so how to write that please


No, the macro will not stop with your code.
It will always go beyond the }, except if there is a return; code is inside the brackets somewhere.
The return; means for the compiler to return to the caller function, so it will end the macro when it executes.

Test this code:
Code: Select all
double myvar=exec.Getvar(1); // for variable #1
if(myvar>0)
{
//skip this code
}

MessageBox.Show(this.exec.mainform, "hey!");


And you will see that the MessageBox always shows up whatever the myvar value is.
cncdrive
Site Admin
 
Posts: 4887
Joined: Tue Aug 12, 2014 11:17 pm

Re: Variables in macros

Postby beefy » Fri Nov 04, 2016 10:18 pm

As soon as I read your code I got confused why there were "return;" statements inside the blocks of conditional code.

I've only ever used "return" to prematurely return from a method/function based on some conditional test within the method, or to return a value from the method. I've never even seen return used the way it is in your code.

Here is Microsofts description of "return;":

The return statement terminates execution of the method in which it appears and returns control to the calling method. It can also return an optional value. If the method is a void type, the return statement can be omitted.
If the return statement is inside a try block, the finally block, if one exists, will be executed before control returns to the calling method.

Keith
beefy
 
Posts: 449
Joined: Mon Sep 05, 2016 10:34 am

Re: Variables in macros

Postby Battwell » Sat Nov 05, 2016 11:09 am

I have message box at the end and it doesn't show
I will look again later

The return is from Cnc drive original m6 which I just edited and edited more to get to what I have today!
The returns are inside other if loops so shouldn't be seen unless stop is pressed?

The open and closed brackets are at the part of the code I want to skip only
Uc300eth on router and mill.
UK uccnc powered machine sales. https://cncrouter.uk/atc-cnc-routers.htm
Automateanything/duzzit cnc/mercury cnc
Battwell
 
Posts: 867
Joined: Sun Sep 25, 2016 7:39 pm
Location: South Wales. Uk

Re: Variables in macros

Postby cncdrive » Sat Nov 05, 2016 12:54 pm

Battwell wrote:I have message box at the end and it doesn't show
I will look again later

The return is from Cnc drive original m6 which I just edited and edited more to get to what I have today!
The returns are inside other if loops so shouldn't be seen unless stop is pressed?

The open and closed brackets are at the part of the code I want to skip only


You can use the return; to terminate the macro at any point, there is no problem with that.
I just wanted you to warn that a return will terminate the macro and it might be the problem.

I think you should place some more MessageBoxes or Add status messages to debug at what point exactly the macro terminates.
cncdrive
Site Admin
 
Posts: 4887
Joined: Tue Aug 12, 2014 11:17 pm

Re: Variables in macros

Postby Battwell » Sun Nov 06, 2016 5:23 pm

Ok I solved it by moving the } to the end of the main file and using else
Then re pasting the last bits of code that I wanted again
Much nicer now- run the program once with probing which saves all lengths to tool table
Then set #1 to 0
Saves about 2 minutes of probing over 7 tool change job
Uc300eth on router and mill.
UK uccnc powered machine sales. https://cncrouter.uk/atc-cnc-routers.htm
Automateanything/duzzit cnc/mercury cnc
Battwell
 
Posts: 867
Joined: Sun Sep 25, 2016 7:39 pm
Location: South Wales. Uk

Re: Variables in macros

Postby Battwell » Sun Nov 06, 2016 5:29 pm

Nice with status widow worked out !
Attachments
image.jpg
Uc300eth on router and mill.
UK uccnc powered machine sales. https://cncrouter.uk/atc-cnc-routers.htm
Automateanything/duzzit cnc/mercury cnc
Battwell
 
Posts: 867
Joined: Sun Sep 25, 2016 7:39 pm
Location: South Wales. Uk

Previous

Return to Macros

Who is online

Users browsing this forum: No registered users and 8 guests