Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: jevs on August 06, 2019, 11:28:22 PM

Title: How to use a variable in a Gcode script line?
Post by: jevs on August 06, 2019, 11:28:22 PM
mc.mcCntlGcodeExecuteWait(inst, "G90 G53 G0 X"VariableforXhere" Y"VariabeforYhere"")

I have tried about everything I can think of at this point. I tried a bunch of other ways to use the variables. This format above is not necessarily what I expect to work, but I am looking for the correct format to do this.

 I want to use a variable value for the X and Y in a Gcode command. At the beginning of my M6 script I store the X and Y positions before I move anything. Then I do all my tool change stuff, and I want to go back where I started.

There may be a better way to do this....if so, what is it?

I thought I was done with my M6 script and then this causes an error in the real run. I got it in a format that it would compile, but then it didn't move anywhere on this line. Everything I try either does nothing or won't compile with an error so far. 
Title: Re: How to use a variable in a Gcode script line?
Post by: joeaverage on August 06, 2019, 11:56:18 PM
Hi,
use pound variables or registers.

You update a pound variable (register) with a script. Then you use the format:

mc.mcCntlGcodeExecuteWait(inst, "G90 G53 G0 X"..#500.."Y"..#501)

where #500 and #501 are the pound variables that hold the values you want. You can do the same thing
more formally using register values which in turn require register handles.

Note I am at work and have not tested this....may do later when I get home.

Craig

Title: Re: How to use a variable in a Gcode script line?
Post by: jevs on August 07, 2019, 12:13:46 AM
I just figured out something that works.....

Code: [Select]
--Get positions before moving to do tool change
local valX, rc = mc.mcAxisGetMachinePos(inst, 0, mc.X_AXIS) --Get the position of the X axis in Machine Position
local valY, rc = mc.mcAxisGetMachinePos(inst, 1, mc.Y_AXIS) --Get the position of the Y axis in Machine Position

--Move back to X & Y initial location
mc.mcCntlGcodeExecuteWait(inst, "G90 G53 G0 X" .. tonumber(valX) .. "Y" .. tonumber(valY))
   
abbreviated from my code, but this works! Took me about an hour and half to figure this out though...
Title: Re: How to use a variable in a Gcode script line?
Post by: joeaverage on August 07, 2019, 05:26:57 AM
Hi,
good.

You are misreading the syntax......in Lua syntax your API calls have one too many parameters. I think you are confusing the
example in API.chm which is ALWAYS C++ syntax.

Lua syntax is:

Quote
val, rc = mc.mcAxisGetPos(
   number mInst,
   number axisId)

Thus in Lua:
local valX,rc=mc.mcAxisGetMachinePos(inst, mc.X_AXIS)

Whereas C++ syntax is:

Quote
int mcAxisGetPos(
   MINSTANCE mInst,
   int axisId,
   double *val);

Thus the API call would have three parameters, the instance number, the axis number AND a pointer to the data.

Craig