Hello Guest it is April 24, 2024, 04:19:05 AM

Author Topic: Read Register from a text file  (Read 547 times)

0 Members and 1 Guest are viewing this topic.

Read Register from a text file
« on: August 08, 2022, 06:51:35 PM »
Hi

Is it possible to read registers or variables from a text file using Lua scripts?

The idea is to overwrite the text file when needed while a gcode is running, and therefore change the registers/variables in the process but from the text file.

Hope you can help me

Pablo
Re: Read Register from a text file
« Reply #1 on: August 09, 2022, 03:36:51 PM »
There is a standard library that allows you to read/write a file. See the following link for more info

https://www.tutorialspoint.com/lua/lua_file_io.htm

If you can give a bit more detail on what you want to do, might be able to help more. What will the structure of the text file be?
Re: Read Register from a text file
« Reply #2 on: August 09, 2022, 04:32:16 PM »
Sorry for the long explanation, but I cannot find better words. Also English is not my native language, so please accept my apologies for any mistake, though I lived quite a few years in the UK.

Right now I am studying material extrusion for 3D printing which uses a coordinated axis A, and an out-of-band axis. The coordinated axes push a specific quantity of material, and the OB axis acts as a shutter valve opening and closing an aperture. Apart we have the normal X-Y-Z axes.
A gcode program is loaded from a slicer (which is post-processed to add the OB axis commands using pound variables #600,#601), and we wait until it finishes with the parameters given.

We are studying an option to do in-situ modification or closed-loop feedback while running the gcode program to achieve a good quality part. Furthermore, we also are studying a defect correction by using a spindle that removes material and then we can re-start the extrusion from a previous line in the gcode.

While it is printing, we can see if the extrusion is good or not, and then we use the feed rate override to improve it, but this is dependent on an operator's decision, and this is one of the parameters we want to automate if possible. In the second case, if there is a defect that can be removed, we stop, we manually removed the material, and then restart the gcode from the line we want. Again, we would like to automate this.

Our options for external sensors are Camera vision, laser scanner, and possible a flow rate or pressure sensor with an analogue output.

In all cases, we will need an external interface (hardware and/or software) to do the data post-processing and decision-making and the person in charge of that wants to use python. Furthermore, we do not have analog inputs in our controller.

What we are not sure about is how this "decision-making" from python can be communicated to Mach4.

I believe using registers is our best shot because of their flexibility: they can be either signal states, numbers, or strings. Using only digital inputs doesn't look logical.

For example, If a text file contains a table with registers, this file can be updated by python when needed (txt or csv file), then the PLC script in mach4 can open, read the file, and set the new values of the registers such as the FRO, the gcode line to start, Depth of cut, or others, or do nothing if there are no modifications in the register values, and then close the file. Because it is the PLC script it will check this file every 50 ms. It is like feeding variables (numbers/string) to mach4 to do online modifications. Of course, there will be a lag, but we want to study the efficiency of the method.

In the case of stoping, machining, and restarting, I believe a lua program can use a depth_of_cut register to perform a machining operation. Once it finished machining, then restart the gcode program in a previous line.

The third idea from one of our colleagues is to modify the gcode on the go and sent it and execute it in mach4 line by line. Again the same, the problem is how to communicate between another software and mach4.

As a reference from a previous project.
In the past, I achieved a "closed-loop force system" for a totally different application and with a different controller and software (GRBL) in which Labview was the CNC graphical interface sending Gcode commands through serial communication to the GRBL microcontroller (see link below). I was able to modify the position of the Z axis based on the force sensor values only because the microcontroller responded to serial commands in the shape of Gcode. Here I did not care about axis accuracy but having a "constant" force
https://www.youtube.com/watch?v=k0nlSpAebmA


I hope I described the goals as clear as possible. I cannot stop thinking how to do it so I hope someone can give me some direction.

Pablo

Re: Read Register from a text file
« Reply #3 on: August 16, 2022, 01:37:10 AM »
first you have Api very powerfull can read write registry
but you can also add m function that will read or right text file or registry or run external app that do....
so you can do every thing but only one point i cant find solution its about feedback
for example when you load gcode mach generate it and this very dangerous point ,because if you command run before mach finish generate ,you will gen unexcepted behavior
can start run not from start ,can stack ....and its real problem ,and after so many test(please not advice read the return value because this answer mach engineer tell me and its not help at all)only solution for me now its delay according file size....yes very very poor solution


Re: Read Register from a text file
« Reply #4 on: August 20, 2022, 12:02:48 AM »
best way to do this is with hardware like an arduino and using the modbus plugin

second best way to do this is change the registers on the fly in the PLC without an external program or software

but, if you use python and want to update registers in the PLC, don't open a file every 50ms. It will like just freeze Mach. Instead, in your python program, write an extra [blank] file to the filesystem everytime you "update" the values you want to send to Mach4. Then in the PLC, monitor and look for the "update file". if you find it, then delete the update file, open the file that contains the register settings and write your registers. This blank file would act like a 'nonce' in comm protocols. except the filename for the update file can be the same everytime since you will always delete it.

python has modbus libs. You might be able to create a virtual com, similar to a loopback address and communicate via the modbus plugin. Or maybe the tcp modbus can work that way, you'll have to find out for yourself.
« Last Edit: August 20, 2022, 12:13:53 AM by compewter_numerical »