Overtravel

If an axis moves too far, an unexpected error may occur. To prevent overtravel, you can limit the distance the axis travels using a limit switch. In KINGSTAR, you have two ways to set a limit switch: a software limit switch or a limit sensor. A software limit switch, as its name implies, uses software to set a position to limit the distance the axis can travel. A limit sensor is a sensor used as a limit switch. In this section, you'll learn how to set a software limit switch and a limit sensor.

The process for setting a software limit switch

The following process shows how to set a software limit switch:

Enable a positive or negative software limit switch -> Set a position for the limit switch

Software limit switches are easy to use. Just enable the limit switch and set a position for it.

The process for setting a limit sensor

The following process shows how to set a limit sensor:

Set a positive or negative limit sensor -> Select the type of the limit sensor -> Select the index of an axis or the I/O module that contains the limit sensor -> (optional) Set an offset value to the digital input in the I/O module input buffer -> (optional) Invert the sensor value

First, you need to decide if it's positive or negative sensor you want to set, or both. The limit sensor can be connected to an axis or I/O module's input, so you need to decide which type it is, and select the correct index of an axis or the I/O module that contains the limit sensor. It's optional to set an offset value to the digital input and invert the sensor value. By default, the sensor is considered touched as it is on. If it is inverted, it is considered touched as it is off.

Software limit switches

To set software limit switches, use SetAxisParameter to write each value one by one.

Software limit parameters

The uses of positive and negative limit sensors are the same. The difference is that they have individual parameters. You can find these parameters in the McAxisParameter type.

mcSoftLimitPositive: positive software limit switch position. If the commanded value is greater than this, the axis will move forwards and stops when it reaches the software limit switch.

mcSoftLimitNegative: negative software limit switch position. If the commanded value is less than this, the axis will move backwards and stops when it reaches the software limit switch.

mcEnableLimitPositive: enables positive software limit switch.

mcEnableLimitNegative: enables negative software limit switch.

Code

In AxisConfiguration.cpp, add the following code:

Copy
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");
}

Limit sensors

To set limit sensors, you can either use SetAxisParameter to write each value one by one, or use SetAxisPositiveLimitSwitch and SetAxisNegativeLimitSwitch. We suggest you use LimitSwitch functions because they set limit sensors quickly.

Limit sensors parameters

The uses of positive and negative limit sensors are the same. The difference is that they have individual parameters. You can find these parameters in the McAxisParameter type.

mcPositiveLimitSwitchModuleType: configures the maximum limit sensor. Set to TRUE if the sensor is connected to an axis input; set to FALSE if it is connected to an I/O module.

mcPositiveLimitSwitchModuleIndex: configures the maximum limit sensor. It identifies which axis or I/O module contains the sensor. Aliases affect this parameter.

mcPositiveLimitSwitchOffset: configures the maximum limit sensor. It's the offset of the digital input in the I/O module input buffer.

mcPositiveLimitSwitchInvert: configures the maximum limit sensor and inverts the sensor value.

mcNegativeLimitSwitchModuleType: configures the minimum limit sensor. Set to TRUE if the sensor is connected to an axis input; set to FALSE if it is connected to an I/O module.

mcNegativeLimitSwitchModuleIndex: configures the minimum limit sensor. It identifies which axis or I/O module contains the sensor. Aliases affect this parameter.

mcNegativeLimitSwitchOffset: configures the minimum limit sensor. It's the offset of the digital input in the I/O module input buffer.

mcNegativeLimitSwitchInvert: configures the minimum limit sensor. It determines whether to invert the sensor value.

mcEnablePositiveLimitSwitch: enables or disables the maximum limit sensor.

mcEnableNegativeLimitSwitch: enables or disables the minimum limit sensor.

Code

The following code shows how to use SetAxisPositiveLimitSwitch and SetAxisNegativeLimitSwitch.

In AxisConfiguration.cpp, add the following code:

Copy
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");
}