Hello Guest it is April 29, 2024, 12:53:46 AM

Author Topic: Mach4: Log Axis movement time.  (Read 1257 times)

0 Members and 1 Guest are viewing this topic.

Mach4: Log Axis movement time.
« on: January 09, 2022, 08:15:05 PM »
Is there a way in mach4 to log the amount of time that an axis moves? I would like for my Automatic lube to kick on for 7 seconds for every 240 sec of axis movement.

So I would want mach to log every time an axis moves. X or Z independently. X and Z simultaneously. Then add those together until it reaches 240 and then trigger the output and reset. Anyone have any idea of how to accomplish this? 
Re: Mach4: Log Axis movement time.
« Reply #1 on: January 20, 2022, 08:18:39 PM »
Anyone have any idea what command to use within mach to log the time an axis is moving? Should I just use the motors step or direction pin in my signal library and log how long it is active for? I've looked through the API but nothing is popping out at me. If someone could point me in the right direction it would be greatly appreciated.
Re: Mach4: Log Axis movement time.
« Reply #2 on: January 20, 2022, 08:37:59 PM »
Hi,

Quote
Should I just use the motors step or direction pin in my signal library and log how long it is active for?

That won't work, the Step/Dir outputs are outputs of your motion controller....not Mach. Mach does not pulse, it passes numeric movement commands to the
controller and the controller generates the pulse streams to enact the movement. You could presumably monitor the output of the controller but is would a factor of a 100,000
or more too fast for Mach.

May I suggest use these:

Code: [Select]
still, rc = mc.mcAxisIsStill(
number mInst,
number axisId)

Description:
Report if the axis is still.
Code: [Select]
velocity, rc = mc.mcAxisGetVel(
number mInst,
number axisId)

Description:
Get the velocity of an axis in user units per min.


Either could be used to determine whether an axis is moving.

Because I'm so comfortable with the concept...I would use the PLC script. At each pass of the script determine if an axis is moving and if so increment a register (of your devising)
pertaining to the accumulated time and axis has moved.

The PLC script runs every 10ms or so, you could consider it a live update. The only fly in the ointment is that your code would want to be fairly slick otherwise the computing overhead
would count against the overall performance of the machine. What I would suggest is somehow time the repeat rate of the PLC, and just increment a register by one unit, that unit being
equivalent to 10.5ms say.  Every 22857 units, ie 240 secs 'Do Some Lube Process" and reset the register.

Craig

'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Mach4: Log Axis movement time.
« Reply #3 on: January 20, 2022, 10:59:28 PM »
mcAxisIsStill doesn't really work for real time info. It will report still even when it looks like the dro is moving on screen. With PLC interval at 10ms you can expect it to report still much more than it reports moving. 

from the docs:

Code: [Select]
Note that an axis can be marked as still even when G code is still processing. A report that the axis is still simply means that the axis is not moving at the point in time the function is called.

You are better off making a cheap hardware device with an RTC or timing scheme to monitor motion and trigger an output. You can do it in LUA but there are soo many instances you'll have to account for.

If you do it in LUA, you could use os.clock or wx.wxTimer

Either way is a PITA

Code: [Select]
-- ScreenLoad

axisTime = 1
coolantTime = 1
secondPassed = true

timerPanel = wx.wxPanel(wx.NULL, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize)
timer = wx.wxTimer(timerPanel)
coolantSig = mc.OSIG_OUTPUT0

timerPanel:Connect(wx.wxEVT_TIMER,
function (event)
secondPassed = true
axisTime = axisTime + 1
mc.mcCntlSetLastError(inst, 'axisTime: ' .. tostring(axisTime))
if axisTime >= 240 then
mc.mcSignalSetState(coolantSig, true)
coolantTime = coolantTime + 1
mc.mcCntlSetLastError(inst, 'coolantTime: ' .. tostring(coolantTime))
if coolantTime >= 7 then
mc.mcSignalSetState(coolantSig, false)
axisTime = 1
coolantTime = 1
end
end
end)

-- PLC

if (machEnabled == 1) and (xAxisStill ~= 1 or zAxisStill ~= 1) then
--mc.mcCntlSetLastError(inst, 'x: ' .. tostring(xAxisStill) .. ' z: ' .. tostring(zAxisStill))
if secondPassed then
timer:Start(1000)
end
secondPassed = false
else
timer:Stop()
secondPassed = true
end
Re: Mach4: Log Axis movement time.
« Reply #4 on: January 20, 2022, 11:27:03 PM »
Hi,

Quote
mcAxisIsStill doesn't really work for real time info. It will report still even when it looks like the dro is moving on screen. With PLC interval at 10ms you can expect it to report still much more than it reports moving.

Does it matter? Who cares if it out by 10% either way?. The axes will still get lubed, and that is using relatively simple and light weight code with minimal impact on important (ie motion)
processing.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Mach4: Log Axis movement time.
« Reply #5 on: January 20, 2022, 11:40:46 PM »
Hi,

Quote
mcAxisIsStill doesn't really work for real time info. It will report still even when it looks like the dro is moving on screen. With PLC interval at 10ms you can expect it to report still much more than it reports moving.

