Machsupport Forum

Mach Discussion => VB and the development of wizards => Topic started by: Gakiem on June 28, 2006, 03:34:59 PM

Title: The basics of making a switch run a script?
Post by: Gakiem on June 28, 2006, 03:34:59 PM
Hi All

A very new user here, with a big router that has some push-button switches on the gantry. First I'll tell you what took me all day to figure out, so you can see my "expertise"....

Have 4 pushbutton switches and 1 probe (Normally Open). Got the red switch to do E-stop - easy. Then a few hours on getting the Green switch to do equivalent of Alt-R with OEM trig on Ports&Pins and External Buttons-OEM Codes on SystemHotkeysSetup (1000). 5 minutes to setup the Yellow feedhold OEM 1001.

Now to the problem at hand. The last pushbutton must plunge the z and zero the z off the probe switch. I have the probe working in Ports&Pins, and can get an OEM Trig to work off that switch. I have been told I must execute the following script when hitting the black switch:

code "g31 z-10 f50"
While IsMoving()
Wend
code "G92 Z0"
code "G0Z3"
While IsMoving()
Wend
code "g92z0"

Where do I save this code? Is it with the M12**.m1s files? Can I call an M.....m1s file with an OEM trig?

Thanks for any hints!

G

Title: Re: The basics of making a switch run a script?
Post by: Brian Barker on June 28, 2006, 07:39:05 PM
There is a way to call a script from an OEMtrig ... I can't think of it at this point :( I will get back to you
Brian
Title: Re: The basics of making a switch run a script?
Post by: Brian Barker on June 28, 2006, 08:24:07 PM
You need to set the Macro to be called with a VBscript... SetTriggerMacro (450) would set it to M450

And put 177 for the OEM code .. Or 277.. it is one of them ... All this is in the manual if you would like to know more :) And if you have any trouble please feel free to ask!

Title: Re: The basics of making a switch run a script?
Post by: Gakiem on June 29, 2006, 09:29:21 AM
I feel almost too shy to confess that I understand little of what you said. I went backwards and forwards through Using Mach3 Mill and think I am further lost now. Are you perhaps talking of some other manual?
Title: Re: The basics of making a switch run a script?
Post by: fer_mayrl on June 29, 2006, 12:05:40 PM
I think this should work...

In the Ports and Pins config menu, go to inputs, select an input or an OEM trigger, put your pin and port settings for the switch you are using, and assign a hotkey to that input. Now go and create a button on screendesigner, make that button run the sI thicript you want and assign that buttin the same hotkey you set on the input ports and pins. I think its in the ScanCode box. Now if you dont want that button to show up on your screen use screen designer to drag the button out of the workarea.

I think that might work, havent tried it, please let me know.
Regards
Fernando
Title: Re: The basics of making a switch run a script?
Post by: Brian Barker on June 29, 2006, 12:41:46 PM
Sorry about that :(

look in the Mach2 Customization manual

thanks
Brian
Title: Re: The basics of making a switch run a script?
Post by: Brian Barker on June 29, 2006, 01:10:59 PM
The OEM code that you need to put into the OEMTrig box is 301.. This will call the Macro that you set with the Call SetTriggerMacro

IF you upload your XML I will make it work :)
Title: Re: The basics of making a switch run a script?
Post by: Gakiem on June 30, 2006, 07:59:08 AM
Hi Brian

Thanks for the offer! Here is the file:


G
Title: Re: The basics of making a switch run a script?
Post by: Brian Barker on June 30, 2006, 08:36:40 AM
Here you go :)

Copy in this new XML and copy the M450 and M451 Macros in to the macros/Mach3Mill directory.... The M450 is the one that you can put you VB code in, and I have it set to work with  OEMTrig#4
Title: Re: The basics of making a switch run a script?
Post by: Gakiem on June 30, 2006, 09:20:40 AM
Thanks Brian!

G
Title: Re: The basics of making a switch run a script?
Post by: fer_mayrl on July 04, 2006, 03:04:10 PM
Ok, i tried what I posted on my previous post and it doesnt work, so I tried Brians way and it works!
3 Questions though.

1. The M450 macro does not run unless you run the M451 first, how can you make the running of M451 automatic at startup or some other time as for the user no to do it each time Mach3 is loaded.
2. What happens if you want to use more than one external button, what OEM codes (like the 301) and what macro numbers like the (M450 and M451) should be used for each OEM trigger?
3. I noticed that if you press the button the script starts to run, and if you press it again while the code is running, as soon as the first code is finished a second code starts to run. How can this be avoided?

Best Regards
Fernando
Title: Re: The basics of making a switch run a script?
Post by: Brian Barker on July 04, 2006, 05:42:15 PM
The best way to get the macro number loaded on startup is to add the M450 to the init string (under Config/State)

that should do it...
Title: Re: The basics of making a switch run a script?
Post by: fer_mayrl on July 04, 2006, 06:06:58 PM
Thanks a lot Brian,
Any thoughts on the other two questions?
Regards
Fernando
Title: Re: The basics of making a switch run a script?
Post by: Brian Barker on July 04, 2006, 06:33:25 PM
you can use any macro that you like :) The problem is that you can only have one button the is doing everything. Art and I have talked about changing this but it is going to be after the G100 is done...

There will be a bug fix for the buffered commands..
Thanks
Brian
Title: Re: The basics of making a switch run a script?
Post by: fer_mayrl on July 04, 2006, 06:42:46 PM
Thanks a lot Brian,
I guess meanwhile a keyboard emulator would have to do the trick along with a scripted button.
And maybe a set of one shot relays to avoid the buffered commands.
Thanks again
Fernando
Title: Re: The basics of making a switch run a script?
Post by: Brian Barker on July 04, 2006, 07:14:16 PM
Afer talking to Art we think it is best that you put code in your macro to see IF the macro should be run. It is very easy to add code that looks at the state of an LED and figure out if the macro should run. so lets say that your macro loads a Gcode file and runs it SO at the start of tha mcaro you should look and see if there is a job running. Here is an example:

Sub Main()

if (GetLED(4)) Then'This is where the Macro is told not to run if there is a job running
MsgBox("Could Not load there is a Job running")
Exit Sub
End if

'TODO add Macro code here

Main

This is more work but gives you more power in the end...
Title: Re: The basics of making a switch run a script?
Post by: fer_mayrl on July 05, 2006, 12:41:14 PM
I dont know if this would be possible, practical or even reasonable or easy enough to implement...

If when an input or an oemtrig is activated via a switch (button, limit switch or whatever) The keystroke or hotkey related to that input can be emulated, then a button on the screen which is related to that hotkey would be run.
For instance:
If I have OemTrig #1 set to port 1 and pin 10 and assign the hotkey Q (81) to that trigger, then every time the pin 10 is grounded Mach will know when that trigger has been activated, then mach can emulate that keystroke. If that is done, a user button that has a script within can be assigned to hotkey Q(81) and the script would run everytime pin 10 is grounded.

Regards
Fernando
Title: Re: The basics of making a switch run a script?
Post by: fer_mayrl on July 05, 2006, 01:58:35 PM
Afer talking to Art we think it is best that you put code in your macro to see IF the macro should be run. It is very easy to add code that looks at the state of an LED and figure out if the macro should run. so lets say that your macro loads a Gcode file and runs it SO at the start of tha mcaro you should look and see if there is a job running. Here is an example:

Sub Main()

if (GetLED(4)) Then'This is where the Macro is told not to run if there is a job running
MsgBox("Could Not load there is a Job running")
Exit Sub
End if

'TODO add Macro code here

Main

This is more work but gives you more power in the end...

Ok I tried this and it said compile error syntax error. So I tried some other way, like this:

If IsMoving() then
MsgBox ("Cant do it, its moving")
else
Code "F100"
Code "G01 x10"
Code "G01 x0"
Code "G01 x10"
Code "G01 x0"
End If

And while debugging in the VB editor, it works, if the machine is moving, the cycle displays the message and ends, but when I do the actual test, it doesnt work, it sometimes displays the message, and still move, and sometimes it just moves without displaying the message.
Dont know whats going on
Regards
Fernando
Title: Re: The basics of making a switch run a script?
Post by: fer_mayrl on July 05, 2006, 03:05:18 PM
I just found out another thing,

If I press the button for the oemtrig 3 times, i find that it runs the script 2 times, and when the first script finishes the message box pops up, and does not run the script the third time... Same thing happens if you press the button 5 times, it runs 2 times and 3 message boxes pop up.
So the If then else script works fine, except for the second time, more than two times and it works fine.
Regards
Fernando
Title: Re: The basics of making a switch run a script?
Post by: Brian Barker on July 05, 2006, 09:23:27 PM
Sorry i missed the End sub... It is hard to go from C++ to VB :(  I know this is not as simple as you would like but this gives you the most power...

Sub Main()

if (GetLED(4)) Then'This is where the Macro is told not to run if there is a job running
MsgBox("Could Not load there is a Job running")
Exit Sub
End if

'TODO add Macro code here
End Sub
Main
Title: Re: The basics of making a switch run a script?
Post by: fer_mayrl on July 06, 2006, 12:35:20 PM
Sorry i missed the End sub... It is hard to go from C++ to VB :(  I know this is not as simple as you would like but this gives you the most power...

Sub Main()

if (GetLED(4)) Then'This is where the Macro is told not to run if there is a job running
MsgBox("Could Not load there is a Job running")
Exit Sub
End if

'TODO add Macro code here
End Sub
Main

Tried this method, works the same way the IF THEN ELSE method i tried earlier, if you press it two times fast, it runs 2 times, if you press it 3 times fast it runs two times and the third displays the message box.
BTW, i tried it with the ISMoving command because my script does not load a file and run it, and my screen does not have a led 4. might this be the problem...
Im starting to think about a workaround, have a led light up at the begining of my script, and turn off at the end, and check for that lead instead.
Regards
Fernando
Title: Re: The basics of making a switch run a script?
Post by: fer_mayrl on July 06, 2006, 12:58:09 PM
Ok I tried this with the same results as the previous two versions:

Sub Main()

if (GetUserLED(1000)) Then'This is where the Macro is told not to run if there is a job running
MsgBox("Could Not load there is a Job running")
Exit Sub
End if

SetUserLed(1000,1)
Code "F1350"
Code "G01 x100"
Code "G01 X0"
While IsMoving()
Wend
SetUserLed(1000,0)
End Sub
Main
Title: Re: The basics of making a switch run a script?
Post by: Brian Barker on July 06, 2006, 02:56:20 PM
I have found a way but it is using the Macro pump  as well as a few LED's
Title: Re: The basics of making a switch run a script?
Post by: fer_mayrl on July 10, 2006, 01:54:38 AM
I guess I can wait till you get around to buffered comands.
Any ideas on why all the scripts I tried worked the way they did?
Cant think of any reason why they only work when pressed more than 2 times fast.
regards
Fernando