Hello Guest it is April 19, 2024, 06:50:10 AM

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - pjcevallos@gmail.com

Pages: 1 2 3 »
1
Mach4 General Discussion / Re: PLC script to write to a text file
« on: March 11, 2024, 04:34:40 AM »
Hi swiftyJ

Thanks for your reply, However, It also doesn't work showing the same error.
it thinks "file" is a variable.
If I add a "local" before variable, that line is not the error, but then the error goes to:
  • file:write
  • if file:write (text) commented, file:close()

I wonder if the PLC script can open and write files.

2
Mach4 General Discussion / PLC script to write to a text file
« on: March 09, 2024, 08:37:05 PM »
Hi

I have been trying to debug a code without success for the PLC script (See below). I want to record X,Y,Z vs time in an external text file activated by an output that will start data acquisition of another hardware.

I can successfully monitor the values I want in registers. However, I get an error as soon as I want to use
Code: [Select]
file = io.open(MyFile, "a") --open the file in append mode,
--attempt to index a nil value (global 'file')
--strack traceback
-- in function 'Mach_PLC_script'


I can open, write, save, and close files in Macros, but that is inconvenient.

Does any one have any idea how to approach this problem or what I am doing wrong?


Code that is added to the PLC:




Code: [Select]
-------------------------------------------------------
--  position Data
-------------------------------------------------------
local val_x = mc.mcAxisGetPos(inst, mc.X_AXIS) -- get X position
local val_y= mc.mcAxisGetPos(inst, mc.Y_AXIS) -- get Y position
WriteRegister("X_pos",val_x) -- to monitor in register diagnostics ok
WriteRegister("Y_pos",val_y) -- to monitor in register diagnostics ok

local LoadCellSignal = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT9 ) -- monitor output 9 activated by macro
local LoadCellSignalState = mc.mcSignalGetState(LoadCellSignal)

if (LoadCellSignalState == 1) then -- if output 9 is activated

local hregTime_LC = mc.mcRegGetHandle(inst, "iRegs0/Time_LC") -- get register Time_LC = 0
local LoadCellTime= mc.mcRegGetValue(hregTime_LC)

if (LoadCellTime==0) then
local Path = GetRegister(Layer_path) -- get File path from register
local MyFile = wx.wxGetCwd() .. Path -- define the file path in Mach4
file = io.open(MyFile, "a") -- open file in append mode
end


LoadCellTime = LoadCellTime+0.05 - follow PLC cycle time as sample rate
WriteRegister("Time_LC",LoadCellTime) --  monitor in register ok


local text = string.format ("%.4f, %.4f, %.4f \n", val_x, val_y, LoadCellTime );
file:write (text ) --Write the Gcode file
file:flush (MyFile) --Save written data
file:close (MyFile) --Close file
end


3
Mach4 General Discussion / Re: Connect a button for tool change
« on: November 12, 2023, 06:21:00 AM »
I believe it is quite straightforward

You need to use the PLC script and monitor the input signal. eg see the code below

Code: [Select]
local Button_Input= mc.ISIG_INPUT1 --define the variable for the input
local output_1 = mc.OSIG_OUTPUT1 -- define variable for output

-- get handles and states (states will be helpful to check if inputs outputs are active, use a message box, make it a comment )
local hsig_input = mc.mcSignalGetHandle(inst, Button_Input) --
local Button_Input_State= mc.mcSignalGetState(hsig_input )
local hsig_output = mc.mcSignalGetHandle(inst, output_1)
local output_1_State= mc.mcSignalGetState(hsig_input )

if (Button_Input_State== true) then
mc.mcSignalSetState(hsig_output ,TRUE)
end



I think that will do it, maybe someone else have a better way. Maybe the PMC would be faster.
Check the documentation for scripting and PMC

4
Mach4 General Discussion / Homing for Absolute Encoders
« on: November 12, 2023, 06:03:38 AM »
Hi

Following this thread, https://www.machsupport.com/forum/index.php?topic=47822.0

I wonder what is the procedure when you have absolute encoders for homing operation.

At the moment we do not home the machine as the absolute encoders remove the need for a physical limit switch.
In fact, the Origin is set as an offset. Whenever we need to go to "home", we put G53 x0 y0 z0, and it goes to place.

We have tried to "home in place" when the machine is in its origin, but it tries to move and makes a bad noise.

Just wondering if our approach is wrong. Maybe I am overthinking it.

kind regards

Pablo

5
Hi

were you successful with this?

Pablo

6
Mach4 General Discussion / Re: Doing math to DRO values
« on: December 02, 2022, 04:54:44 AM »
Hi

Craig is right, but I am not sure it depends on the Mach4 version. In my case, the Screen Load script already came with a reading and writing registers according to the Lua script manual as below. please check the manual. This means reading and writing registers is straight forward, but please remember they are write and read as strings, not numbers.

Code: [Select]
function GetRegister(regname)
local inst = mc.mcGetInstance()
local hreg = mc.mcRegGetHandle(inst, string.format("iRegs0/%s", regname))
return mc.mcRegGetValueString(hreg)
end

Code: [Select]
function WriteRegister(regname, regvalue)
local inst = mc.mcGetInstance()
local hreg = mc.mcRegGetHandle(inst, string.format("iRegs0/%s", regname))
mc.mcRegSetValueString(hreg, tostring(regvalue))
end

That meant that the registers are strings, even though they are numbers. You have to convert them into a number
Code: [Select]
val = tonumber(val)
Then you can use the variables to do math.

Something that I did not have time to check is that I could not perform an if the operation (Equal) because the decimals were not the same.
I guess I need to use the following, but I have not had time to do it.

Code: [Select]
tonumber(string.format("%." .. (numDecimalPlaces or 0) .. "f", num))
Silly things happened to me and crashed mach4, but now I learned (literally this week)

Check your register's name: an empty space at the end of the register. I copy/pasted from notepad and didn't check the empty space, which crashed the PLC script because a "new" variable with nil value was being used.

Pablo

7
Mach4 General Discussion / Re: Read Register from a text file
« 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


8
Mach4 General Discussion / 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

9
Mach4 General Discussion / Re: Programs won't continue after m6
« on: August 08, 2022, 05:14:20 PM »
does your m6 macro has a "return"

for example

Code: [Select]
if selectedTool == currentTool then
mc.mcCntlSetLastError(inst,'Tool Change activated not required')
return
elseif currentTool == 1 then  --DROP TOOL 1 BLANK AND COLLECT TOOL 2

mc.mcCntlGcodeExecuteWait(inst, "G90 G53 G0 Z-70 F3500")

return

end


10
Modbus / Re: Where to start
« on: August 08, 2022, 08:40:14 AM »
Hi Craig

Just wondering if you have ever sent data (numbers, string, no digital/analogue inputs) from another software/interface (python/arduino) to Mach4?

My idea is to have an input signal that stops the gcode running, and then the "software/interface" decides the new gcode line where to start (another piece of data sent to Mach4), and then an input signal (string, or number) to start the cycle from that line.

I hope my idea is clear

kind regards

Pablo

Pages: 1 2 3 »