Single-Axis Motion complete code

This page contains the complete code of Single-Axis Motion.

In SingleAxisMotion.cpp, your Single-Axis Motion code should be as follows:

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

VOID GetAnAxisState(int& Index);
VOID GetAnAxisPosition(int& Index);
VOID GetAnAxisVelocity(int& Index);

BOOL EnableAxis(int Index)
{
    //Reset an alarm.
    KsCommandStatus status = WaitForCommand(5, TRUE, ResetAxis(Index));
    if (status.Error)
    {
        RtPrintf("ResetAxis failed: %d\n", status.ErrorId);
        return FALSE;
    }

    //Enable an axis. You can limit the direction the axis moves.
    status = WaitForCommand(5, TRUE, PowerAxis(Index, TRUE, TRUE, TRUE));
    if (status.Error)
    {
        RtPrintf("PowerAxis failed: %d\n", status.ErrorId);
        return FALSE;
    }

    RtPrintf("Enable Axis %d.\n\n", Index);

    return TRUE;
}

BOOL DisableAxis(int Index)
{
    //Disable an axis.
    KsCommandStatus status = WaitForCommand(5, TRUE, PowerAxis(Index, FALSE, TRUE, TRUE));
    if (status.Error)
    {
        RtPrintf("PowerAxis failed: %d\n", status.ErrorId);
        return FALSE;
    }

    RtPrintf("Disable Axis %d.\n\n", Index);

    return TRUE;
}

VOID MoveAbsolute(int Index, double Target)
{
    RtPrintf("Make an absolute move.\n\n");

    //Start an absolute move.
    KsCommandStatus status = WaitForCommand(30, TRUE, MoveAxisAbsolute(Index, Target, MAXIMUM_VELOCITY, 
        MAXIMUM_ACCELERATION, MAXIMUM_DECELERATION, MAXIMUM_JERK, mcPositiveDirection, mcAborting));
    if (status.Error)
        RtPrintf("MoveAxisAbsolute failed: %d\n", status.ErrorId);

    //Wait a few cycles to get the correct end positions.
    Sleep(5);

    RtPrintf("End position:\n");
    GetAnAxisPosition(Index);
}

VOID MoveContinuousAbsolute(int Index, double Target)
{
    RtPrintf("Make a continuous absolute move.\n\n");

    //Start a continuous absolute move.
    KsCommandStatus status = MoveAxisContinuousAbsolute(Index, Target, MAXIMUM_VELOCITY, END_VELOCITY, 
        MAXIMUM_ACCELERATION, MAXIMUM_DECELERATION, MAXIMUM_JERK, mcPositiveDirection, mcAborting);
    if (status.Error)
        RtPrintf("MoveContinuousAbsolute failed: %d\n", status.ErrorId);

    Sleep(6000);

    RtPrintf("End position:\n");
    GetAnAxisPosition(Index);
}

VOID MoveRelative(int Index, double Distance)
{
    RtPrintf("Make a relative move.\n\n");

    //Start a relative move.
    KsCommandStatus status = WaitForCommand(30, TRUE, MoveAxisRelative(Index, Distance,
        MAXIMUM_VELOCITY, MAXIMUM_ACCELERATION, MAXIMUM_DECELERATION, MAXIMUM_JERK, mcAborting));
    if (status.Error)
        RtPrintf("MoveAxisRelative failed: %d\n", status.ErrorId);

    //Wait a few cycles to get the correct end positions.
    Sleep(5);
    RtPrintf("End position:\n");
    GetAnAxisPosition(Index);
}

VOID MoveContinuousRelative(int Index, double Distance)
{
    RtPrintf("Make a continuous relative move.\n\n");

    //Start a continuous relative move.
    KsCommandStatus status = MoveAxisContinuousRelative(Index, Distance, MAXIMUM_VELOCITY,
        END_VELOCITY, MAXIMUM_ACCELERATION, MAXIMUM_DECELERATION, MAXIMUM_JERK, mcAborting);
    if (status.Error)
        RtPrintf("MoveContinuousRelative failed: %d\n", status.ErrorId);

    Sleep(6000);

    RtPrintf("End position:\n");
    GetAnAxisPosition(Index);
}

VOID MoveAdditive(int Index, double Distance)
{
    RtPrintf("Make an additional move.\n\n");

    //Start an additive move.
    KsCommandStatus status = WaitForCommand(30, FALSE, MoveAxisAdditive(Index, Distance,
        MAXIMUM_VELOCITY, MAXIMUM_ACCELERATION, MAXIMUM_DECELERATION, MAXIMUM_JERK, mcAborting));
    if (status.Error)
        RtPrintf("MoveAxisAdditive failed: %d\n", status.ErrorId);

    //Wait a few cycles to get the correct end positions.
    Sleep(5);
    RtPrintf("End position:\n");
    GetAnAxisPosition(Index);
}

VOID Halt(int Index)
{
    //Give some time for the axis to be halted.
    KsCommandStatus status = WaitForCommand(5, FALSE, HaltAxis(Index, MAXIMUM_DECELERATION,
        MAXIMUM_JERK, mcAborting));
    if (status.Error)
        RtPrintf("HaltAxis failed: %d\n", status.ErrorId);

    RtPrintf("Halt Axis %d.\n\n", Index);
}

VOID Stop(int Index)
{
    //Give some time for the axis to be stopped.
    KsCommandStatus status = WaitForCommand(5, FALSE, StopAxis(Index, MAXIMUM_DECELERATION, MAXIMUM_JERK));
    if (status.Error)
        RtPrintf("StopAxis failed: %d\n", status.ErrorId);

    RtPrintf("Stop Axis %d.\n\n", Index);

    //Get the state of the axis. The axis should be in the Stopping state.
    GetAnAxisState(Index);

    //Release the axis from the Stopping state.
    RtPrintf("Release the axis from the Stopping state.\n\n");

    KsError error = ReleaseAxis(Index);
    if (error != errNoError)
        RtPrintf("ReleaseAxis failed: %x\n", error);

    //Give some time for the axis state to be changed, and get the state again.
    Sleep(5);
    GetAnAxisState(Index);
}

VOID Jog(int Index)
{
    RtPrintf("Make a jog move.\n\n");

    //Start a jog move.
    KsCommandStatus status = JogAxis(Index, MAXIMUM_VELOCITY, MAXIMUM_ACCELERATION,
        MAXIMUM_DECELERATION, MAXIMUM_JERK, mcNegativeDirection);
    if (status.Error)
        RtPrintf("JogAxis failed: %d\n", status.ErrorId);

    //Let the axis moves for a while.
    Sleep(5000);

    GetAnAxisVelocity(Index);

    RtPrintf("End position:\n");
    GetAnAxisPosition(Index);
}

VOID Inch(int Index, double Distance)
{
    RtPrintf("Make an inch move.\n\n");

    //Start an inch move.
    KsCommandStatus status = WaitForCommand(30, TRUE, InchAxis(Index, Distance, MAXIMUM_VELOCITY,
        MAXIMUM_ACCELERATION, MAXIMUM_DECELERATION, MAXIMUM_JERK, mcPositiveDirection));
    if (status.Error)
        RtPrintf("InchAxis failed: %d\n", status.ErrorId);

    //Wait a few cycles to get the correct end positions.
    Sleep(5);

    RtPrintf("End position:\n");
    GetAnAxisPosition(Index);
}

VOID Velocity(int Index)
{
    RtPrintf("Make a velocity move.\n\n");

    //Start a velocity move.
    KsCommandStatus status = MoveAxisVelocity(Index, MAXIMUM_VELOCITY, MAXIMUM_ACCELERATION,
        MAXIMUM_DECELERATION, MAXIMUM_JERK, mcNegativeDirection, mcAborting);
    if (status.Error)
        RtPrintf("MoveAxisVelocity failed: %d\n", status.ErrorId);

    //Let the axis moves for a while.
    Sleep(6000);

    GetAnAxisVelocity(Index);

    RtPrintf("End position:\n");
    GetAnAxisPosition(Index);
}

VOID QueueSample01(int Index, double Distance1, double Distance2)
{
    RtPrintf("Queue sample: send commands one by one.\n\n");

    /*-----First motion command-----*/

    //Start a relative move.
    RtPrintf("Make a relative move.\n");
    KsCommandStatus status = WaitForCommand(30, FALSE, MoveAxisRelative(Index, Distance1,
        MAXIMUM_VELOCITY, MAXIMUM_ACCELERATION, MAXIMUM_DECELERATION, MAXIMUM_JERK, mcAborting));
    if (status.Error)
        RtPrintf("MoveAxisRelative failed: %d\n", status.ErrorId);

    RtPrintf("End position:\n");
    GetAnAxisPosition(Index);

    /*-----Second motion command-----*/

    //Start an additive move.
    RtPrintf("Make an additive move.\n");
    status = WaitForCommand(30, FALSE, MoveAxisAdditive(Index, Distance2, MAXIMUM_VELOCITY, 
        MAXIMUM_ACCELERATION, MAXIMUM_DECELERATION, MAXIMUM_JERK, mcAborting));
    if (status.Error)
        RtPrintf("MoveAxisAdditive failed: %d\n", status.ErrorId);

    //Wait a few cycles to get the correct end positions.
    Sleep(5);

    RtPrintf("End position:\n");
    GetAnAxisPosition(Index);
}

VOID QueueSample02(int Index, double Target, double Distance1, double Distance2)
{
    RtPrintf("Queue sample: send a sequence of commands.\n\n");

    /*-----Send motion commands to a queue with mcBuffered-----*/
    //The first command's BufferMode can be set to any buffer mode.

    //Start an absolute move.
    RtPrintf("Make an absolute move.\n");
    KsCommandStatus move1 = MoveAxisAbsolute(Index, Target, MAXIMUM_VELOCITY, MAXIMUM_ACCELERATION, 
        MAXIMUM_DECELERATION, MAXIMUM_JERK, mcNegativeDirection, mcAborting);
    if (move1.Error)
        RtPrintf("MoveAxisAbsolute failed: %d\n", move1.ErrorId);

    //Start a relative move.
    RtPrintf("Make a relative move.\n");
    KsCommandStatus move2 = MoveAxisRelative(Index, Distance1, MAXIMUM_VELOCITY, 
        MAXIMUM_ACCELERATION, MAXIMUM_DECELERATION, MAXIMUM_JERK, mcBuffered);
    if (move2.Error)
        RtPrintf("MoveAxisRelative failed: %d\n", move2.ErrorId);

    //Start an additive move.
    RtPrintf("Make an additive move.\n\n");
    KsCommandStatus move3 = MoveAxisAdditive(Index, Distance2, MAXIMUM_VELOCITY, 
        MAXIMUM_ACCELERATION, MAXIMUM_DECELERATION, MAXIMUM_JERK, mcBuffered);
    if (move3.Error)
        RtPrintf("MoveAxisAdditive failed: %d\n\n", move3.ErrorId);

    /*-----Give some time for the motion commands to finish their work-----*/

    /*Wait MoveAxisAbsolute to be completed.*/
    move1 = WaitForCommand(30, TRUE, move1);
    if (move1.Error)
        RtPrintf("MoveAxisAbsolute failed: %d\n", move1.ErrorId);

    RtPrintf("End position:\n");
    GetAnAxisPosition(Index);

    /*Wait MoveAxisRelative to be completed.*/
    move2 = WaitForCommand(30, TRUE, move2);
    if (move2.Error)
        RtPrintf("MoveAxisRelative failed: %d\n", move2.ErrorId);

    RtPrintf("End position:\n");
    GetAnAxisPosition(Index);

    /*Wait MoveAxisAdditive to be completed.*/
    move3 = WaitForCommand(30, TRUE, move3);
    if (move3.Error)
        RtPrintf("MoveAxisAdditive failed: %d\n", move3.ErrorId);

    //Wait a few cycles to get the correct end positions.
    Sleep(5);

    RtPrintf("End position:\n");
    GetAnAxisPosition(Index);
}

VOID Blending(int Index, double Distance, double Target)
{
    RtPrintf("Blend velocity.\n\n");

    /*-----First motion command-----*/

    //Start a relative move.
    //The first command's BufferMode can be set to any buffer mode.
    RtPrintf("Make a relative move.\n\n");
    KsCommandStatus status = MoveAxisRelative(Index, Distance, MAXIMUM_VELOCITY,
        MAXIMUM_ACCELERATION, MAXIMUM_DECELERATION, MAXIMUM_JERK, mcAborting);
    if (status.Error)
        RtPrintf("MoveAxisRelative failed: %d\n", status.ErrorId);

    Sleep(3000);

    RtPrintf("End position:\n");
    GetAnAxisPosition(Index);

    /*-----Second motion command-----*/

    //The blending mode decides how the previous and this command's velocity is blended.
    RtPrintf("Make an absolute move.\n\n");
    status = MoveAxisAbsolute(Index, Target, 2000, 20000, 20000, 2000000,
        mcPositiveDirection, mcBlendingLow);
    if (status.Error)
        RtPrintf("MoveAxisAbsolute failed: %d\n", status.ErrorId);

    Sleep(3000);

    RtPrintf("End position:\n");
    GetAnAxisPosition(Index);
}

VOID SetAxisStartingPosition(int Index)
{
    //Set the starting positions.
    double setPosition = 0;

    KsCommandStatus Position = WaitForCommand(5, FALSE, SetAxisPositionOffset(Index,
        setPosition, FALSE, mcImmediately));
    if (Position.Error)
        RtPrintf("SetAxisPositionOffset failed: %d\n\n", Position.ErrorId);

    //Wait a few cycles for the set position to be set.
    Sleep(5);

    GetAnAxisPosition(Index);
}

VOID GetAnAxisState(int& Index)
{
    //Display the state of an axis in string.

    string axisState[10] = { "axisOffline", "axisCommunicationError", "axisMotionError",
        "axisDisabled", "axisLocked", "axisStandStill", "axisHoming", "axisDiscreteMotion",
        "axisContinuousMotion", "axisSynchronizedMotion" };

    AxisState state = axisOffline;

    KsError nRet = GetAxisState(Index, &state);
    if (nRet != errNoError)
        RtPrintf("GetAxisState failed: %x\n", nRet);

    else if (nRet == errNoError)
    {
        for (int i = 0; i < 10; i++)
        {
            if (state == i)
            {
                RtPrintf("The current axis state is %s.\n\n", axisState[i].c_str());
                break;
            }
        }
    }
}

VOID GetAnAxisPosition(int& Index)
{
    //Display the end position of an axis.

    double setPosition = 0;
    double actualPosition = 0;
    KsError nRet = errNoError;

    //Get the set position of the axis.
    nRet = GetAxisPosition(Index, mcSetValue, &setPosition);
    if (nRet != errNoError)
        RtPrintf("Unable to get the set position: %x\n", nRet);

    //Get the actual position of the axis.
    nRet = GetAxisPosition(Index, mcActualValue, &actualPosition);
    if (nRet != errNoError)
        RtPrintf("Unable to get the actual position: %x\n", nRet);

    printf("Set position: %f, Actual position: %f\n\n", setPosition, actualPosition);
}

VOID GetAnAxisVelocity(int& Index)
{
    //Display the velocity of an axis.

    double velocity = 0;
    KsError nRet = errNoError;

    nRet = GetAxisVelocity(Index, mcActualValue, &velocity);
    if (nRet != errNoError)
        RtPrintf("GetAxisVelocity failed: %x\n", nRet);

    printf("Velocity: %f\n\n", velocity);
}