Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: Bill_O on February 28, 2019, 05:46:03 PM

Title: last line not executing
Post by: Bill_O on February 28, 2019, 05:46:03 PM
The last line of the attached mcs file is not happening.
I am sure it is a syntax error but I can not see it.
Title: Re: last line not executing
Post by: joeaverage on February 28, 2019, 06:18:04 PM
Hi,
the most likely reason its not executing is because the Gcode interpreter is busy or the motion controller is under command by
another chunk, the GUI chunk for instance. You are overlooking the fact that the API like all Lua functions has a return code.
You need to capture it and test it.

Quote
LUA Syntax:
rc = mc.mcCntlGcodeExecute(
      number mInst,
      string commands)

Description:
Execute G code as a unit of work and wait until it is complete.

Parameters: Parameter Description
mInst The controller instance.
commands A string buffer containing G code.


Returns: Return Code Description
MERROR_NOERROR No Error.
MERROR_INVALID_INSTANCE The mInst parameter was out of range.
MERROR_NOT_NOW The operation could not be completed at this time.
MERROR_NOT_COMPILED The macro scripts could not be compiled.

In particular MERROR_NOT_NOW, which is the same as the number -18.

So try adding this to your code:

Code: [Select]
local rc=mc.mcCntlGcodeExecuteWait(inst, GCodeXMov)
--use encoder positioning
If (rc== -18) then
       wx.wxMessageBox("MERROR_NOT_NOW)
      do return end
end


--set x position to 0.0

This wont solve your problem but will indicate if my guess that the Gcode interpreter is able to accept instructions.
You could also test and message the machine state which might give you an indication which chunk is I control.

Quote
LUA Syntax:
mcState, rc = mc.mcCntlGetState(
      MINSTANCE mInst)

Description:
Get the current controller state.

Parameters: Parameter Description
mInst The controller instance.
state The address of a mcState variable to receive the controller state.


Returns: Return Code Description
MERROR_NOERROR No Error.
MERROR_INVALID_INSTANCE The mInst parameter was out of range.
MERROR_INVALID_ARG state is NULL.

Craig

Title: Re: last line not executing
Post by: rhtuttle on February 28, 2019, 07:39:58 PM
GCodeX0 = string.format("G92 X%.4f\n", zero)
mc.mcCntlGcodeExecuteWait(inst, GCodeX0)
GCodeY0 = string.format("G92 Y%.4f\n", zero)
mc.mcCntlGcodeExecuteWait(inst, GCodeY0)

--move material back to start position
GCodeXMov = "G01 X-7.0 F100.0"
mc.mcCntlGcodeExecuteWait(inst, GCodeXMov)

I believe the recommended way to do this is:

GCodeX0 = string.format("G92 X%.4f\n", zero)
GCodeY0 = string.format("G92 Y%.4f\n", zero)
GCodeXMov = "G01 X-7.0 F100.0\n"

local s=GCodeX0..GCodeY0..GCodeXMov
mc.mcCntlGCodeExecute(inst,s)
or
mc.mcCntlMdiExecute(inst,s)

HTH

RT

Title: Re: last line not executing
Post by: Bill_O on March 01, 2019, 05:58:58 PM
thanks all for your input.
after some testing and seeing when things are happening it really looks like mach is busy or somehow skipping lines of script
i will be trying to do some testing and see what is happening next week

is there a way to force mach to finish the instructions before it goes on to the next command?
mach3 had sleep you could you to force the script to slow down or wait a set amount of time
Title: Re: last line not executing
Post by: joeaverage on March 01, 2019, 11:14:32 PM
Hi,

Quote
is there a way to force mach to finish the instructions before it goes on to the next command?

That is exactly what mcCntlGcodeExecuteWait() does. Your problem is you are asking an API to do something it cannot
do because the Gcode interpreter is busy. You should test the machine state prior to calling the API if it critical that the
call progresses and you can, in fact should, test the return code when it completes.

Craig
Title: Re: last line not executing
Post by: Bill_O on March 04, 2019, 08:13:41 AM
Craig,

Obviously this is causing me some problems.
If the mcCntlGcodeExecuteWait() is to insure the gcode gets done before moving on to the next line then how is the API busy when it gets to the next line?
I really am not trying to be a jerk I just really am having a hard time understanding how some of this works.
I put the error code you recommended earlier at the end and it did nothing.
I am going to put it up farther and see if anything shows up.
I really do appreciate the help.

Bill
Title: Re: last line not executing
Post by: Bill_O on March 04, 2019, 11:47:50 AM
I have been commenting out lines trying to find where the problem is.
I have attached the current mcs.
In this one everything is commented out other than the home of axis 0 then a move of that axis then what i believe should be a message box.
It homes and nothing else.
If I comment out the home it will move but the message box does not appear.
Any ideas on what I can do to allow me to do anything after the home?
Title: Re: last line not executing
Post by: joeaverage on March 04, 2019, 02:19:15 PM
Hi,
the message box only pops up if the mc.CntlGcodeExecuteWait() call FAILS. You wont see it if the call
progresses.

What's happening is the mcAxisHome() call is tying up the axis so the subsequent mcCntlGcodeExecuteWait() call
fails. We need to give some more thought to how to ensure that the X axis is homed before progressing.

I will give it some thought over the day and come back to you.

Craig
Title: Re: last line not executing
Post by: Bill_O on March 04, 2019, 02:59:08 PM
Craig,

What about some kind of counting loop.

count = 1
while count < 10000
   count = count +1
end while

Bill
Title: Re: last line not executing
Post by: smurph on March 04, 2019, 05:55:32 PM
I'm not sure what your end goal is, but you won't be able to home an axis in an M code script.  The script you posted would work (with modifications) in a button script though. 

Why do we not allow homing in M code macros?  Well..  it is because the machine is not in the IDLE state.  It is in automatic mode.  You can't do a manual mode function (jog, MPG, homing, etc...) while in automatic mode. 

The thing that isn't happening with the script is waiting for the home ops to complete.  You can wait on them by checking the state of the machine with mc.mcCntlGetState().  The first thing to do is call the home ops (checking the return codes to make sure there are no errors).  Then loop until the state is not equal to mc.MC_STATE_IDLE.  Once the state is no longer IDLE, loop and wait for it to return to idle.  The home ops will have completed at that time. 

None of that script checks any return codes.  There are no mysteries if you check the return codes.  :)

