Machsupport Forum
Mach Discussion => Mach4 General Discussion => Topic started by: RecceDG on December 10, 2021, 10:40:20 AM
-
I'm designing my mill control panel and I think I may have (again!) stepped on a problem.
I have a 3-position switch that controls 2 input lines:
SPINDLE OVERRIDE
FEED OVERRIDE
With the third position being "JOG", but that is considered the default so it doesn't need an explicit control line.
The idea here is that if this switch is in either one of the override positions, the MPG jog handle now controls the respective override functions.
Looking at my lathe control panel code, there is a function to assign a jog handle to an AXIS, but I don't see a way to read the handle as a +/- device I could assign to a variable.
Is there a way to do this?
-
OK, so research and psudocode:
if (SPINDLE_OVERRIDE_SWITCH == ON) {
old_axis = mc.mcMpgGetAxis(MPG_INDEX) // Find out what axis the MPG is currently set to and store it
mc.mcMpgSetAxis(MPG_INDEX, SOME_UNUSED_AXIS) // Set the MPG to an axis we aren't using
current_override = mcSpindleGetOverride() // Get the current spindle override value
counts = mc.mcMpgGetInc(MPG_INDEX) // Read how much the handle moved NOT SURE THIS IS RIGHT
new_override = current_override + (counts * SCALING_FACTOR) // Compute the delta
mc.mcSpindleSetOverride(new_override) // Apply the override to the spindle
mc.mcMpgSetAxis(MPG_INDEX, old_axis) // Reset the MPG axis
}
I'm not sure how this gets called when the handle moves though. For sure I can call it when the override switch state changes, but how do I call it when the handle moves?
Feed override is similar, using GetFRO / SetFRO.
-
This isn't quite right.
I can assign the MPG to an unused axis easily enough, then "moving" that axis with the handle results in the machine thinking something moved and updating coordinates on that axis's DRO. If every time I read that axis, I zero it out, whatever the current coordinates are is how far the handle was turned since the last read.
What I don't have is a way to call the script when the handle moves / when that axis moves.
How do I do that?
I could really use some help here....
-
If you can read the MPG counts through a register you could monitor the register in the PLC script and when it changes if the inputs for spindle override or feed override are on then adjust. Maybe something like this..?
Get register count
if register count does not equal last count then
if spindle override switch is on
adjust spindle override
end
if feed override switch is on
adjust feed override
end
end
last register count = current register count
-
https://youtu.be/4KEZEf93Qxk
PLC Script
---------------------------------------------------
--DG 9 April 2022
-- Enable panel MPG to change override values like a volume control if the selector switch is in the correct position.
-- This could (should?) be optimized to use less variables to reduce function overhead
-- but at the moment, it is optimized for readability/comprehension in case Future Me needs
-- to change it.
--
-- NOTE THAT FEED OVERRIDE USES 0-100 CONTROL VALUES AND SPINDLE OVERRIDE USES 0-1
-- handles
local hAxis_Spindle
local hAxis_Feed
local SelectAxis_Spindle
local SelectAxis_Feed
local hEncoderPosition
local MPGHandleRawCount = 0
local MPGHandleRecenteredCount = 0
local MPGHandleOverrideDelta = 0
local OverridePercentChange = 0
local OverrideValue = 0
-- Grab the switch handles
hAxis_Spindle, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT2)
SelectAxis_Spindle, rc = mc.mcSignalGetState(hAxis_Spindle)
hAxis_Feed, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT3)
SelectAxis_Feed, rc = mc.mcSignalGetState(hAxis_Feed)
if ((SelectAxis_Feed == 1) or (SelectAxis_Spindle == 1)) then
-- Grab the current MPG value
hEncoderPosition, rc = mc.mcRegGetHandle(inst,"ESS/Encoders/Encoder_0")
MPGHandleRawCount, rc = mc.mcRegGetValue(hEncoderPosition)
-- Adjust for the zero point (global variable) we set when the HANDLE_MODE switch was activated
-- Set by PanelHandleModeChange() in the screen load script
MPGHandleRecenteredCount = MPGHandleRawCount - GlobalMPGHandleOverrideCurrentOffset
-- Compute the delta from the global variable where we keep the last known value, and store the current raw count there
MPGHandleOverrideDelta = MPGHandleRecenteredCount - GlobalMPGHandleOverrideCurrentValue
GlobalMPGHandleOverrideCurrentValue = MPGHandleRecenteredCount
-- Convert counts to a percentage change
OverridePercentChange = MPGHandleOverrideDelta / GlobalPanelMPGOverrideStepsPerPercent
if (SelectAxis_Feed == 1) then
OverrideValue, rc = mc.mcCntlGetFRO(inst) -- Get the current FRO
mc.mcCntlSetLastError(inst, string.format("Current Feed Rate Override: %.4f | Percent Change: %.4f", OverrideValue, OverridePercentChange))
OverrideValue = OverrideValue + OverridePercentChange
-- Check if we are out of bounds
if (OverrideValue > GlobalPanelFeedrateOverrideMaxPercent) then
OverrideValue = GlobalPanelFeedrateOverrideMaxPercent
-- We don't want to have to "unwind" the MPG if we keep trying to increase the override value past the left edge
-- So change the "zero point" by however far we moved past the left edge
GlobalMPGHandleOverrideCurrentOffset = GlobalMPGHandleOverrideCurrentOffset + MPGHandleOverrideDelta
elseif (OverrideValue < GlobalPanelFeedrateOverrideMinPercent) then
OverrideValue = GlobalPanelFeedrateOverrideMinPercent
-- We don't want to have to "unwind" the MPG if we keep trying to increase the override value past the right edge
-- So change the "zero point" by however far we moved past the right edge
GlobalMPGHandleOverrideCurrentOffset = GlobalMPGHandleOverrideCurrentOffset + MPGHandleOverrideDelta
end
-- mc.mcCntlSetLastError(inst, string.format("Feed Rate Override: %.4f | Zero Point: %.4f", OverrideValue, GlobalMPGHandleOverrideCurrentOffset))
rc = mc.mcCntlSetFRO(inst, OverrideValue)
elseif (SelectAxis_Spindle == 1) then
OverrideValue, rc = mc.mcSpindleGetOverride(inst) -- Looks like they harmonized the API
-- We have to convert the percent change for spindle from a 0-100 value to a 0 to 1 value BECAUSE OF COURSE WE *********ING DO
-- CAN'T USE THE SAME MECHANISM AS THE FEED API THAT WOULD MAKE TOO MUCH SENSE
OverridePercentChange = OverridePercentChange / 100
mc.mcCntlSetLastError(inst, string.format("Current Spindle Speed Override: %.4f | Percent Change: %.4f", OverrideValue, OverridePercentChange))
OverrideValue = OverrideValue + OverridePercentChange
-- Check if we are out of bounds
if (OverrideValue > GlobalPanelSpindleOverrideMaxPercent) then
OverrideValue = GlobalPanelSpindleOverrideMaxPercent
-- We don't want to have to "unwind" the MPG if we keep trying to increase the override value past the left edge
-- So change the "zero point" by however far we moved past the left edge
GlobalMPGHandleOverrideCurrentOffset = GlobalMPGHandleOverrideCurrentOffset + MPGHandleOverrideDelta
elseif (OverrideValue < GlobalPanelSpindleOverrideMinPercent) then
OverrideValue = GlobalPanelSpindleOverrideMinPercent
-- We don't want to have to "unwind" the MPG if we keep trying to increase the override value past the right edge
-- So change the "zero point" by however far we moved past the right edge
GlobalMPGHandleOverrideCurrentOffset = GlobalMPGHandleOverrideCurrentOffset + MPGHandleOverrideDelta
end
rc = mc.mcSpindleSetOverride(inst, OverrideValue)
else
-- Should never get here!
mc.mcCntlSetLastError(inst, "Mach lost its mind in PLC_PanelHandleOverride - ABORTING")
eStop()
end
else
-- This is a NO-OP
end
Screen Load Script
----------------------------------
-- Global Variables to support using panel MPG as
-- a volume knob to adjust overrides
GlobalMPGHandleOverrideCurrentOffset = 0
GlobalMPGHandleOverrideDeltaOffset = 0
GlobalMPGHandleOverrideCurrentValue = 0
GlobalPanelSpindleOverrideMaxPercent = 2.0
GlobalPanelSpindleOverrideMinPercent = 0.5
GlobalPanelFeedrateOverrideMaxPercent = 150
GlobalPanelFeedrateOverrideMinPercent = 50
GlobalPanelMPGOverrideStepsPerPercent = 20
---------------------------------------------------------------
-- The Panel's Override switch changed.
---------------------------------------------------------------
function PanelHandleModeChange()
-- handles
local hAxis_Spindle
local hAxis_Feed
local SelectAxis_Spindle
local SelectAxis_Feed
-- Jog Active Light handle
local hOverrideLight
hOverrideLight, rc = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT3) -- Override Light Signal 3
local AxisState
AxisState, rc = mc.mcMpgGetAxis(inst, MachMpgNumberForPanel)
hAxis_Spindle, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT2)
SelectAxis_Spindle, rc = mc.mcSignalGetState(hAxis_Spindle)
hAxis_Feed, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT3)
SelectAxis_Feed, rc = mc.mcSignalGetState(hAxis_Feed)
-- If both Spindle Override and Axis Ovveride are set, that's a wiring fault
if ((SelectAxis_Spindle == 1) and (SelectAxis_Feed == 1)) then
mc.mcCntlSetLastError(inst, "Spindle and Feed Overrides active simultaniously! WIRING FAULT")
eStop()
-- If both are off, we are back to jog mode.
-- Turn off the override active light,
-- unmap the axis, and re-scan the jog enable/speed switch
elseif ((SelectAxis_Spindle == 0) and (SelectAxis_Feed == 0)) then
mc.mcSignalSetState(hOverrideLight, 0) -- Turn Override light off
mc.mcMpgSetAxis(inst, MachMpgNumberForPanel, UnmapMPG ) -- Unmap the MPG, so it won't control any axes
mc.mcCntlSetLastError(inst, "Override switch to JOG MODE") -- Show a message in the Screen Set
PanelSpeedChange() -- Scan jog controls and set state
elseif (SelectAxis_Spindle == 1) then
mc.mcSignalSetState(hOverrideLight, 1) -- Turn Override light on
mc.mcCntlSetLastError(inst, "Override switch to SPINDLE OVERRIDE") -- Show a message in the Screen Set
mc.mcMpgSetAxis(inst, MachMpgNumberForPanel, UnmapMPG ) -- Unmap the MPG, so it won't control any axes
-- Grab the current MPG counter and save it in the global variable
-- where the PLC script can see it
hEncoderPosition, rc = mc.mcRegGetHandle(inst,"ESS/Encoders/Encoder_0")
GlobalMPGHandleOverrideCurrentOffset, rc = mc.mcRegGetValue(hEncoderPosition)
GlobalMPGHandleOverrideCurrentValue = 0 -- Reset last known value counter
elseif (SelectAxis_Feed == 1) then
mc.mcSignalSetState(hOverrideLight, 1) -- Turn Override light on
mc.mcCntlSetLastError(inst, "Override switch to FEED OVERRIDE") -- Show a message in the Screen Set
mc.mcMpgSetAxis(inst, MachMpgNumberForPanel, UnmapMPG ) -- Unmap the MPG, so it won't control any axes
-- Grab the current MPG counter and save it in the global variable
-- where the PLC script can see it
hEncoderPosition, rc = mc.mcRegGetHandle(inst,"ESS/Encoders/Encoder_0")
GlobalMPGHandleOverrideCurrentOffset, rc = mc.mcRegGetValue(hEncoderPosition)
GlobalMPGHandleOverrideCurrentValue = 0 -- Reset last known value counter
end
end