Group Configuration complete code

This page contains the complete code of Group Configuration.

In GroupConfiguration.cpp, your group configuration code should be as follows:

Copy
#include "RT_Project_01.h"
#include "GroupConfiguration.h"
using namespace std;

VOID AssignAxis(int Group, int Axis, int IndexInGroup)
{
    RtPrintf("Add Axis %d to Group %d.\n\n", Axis, Group);

    //AddAxisToGroup can be used only in the GroupDisabled state.
    /*Each IndexInGroup can be used only once. If you use the same IndexInGroup for two axes,
      an error will occur when you add the third axis to a group.*/
    KsError nRet = AddAxisToGroup(Group, Axis, IndexInGroup);
    if (nRet != errNoError)
        RtPrintf("AddAxisToGroup failed: %x\n\n", nRet);
}

VOID RemoveAxis(int Group, int IndexInGroup)
{
    //Get the original index of an axis in a group.

    int AxisIndex = 0;

    KsError nRet = GetGroupConfiguration(Group, IndexInGroup, mcAxisCoordSystem, &AxisIndex);
    if (nRet != errNoError)
        RtPrintf("GetGroupConfiguration failed: %x\n", nRet);

    //RemoveAxisFromGroup can be used only in the GroupDisabled state.

    nRet = RemoveAxisFromGroup(Group, AxisIndex);
    if (nRet != errNoError)
        RtPrintf("RemoveAxisFromGroup failed: %x\n\n", nRet);

    RtPrintf("Remove Axis %d from Group %d.\n\n", AxisIndex, Group);
}

VOID GroupEnable(int Group)
{
    RtPrintf("Enable Group %d.\n\n", Group);

    KsError nRet = EnableGroup(Group);
    if (nRet != errNoError)
        RtPrintf("EnableGroup failed: %x\n\n", nRet);

    //ResetGroup can be used only after the group is enabled.
    KsCommandStatus resetGroup = WaitForCommand(3, FALSE, ResetGroup(Group));
    if (resetGroup.Error)
        RtPrintf("ResetGroup failed: %d\n\n", resetGroup.ErrorId);
}

VOID GroupDisable(int Group)
{
    RtPrintf("Disable Group %d.\n\n", Group);

    KsError nRet = DisableGroup(Group);
    if (nRet != errNoError)
        RtPrintf("DisableGroup failed: %x\n\n", nRet);
}

VOID RemoveGroup(int Group)
{
    RtPrintf("Remove all axes from Group %d.\n\n", Group);

    KsError nRet = UngroupAllAxes(Group);
    if (nRet != errNoError)
        RtPrintf("UngroupAllAxes failed: %x\n\n", nRet);
}

VOID MotionProfileGroup(int Group)
{
    RtPrintf("Create a motion profile for Group %d.\n\n", Group);

    KsCommandStatus MotionProfile = SetGroupParameter(Group, mcMotionProfileTypeACS,
        MOTION_GROUP_PROFILE_TYPE, mcImmediately);
    if (MotionProfile.Error)
        RtPrintf("SetGroupParameter failed: %d\n\n", MotionProfile.ErrorId);

    KsCommandStatus Velocity = SetGroupParameter(Group, mcMaxVelocityACS,
        MAXIMUM_GROUP_VELOCITY, mcImmediately);
    if (Velocity.Error)
        RtPrintf("SetGroupParameter failed: %d\n\n", Velocity.ErrorId);

    KsCommandStatus Acceleration = SetGroupParameter(Group, mcMaxAccelerationACS,
        MAXIMUM_GROUP_ACCELERATION, mcImmediately);
    if (Acceleration.Error)
        RtPrintf("SetGroupParameter failed: %d\n\n", Acceleration.ErrorId);

    KsCommandStatus Deceleration = SetGroupParameter(Group, mcMaxDecelerationACS,
        MAXIMUM_GROUP_DECELERATION, mcImmediately);
    if (Deceleration.Error)
        RtPrintf("SetGroupParameter failed: %d\n\n", Deceleration.ErrorId);

    KsCommandStatus Jerk = SetGroupParameter(Group, mcMaxJerkACS, MAXIMUM_GROUP_JERK,
        mcImmediately);
    if (Jerk.Error)
        RtPrintf("SetGroupParameter failed: %d\n\n", Jerk.ErrorId);
}

