Hello Guest it is March 02, 2024, 10:21:43 PM

Author Topic: Simple way to have Mach4 remember axis positions on startup.  (Read 402 times)

0 Members and 1 Guest are viewing this topic.

Simple way to have Mach4 remember axis positions on startup.
« on: April 26, 2023, 01:33:03 PM »
I've searched and found some explanations for why Mach4 doesn't remember the axis positions when it is shut down and restarted. They make sense but Mach3 used to do it and the examples I found were not working or I did not understand them. After searching and experimenting I came up with a simple way that works for me.
Firsst go to Configure - Plugins - Regfile.
  Click on the + and add three new registers: Xpos, Ypos and Zpos.
  make sure the persistent colume is green checked.
Next add the following to the screen load script:
---------------------------------------------------------------
-- Get register
---------------------------------------------------------------
function GetRegister(regname)
 local inst = mc.mcGetInstance()
 local hreg = mc.mcRegGetHandle(inst, string.format("iRegs0/%s", regname))
 return mc.mcRegGetValueString(hreg)
end

---------------------------------------------------------------
-- Save register
---------------------------------------------------------------
function WriteRegister(regname, regvalue)
 local inst = mc.mcGetInstance()
 local hreg = mc.mcRegGetHandle(inst, string.format("iRegs0/%s", regname))
 mc.mcRegSetValueString(hreg, tostring(regvalue))
end

Next  make a new Save button and add the following to the left up script:

posx = scr.GetProperty("droCurrentX","Value")
WriteRegister("Xpos", posx)
posy = scr.GetProperty("droCurrentY","Value")
WriteRegister("Ypos", posy)
posz = scr.GetProperty("droCurrentZ","Value")
WriteRegister("Zpos", posz)

Next  make a new Load button and add the following to the left up script:

regvalx = GetRegister("Xpos")
regvalx = tonumber(regvalx)
mc.mcAxisSetPos(inst, mc.X_AXIS,regvalx)
regvaly = GetRegister("Ypos")
regvaly = tonumber(regvaly)
mc.mcAxisSetPos(inst, mc.Y_AXIS,regvaly)
regvalz = GetRegister("Zpos")
regvalz = tonumber(regvalz)
mc.mcAxisSetPos(inst, mc.Z_AXIS,regvalz)

That should be it. Save all changes and When you click the save button it will save positions in the registers.  When exiting the registers will be saved in the machine.ini file. When mach4 is restarted click the load button and it will load the positions from the ini file.

If you want it to do it automaticly put the button codes into the screen load script and the screen unload script instead of buttons.

Yes things could move on the machine but this is how Mach3 worked and that was fine. It won't work if Mach crashes but it didn't on Mach3 either.
Hope this helps somebody.
Re: Simple way to have Mach4 remember axis positions on startup.
« Reply #1 on: April 26, 2023, 09:05:57 PM »
Hi,
I'm not so sure that will work.

In particular your use of:
mc.mcAxisSetPos(inst, mc.X_AXIS,regvalx)

will set the WorkOffset for that axis such that the DRO reports the required position relative to machine coordinates....BUT.....you've just fired up Mach......the machine
coordinates are undefined...so what does your WorkOffset mean? It traditionally and normally means a certain distance from the Home position but you have no
defined Home position when you first fire up Mach.

If you are trying to set machine coordinates to some previously saved value then your code fails.....that API does not set machine coordinates. The ONLY way to set machine coordinates
is by one of the Home procedures.

If you however Home first.....then you can set the machine to whatever you want....but why not just use G54, that is saved at shutdown and you don't have to write any code.
In fact all the WorkOffsets G54, G55, G56 etc are saved at shutdown automatically.

Just to illustrate the procedure:
Lets say I'm working on a project. The machine is Homed and I have set the G54 work coordinates to the very corner of the material in the vice. I do two of the three ops
before leaving for the night. I can shut the machine down. The next morning I'll power up the machine and FIRST thing I do is Home. This sets the machine coordinates.
My Home switches allow me to Home to the same location to better than 0.02mm every time.

I now MDI G54, being the work offset set that was in use the day before, so my machine now knows exactly where the corner of the material is and I can now hit <CycleStart> on
the third and final op to finish the part.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Simple way to have Mach4 remember axis positions on startup.
« Reply #2 on: April 26, 2023, 11:09:21 PM »
Well, I have seen you keep talking about that. I don't understand everything about this stuff , I don't have home switches and I suppose the other people that gave asked don't have them either.  I have mach set to home in place and this works every time. It may be off a thousandth or two, but it is close enough not to have to set it up from scratch.
If someone else tries this and it works for them please let us know.
All I know right now is it is working every time for me now, just like Mach3 worked. ;D
Re: Simple way to have Mach4 remember axis positions on startup.
« Reply #3 on: April 26, 2023, 11:38:15 PM »
Hi,
yes HomeInPlace is the ONLY way you can set a machine coordinate at will.

The problem arises that when you stop at a location the machine coords will be xmcymczmc and due to the work Offsets
the position of the machine is xwcywczwc, this is the position the DRO shows, but is in fact the arithmetic combination
of the applicable work offsets AND the machine coordinates.

When you turn Mach on you want the DRO to read the same....but you also want the machine coordinates to be the same. The code you have written does not set
the machine coordinates. How do you intend on setting them?

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'