Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: BGEvents on October 09, 2019, 10:58:01 AM

Title: Mach 4 Screen Editor GCode Help
Post by: BGEvents on October 09, 2019, 10:58:01 AM
Please can somebody help out with a screen editor question:

I require a button on a Mach4 screen that moves the Y axis on my router table forward by 10mm, and then once the button is toggled again it should move back 10mm to its original location. I would also like for this to be triggered by an input on my Hicon Integra motion controller if possible?

I would imagine it would require running a line of GCode, but I don't know where to start with the LUA script  :(

Thanks!
Title: Re: Mach 4 Screen Editor GCode Help
Post by: Chaoticone on October 09, 2019, 11:33:14 AM
What is the purpose of this?
Title: Re: Mach 4 Screen Editor GCode Help
Post by: BGEvents on October 09, 2019, 11:43:30 AM
What is the purpose of this?

I sometimes mount a chop saw on the end of my router table and use the Y axis as a back stop for the material I’m cutting which has worked great (I’m very limited in space in my workshop!)

It would be good to have the Y Axis move forward slightly once the material  to be cut has been clamped. Then after the cut the axis should move back into place for the next cut.

At the moment I’m achieving this manually by inputting GCode in the MDI each time.

Title: Re: Mach 4 Screen Editor GCode Help
Post by: Chaoticone on October 09, 2019, 01:08:11 PM
Hmmmmmmm, ok. I like it.  :)

This will move the Y axis 10 + from current position

Code: [Select]
local inst = mc.mcGetInstance()
local YMach, rc = mc.mcAxisGetMachinePos(inst, mc.Y_AXIS)
rc = mc.mcCntlMdiExecute(inst, string.format("G0 G53 Y%4.4f", (YMach + 10)))

This will move the Y 10 - from current position

Code: [Select]
local inst = mc.mcGetInstance()
local YMach, rc = mc.mcAxisGetMachinePos(inst, mc.Y_AXIS)
rc = mc.mcCntlMdiExecute(inst, string.format("G0 G53 Y%4.4f", (YMach - 10)))

Plop you a toggle button on the screen and put one script in the buttons Up script event and the other script in the buttons Down script event. Put whatever you want for the buttons up and down colors and text. That will take care of the button.
Title: Re: Mach 4 Screen Editor GCode Help
Post by: Chaoticone on October 09, 2019, 01:14:16 PM
Also set your button you add enabled states to something that won't get you into trouble. For example, I'm pretty sure you don't want this to happen if the axis is executing a gcode move already and you hit the button.
Title: Re: Mach 4 Screen Editor GCode Help
Post by: BGEvents on October 09, 2019, 01:28:24 PM
Also set your button you add enabled states to something that won't get you into trouble. For example, I'm pretty sure you don't want this to happen if the axis is executing a gcode move already and you hit the button.


Legend! Thanks a lot for this - will test it out later  :)

Is it quite straightforward to link this to an input on my motion controller so I can have a foot switch toggle the button as well?
Title: Re: Mach 4 Screen Editor GCode Help
Post by: Chaoticone on October 09, 2019, 02:42:40 PM
Quote
Is it quite straightforward to link this to an input on my motion controller so I can have a foot switch toggle the button as well?

Yes, get the button working first. You can then add the input to the sigLib in screenload script and set the buttons state based on the state of the input.
Title: Re: Mach 4 Screen Editor GCode Help
Post by: Bill_O on October 09, 2019, 03:39:07 PM
You might want to have a g91 before those moves in case the current position is not at 0 then switch back to g90
Title: Re: Mach 4 Screen Editor GCode Help
Post by: Chaoticone on October 09, 2019, 05:00:21 PM
Not necessary Bill.

The way he described it he just wants to move 10 + or - from where he is at. This is why I am getting the current position and moving + or - of it. And I'm doing it using a G53 so I don't have to get, change and set back modal modes he may be in when he wants to do this. If he wanted to go to an absolute position every time I would still do it with G53 for the same reasons listed above but I would not need to get the current position or do any math to it to find out where I wanted to end up.
Title: Re: Mach 4 Screen Editor GCode Help
Post by: Chaoticone on October 09, 2019, 05:14:01 PM
Also, not a good idea to just "switch back to g90". How do you know it was in G90 when the script ran? You don't unless you check. May well have been in G91 before the script ran, Gcode is perfect and doing exactly what it should (setting G91 mode) when it should. Then you run the script and that script changes it to G90. How does the Gcode know this changed and how does it overcome this? Well, it doesn't. That is why if you set any modal modes in your script you better check what they were before you set them and set them back to previous status before you finish your script. 
Title: Re: Mach 4 Screen Editor GCode Help
Post by: Bill_O on October 11, 2019, 03:09:21 PM
Guess I should have looked closer at the code you gave him.
I see where you are getting the current position.
Not sure why you think it is a bad idea to change between g90 and g91 as long as you put things back where you need it when you are done.

Bill
Title: Re: Mach 4 Screen Editor GCode Help
Post by: BGEvents on October 22, 2019, 06:02:43 AM
Quote
Is it quite straightforward to link this to an input on my motion controller so I can have a foot switch toggle the button as well?

Yes, get the button working first. You can then add the input to the sigLib in screenload script and set the buttons state based on the state of the input.


Thanks very much for your help Chaoticone! The on screen button works as intended which is great!

I've had a go at adding code to link a physical input button to change the state of the on screen button in the sigLib but cant get it to do anything :(
Could you please shed some light on how this is possible??
I noticed you can link an input to a toggle button in the buttons properties, but this just seems to toggle the appearance of the button but not change its state.
Title: Re: Mach 4 Screen Editor GCode Help
Post by: Chaoticone on October 22, 2019, 09:56:04 AM
Quote
Is it quite straightforward to link this to an input on my motion controller so I can have a foot switch toggle the button as well?

Yes, get the button working first. You can then add the input to the sigLib in screenload script and set the buttons state based on the state of the input.


Thanks very much for your help Chaoticone! The on screen button works as intended which is great!

I've had a go at adding code to link a physical input button to change the state of the on screen button in the sigLib but cant get it to do anything :(
Could you please shed some light on how this is possible??
I noticed you can link an input to a toggle button in the buttons properties, but this just seems to toggle the appearance of the button but not change its state.

No problem.

I'm doing some testing right now. Let me get back to you later on this. If you don't hear from me by tomorrow afternoon give this topic a bump. The broad idea is to put your button script into a function in the screen load script, add your input signal used for it to the sigLib table and have the inputs state and the buttons clicked script call that function. But there may be a simpler way........ catch you soon i hope.

Not sure why you think it is a bad idea to change between g90 and g91 as long as you put things back where you need it when you are done.

Bill

Bill, no idea how you extracted that out of what I posted. However the simplest way is to avoid checking, changing and setting back all together (which is what my code does) when you can. But you made no mention of checking and setting back so I had to expand to clarify. Not doing so could cause someone unaware a ton of grief. Solutions posted need to be accurate and like good scripts should not assume anything. If not and I see them, I will correct/clarify it the best I can or just delete them all together. No telling how many will see the post over time.

You might want to have a g91 before those moves in case the current position is not at 0 then switch back to g90
Title: Re: Mach 4 Screen Editor GCode Help
Post by: Chaoticone on October 22, 2019, 09:37:30 PM
Hey  BGEvents, can you tell me what input signal in Mach you are using and the exact name of the toggle button you pasted the code into?
Title: Re: Mach 4 Screen Editor GCode Help
Post by: BGEvents on October 23, 2019, 02:53:33 AM
Hey  BGEvents, can you tell me what input signal in Mach you are using and the exact name of the toggle button you pasted the code into?

I have the input linked to Input3 and the button name is move10

Thanks
Title: Re: Mach 4 Screen Editor GCode Help
Post by: Chaoticone on October 23, 2019, 09:42:38 AM
OK, below is a pretty simple way to do it. But it may not be as robust as it should be. There might need to be some checks/lockouts that prevent the function from being called again before the previous move is finished. What can happen now is you can click the button or pedal and while it is moving the 10 units let off the pedal or click the button again. This will make it move again in the same direction (like a step jog would) the next time the button or pedal changes states. But it will move another 10 form its current position at that time. You might even find that a bit helpful? But if you press the pedal down and hold until it has moved the 10 it will move in one direction when pressed and the other when released. Same with the button. If you click it one time and wait until it finishes moving before pressing it again up will move it one direction and down will move it in the other.

Add this to the top of the sigLib table in the screen load script.
Code: [Select]
[mc.ISIG_INPUT3] = function (state)
moveYhardStop(state)
end,

Add this function to the screen load script anywhere after the sigLib table.
Code: [Select]
function moveYhardStop(dir)
scr.SetProperty("togBGtest", "Button State", tostring(dir))
local inst, YMach, rc
inst = mc.mcGetInstance()
YMach, rc = mc.mcAxisGetMachinePos(inst, mc.Y_AXIS)
if (dir == 0) then --Up == "0" Down == "1"
rc = mc.mcCntlMdiExecute(inst, string.format("G0 G53 Y%4.4f", (YMach + 10)))
else
rc = mc.mcCntlMdiExecute(inst, string.format("G0 G53 Y%4.4f", (YMach - 10)))
end
end

Now for your toggle button. First clear the Input Signal property you have that input set to. Then in its Up and Down Script events put this code which calls the function you put in the screen load script passing it the direction parameter. The direction parameter (dir) needs to be the number 0 or 1. So, both Up and Down script events will have the same code. the only difference is one will have a 0 and the other will have a 1. I think your up script should have a 0 and down a 1 but that depends on how/when your foot pedal goes low and high and if you want pedal down to == button down or button up.
Code: [Select]
moveYhardStop(0)
Play with that and see if you like it, need to add some checks, etc.
Title: Re: Mach 4 Screen Editor GCode Help
Post by: BGEvents on October 30, 2019, 02:56:56 PM
Thanks Chaoticone - you've nailed it!

It took me a while to realise that the button will only work once I restart mach4  ::) ;D

I actually really like the double tap to move further and auto return when you hold, it works really well with my application.

Cheers,
Ben
Title: Re: Mach 4 Screen Editor GCode Help
Post by: Chaoticone on October 30, 2019, 06:49:27 PM
 :)