Homing complete code

This page contains the complete code of Homing.

In Homing.cpp, your Home code should be as follows:

Copy
#include "RT_Project_01.h"
#include "AxisConfiguration.h"
#include "Homing.h"

VOID HomingLatch(int Index)
{
    RtPrintf("Home an axis using homingLatch.\n\n");

    double PosRecord = 0;

    //Configure the probe to use for position latching.
    /*The axis moves in the preset homing direction at the high velocity until the probe is triggered.
      The axis will then move back to the latched position using the low velocity.*/
    //We use the phase Z signal from a servo drive.

    McProbeTrigger ProbeTrigger = { 0 };
    ProbeTrigger.TouchProbeId = 0;   //Use TouchProbe Zero.
    ProbeTrigger.IndexPulse = TRUE;  /*TRUE: Use a Z signal to trigger.
                                       FALSE: Use a DI from a servo drive to trigger.*/
    ProbeTrigger.Edge = TRUE;        //TRUE: Rising edge. FALSE: Falling edge.

    KsCommandStatus probe = SetAxisTouchProbe(Index, ProbeTrigger, FALSE, 0, 0, &PosRecord);
    if (probe.Error)
        RtPrintf("SetAxisTouchProbe failed: %d\n", probe.ErrorId);

    //Start homing.
    KsCommandStatus home = WaitForCommand(30, TRUE, HomeAxis(Index, 0, HOMING_HIGH_VELOCITY, 
       HOMING_LOW_VELOCITY, HOMING_ACCELERATION, HOMING_DECELERATION, MAXIMUM_JERK, 
       HOMING_DIRECTION, homingLatch));

    if (home.Error)
        RtPrintf("homingLatch failed: %d\n", home.ErrorId);

    else if (home.Done)
        RtPrintf("homingLatch is completed.\n\n");
}

VOID HomingSoft(int Index)
{
    RtPrintf("Home an axis using homingSoft.\n\n");
    
    //Configure a homing sensor.
    KsError nRet = SetAxisHomeSwitch(Index, HOME_SENSOR_TYPE, HOME_SENSOR_INDEX,
        HOME_SENSOR_OFFSET, HOME_SENSOR_INVERT);
    if (nRet != errNoError)
        RtPrintf("SetAxisHomeSwitch failed: %x\n", nRet);
    
    //Start homing.
    //An axis moves in the preset homing direction using high velocity.
    /*An axis reverses the direction every time it touches a sensor, because the sensor's signal changes
      whenever the sensor is touched.*/
    /*The velocity is divided by 10 every time the direction is reversed until it finds the sensor
      in the preset direction at the low velocity.*/
    KsCommandStatus home = WaitForCommand(30, TRUE, HomeAxis(Index, 0, HOMING_HIGH_VELOCITY,
        HOMING_LOW_VELOCITY, HOMING_ACCELERATION, HOMING_DECELERATION, MAXIMUM_JERK,
        HOMING_DIRECTION, homingSoft));
    
    if (home.Error)
        RtPrintf("homingSoft failed: %d\n", home.ErrorId);
    
    else if (home.Done)
        RtPrintf("homingSoft is completed.\n\n");
}

VOID HomingSensor(int Index)
{
    RtPrintf("Home an axis using homingSensor.\n\n");
    
    //Configure a homing sensor.
    KsError nRet = SetAxisHomeSwitch(Index, HOME_SENSOR_TYPE, HOME_SENSOR_INDEX,
         HOME_SENSOR_OFFSET, HOME_SENSOR_INVERT);
    if (nRet != errNoError)
        RtPrintf("SetAxisHomeSwitch failed: %x\n", nRet);
    
    //Start homing.
    //An axis moves in the preset homing direction using high velocity.
    /*The moving direction of an axis is determined by a sensor's signal. If the signal is zero,
      it moves forward; if the signal is one, it moves backward.*/
    /*The velocity is divided by 10 every time the direction is reversed until it finds the sensor
        in the preset direction at the low velocity.*/
    KsCommandStatus home = WaitForCommand(30, TRUE, HomeAxis(Index, 0, HOMING_HIGH_VELOCITY,
        HOMING_LOW_VELOCITY, HOMING_ACCELERATION, HOMING_DECELERATION, MAXIMUM_JERK,
        HOMING_DIRECTION, homingSensor));
    
    if (home.Error)
        RtPrintf("homingSensor failed: %d\n", home.ErrorId);
    
    else if (home.Done)
        RtPrintf("homingSensor is completed.\n\n");
}

VOID HomingSlave(int Index)
{
    RtPrintf("Home an axis using homingSlave.\n\n");
    
    KsCommandStatus status = WaitForCommand(5, FALSE, SetAxisParameter(Index, mcSlaveHomingMode,
        35, mcImmediately));
    if (status.Error)
        RtPrintf("SetAxisParameter failed: %d\n", status.ErrorId);
    
    KsCommandStatus home = WaitForCommand(30, TRUE, HomeAxis(Index, 0, HOMING_HIGH_VELOCITY,
        HOMING_LOW_VELOCITY, HOMING_ACCELERATION, HOMING_DECELERATION, MAXIMUM_JERK,
        HOMING_DIRECTION, homingSlave));
    
    if (home.Error)
        RtPrintf("homingSlave failed: %d\n", home.ErrorId);
    
    else if (home.Done)
        RtPrintf("homingSlave is completed.\n\n");
}

VOID HomingOnPosition(int Index)
{
    RtPrintf("Home an axis using homingOnPosition.\n\n");

    KsCommandStatus home = WaitForCommand(30, TRUE, HomeAxis(Index, 0, HOMING_HIGH_VELOCITY,
        HOMING_LOW_VELOCITY, HOMING_ACCELERATION, HOMING_DECELERATION, MAXIMUM_JERK,
        HOMING_DIRECTION, homingOnPosition));

    if (home.Error)
        RtPrintf("homingOnPosition failed: %d\n", home.ErrorId);

    else if (home.Done)
        RtPrintf("homingOnPosition is completed.\n\n");
}