VOID ACSToMCS(int Group)
{
    RtPrintf("Transform the coordinates of Group %d from ACS to MCS.\n\n", Group);

    /* Set the values for ACS-MCS transformation */

    double ACSPositions[3] = { 0, 0, 0 };           //ACS positions.
    double MCSPositions[3] = { 0, 0, 0 };           //MCS positions.
    //double kinParameters[3] = { -20, -40, -60 };    //Values for transformation.
    double kinParameters[3] = { 4, 3, 6 };    //Values for transformation.
    McKinRef Transform = { kinCartesian3D1, kinParameters };

    //Use SetKinTransform to transform coordinates.
    KsCommandStatus SetKin = WaitForCommand(3, FALSE, SetKinTransform(Group, Transform, mcImmediately));
    RtPrintf("SetKinTransform: Done: %d, ErrorID: %d\n", SetKin.Done, SetKin.ErrorId);

    /* Get the values for ACS-MCS transformation */

    /*You need to assign space to the instance of the McKinRef structure first, and then the read data
      can be written into the space you assigned.*/
    McKinRef Transformout;
    double KinParameterBuffer[3] = { 0 };
    Transformout.KinParameters = KinParameterBuffer;

    KsError err = GetKinTransform(Group, &Transformout);
    Sleep(3);
    printf("KinTransform is: KinType: %d, KinParameters: %f, %f, %f\n\n", Transformout.KinType, 
        Transformout.KinParameters[0], Transformout.KinParameters[1], Transformout.KinParameters[2]);

    /* Display the transformed positions */

    /*The transformation process is ACS->MCS->PCS. When we use mcMachineCoordSystem, it affects
      MCS and PCS, but doesn't affect ACS. If you use mcAxisCoordSystem, it affects ACS, MCS, and PCS.*/
    GetGroupPosition(Group, mcAxisCoordSystem, mcSetValue, 3, ACSPositions);
    GetGroupPosition(Group, mcMachineCoordSystem, mcSetValue, 3, MCSPositions);
    printf("ACS Positions: %f, %f, %f\n", ACSPositions[0], ACSPositions[1], ACSPositions[2]);
    printf("MCS Positions: %f, %f, %f\n\n", MCSPositions[0], MCSPositions[1], MCSPositions[2]);
}

VOID MCSToPCS(int Group)
{
    RtPrintf("Transform the coordinates of Group %d from MCS to PCS.\n\n", Group);

    //You must transform the ACS positions into MCS first, and then transform MCS into PCS.

    /* Set the values for MCS-PCS transformation */

    double MCSPositions[3] = { 0, 0, 0 };   //MCS positions.
    double PCSPositions[3] = { 0, 0, 0 };   //PCS positions.
    double Positions[3] = { 0, 0, 0 };    //Set MCS positions to zero.

    KsCommandStatus Position = WaitForCommand(3, FALSE, SetGroupPositionOffset(Group, 3, Positions,
      FALSE, mcMachineCoordSystem, mcImmediately));
    //KsCommandStatus SetCartesian = SetCartesianTransform(Group, 10, 20, 15, -45, 30, 20, mcImmediately);
    KsCommandStatus SetCartesian = SetCartesianTransform(Group, 4, 3, 6, 30, 45, 30, mcImmediately);
    RtPrintf("SetCartesianTransform: Done: %d, ErrorID: %d\n", SetCartesian.Done, SetCartesian.ErrorId);

    /* Get the values for MCS-PCS transformation */

    double Trans[3] = { 0 };                //Points of translation.
    double Rotate[3] = { 0 };               //Angles of rotation.

    KsError err = GetCartesianTransform(Group, &Trans[0], &Trans[1], &Trans[2], &Rotate[0], &Rotate[1], &Rotate[2]);
    Sleep(3);
    printf("CartesianTransform is: TransX: %f, TransY: %f, TransZ: %f, RotateZ: %f, RotateY: %f, RotateX: %f\n\n",
        Trans[0], Trans[1], Trans[2], Rotate[0], Rotate[1], Rotate[2]);

    /* Display the transformed positions */

    /*The transformation process is ACS->MCS->PCS. When we use mcMachineCoordSystem, it affects
      MCS and PCS, but doesn't affect ACS. If you use mcAxisCoordSystem, it affects ACS, MCS, and PCS.*/
    GetGroupPosition(Group, mcMachineCoordSystem, mcSetValue, 3, MCSPositions);
    GetGroupPosition(Group, mcProductCoordSystem, mcSetValue, 3, PCSPositions);
    printf("MCS Positions: %f, %f, %f\n", MCSPositions[0], MCSPositions[1], MCSPositions[2]);
    printf("PCS Positions: %f, %f, %f\n\n", PCSPositions[0], PCSPositions[1], PCSPositions[2]);
}

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

    for (int i = 0; i < totalAxis; i++)
    {
        KsCommandStatus Position = WaitForCommand(5, FALSE, SetAxisPositionOffset(i,
            setPosition, FALSE, mcImmediately));
        if (Position.Error)
            RtPrintf("SetAxisPositionOffset failed: %d\n\n", Position.ErrorId);
    }
}

VOID GetAGroupState(int Group)
{
    //The group state's value starts from 2.
    string groupState[8] = { "NULL", "NULL", "groupErrorStop", "groupDisabled",
        "groupLocked", "groupStandStill", "groupHoming", "groupMoving" };

    GroupState state = groupStandStill;

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

    else if (nRet == errNoError)
    {
        for (int i = 0; i < 8; i++)
        {
            if (state == i)
            {
                RtPrintf("Group %d is %s.\n\n", Group, groupState[i].c_str());
                break;
            }
        }
    }
}

VOID CheckAxisInGroup(int Group, int AxisInGroup)
{
    //Group: the index of a group.
    /*AxisInGroup: the index of an axis in the group. For example, if Axis 3 is the
      first axis in a group, its AxisInGroup is zero.*/
    //AxisIndex: the original index of an axis.

    int AxisIndex = 0;
    KsError nRet = GetGroupConfiguration(Group, AxisInGroup, mcAxisCoordSystem, &AxisIndex);
    if (nRet != errNoError)
        RtPrintf("GetGroupConfiguration failed: %x\n", nRet);

    RtPrintf("Axis %d is in Group %d.\n\n", AxisIndex, Group);
}