Mach Modules Documentation
Lua Module API Documentation
Loading...
Searching...
No Matches
ReciprocatingAxis.lua File Reference

Go to the source code of this file.

Functions

ReciprocatingAxis Create (axis, pos_a, pos_b)
 Create a new ReciprocatingAxis instance for a specific axis and instance.
ReciprocatingAxis CreateAll ()
 Create ReciprocatingAxis objects for all enabled axes in the given instance.
ReciprocatingAxis GetParameter (default_value)
 Retrieve a named OEM parameter value for this axis, with a default fallback.
ReciprocatingAxis IsRunning ()
 Check if the reciprocating axis cycle is currently active.
ReciprocatingAxis IsStopping ()
 Check if the axis is currently running and a stop has been requested.
ReciprocatingAxis SetPosA ()
 Set the target position for PosA (endpoint 0).
ReciprocatingAxis SetPosB ()
 Set the target position for PosB (endpoint 1).
ReciprocatingAxis SetPosADwell ()
 Set the dwell time in milliseconds after arriving at PosA.
ReciprocatingAxis SetPosBDwell ()
 Set the dwell time in milliseconds after arriving at PosB.
ReciprocatingAxis SetPosAFeedrate ()
 Set the feedrate for reciprocating motion toward PosA.
ReciprocatingAxis SetPosBFeedrate ()
 Set the feedrate for reciprocating motion toward PosB.
ReciprocatingAxis SetFeedrate ()
 Set the feedrate for reciprocating motion in both directions.
ReciprocatingAxis SetPositionThreshold ()
 Set the in-position threshold distance used to detect endpoint arrival.
ReciprocatingAxis Start ()
 Start the reciprocating axis cycle, optionally setting a new feedrate.
ReciprocatingAxis StartCannedCycle (all_axes, axis_id, position_a, position_b, dwell, feedrate)
 Configure and start a reciprocating axis canned cycle with a single call.
ReciprocatingAxis StopAxis (all_axes, axis_id, stop_type)
 Stop a specific axis in the collection using a named stop type.
ReciprocatingAxis Stop ()
 Request the axis to stop at the next reciprocating endpoint.
ReciprocatingAxis StopAll (all_axes)
 Stop all jogging reciprocating axes in the collection.
ReciprocatingAxis StopAtA ()
 Request the axis to stop after it next arrives at PosA.
ReciprocatingAxis StopAtB ()
 Request the axis to stop after it next arrives at PosB.
ReciprocatingAxis StopMidCycle ()
 Immediately halt axis motion at the current position without waiting for an endpoint.
ReciprocatingAxis StopMidCycleAll (all_axes)
 Immediately stop all jogging axes in the collection mid-cycle.
ReciprocatingAxis IsDwelling ()
 Check if the axis is currently in a dwell (pause) state.
ReciprocatingAxis IsAtPosA ()
 Check if the axis is currently within the in-position threshold of PosA.
ReciprocatingAxis IsAtPosB ()
 Check if the axis is currently within the in-position threshold of PosB.
ReciprocatingAxis IsMovingTowardsA ()
 Check if the axis was last commanded to move toward PosA.
ReciprocatingAxis IsMovingTowardsB ()
 Check if the axis was last commanded to move toward PosB.
ReciprocatingAxis IsStopRequested ()
 Check if any stop condition has been requested for this axis.
ReciprocatingAxis Process ()
 Main update function for the reciprocating axis; must be called regularly.
ReciprocatingAxis ProcessAll (all_axes)
 Process all reciprocating axes with error protection.
ReciprocatingAxis Dwell ()
 Handle dwell timing at the current endpoint before the next jog stroke.
ReciprocatingAxis ProcessStop ()
 Evaluate stop conditions and either halt or continue the reciprocating cycle.

Function Documentation

◆ Create()

ReciprocatingAxis Create ( axis ,
pos_a ,
pos_b  )

Create a new ReciprocatingAxis instance for a specific axis and instance.

