Use touch probe to record a position

You can record a position using the touch probe of KINGSTAR or a servo drive. In this section, you'll learn how to use touch probe to record your axis' position.

Process for using touch probe to record a position

The following process shows how a touch probe is set:

McProbeTrigger (configure the settings of touch probe) -> SetAxisTouchProbe (start recording) -> MoveAxis (move an axis)

If you use a continuous motion command such as MoveAxisVelocity, you need to use HaltAxis to stop the axis.

Functions

SetAxisTouchProbe: records the position of an axis at a trigger event.

MoveAxisVelocity: moves an axis at a specified velocity without a target or distance. The axis keeps moving until it is stopped.

AbortCommand: aborts SetAxisTouchProbe after a position is recorded so you can use touch probe to record the position in the second rotation of the servo motor. This function is not required to use if you don't need to record the position more than once.

HaltAxis: decelerates an axis until the velocity of the axis is zero. While the axis is decelerating, it is in the Discrete Motion state. When the axis' velocity is zero, the axis is in the StandStill state. This function is not required to use if the MoveAxis function you use has a target or distance.

Code

We divide the touch probe code into steps to explain. In the last part, we combine all the code for you to see the completed one.

In TouchProbe.cpp, add the following code:

  1. Declare the variable PosRecord to record a position. Create an instance of the McProbeTrigger structure to configure the settings of touch probe. In this guide, we use hardware touch probe and the Z phase signal to trigger the probe.
  2. Copy
    VOID RecordPosition(int Index)
    {
        RtPrintf("Use TouchProbe to record a position.\n\n");

        double PosRecord = 0;

        McProbeTrigger ProbeTrigger = { 0 };

        ProbeTrigger.TouchProbeId = TOUCH_PROBE_ID;     //Use TouchProbe Zero.
        ProbeTrigger.Software = TOUCH_PROBE_SOFTWARE;   //FALSE, use servo drive's touch probe.
        ProbeTrigger.Edge = TOUCH_PROBE_EDGE;           //TRUE, rising edge.
        ProbeTrigger.IndexPulse = TOUCH_PROBE_INDEXPULSE;   //TRUE, use a Z signal to trigger.
        ProbeTrigger.AxisSwitch = TOUCH_PROBE_AXIS; //FALSE, I/O module.
        ProbeTrigger.Index = TOUCH_PROBE_INDEX;     //Zero, the index of an axis or I/O module.
        ProbeTrigger.Offset = TOUCH_PROBE_OFFSET;   //Zero, the offset for an axis or I/O module.
  3. Send SetAxisTouchProbe with the settings we configured and create the KsCommandStatus instance probe to store the state of SetAxisTouchProbe. This instance will be used in the next step.
  4. Copy
        /*-----Record the position in the first rotation-----*/

        KsCommandStatus probe = SetAxisTouchProbe(Index, ProbeTrigger, FALSE, 0, 0, &PosRecord);
        if (probe.Error)
            RtPrintf("SetAxisTouchProbe failed: %d\n", probe.ErrorId);
  5. Move an axis for a while to wait for the position to be recorded. While the axis is moving, we use WaitForCommand to wait probe to be finished. After some time has elapsed, we detect probe's state. If it is done, display the recorded position.
  6. Copy
        //Start a velocity move.
        KsCommandStatus move = WaitForCommand(10, FALSE, MoveAxisVelocity(Index, 1000, 10000,
            10000, 1000000, mcNegativeDirection, mcAborting));
        if (move.Error)
            RtPrintf("MoveAxisVelocity failed: %d\n", move.ErrorId);

        //Wait for position to be recorded.
        probe = WaitForCommand(30, TRUE, probe);
        
        if (probe.Done)
            printf("Recorded position: %f\n\n", PosRecord);
  7. To record the position in the second rotation of the servo motor, we use AbortCommand to abort probe and reset PosRecord to zero. This part is to demonstrate how to use SetAxisTouchProbe when you want to use it to record positions more than once.
  8. Copy
        /*-----Record the position in the second rotation-----*/
        /*We add this part to demonstrate how to record the position in the second rotation.
          Feel free to remove this code block if you don't need it.*/

        //Abort the touch probe.
        RtPrintf("Abort the TouchProbe command.\n\n");
        AbortCommand(probe);

        RtPrintf("Use TouchProbe to record the position in the second rotation.\n\n");
        PosRecord = 0;
  9. Repeat the code of the first part used to record a position with SetAxisTouchProbe.
  10. Copy
        probe = SetAxisTouchProbe(Index, ProbeTrigger, FALSE, 0, 0, &PosRecord);
        if (probe.Error)
            RtPrintf("SetAxisTouchProbe failed: %d\n", probe.ErrorId);

        RtPrintf("Make a velocity move.\n\n");

        //Start a velocity move.
        move = WaitForCommand(10, FALSE, MoveAxisVelocity(Index, 1000, 10000,
            10000, 1000000, mcNegativeDirection, mcAborting));
        if (move.Error)
            RtPrintf("MoveAxisVelocity failed: %d\n", move.ErrorId);

        //Wait for the position to be recorded.
        probe = WaitForCommand(30, TRUE, probe);

        if (probe.Done)
            printf("Recorded position: %f\n\n", PosRecord);

        /*-----End of touch probe-----*/

  11. After the positions are recorded, use HaltAxis to stop an axis since we use a continuous motion command.
  12. Copy
        //Halt the axis.
        KsCommandStatus halt = WaitForCommand(5, FALSE, HaltAxis(Index, 10000, 1000000,
            mcAborting));
        if (halt.Error)
            RtPrintf("HaltAxis failed: %d\n", halt.ErrorId);
    }

Complete code

The complete code should be as follows:

Copy
#include "RT_Project_01.h"
#include "SingleAxisMotion.h"
#include "TouchProbe.h"

VOID RecordPosition(int Index)
{
    RtPrintf("Use TouchProbe to record a position.\n\n");

    double PosRecord = 0;

    McProbeTrigger ProbeTrigger = { 0 };

    ProbeTrigger.TouchProbeId = TOUCH_PROBE_ID;     //Use TouchProbe Zero.
    ProbeTrigger.Software = TOUCH_PROBE_SOFTWARE;   //FALSE, use servo drive's touch probe.
    ProbeTrigger.Edge = TOUCH_PROBE_EDGE;           //TRUE, rising edge.
    ProbeTrigger.IndexPulse = TOUCH_PROBE_INDEXPULSE;   //TRUE, use a Z signal to trigger.
    ProbeTrigger.AxisSwitch = TOUCH_PROBE_AXIS; //FALSE, I/O module.
    ProbeTrigger.Index = TOUCH_PROBE_INDEX;     //Zero, the index of an axis or I/O module.
    ProbeTrigger.Offset = TOUCH_PROBE_OFFSET;   //Zero, the offset for an axis or I/O module.

    /*-----Record the position in the first rotation-----*/

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

    RtPrintf("Make a velocity move.\n\n");

    //Start a velocity move.
    KsCommandStatus move = WaitForCommand(10, FALSE, MoveAxisVelocity(Index, 1000, 10000,
        10000, 1000000, mcNegativeDirection, mcAborting));
    if (move.Error)
        RtPrintf("MoveAxisVelocity failed: %d\n", move.ErrorId);

    //Wait for the position to be recorded.
    probe = WaitForCommand(30, TRUE, probe);

    if (probe.Done)
        printf("Recorded position: %f\n\n", PosRecord);

    /*-----Record the position in the second rotation-----*/
    /*We add this part to demonstrate how to record the position in the second rotation.
      Feel free to remove this code block if you don't need it.*/

    //Abort the touch probe.
    RtPrintf("Abort the TouchProbe command.\n\n");
    AbortCommand(probe);

    RtPrintf("Use TouchProbe to record the position in the second rotation.\n\n");
    PosRecord = 0;

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

    RtPrintf("Make a velocity move.\n\n");

    //Start a velocity move.
    move = WaitForCommand(10, FALSE, MoveAxisVelocity(Index, 1000, 10000,
        10000, 1000000, mcNegativeDirection, mcAborting));
    if (move.Error)
        RtPrintf("MoveAxisVelocity failed: %d\n", move.ErrorId);

    //Wait for the position to be recorded.
    probe = WaitForCommand(30, TRUE, probe);

    if (probe.Done)
        printf("Recorded position: %f\n\n", PosRecord);

    /*-----End of touch probe-----*/

    //Halt the axis.
    KsCommandStatus halt = WaitForCommand(5, FALSE, HaltAxis(Index, 10000, 1000000,
        mcAborting));
    if (halt.Error)
        RtPrintf("HaltAxis failed: %d\n", halt.ErrorId);
}