Send commands one by one

"Send commands one by one" sends a motion command, give it some time to run and read its state, and then send another motion command. We are taking you through the steps and giving you the complete code at the end.

Send a motion command and display the end positions

We send a motion command and use WaitForCommand to let it run for a while. To get the state of the command, we declare an instance of the KsCommandStatus structure and use it to check whether the command is run successfully. To make sure the axis has traveled the specified distance, we use the function GetAnAxisPosition to call GetAxisPosition and display the end positions. For more information about GetAnAxisPosition, see Get and set values.

In SingleAxisMotion.cpp, in the code block QueueSample01, add the following code:

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

Send another motion command

Send another motion command and repeat the previous steps.

In SingleAxisMotion.cpp, in the code block QueueSample01, under GetAnAxisPosition(Index);, add the following code:

Copy
    /*-----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);

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

Complete code

The following code combines all the code above. For more information about the motion commands, see Single-axis movement.

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

 

Output:

MoveAxisRelative Distance: 10000

MoveAxisAdditive Distance: -5000