Constructs and initializes a ReciprocatingAxis object with the given axis and instance. Initial positions pos_a and pos_b are stored as pos0 and pos1 respectively. Configuration values are loaded from OEM parameters using GetParameter() with the following defaults if parameters are missing:

  • DefaultPosADwell: 0 ms
  • DefaultPosBDwell: 0 ms
  • DefaultPosAFeedrate: 10
  • DefaultPosBFeedrate: 10
  • InPositionThreshold: 0.0005
  • StopWithPoundNumber: 0 (disabled)
    Parameters
    instance(number) The Mach4 instance handle to use for all motion and API calls
    axis(number) The axis number to reciprocate (e.g., mc.X_AXIS)
    pos_a(number) The machine coordinate for endpoint A (pos0)
    pos_b(number) The machine coordinate for endpoint B (pos1)
    Returns
    (table) A new ReciprocatingAxis object initialized and ready to configure
    Note
    Documentation generated by AI on 2026-03-03

◆ CreateAll()

ReciprocatingAxis CreateAll ( )

Create ReciprocatingAxis objects for all enabled axes in the given instance.

Iterates from mc.X_AXIS to mc.MC_MAX_AXES - 1 and calls mc.mcAxisIsEnabled() for each axis. For every enabled axis, creates a ReciprocatingAxis object via Create() with pos_a and pos_b both initialized to 0. Returns a sparse table indexed by axis number containing only the enabled axes.

Parameters
instance(number) The Mach4 instance handle to query axis enable state and use for motion
Returns
(table) Sparse table of ReciprocatingAxis objects indexed by axis number (mc.X_AXIS to mc.MC_MAX_AXES-1)
Note
Documentation generated by AI on 2026-03-03

◆ Dwell()

ReciprocatingAxis Dwell ( )

Handle dwell timing at the current endpoint before the next jog stroke.

Manages the dwell (pause) period after arriving at an endpoint. On the first call after arrival, records the start time via os.clock(). Subsequent calls check elapsed time against the configured dwell duration (dwell_0 for pos0, dwell_1 for pos1). Returns true when the dwell period has elapsed or no dwell is needed. Dwell is skipped on the initial start (commanded_position_idx == -1) and when a stop has been requested.

Returns
(boolean) True if dwell is complete or not required, false if still dwelling
Note
This is an internal processing function called by Process(). Do not call it directly.
Documentation generated by AI on 2026-03-03

◆ GetParameter()

ReciprocatingAxis GetParameter ( default_value )

Retrieve a named OEM parameter value for this axis, with a default fallback.

Constructs the parameter name by combining the axis number (zero-padded to two digits) with the provided parameter name in the format "ReciprocatingAxis##<param>". Looks up the value via w.GetOEMParamValue(). If the value is nil or non-positive and differs from the default, logs a warning and returns the default value instead.

Parameters
param(string) The parameter name suffix (e.g., "DefaultPosADwell", "InPositionThreshold")
default_value(number) The fallback value to use if the parameter is missing or invalid
Returns
(number) The retrieved OEM parameter value, or default_value if unavailable
Note
Documentation generated by AI on 2026-03-03

◆ IsAtPosA()

ReciprocatingAxis IsAtPosA ( )

Check if the axis is currently within the in-position threshold of PosA.

Returns
(boolean) True if the absolute distance from PosA is within pos_threshold, false otherwise
Note
Documentation generated by AI on 2026-03-03

◆ IsAtPosB()

ReciprocatingAxis IsAtPosB ( )

Check if the axis is currently within the in-position threshold of PosB.

Parameters
(none)
Returns
(boolean) True if the absolute distance from PosB is within pos_threshold, false otherwise
Note
Documentation generated by AI on 2026-03-03

◆ IsDwelling()

ReciprocatingAxis IsDwelling ( )

Check if the axis is currently in a dwell (pause) state.

Returns
(boolean) True if a dwell is in progress (dwell_start timestamp is set), false otherwise
Note
Documentation generated by AI on 2026-03-03

◆ IsMovingTowardsA()

ReciprocatingAxis IsMovingTowardsA ( )

Check if the axis was last commanded to move toward PosA.

Returns
(boolean) True if the last commanded position was PosA (index 0), false otherwise
Note
Documentation generated by AI on 2026-03-03

◆ IsMovingTowardsB()

ReciprocatingAxis IsMovingTowardsB ( )

Check if the axis was last commanded to move toward PosB.

Returns
(boolean) True if the last commanded position was PosB (index 1), false otherwise
Note
Documentation generated by AI on 2026-03-03

◆ IsRunning()

ReciprocatingAxis IsRunning ( )

Check if the reciprocating axis cycle is currently active.

Returns
(boolean) True if the axis is currently reciprocating, false if stopped
Note
Documentation generated by AI on 2026-03-03

◆ IsStopping()

ReciprocatingAxis IsStopping ( )

Check if the axis is currently running and a stop has been requested.

Returns true only when the axis is both actively reciprocating (IsRunning() is true) and at least one stop condition has been flagged (IsStopRequested() is true). This indicates the axis is in the process of completing its final stroke before halting.

Returns
(boolean) True if the axis is running and a stop has been requested, false otherwise
Note
Documentation generated by AI on 2026-03-03

◆ IsStopRequested()

ReciprocatingAxis IsStopRequested ( )

Check if any stop condition has been requested for this axis.

Returns
(boolean) True if a generic stop, stop-at-A, or stop-at-B has been requested
Note
Documentation generated by AI on 2026-03-03

◆ Process()

ReciprocatingAxis Process ( )

Main update function for the reciprocating axis; must be called regularly.

Drives the reciprocating axis state machine. Should be called on every screen update cycle or timer tick. Performs the following steps in order:

  1. Returns immediately if not running
  2. Stops the axis if the machine is disabled (MACH_ENABLED is false)
  3. Checks the configured pound variable (stop_with_pound_number) and stops if it equals 1
  4. If not jogging, calls Dwell() and then either ProcessStop() or ProcessRun() based on state
  5. If jogging, updates the feedrate to account for feedrate override changes
  6. Calls ProcessGMSNotification() to manage the on-screen stop notification
    Warning
    Must be called regularly (e.g., every screen update). Calling infrequently will result in sluggish or incorrect reciprocating behavior.
    Note
    Documentation generated by AI on 2026-03-03

◆ ProcessAll()

ReciprocatingAxis ProcessAll ( all_axes )

Process all reciprocating axes with error protection.

Calls _ProcessAll() inside a w.pcall() protected call. If an error occurs and the machine is still enabled, the error is reported via w.AlarmMessage(). This prevents a single axis error from crashing the calling script.

Parameters
all_axes(table) Table of ReciprocatingAxis objects indexed by axis ID
See also
ReciprocatingAxis._ProcessAll for the unprotected implementation
Note
Documentation generated by AI on 2026-03-03

◆ ProcessStop()

ReciprocatingAxis ProcessStop ( )

Evaluate stop conditions and either halt or continue the reciprocating cycle.

Called when the axis has arrived at an endpoint and a stop has been requested. Determines whether to stop immediately or run one more stroke based on which endpoint was just reached versus which endpoint the stop was requested at:

  • If never started (idx == -1) or a generic stop was requested, stops immediately
  • If just arrived at pos0 and stop_at_0 is set, stops immediately
  • If just arrived at pos1 and stop_at_1 is set, stops immediately
  • Otherwise, runs one more stroke to reach the desired stop endpoint via ProcessRun()
    Note
    This is an internal processing function. Do not call it directly.
    Documentation generated by AI on 2026-03-03

◆ SetFeedrate()

ReciprocatingAxis SetFeedrate ( )

Set the feedrate for reciprocating motion in both directions.

Calls SetPosAFeedrate() and SetPosBFeedrate() with the same rate value, applying it to both the PosA and PosB strokes simultaneously.

Parameters
rate(number) Feedrate in machine units per minute for both directions
See also
ReciprocatingAxis:SetPosAFeedrate() to set direction A feedrate independently
ReciprocatingAxis:SetPosBFeedrate() to set direction B feedrate independently
Note
Documentation generated by AI on 2026-03-03

◆ SetPosA()

ReciprocatingAxis SetPosA ( )

Set the target position for PosA (endpoint 0).

Updates the stored PosA coordinate. The new value takes effect on the next stroke commanded toward PosA; any in-progress stroke is not affected.

Parameters
pos(number) The target machine coordinate for PosA in machine units
See also
ReciprocatingAxis:SetPosB() to set the PosB endpoint
Note
Documentation generated by AI on 2026-03-03

◆ SetPosADwell()

ReciprocatingAxis SetPosADwell ( )

Set the dwell time in milliseconds after arriving at PosA.

Converts dwell_ms to a number via tonumber(). If conversion fails, defaults to 0. The dwell value is clamped to a minimum of 0 via math.max(). The dwell is applied on each arrival at PosA before the axis reverses direction toward PosB.

Parameters
dwell_ms(number) Dwell duration in milliseconds. Negative values are clamped to 0
Note
Documentation generated by AI on 2026-03-03

◆ SetPosAFeedrate()

ReciprocatingAxis SetPosAFeedrate ( )

Set the feedrate for reciprocating motion toward PosA.

Converts the rate to a number via tonumber(). If conversion fails, defaults to 10. This feedrate is applied when the axis is commanded to move toward PosA (index 0).

Parameters
rate(number) Feedrate in machine units per minute for the PosA direction
Note
Documentation generated by AI on 2026-03-03

◆ SetPosB()

ReciprocatingAxis SetPosB ( )

Set the target position for PosB (endpoint 1).

Updates the stored PosB coordinate. The new value takes effect on the next stroke commanded toward PosB; any in-progress stroke is not affected.

Parameters
pos(number) The target machine coordinate for PosB in machine units
See also
ReciprocatingAxis:SetPosA() to set the PosA endpoint
Note
Documentation generated by AI on 2026-03-03

◆ SetPosBDwell()

ReciprocatingAxis SetPosBDwell ( )

Set the dwell time in milliseconds after arriving at PosB.

Converts dwell_ms to a number via tonumber(). If conversion fails, defaults to 0. The dwell value is clamped to a minimum of 0 via math.max(). The dwell is applied on each arrival at PosB before the axis reverses direction toward PosA.

Parameters
dwell_ms(number) Dwell duration in milliseconds. Negative values are clamped to 0
Note
Documentation generated by AI on 2026-03-03

◆ SetPosBFeedrate()

ReciprocatingAxis SetPosBFeedrate ( )

Set the feedrate for reciprocating motion toward PosB.

Converts the rate to a number via tonumber(). If conversion fails, defaults to 10. This feedrate is applied when the axis is commanded to move toward PosB (index 1).

Parameters
rate(number) Feedrate in machine units per minute for the PosB direction
Note
Documentation generated by AI on 2026-03-03

◆ SetPositionThreshold()

ReciprocatingAxis SetPositionThreshold ( )

Set the in-position threshold distance used to detect endpoint arrival.

The threshold is used by IsAtPosA() and IsAtPosB() to determine whether the axis has arrived at an endpoint. A smaller value requires more precise positioning before a dwell or direction reversal is triggered.

Parameters
thresh(number) The in-position threshold in machine units
See also
ReciprocatingAxis:GetInPositionThreshold() to retrieve the current value
Note
Documentation generated by AI on 2026-03-03

◆ Start()

ReciprocatingAxis Start ( )

Start the reciprocating axis cycle, optionally setting a new feedrate.

Validates that both feedrates are positive before starting. If an optional rate is provided, SetFeedrate() is called to apply it to both directions before starting. Resets all stop flags (stop, stop_at_0, stop_at_1) and sets the running flag to true. Raises a Lua error via w.Error() if either feedrate is nil or non-positive.

Parameters
rate(number|nil) Optional feedrate to apply to both directions before starting. If nil, the previously configured feedrates are used
Warning
Raises a Lua error if feedrate0 or feedrate1 is nil or <= 0
Note
Documentation generated by AI on 2026-03-03

◆ StartCannedCycle()

ReciprocatingAxis StartCannedCycle ( all_axes ,
axis_id ,
position_a ,
position_b ,
dwell ,
feedrate  )

Configure and start a reciprocating axis canned cycle with a single call.

Convenience function that sets PosA, PosB, dwell at both endpoints, and feedrate for both directions on the specified axis, then starts the reciprocating cycle. Any parameter that is nil is left at its previously configured value. Dwell is applied equally to both PosA and PosB; feedrate is applied equally to both directions.

Parameters
all_axes(table) Table of ReciprocatingAxis objects indexed by axis ID
axis_id(number) The axis ID key used to look up the axis in all_axes
position_a(number|nil) Target position for PosA, or nil to keep current value
position_b(number|nil) Target position for PosB, or nil to keep current value
dwell(number|nil) Dwell time in milliseconds at each endpoint, or nil to keep current value
feedrate(number|nil) Feedrate for both directions, or nil to keep current value
Note
Documentation generated by AI on 2026-03-03

◆ Stop()

ReciprocatingAxis Stop ( )

Request the axis to stop at the next reciprocating endpoint.

Sets the stop flag, which causes the axis to cease reciprocating after it arrives at whichever endpoint it reaches next. Does not halt motion immediately; the axis will complete its current stroke before stopping.

See also
ReciprocatingAxis:StopAtA() to stop specifically at PosA
ReciprocatingAxis:StopAtB() to stop specifically at PosB
ReciprocatingAxis:StopMidCycle() for immediate mid-stroke stopping
Note
Documentation generated by AI on 2026-03-03

◆ StopAll()

ReciprocatingAxis StopAll ( all_axes )

Stop all jogging reciprocating axes in the collection.

Iterates over all axes in the provided table and calls Stop() on each one that is currently jogging. Axes that are not jogging are left unaffected. This requests a stop at the next endpoint rather than halting motion immediately mid-stroke.

Parameters
all_axes(table) Table of ReciprocatingAxis objects indexed by axis ID
See also
ReciprocatingAxis.StopMidCycleAll for immediate mid-stroke stopping
Note
Documentation generated by AI on 2026-03-03

◆ StopAtA()

ReciprocatingAxis StopAtA ( )

Request the axis to stop after it next arrives at PosA.

Sets the stop_at_0 flag. The axis will complete its current stroke and, once it arrives at PosA (within pos_threshold), will cease reciprocating. Has no effect if the axis is not currently running.

Note
Documentation generated by AI on 2026-03-03

◆ StopAtB()

ReciprocatingAxis StopAtB ( )

Request the axis to stop after it next arrives at PosB.

Sets the stop_at_1 flag. The axis will complete its current stroke and, once it arrives at PosB (within pos_threshold), will cease reciprocating. Has no effect if the axis is not currently running.

Note
Documentation generated by AI on 2026-03-03

◆ StopAxis()

ReciprocatingAxis StopAxis ( all_axes ,
axis_id ,
stop_type  )

Stop a specific axis in the collection using a named stop type.

Dispatches a stop command to the specified axis based on the numeric stop_type:

  • 0: StopMidCycle() - halt immediately at current position
  • 1: StopAtA() - stop after next arrival at PosA
  • 2: StopAtB() - stop after next arrival at PosB
  • 3: Stop() - stop at whichever endpoint is reached next
    Parameters
    all_axes(table) Table of ReciprocatingAxis objects indexed by axis ID
    axis_id(number) The axis ID key used to look up the axis in all_axes
    stop_type(number) Numeric stop type (0=mid-cycle, 1=at A, 2=at B, 3=any end)
    Note
    Documentation generated by AI on 2026-03-03

◆ StopMidCycle()

ReciprocatingAxis StopMidCycle ( )

Immediately halt axis motion at the current position without waiting for an endpoint.

Requests a stop via Stop(), then checks if the axis is still jogging. If jogging and not already decelerating (mcJogIsStopping != 1), issues mcJogVelocityStop to halt motion immediately. Updates commanded_position to the current machine position so the state machine does not attempt to resume the interrupted stroke.

Warning
This halts motion mid-stroke and does not wait for an endpoint. The axis position after stopping may be anywhere between PosA and PosB.
Note
Documentation generated by AI on 2026-03-03

◆ StopMidCycleAll()

ReciprocatingAxis StopMidCycleAll ( all_axes )

Immediately stop all jogging axes in the collection mid-cycle.

Iterates over all axes in the provided table and calls StopMidCycle() on each one that is currently jogging. Axes that are not jogging are left unaffected.

Parameters
all_axes(table) Table of ReciprocatingAxis objects indexed by axis ID
Note
Documentation generated by AI on 2026-03-03