Machsupport Forum

MachPro => MachPro General Discussion => Topic started by: tbadger on March 07, 2026, 04:45:09 PM

Title: PLC Scripts
Post by: tbadger on March 07, 2026, 04:45:09 PM
I'm wanting to add a PLC script that calls a module for Mach status lighting. In Mach 4, that seemed pretty straight forward. I'm not a seasoned coder, so there is little that is intuitive about LUA coding for me.  In Mach Pro, the "new" PLC Script just has the following:


Code: [Select]
if m.CommonPLCScript ~= nil then
m.CommonPLCScript()
end

I want to insert this code into the PLC Script, so that it calls the towerscript.lua module.

Code: [Select]
if towerscript == nil then
towerscript = require "towerscript" -- Load's towerscript.lua from a path. probably C:\Mach4\Modules
end
towerscript.PLCScript()

I tried just pasting it at the end of the previous lines, but that did not seem to work. I've found little information in the documentation that would guide me in the right direction.


Title: Re: PLC Scripts
Post by: tbadger on March 07, 2026, 05:02:03 PM
Okay, might just be user error. I did not restart Mach Pro after updating the script. On restarting, it does seem to be working as expected.
Title: Re: PLC Scripts
Post by: paul.stephens on March 09, 2026, 09:48:41 AM
In MachPro there's actually already a built in method for handling Light Stacks inside of Configure → Control → Settings. Alternatively you can also handle it through the GMS plugin as well.
If you prefer the lua code route, under the Service → Maintenance tabs, we have a Compile Scripts button that should apply your custom programming and most changes, so you no longer have to power cycle Mach.

In an ideal sense, we prefer to not have custom programming on the newer software as it becomes a nightmare to support. We are working on documentation on how to handle all these things.
Title: Re: PLC Scripts
Post by: tbadger on March 09, 2026, 09:56:11 AM
Thanks Paul - looking forward to the documentation. As I'm not a coder, but can figure out mostly how to add custom stuff that others have built and get it to work. Since Mach Pro has changed so much, its a challenge sometimes to figure out where to get things done.  While exploring further, I did note the Compile Scripts button on the Service tab. I think it is mostly getting used to the changes and knowing how things are done in Pro.

I think some clear examples of what might be considered typical of what someone might want to do would be helpful. In other words, beginning to end.

BTW, I am on the Beta, so happy to review the doc in progress.
Title: Re: PLC Scripts
Post by: cd_edwards on March 09, 2026, 01:24:08 PM
Edit Screen -> PLC script edit.
Add that snippet to the PLC script. Exit editor and then exit from Edit screen. It will ask if you want to save.
Say yes and your done. there should not be any reason to restart Mach at that point.

As for the config->control->settings light stack, it is NOT similar to what you are putting in place. It is meant for a tower Light that has 3 positions. And you have no control really on those.
the PLC.Towerscript() has alot more to say about machine status and is programmable to change what you'd like to see. I'm the one who wrote the towerscript and looked at various methods to implement
what I wanted, which did include the settings light stack.

Colten
Title: Re: PLC Scripts
Post by: cd_edwards on March 09, 2026, 01:36:13 PM
In an ideal sense, we prefer to not have custom programming on the newer software as it becomes a nightmare to support. We are working on documentation on how to handle all these things.


thank you for verifying what I'm suspected from the start of the beta program. Mach it seems is locking down features from consumer use. When Mach removes the Edit->screen function, I'll be saying goodbye and thanks for all the fish. I understand the support view point. but locking it down so the consumer ie me and many others is rather short sighted. Fixing bugs in the program that have been IN mach for years now would definitely be worthwhile. But then I'll assume it will never be backported to Mach4. That sucks. We all paid good money for a program which was supposed to be and usually is a massive upgrade from Mach3.
Title: Re: PLC Scripts
Post by: tbadger on March 09, 2026, 10:07:54 PM
I recognized who you were  :)  - I was able to add it and get it working fine in Mach Pro. But it would be helpful to understand how stuff like this will be possible in the future without the same level of access that was available in Mach 4, despite it's shortcomings.
Title: Re: PLC Scripts
Post by: paul.stephens on March 10, 2026, 09:50:25 AM
Hi,

The planned programming method for MachPro is going to be vastly different from what you have experienced on Mach4. We are continuously working on documentation for both the operator side and the programming side as there are hundreds of more API calls that can be made now.
Title: Re: PLC Scripts
Post by: tbadger on March 10, 2026, 12:10:31 PM
I do have to admit, that simple things like adding a button that toggles an output is far easier than having to write a script. Not being a coder, I'm able to get these simple things done far easier than before.

Using User Commands under the Screen Configuration option on the Service tab has been so far very easy to use. Once I've set up the output, then it is pretty straight forward to add a new command, select the command and the options, and it is done. No script.

However, for things like the script in this post, it would not have been possible with my skills to accomplish.
Title: Re: PLC Scripts
Post by: paul.stephens on March 10, 2026, 02:44:12 PM
Yeah, Ideally we are trying to remove some of this logic from the screens themselves. The GMS is amatively a little clucky - We are looking to improve it - but can be a powerful tool that could replace a lot of PMC and PLC logic that users have built into the GUI. Is it all powerful? no. Will it get most tasks done well or well enough? yes. When it comes to custom programming, we have a user GUI module and a user M code module for all custom logic or code. This is for modifying existing features or adding new features to our screens. We will have proper documentation on this in the -hopefully- near future.
Title: Re: PLC Scripts
Post by: tbadger on March 10, 2026, 05:04:54 PM
If you meant Clunky, I'd agree with you!  ;D  Given the movement towards AI, I was just thinking about how it could be used to implement things like this in the CNC world. Besides creating 3D imagery, this seems to be an underserved area in the CNC world.

I've used AI when doing coding on Android, and have found it pretty useful to help me when I've got stuck. Why not here?  Someone needs to create an LLM around the scripting and other languages used in the CNC world. That would be killer.
Title: Re: PLC Scripts
Post by: paul.stephens on March 11, 2026, 10:37:57 AM
Technically the GMS flow is a standard of flow but it's not very intuitive for users who are not used to it. We are trying to determine what kind of flow we want in the future. Personally I'm for a drag and drop method, but we could technically also do ladder logic or refine the text based version we currently have It's more about deciding how we want the product to look and feel at the end of the day. We have been moving towards the AI route to do prototyping. For example, we are trying to do a web based interface and using AI to help test for what is possible and not possible to determine what and how we want to implement said technology.
Title: Re: PLC Scripts
Post by: cd_edwards on March 11, 2026, 10:41:34 AM
maybe make it so it works as well. I've found a couple of errors which make it unusable. For instance it does not save the custom IO. When you restart the program, they disappear completely. Then there's the unhandled exception that pops up and causes Machpro to be unusable. I've already posted about this to Carl.
Title: Re: PLC Scripts
Post by: cd_edwards on March 11, 2026, 11:03:21 AM
There is also no method of sorting the items. What I'm looking for is a big if then elseif  end or switch solution. Some things take priority over others. ESTOP for example should be an absolute. Nothing else matters. it looks like this will go through ALL of the options and without sorting it, well it's not correct interpreting what the state of the machine is.
Title: Re: PLC Scripts
Post by: paul.stephens on March 11, 2026, 12:44:58 PM
Currently, I do wish to add other logic gates like NAND, NOR, XOR, XNOR.

When it comes to an elseif statement, you're right. You would have to make multiple GMS processes Each one setting a register or #var and base the other processes to read said register or #var making your conditions work. It is a lot of extra clutter and steps. I'll try to think of a process that can cover these things and go to make a proposal.