Send a sequence

"Send a sequence" send all motion commands to a motion queue at once, give them some time to run, and read their states.

We are taking you through the steps and give you the complete code at the end.

Send motion commands

We send a sequence of motion commands. In this sample we send three commands: MoveAxisAbsolute, MoveAxisRelative, and MoveAxisAdditive. We declare three KsCommandStatus instances, move1, move2, and move3, to save their returned structure, and then we use WaitForCommand to wait each command to finish their moves.

Notice that we use mcBuffered for BufferMode — when you send a sequence of motion commands, you must set them (except the first one) to mcBuffered so that the next command will be run after the previous is done. For the first motion command, it doesn't matter if you set it to mcAborting, mcBuffered, or mcCancel, because there is no preceding command, but others have to use mcBuffered.

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

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

Wait the motion commands and display the end positions

Since each command has its own KsCommandStatus instance, we can apply these instances to WaitForCommand to give each command certain time to run and read their states. After the specified time has elapsed, we check whether the command is done and has any error. Each time a command is done, we display the position of an axis to make sure that the axis has traveled the distance we specified. We use the function GetAnAxisPosition to call GetAxisPosition and print the end positions out. For more information about GetAnAxisPosition, see Get and set values.

In SingleAxisMotion.cpp, in the code block QueueSample02, under RtPrintf("MoveAxisAdditive failed: %d\n\n", move3.ErrorId);, add the following code:

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

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

 

Output:

MoveAxisAbsolute Target: -2000

MoveAxisRelative Distance: 5000

MoveAxisAdditive Distance: -4000

You probably wonder why MoveAxisRelative moves the axis to the position 5000, not 3000, because the distance of MoveAxisRelative is determined by the time you call it. When we call MoveAxisRelative, the axis stops at the position zero, so it moves the axis to the position 5000.