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 work with user variables and record them. 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 DigitalCamSwitch.h, add the following code.
  2. Copy
    #include "Log.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 work with user variables and record them. We comment out most of the functions because they are better to be demonstrated on their own instead of running together with others. To see the output of each function, see its help topic.
  6. Copy
        //Work with user variables.
        CreateUserVariables();

        //Record the change of user variables.
        //RecordData();

        //Display user variables' values in Analysis Console.
        //DebugMessages();

        //Use user variables between two projects.
        //LogProject();

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"
#include "TouchProbe.h"
#include "DigitalCamSwitch.h"
#include "Log.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");

    //Work with user variables.
    CreateUserVariables();

    //Record the change of user variables.
    //RecordData();

    //Display user variables' values in Analysis Console.
    //DebugMessages();

    //Use user variables between two projects.
    //LogProject();
    
    //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: