Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

5 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CODESYS single-axis motion control

Vibe coded CODESYS softmotion wrapper.

This is a fairly common thing in complex machines where you don't want to deal with the MC FBs directly. It provides a top level AxisData DUT that allows a clear view into the actual and desired state of the axis that is easily serializable for use in HMIs or SCADA systems. In addition the AxisCtrl FB provides a stripped down set of commonly used motion controls with a internal state machine that ensures ordered start up of the axis.

Usage

Select the application in the device tree, then import AxisControl.export with Project → Import in CODESYS 3.5. Select the DUTs and function block. Enable SoftMotion on the controller so the SM3_Basic library and its dependencies are available. The wrapper accepts a configured SoftMotion axis of type AXIS_REF_SM3. CODESYS import instructions · Enable SoftMotion

Create one AxisData and one AxisCtrl for each axis. Keep both non-retained and call the FB once per scan from the axis's motion task, including while stopped or faulted. Call Bind(Axis := ..., Data := ...) before issuing commands. The first call binds the pair. Calling Bind again with the same pair returns TRUE and preserves the controller state. A different axis or DUT returns FALSE and leaves the original binding in use. Before binding, command methods and the cyclic call return immediately. Route commands through a single owner in that task and give the FB exclusive control of the axis's motion and power blocks.

Example

// Declaration
VAR
    axisCtrlX       : AxisCtrl;
    startEdge       : R_TRIG;
    moveEdge        : R_TRIG;
    resetEdge       : R_TRIG;
    axisDataX       : AxisData := (
        MotionDynamics := (
            Acceleration := 100.0, Deceleration := 100.0, Jerk := 1000.0,
            StopDeceleration := 100.0, StopJerk := 1000.0
        )
    );
END_VAR
IF NOT axisCtrlX.Bind(Axis := AxisX, Data := axisDataX) THEN
    axisCtrlX(); // Keep servicing the original pair if the binding is rejected.
    RETURN;
END_IF;

// Sample the pushbuttons every scan, including while stop or disable has priority.
startEdge(CLK := startPressed);
moveEdge(CLK := movePressed);
resetEdge(CLK := resetPressed);

IF disablePressed THEN
    axisCtrlX.Disable();
ELSIF stopPressed THEN
    axisCtrlX.Stop();
ELSIF resetEdge.Q THEN
    axisCtrlX.ResetFault();
ELSIF startEdge.Q THEN
    axisCtrlX.Start(Speed := 25.0);
ELSIF moveEdge.Q THEN
    axisCtrlX.MoveAbsolute(Position := 50.0, Speed := 25.0);
ELSE
    axisCtrlX.Jog(Forward := jogForward,
                Reverse := jogReverse, Speed := 5.0);
END_IF;

axisCtrlX();
// Read axisDataX.Actual after the call.

AxisX is the configured drive. Declare the application's command inputs and use axis-specific speed, acceleration, deceleration and jerk values. Gate the jog inputs with the application's jog permission and release logic.

Each press of Start, Move or Reset generates one request. A press made while Stop or Disable has priority is discarded. The library itself DOES NOT provide interlocks.

Single-axis machine demo

Here's a more involved demo to give a better idea of how the API works.

Assume a finite SoftMotion axis named AxisX with a configured homing routine that references the axis at 0.0. For this demo, the four Boolean inputs can be set manually. runPressed and stopPressed are pushbuttons, photoeyeBlocked is true when the beam is blocked, and safetyAsserted is true when the E-stop or safety relay trips. That last input is status feedback from the independent safety system.

Press Run to start cycling. The axis homes if needed and moves to 0.0. Each cycle rapids to 200.0, feeds toward 250.0 or until the photoeye is tripped, rapids back to 200.0, then returns to 0.0. The cycle repeats automatically each time the axis returns to 0.0. Press Stop to finish the current cycle and remain idle at 0.0.

A safety assertion or axis fault cancels the cycle and clears the non-retained homed flag. Once the safety system permits operation and the wrapper reports a latched fault, a fresh Stop press requests fault acknowledgement.

Create this DUT and call MachineDemo once per scan in the axis motion task.

{attribute 'qualified_only'}
{attribute 'strict'}
TYPE FeederStep :
(
    Idle,
    Prepare,
    Homing,
    Start,
    RapidIn,
    Feed,
    RapidOut,
    RepeatCycle
);
END_TYPE
PROGRAM MachineDemo
VAR CONSTANT
    rapidSpeed      : LREAL := 250.0; // Axis units/s
    feedSpeed       : LREAL := 10.0;
END_VAR
VAR
    runPressed      : BOOL;
    stopPressed     : BOOL;
    photoeyeBlocked : BOOL;
    safetyAsserted  : BOOL := TRUE;

    runWasReleased  : BOOL;
    stopEdge        : R_TRIG;
    step            : FeederStep := FeederStep.Idle;
    stopCycle       : BOOL;
    homed           : BOOL;
    moveDone        : BOOL;
    feeder       : AxisCtrl;
    feederData       : AxisData := (
        MotionDynamics := (
            Acceleration := 1000.0, Deceleration := 1000.0, Jerk := 1000.0,
            StopDeceleration := 100.0, StopJerk := 1000.0
        )
    );
END_VAR
// Associate the axis data with the controller (only needs to be called once)
IF NOT feeder.Bind(Axis := AxisX, Data := feederData) THEN
    feeder();
    RETURN;
END_IF;

// Read the feedback published on the previous scan.
stopEdge(CLK := stopPressed);
moveDone := feederData.Actual.Result = AxisResult.Done AND
             feederData.Actual.LastCommandId = feederData.Desired.CommandId;

IF safetyAsserted THEN
    feeder.Disable();
    homed := FALSE;
    step := FeederStep.Idle;
ELSIF feederData.Actual.Fault <> AxisFault.None THEN
    homed := FALSE;
    step := FeederStep.Idle;
    IF stopEdge.Q AND feederData.Actual.State = AxisState.Faulted THEN
        feeder.ResetFault();
    ELSE
        feeder.Stop();
    END_IF;
ELSIF stopEdge.Q THEN
    stopCycle := TRUE;
ELSE
    CASE step OF
        FeederStep.Idle:
            stopCycle := FALSE;
            feeder.Enable();
            IF runPressed AND runWasReleased THEN
                step := FeederStep.Prepare;
            END_IF;

        FeederStep.Prepare:
            IF feederData.Actual.Ready THEN
                IF homed THEN
                    feeder.MoveAbsolute(Position := 0.0, Speed := rapidSpeed);
                    step := FeederStep.Start;
                ELSE
                    feeder.Home(ReferencePosition := 0.0);
                    step := FeederStep.Homing;
                END_IF;
            END_IF;

        FeederStep.Homing:
            IF moveDone THEN
                homed := TRUE;
                feeder.MoveAbsolute(Position := 0.0, Speed := rapidSpeed);
                step := FeederStep.Start;
            END_IF;

        FeederStep.Start:
            IF moveDone THEN
                feeder.MoveAbsolute(Position := 200.0, Speed := rapidSpeed);
                step := FeederStep.RapidIn;
            END_IF;

        FeederStep.RapidIn:
            IF moveDone THEN
                feeder.MoveAbsolute(Position := 250.0, Speed := feedSpeed);
                step := FeederStep.Feed;
            END_IF;

        FeederStep.Feed:
            IF photoeyeBlocked OR moveDone THEN
                feeder.MoveAbsolute(Position := 200.0, Speed := rapidSpeed);
                step := FeederStep.RapidOut;
            END_IF;

        FeederStep.RapidOut:
            IF moveDone THEN
                feeder.MoveAbsolute(Position := 0.0, Speed := rapidSpeed);
                step := FeederStep.RepeatCycle;
            END_IF;

        FeederStep.RepeatCycle:
            IF moveDone THEN
                IF stopCycle THEN
                    feeder.Stop();
                    step := FeederStep.Idle;
                ELSE
                    step := FeederStep.Start;
                END_IF;
            END_IF;

    END_CASE;
END_IF;

// Sample Run even while inhibited so a held button cannot restart the cycle.
runWasReleased := NOT runPressed;
feeder();

Commands and feedback

Method Behavior
Enable, Disable Request powered idle or disable the drive. Disabling during motion waits for the controlled stop before dropping the power request.
Start, Stop Run at signed velocity or request a controlled stop with power retained. Motion requests also request power.
Jog Call every scan with a positive speed and held direction inputs. Releasing both inputs or asserting both ends an active jog.
Home, MoveAbsolute, MoveRelative Call once per move, using a rising edge for a held pushbutton. Each call creates a fresh command ID. Homing uses the drive's configured routine. Absolute moves use the shortest path on modulo axes, and relative moves use a signed distance.
ResetFault Acknowledge a latched fault from Faulted. The method requests powered idle. The controller calls MC_Reset when the axis reports ErrorStop and clears wrapper faults once the axis is stationary or disabled and the power block is healthy.

You can write Desired directly using the same contract. Set Mode and its parameters before the cyclic call. For each finite move, supply a new nonzero CommandId. Read Actual.LastCommandId, LastMode, and Result to track the last accepted move. Leave the ID unchanged after completion and the move executes only once. To acknowledge a fault directly, set Mode := AxisMode.Idle and change ResetId on the same scan while Actual.State is Faulted. Reset requests are consumed when sampled.

Changing the active motion mode, changing continuous speed or submitting a new finite command during motion goes through MC_Stop. The controller releases MC_Stop.Execute after Done and waits for standstill before accepting the latest demand. There is no command queue. The controller captures MotionDynamics when it accepts a move, including AxisDynamics.StopDeceleration and AxisDynamics.StopJerk. CODESYS MC_Stop behavior

Actual reports the wrapper state separately from PLCopen axis feedback. Check StatusValid, PositionValid, VelocityValid and DriveErrorValid before using the corresponding values. Fault and ErrorId preserve the first failure, with separate stop and reset error codes. Power is withdrawn if the controlled stop errors or power readiness is lost during motion. After acknowledgement, keep the demand in Idle until Actual.Ready is true, then issue a fresh motion request. The stop and disable commands are for process control. Machine safety functions must be handled by the machine's safety system. CODESYS MC_Reset behavior

The editable declarations and implementations are in src. To regenerate the native export, start CODESYS with --noUI and --runscript pointing to the full path of tools/export_axis_control.py. CODESYS scripting command line

Pass --scriptargs:'--prefix Acme' to generate Acme_AxisControl.export with names such as Acme_AxisCtrl and Acme_AxisData. Import that file and use the prefixed FB and DUT names in your declarations.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages