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 axis 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 #include "IOModule.h", add the following code:
  2. Copy
    #include "AxisConfiguration.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 an axis. We comment out the LimitSensor function because its use is similar to SoftLimitSwitch.
  6. Copy
        int Axis = 0;

        //Display the information of a device.
        PrintDeviceInformation(Axis);

        //Display the information of an axis.
        PrintAxisInformation(Axis);

        //Display the motion states of an axis.
        PrintMotionStates(Axis);

        //Get the available control modes from an axis. 
        GetAvailableControlMode(Axis);

        //Set a control mode for an axis.
        SetControlMode(Axis);

        //Create a motion profile for an axis.
        MotionProfile(Axis);

        //Convert the unit. In this tutorial we convert it to degrees.
        PositionUnitConversion(Axis);

        //Set a soft limit switch for an axis.
        SoftLimitSwitch(Axis);

        //Set a limit sensor for an axis.
        //We comment out this function because we have used SoftLimitSwitch.
        //LimitSensor(Axis);

        //Limit the direction of an axis.
        LimitDirection(Axis);

        //PID tuning.
        UpdatePID(Axis);

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"

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

    int Axis = 0;

    //Display the information of a device.
    PrintDeviceInformation(Axis);

    //Display the information of an axis.
    PrintAxisInformation(Axis);

    //Display the motion states of an axis.
    PrintMotionStates(Axis);

    //Get the available control modes from an axis. 
    GetAvailableControlMode(Axis);

    //Set a control mode for an axis.
    SetControlMode(Axis);

    //Create a motion profile for an axis.
    MotionProfile(Axis);

    //Convert the unit. In this tutorial we convert it to degrees.
    PositionUnitConversion(Axis);

    //Set a soft limit switch for an axis.
    SoftLimitSwitch(Axis);

    //Set a limit sensor for an axis.
    //We comment out this function because we have used SoftLimitSwitch.
    //LimitSensor(Axis);

    //Limit the direction of an axis.
    LimitDirection(Axis);

    //PID tuning.
    UpdatePID(Axis);
    
    //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 configure axes, we leave only necessary code in the main function. You can add more code depending on your needs.

Output: