Safety complete code

This page contains the complete code of Safety.

In AxisConfiguration.cpp, your Safety code should be as follows:

Copy
#include "RT_Project_01.h"
#include "AxisConfiguration.h"
using namespace std;

VOID SoftLimitSwitch(int Index)
{
    RtPrintf("Set software limit switches.\n");

    //Enable positive software limit switch.
    KsCommandStatus status = SetAxisParameter(Index, mcEnableLimitPositive, ENABLE_SOFTLIMIT_POSITIVE, mcImmediately);
    if (status.Error)
        RtPrintf("Failed to set mcEnableLimitPositive: %d\n", status.ErrorId);
    
    //Enable negative software limit switch.
    status = SetAxisParameter(Index, mcEnableLimitNegative, ENABLE_SOFTLIMIT_NEGATIVE, mcImmediately);
    if (status.Error)
        RtPrintf("Failed to set mcEnableLimitNegative: %d\n", status.ErrorId);

    //Set the positive switch's position to 15000.
    status = SetAxisParameter(Index, mcSoftLimitPositive, SOFT_LIMIT_POSITIVE, mcImmediately);
    if (status.Error)
        RtPrintf("Failed to set mcSoftLimitPositive: %d\n", status.ErrorId);

    //Set the negative switch's position to -15000.
    status = SetAxisParameter(Index, mcSoftLimitNegative, SOFT_LIMIT_NEGATIVE, mcImmediately);
    if (status.Error)
        RtPrintf("Failed to set mcSoftLimitNegative: %d\n", status.ErrorId);

    RtPrintf("\n");
}

VOID LimitSensor(int Index)
{
    RtPrintf("Set limit sensors.\n");

    KsError nRet = SetAxisPositiveLimitSwitch(
        Index,              //The index of an axis.
        MAX_SENSOR_TYPE,    //TRUE: axis. FALSE: I/O module.
        MAX_SENSOR_INDEX,   //Which axis or I/O module contains the sensor.
        MAX_SENSOR_OFFSET,  //The offset in bit of the sensor digital input in the module input variables.
        MAX_SENSOR_INVERT,  //Invert the sensor value.
        MAX_SENSOR_ENABLE   //TRUE: enable a positive limit switch. FALSE: disable a positive limit switch.
    );
    if (nRet != errNoError)
        RtPrintf("SetAxisPositiveLimitSwitch failed: %x\n", nRet);

    nRet = SetAxisNegativeLimitSwitch(
        Index,              //The index of an axis.
        MIN_SENSOR_TYPE,    //TRUE: axis. FALSE: I/O module.
        MIN_SENSOR_INDEX,   //Which axis or I/O module contains the sensor.
        MIN_SENSOR_OFFSET,  //The offset in bit of the sensor digital input in the module input variables.
        MIN_SENSOR_INVERT,  //Invert the sensor value.
        MIN_SENSOR_ENABLE   //TRUE: enable a negative limit switch. FALSE: disable a megative limit switch.
    );
    if (nRet != errNoError)
        RtPrintf("SetAxisNegativeLimitSwitch failed: %x\n", nRet);

    RtPrintf("\n");
}

VOID LimitDirection(int Index)
{
    RtPrintf("Limit the direction of an axis.\n");

    //Reset an axis.
    KsCommandStatus status = ResetAxis(Index);
    if (status.Error)
        RtPrintf("ResetAxis failed: %d\n", status.ErrorId);
    
    Sleep(30);

    //Enable an axis and only allow it to move in negative direction.
    PowerAxis(
        Index,  //The index of an axis.
        TRUE,   //TRUE to enable the axis, FALSE to disable the axis.
        FALSE,  //Forbid the axis to move in positive direction.
        TRUE    //Allow the axis to move in negative direction.
    );
    if (status.Error)
        RtPrintf("PowerAxis failed: %d\n", status.ErrorId);

    Sleep(30);

    RtPrintf("\n");
}