Does it matter? Who cares if it out by 10% either way?. The axes will still get lubed, and that is using relatively simple and light weight code with minimal impact on important (ie motion)
processing.

Craig

It does matter as far as i can see.

when you print out the result of axis still (I used ess and just the simulator). It prints out 1 for axis still much more than it ever does 0. Your mileage may vary but that is what it did to me. I didn't try the velocity feature you said would work.

I was just saying it's a PITA because the person asking is basically looking for a stopwatch solution. it's just not that easy. especially considering the irregular behavior of user input when just depending on the PLC for input. There will be pauses, estops, cycle stops, program termination...etc.

In the latest mach4 there is a timer script function in the screen editor. that may be a solution. I was just following a dazthegas youtube video with the wxtimer. I was trying to add seconds with wxtimer events using the PLC. The problem was with the axis is still api function from Mach4. I noticed it doesn't report moving all the time. It's wayyyyy tooo fast to use it.

That was all i noticed. It was reporting results so fast that even the PLC couldn't have a conditional to detect it realistically. I even printed it using lasterror api and the trues still greatly outweighed the false.

and when mach is not enabled then there is still that condition in the PLC unless you handle machEnabled
« Last Edit: January 20, 2022, 11:56:40 PM by compewter_numerical »
Re: Mach4: Log Axis movement time.
« Reply #6 on: January 21, 2022, 12:57:41 AM »
Hi,

Quote
It does matter as far as i can see.

Rubbish, if its out, even by quite a margin, the axes will still get lubed....and thats all that matters.

Quote
I was just saying it's a PITA because the person asking is basically looking for a stopwatch solution.

We agree there, a genuine stopwatch solution would not be easy, if its even possible, and what would all that extra effort and CPU processing get you...
not  much.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Mach4: Log Axis movement time.
« Reply #7 on: January 21, 2022, 01:08:21 AM »
It triggers erratically with the api...mostly on sometimes off

try timing that in a real time system, with conditionals

but even you never solved this and here you are

https://www.machsupport.com/forum/index.php?topic=41007.0

the default interval of the plc on default mach install is 50ms btw

even you say it's an unrealistic timing mechanism

relying on software in this case may not be ideal
« Last Edit: January 21, 2022, 01:21:53 AM by compewter_numerical »

Offline jbuehn

*
  •  101 101
    • View Profile
Re: Mach4: Log Axis movement time.
« Reply #8 on: January 21, 2022, 11:40:13 AM »
FWIW, I've never has consistent results using mcAxisIsStill() either. The ESS has some per motor feed rate registers. In some cases it was easiest to just look at those to determine if an axis was moving.
Re: Mach4: Log Axis movement time.
« Reply #9 on: January 21, 2022, 04:06:48 PM »
Sorry for the "arguing" back and forth.

Looks like it can easily be done in LUA just not using the mcAxisIsStill() API.

Here is a video of it working: https://www.youtube.com/watch?v=HVh-0IxkmSo

I'm printing the seconds counter in the history log at the bottom and output 0 is used, so you'll see the LED turn on for 7 seconds after 240 seconds of x or z movement.

Here is the example code:
Code: [Select]
-- ScreenLoad Script

axisTime = 0
coolantTime = 0
coolantOn = false
secondPassed = true
outputSig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT0)
lastXPos = mc.mcAxisGetMachinePos(inst, mc.X_AXIS)
lastZPos = mc.mcAxisGetMachinePos(inst, mc.Z_AXIS)

timerPanel = wx.wxPanel(wx.NULL, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize)
timer = wx.wxTimer(timerPanel)

timerPanel:Connect(wx.wxEVT_TIMER,
function (event)
secondPassed = true
axisTime = axisTime + 1
mc.mcCntlSetLastError(inst, 'axisTime: ' .. tostring(axisTime))
if axisTime >= 10 then
mc.mcSignalSetState(outputSig, true)
                        coolantOn = true
coolantTime = coolantTime + 1
mc.mcCntlSetLastError(inst, 'coolantTime: ' .. tostring(coolantTime))
if coolantTime >= 7 then
mc.mcSignalSetState(outputSig, false)
axisTime = 0
coolantTime = 0
coolantOn = false
end
end
end)

-- PLC Script

local axesMoving = (lastXPos ~= mc.mcAxisGetMachinePos(inst, mc.X_AXIS) or lastZPos ~= mc.mcAxisGetMachinePos(inst, mc.Z_AXIS))
lastXPos = mc.mcAxisGetMachinePos(inst, mc.X_AXIS)
lastZPos = mc.mcAxisGetMachinePos(inst, mc.Z_AXIS)

if (machEnabled == 1) and (axesMoving or coolantOn) then
--mc.mcCntlSetLastError(inst, 'axesMoving: ' .. tostring(axesMoving))
if secondPassed then
timer:Start(1000)
end
secondPassed = false
else
timer:Stop()
secondPassed = true
end

One issue with this is that if a movement on the X or Z axes is less than a second, then the timer won't increment. You'll have to handle situations like eStop or disabling the machine or reset, etc by resetting the axisTime, coolantTime variables. The counter will stay at a value if the file reaches end of gcode so, you'll have to handle that if you want the timer to reset.
« Last Edit: January 21, 2022, 04:26:00 PM by compewter_numerical »