Search code examples
plctwincatstructured-text

TwinCAT Motion record travel distance


I need some help writing a function block which I can use to record the travel distance of an axis. This should record every time the axis moves sort of like an odometer, this value will be used for preventative maintenance on the axis. ie greasing the ball screw and linear bearings.

The function has to ignore chatter on the axis when it is not moving and accomodate the homing function which overwrites the position several times.


Solution

  • You can achieve this by integrating absolute value of axis set velocity.

    VAR
        lrCycleTime_s : LREAL;
        lrVelocity_mmPerCycle : LREAL;
        lrDistance_mm : LREAL;
    END_VAR
    
    ======================================
    
    lrCycleTime_s := UDINT_TO_LREAL(_TaskInfo[GETCURTASKINDEXEX()].CycleTime) / 10000000; //Get cycle time in seconds
    lrVelocity_mmPerCycle := Axis.NcToPlc.SetVelo * lrCycleTime_s ; // Convert velocity per second > per cycle
    lrDistance_mm := lrDistance_mm + ABS(lrVelocity_mmPerCycle);
    

    Remember to execute this in a task with cycle time equal to your motion cycle time (default is 2ms)