Camming complete code

This page contains the complete code of Camming.

In Cam.cpp, your cam code should be as follows:

Copy
#include "RT_Project_01.h"
#include "Cam.h"

VOID DefineCamTable(int Index)
{
    RtPrintf("Define a cam table.\n\n");

    //Create an instance of the MC_CamTable structure, which defines a cam table.
    McCamTable Table = { 0 };
    Table.MasterAbsolute = MASTER_ABSOLUTE;
    Table.SlaveAbsolute = SLAVE_ABSOLUTE;
    Table.Periodic = PERIODIC;
    Table.InterpolationType = INTERPOLATION_TYPE;
    Table.Length = TABLE_LENGTH;

    //Define the positions of the master and slave axes in the cam table.
    double MasterPositions[TABLE_LENGTH] = MASTER_POSITIONS;
    double SlavePositions[TABLE_LENGTH] = SLAVE_POSITIONS;
    double PointParameters[TABLE_LENGTH] = POINT_PARAMETERS;

    KsCommandStatus camTable = WaitForCommand(5, FALSE, SetCamTable(Index, Table, MasterPositions,
        SlavePositions, PointParameters));
    if (camTable.Error)
        RtPrintf("SetCamTable failed: %d\n\n", camTable.ErrorId);
}

KsCommandStatus AssignCamSlave(int Master, int Slave, int Table)
{
    RtPrintf("Link the master and slave axes for camming.\n\n");

    /*We use camRampTime for the cam mode. The time you set in WaitForCommand
      must be greater than or equal to StartParameter, or the axes won't be in sync.
      If your slave axis is far away from its synchronized position, you need more time to
      sync the two axes.*/
      
    //We wait 5.01 seconds because InSync needs a few more cycles to become TRUE.

    KsCommandStatus cam = WaitForCommand(5.01, FALSE, SetAxisCam(
        Master,   //The index of the master axis.
        Slave,    //The index of the slave axis.
        FALSE,    //Determine whether the cam state is preserved after the motor is disabled.
                  //TRUE: Preserved. FALSE: Not preserved.
        0,        //Master Offset. Offset to add to the master positions in the cam table.
        0,        //Slave Offset. Offset to add to the slave positions in the cam table.
        1,        /*Master Scaling. Ratio to apply to the distance between the master positions
                    and the original position.*/
        1,        //Slave Scaling. Ratio to apply to the slave positions in the cam table.
        camRampTime, //The cam mode.
                     /*Absolute: the slave axis jumps to its target in the table.
                       Alarms may be triggered if the position is too far.*/
                     /*Relative: the current master/slave positions are used as origin for the cam table.
                       The slave axis jumps to its target in the table if it is not in position.*/
                     /*Ramp Distance: the slave axis is moving to its target position.
                       while the master axis moves along the distance provided in StartParameter.*/
                     /*Ramp Time: the slave axis is moving to its target position over
                       the period of time provided in seconds in StartParameter.*/
        5,        //StartParameter.
        mcSetValue,  /*Master Value Source. Value of the master position used.
                       Can be the target position or the actual position.
                       (commanded value and set value are identical)*/
        Table,    //The index of a cam table.
        mcAborting
    ));
    if (cam.Error)
        RtPrintf("SetAxisCam failed: %d\n\n", cam.ErrorId);

    return cam;
}

VOID RemoveCamSlave(int Index)
{
    RtPrintf("Exit camming.\n\n");

    //Remove the cam.
    KsError removeCam = ReleaseAxis(Index);
    if (removeCam != errNoError)
        RtPrintf("The slave axis has not been detached: %x\n\n", removeCam);

    else if (removeCam == errNoError)
        RtPrintf("The slave axis has been detached.\n\n");
}