Main function

In this section, you'll learn how to apply all the code in this chapter to main.cpp. If everything is done correctly, you should be able to configure your group properly. To help you review what you have learned, we add comments into the code to describe the meaning of the functions.

  1. In main.cpp, under Gear.h, add the following code:
  2. Copy
    #include "GroupConfiguration.h"
  3. In int _tmain, under the code block if (!StartKingstar()), add the following code to get the number of devices on your EtherCAT network.
  4. Copy
        //Create an instance of the SubsystemStatus structure and get the state from it.
        SubsystemStatus Subsystem = { ecatOffline, ecatOffline, 0, 0, 0, {ecatOffline}, {ecatOffline}, {axisOffline} };
        GetStatus(&Subsystem, NULL);

        //Display the details of the EtherCAT network.
        RtPrintf("Number of Devices found: %d\n", Subsystem.SlaveCount);
        RtPrintf("Number of Axes found: %d\n", Subsystem.AxesCount);
        RtPrintf("Number of I/O found: %d\n", Subsystem.IOCount);
        RtPrintf("\n");

  5. Under RtPrintf("\n");, add the following code to configure axis groups.
  6. Copy
        //Display the information of devices.
        PrintDeviceInformation(0);
        PrintDeviceInformation(1);
        PrintDeviceInformation(2);
        PrintDeviceInformation(5);
        PrintDeviceInformation(6);

        //Set motion profiles.
        //You need to set motion profiles, or the minimum following error will be large.
        MotionProfile(0);
        MotionProfile(1);
        MotionProfile(2);
        MotionProfile(5);
        MotionProfile(6);

        //Convert the unit. In this tutorial we convert it to degrees.
        PositionUnitConversion(0);
        PositionUnitConversion(1);
        PositionUnitConversion(2);
        PositionUnitConversion(5);
        PositionUnitConversion(6);

        //PID tuning.
        /*When you use velocity or torque modes, you must tune PID.
          In this tutorial we use modeMasterIntPos, which doesn't require PID,
          but you should offer PID values for your drive to get the best performance.*/
        UpdatePID(0);
        UpdatePID(1);
        UpdatePID(2);
        UpdatePID(5);
        UpdatePID(6);

        //Set all axes' starting positions to zero.
        SetAllAxesStartingPosition(7);

        //Add Axis Zero and One to Group Zero.
        AssignAxis(0, 0, 0);
        AssignAxis(0, 1, 1);
        AssignAxis(0, 2, 2);

        //Add Axis Five and Six to Group One.
        AssignAxis(1, 5, 0);
        AssignAxis(1, 6, 1);

        //Check which axes have been added to which group.
        RtPrintf("Check which axes have been added to which group:\n\n");

        CheckAxisInGroup(0, 0);
        CheckAxisInGroup(0, 1);
        CheckAxisInGroup(0, 2);
        CheckAxisInGroup(1, 0);
        CheckAxisInGroup(1, 1);

        //Set group motion profiles.
        MotionProfileGroup(0);
        MotionProfileGroup(1);

        //Enable the groups.
        GroupEnable(0);
        GroupEnable(1);

        //Get the groups' states.
        GetAGroupState(0);
        GetAGroupState(1);

        //Convert coordinates from ACS to MCS.
        ACSToMCS(0);

        //Convert coordinates from MCS to PCS.
        MCSToPCS(0);

        //Disable Group Zero.
        GroupDisable(0);

        //Remove the axes in Group Zero one by one.
        //Group Zero will be disabled after all axes are removed.
        RemoveAxis(0, 0);
        RemoveAxis(0, 1);

        //Remove all the axes in Group One at once.
        RemoveGroup(1);

        //Get the groups' states.
        GetAGroupState(0);
        GetAGroupState(1);
        
        //Stop the KINGSTAR Subsystem.
        if (!StopKingstar())
        {
            RtPrintf("The KINGSTAR Subsystem is not stopped.\n");
            return -1;
        }

        else
            RtPrintf("The KINGSTAR Subsystem is stopped.\n");
      
        return 0;
    }

Complete code

In conclusion, in main.cpp, your code should be as follows:

Copy
#include "RT_Project_01.h"
#include "SystemInitialization.h"
#include "IOModule.h"
#include "AxisConfiguration.h"
#include "SingleAxisMotion.h"
#include "Homing.h"
#include "Cam.h"
#include "Gear.h"
#include "GroupConfiguration.h"
#include "GroupMotion.h"

int _tmain(int argc, _TCHAR * argv[])
{
    //Start the KINGSTAR Subsystem.
    if (!StartKingstar())
    {
        RtPrintf("The KINGSTAR Subsystem is not started.\n");
        return -1;
    }

    else
        RtPrintf("The KINGSTAR Subsystem is started.\n");

    RtPrintf("\n");

    //Create an instance of the SubsystemStatus structure and get the state from it.
    SubsystemStatus Subsystem = { ecatOffline, ecatOffline, 0, 0, 0, {ecatOffline}, {ecatOffline}, {axisOffline} };
    GetStatus(&Subsystem, NULL);

    //Display the details of the EtherCAT network.
    RtPrintf("Number of Devices found: %d\n", Subsystem.SlaveCount);
    RtPrintf("Number of Axes found: %d\n", Subsystem.AxesCount);
    RtPrintf("Number of I/O found: %d\n", Subsystem.IOCount);
    RtPrintf("\n");

    //Display the information of devices.
    PrintDeviceInformation(0);
    PrintDeviceInformation(1);
    PrintDeviceInformation(2);
    PrintDeviceInformation(5);
    PrintDeviceInformation(6);

    //Set motion profiles.
    //You need to set motion profiles, or the minimum following error will be large.
    MotionProfile(0);
    MotionProfile(1);
    MotionProfile(2);
    MotionProfile(5);
    MotionProfile(6);

    //Convert the unit. In this tutorial we convert it to degrees.
    PositionUnitConversion(0);
    PositionUnitConversion(1);
    PositionUnitConversion(2);
    PositionUnitConversion(5);
    PositionUnitConversion(6);

    //PID tuning.
    /*When you use velocity or torque modes, you must tune PID.
      In this tutorial we use modeMasterIntPos, which doesn't require PID,
      but you should offer PID values for your drive to get the best performance.*/
    UpdatePID(0);
    UpdatePID(1);
    UpdatePID(2);
    UpdatePID(5);
    UpdatePID(6);

    //Set all axes' starting positions to zero.
    SetAllAxesStartingPosition(7);

    //Add Axis Zero and One to Group Zero.
    AssignAxis(0, 0, 0);
    AssignAxis(0, 1, 1);
    AssignAxis(0, 2, 2);

    //Add Axis Five and Six to Group One.
    AssignAxis(1, 5, 0);
    AssignAxis(1, 6, 1);

    //Check which axes have been added to which group.
    RtPrintf("Check which axes have been added to which group:\n\n");

    CheckAxisInGroup(0, 0);
    CheckAxisInGroup(0, 1);
    CheckAxisInGroup(0, 2);
    CheckAxisInGroup(1, 0);
    CheckAxisInGroup(1, 1);

    //Set group motion profiles.
    MotionProfileGroup(0);
    MotionProfileGroup(1);

    //Enable the groups.
    GroupEnable(0);
    GroupEnable(1);

    //Get the groups' states.
    GetAGroupState(0);
    GetAGroupState(1);

    //Convert coordinates from ACS to MCS.
    ACSToMCS(0);

    //Convert coordinates from MCS to PCS.
    MCSToPCS(0);

    //Disable Group Zero.
    GroupDisable(0);

    //Remove the axes in Group Zero one by one.
    //Group Zero will be disabled after all axes are removed.
    RemoveAxis(0, 0);
    RemoveAxis(0, 1);

    //Remove all the axes in Group One at once.
    RemoveGroup(1);

    //Get the groups' states.
    GetAGroupState(0);
    GetAGroupState(1);
    
    //Stop the KINGSTAR Subsystem.
    if (!StopKingstar())
    {
        RtPrintf("The KINGSTAR Subsystem is not stopped.\n");
        return -1;
    }

    else
        RtPrintf("The KINGSTAR Subsystem is stopped.\n");
  
    return 0;
}

NOTE:  To let you easily understand how to move an axis, we leave only necessary code in the main function. You can add more code depending on your needs.

NOTE:  You probably have noticed that several header files are not used because we didn't include their related functions in the main function. Feel free to comment out unused header files. In the tutorial, we preserve all the header files in case you want to use any of the functions you have learned.

 

Output: