Page 1 of 1

DefineVariable in Post Processor

Posted: Tue Oct 04, 2011 10:11 pm
by jnvines
I'm having some difficulty getting DefineVariable to work.

My post processor looks something like this:

DefineVariable("myVar",sc.unitLINEAR,0,10)

function OnInit()
....

//to the end of file

When I save the file it says:
This post has thrown an error:
Lua: Error while running chunk..... global 'DefineVariable' (a nil value)


What am I doing wrong here?





------------------------------------

DefineVariable in Post Processor

Posted: Wed Oct 05, 2011 7:49 am
by Les Newell
You need:
post.DefineVariable("myVar",sc.unitLINEAR,0,10)

Les

On 04/10/11 15:23, jnvines wrote:
[quote]I'm having some difficulty getting DefineVariable to work.

My post processor looks something like this:

DefineVariable("myVar",sc.unitLINEAR,0,10)

function OnInit()
....

//to the end of file

When I save the file it says:
This post has thrown an error:
Lua: Error while running chunk..... global 'DefineVariable' (a nil value)


What am I doing wrong here?





------------------------------------

Re: DefineVariable in Post Processor

Posted: Sun Jan 03, 2021 4:01 pm
by hans
But how to access this variable?

adding somthing like

Code: Select all

circtrans = 360/(diameter*math.pi)
or

Code: Select all

circtrans = 360/(var.diameter*math.pi)
either in function OnInit() or outside a function
gives me the error:

An unexpected error has occurred:
..LinuxCNC_XZA.scpost:43: attempt to perform arithmetic on global 'diameter' (a nil value)
Post processing failed

Re: DefineVariable in Post Processor

Posted: Sun Jan 03, 2021 5:30 pm
by Les Newell
Did you define the variable using post.DefineVariable? For example:

Code: Select all

post.DefineVariable("diameter",sc.unitLINEAR,0,10)

Re: DefineVariable in Post Processor

Posted: Thu Jan 07, 2021 9:42 am
by hans
Les Newell wrote: Sun Jan 03, 2021 5:30 pm Did you define the variable using post.DefineVariable? For example:

Code: Select all

post.DefineVariable("diameter",sc.unitLINEAR,0,10)
Yes

Re: DefineVariable in Post Processor

Posted: Thu Jan 07, 2021 3:37 pm
by Les Newell
It should work then. Could you send me a copy of your post.

Re: DefineVariable in Post Processor

Posted: Fri Jan 08, 2021 9:50 pm
by hans
LinuxCNC_XZA.scpost
(4.65 KiB) Downloaded 189 times

Re: DefineVariable in Post Processor

Posted: Sat Jan 09, 2021 11:19 am
by Les Newell
You are running into a sequencing issue. Code ouside of a function is run once when the post is loaded. For instance you have

Code: Select all

 post.DefineVariable("diameter", sc.unitLINEAR, 0, 100)
circtrans = 360/(diameter*math.pi)
This defines the 'diameter' variable correctly but then immediately tries to do something with the value, before it has been set. The variable 'diameter' will only become valid after your 'Set post variable' opertion has been executed. Try putting the line

Code: Select all

circtrans = 360/(diameter*math.pi)
in function OnNewOperation(). While you are at it, take out the feed and plunge rate warnings. Your set post variable operation will trigger them.

Re: DefineVariable in Post Processor

Posted: Sat Jan 09, 2021 2:18 pm
by hans
Hmm that gives me nearly the same error message:

Code: Select all

Set variable diameter = 100 mm
An unexpected error has occurred:
...home/hans/.SheetCamTNG-dev/posts/LinuxCNC_XZA.scpost:109: attempt to perform arithmetic on global 'diameter' (a nil value)
Post processing failed

Re: DefineVariable in Post Processor

Posted: Sat Jan 09, 2021 2:51 pm
by Les Newell
Sorry, I didn't think it through fully. OnNewOperation is called before the operation is executed so it checks for diameter just before diameter is set.
Thinking about it, the safest thing to do is put it in OnRapid. It won't try to move until the first operation that uses a tool.

Code: Select all

   if diameter then
      circtrans = 360/(diameter*math.pi)
   else
      post.Error("You need to define the diameter!")
   end

Re: DefineVariable in Post Processor

Posted: Sat Jan 09, 2021 3:17 pm
by hans
Thanks that's it!