Steve
Title: Re: last line not executing
Post by: joeaverage on March 04, 2019, 09:45:06 PM
Hi,
smuprh's comment and suggestion is spot on. We have to set the homing function in progress and then wait until it
completes. That could be done as smurph suggests by testing the machine state, knowing that the function has completed
and returned the machine to idle state before progressing.

I had thought of another way which achieves the same result. The API call I am thinking of is:
Code: [Select]
LUA Syntax:
homing, rc= mc.mcAxisIsHoming(
number mInst,
number axisId)

Description:
Check to see if an axis is in a home operation.

We can use this API to test whether the homing function is complete. But we need a delay in the loop.

Quote
What about some kind of counting loop.

The loop idea ties up the CPU. Additionally I would expect a counting loop to count up to 1000 in a millisecond
or so. I think a better idea is to use a wxTimer function. It is a timer  coded in wxLua but creates a Windows
function/event and so has no impact on Mach, its external to Mach. Have a look at Daz's video on wxTimers:

https://www.machsupport.com/forum/index.php?topic=32573.0 (https://www.machsupport.com/forum/index.php?topic=32573.0)

It may appear that it adds significant complexity but I think it would work well and not burden Mach with any
extra code.

This code.....where is it going? I note for example that you have not declared the 'inst' variable? Is it available
within the same scope as this code? If not then have to declare 'inst' otherwise your code will fail.

For the purposes of writing and debugging code I tend to make a function of it so I can use the debug facilities of
the Zero Brane editor.

Craig
Title: Re: last line not executing
Post by: Bill_O on March 05, 2019, 04:08:01 PM
smuprh and Craig,

thanks for the information and input.
the application that i am going to be using the software for is not a cnc machine like a mill or router
i will be needing to home the machine multiple times while in operation
i will start working on the things you have both suggested

thanks again,
bill
Title: Re: last line not executing
Post by: joeaverage on March 05, 2019, 11:48:30 PM
Hi,
the reason I ask is because if this code is going to be attached to a button then it would be complied as part of
the GUI chunk. Therefore any wx.Timer code could go in the Screen Load script, which is of course always available
to all other code in the GUI.

If however you wish to run this code as a macro it will be complied and included in the Gcode interpreter chunk. Thus
any wx.Timer code in the screen load script (ie GUI chunk)  would not be available to it.

So I need to know how you intend to use this code in order that I can structure the code to the conditions of ude.

Craig

Title: Re: last line not executing
Post by: Bill_O on March 06, 2019, 07:50:42 AM
Craig,
right now it is in a button but it will need to be in a macro
Bill
Title: Re: last line not executing
Post by: Bill_O on March 06, 2019, 09:35:39 AM
I was looking at mc.AxisIsHoming
This appears to me this might be the best one to try using.
I just need a little help on the code.
Looking at the Mach4 Core API reference this is what i see.
int mInst = 0
BOOL homing = true
mc.mcAxisIsHoming(mInst, X_Axis, homing)
I think this is how it all needs to be but a few questions.
What is the "int" and why is it needed?
Why does the variable "mInst" need to be set to "0"?
What is the "BOOL" and why is it needed?
And the last and biggest how do I put this in a loop to keep looking until it is false.
I tried but I keep getting errors.

Bill
Title: Re: last line not executing
Post by: joeaverage on March 06, 2019, 11:31:12 AM
Hi,

Quote
right now it is in a button but it will need to be in a macro

You need to make up your mind either way.......they are not the same. If its in a button the code ends up in one chunk
but if it's in a macro it ends up in another.

Quote
int mInst = 0

That comes from the usage example and its in C++, disregard it. That's not how its used in Lua.

Quote
What is the "BOOL" and why is it needed?

BOOL means Boolean, so it is a variable type, either True or False, again its applicable to C++, C and C++ are very
strongly typed languages whereas Lua is not. It does not mean you can disregard it but its less important.

Quote
And the last and biggest how do I put this in a loop to keep looking until it is false.
I tried but I keep getting errors.

As I have posted and ordinary counting type loop wont work. You need to use a timer to generate a delay. Further
the timer or delay can place no burden on Mach because you are waiting for Mach to complete an API function, you
can't tell it to 'count to 1000' and expect it to complete its API call at the same time.
Have you looked at Daz's video? Have you coded a wxTimer? Where did you put the code?
Depending on how you wish this code to be used will alter where the code is placed and how its accessed, thus
I need to know if its a button OR a macro before I can help.

I notice in another of your threads you have some code that you are trying to use in two places, one as a button
and one as a macro. I think you should consider them as distinctly different.

Craig
Title: Re: last line not executing
Post by: Bill_O on March 06, 2019, 12:17:31 PM
Craig,

I had no idea that the code was often different between a button and a macro.
I was so used to Mach3 being the same it never even crossed my mind.
Almost everything I will need to be doing at this time will be macros (M Code) so that is where I will need to focus.
I will look over your earlier information again and look at the videos.
Thanks again,
Bill