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 move 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 "AxisConfiguration.h", add the following code:
  2. Copy
    #include "SingleAxisMotion.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 part of the motion functions because these functions are better to be demonstrated on their own instead of running together with others. To see the output of each motion function, see its help topic.
  6. We display the details of the servo drives so you know we use two different drives for camming. We set the motion profile, convert the unit, and tune the PID. You can use the function in Chapter 4 to do this.

    Copy
        int Axis = 0;

        //Display the information of devices.
        PrintDeviceInformation(Axis);

        //Set a motion profile.
        //You need to set a motion profile, or the minimum following error will be large.
        MotionProfile(Axis);

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

        //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(Axis);

        //Set the starting positions for an axis.
        RtPrintf("Starting positions:\n\n");
        SetAxisStartingPosition(Axis);

        /* Enable an axis. */
        EnableAxis(Axis);
        
        /* Absolute moves */
        MoveAbsolute(Axis, 10000);
        //Sleep(5);
        //RtPrintf("Starting positions:\n\n");
        //SetAxisStartingPosition(Axis);
        //MoveContinuousAbsolute(Axis, 12000);

        /* Relative moves */
        MoveRelative(Axis, 9000);
        //Sleep(5);
        //RtPrintf("Starting positions:\n\n");
        //SetAxisStartingPosition(Axis);
        //MoveContinuousRelative(Axis, 11000);

        /* Additive move */
        MoveAdditive(Axis, -7000);

        /* Test moves */
        //Jog(Axis);
        //Inch(Axis, 8000);
        //Velocity(Axis);

        /* Motion queue samples */
        //QueueSample01(Axis, 10000, -5000);
        //QueueSample02(Axis, -2000, 5000, -4000);

        /* Blending sample */
        //Blending(Axis, 3000, 5000);

        /* Stop an axis */
        //Halt(Axis);
        //Stop(Axis);

        /* Disable an axis. */
        DisableAxis(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"
#include "SingleAxisMotion.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 devices.
    PrintDeviceInformation(Axis);

    //Set a motion profile.
    //You need to set a motion profile, or the minimum following error will be large.
    MotionProfile(Axis);

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

    //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(Axis);

    //Set the starting positions for an axis.
    RtPrintf("Starting positions:\n\n");
    SetAxisStartingPosition(Axis);

    /* Enable an axis. */
    EnableAxis(Axis);
    
    /* Absolute moves */
    MoveAbsolute(Axis, 10000);
    //Sleep(5);
    //RtPrintf("Starting positions:\n\n");
    //SetAxisStartingPosition(Axis);
    //MoveContinuousAbsolute(Axis, 12000);

    /* Relative moves */
    MoveRelative(Axis, 9000);
    //Sleep(5);
    //RtPrintf("Starting positions:\n\n");
    //SetAxisStartingPosition(Axis);
    //MoveContinuousRelative(Axis, 11000);

    /* Additive move */
    MoveAdditive(Axis, -7000);

    /* Test moves */
    //Jog(Axis);
    //Inch(Axis, 8000);
    //Velocity(Axis);

    /* Motion queue samples */
    //QueueSample01(Axis, 10000, -5000);
    //QueueSample02(Axis, -2000, 5000, -4000);

    /* Blending sample */
    //Blending(Axis, 3000, 5000);

    /* Stop an axis */
    //Halt(Axis);
    //Stop(Axis);

    /* Disable an axis. */
    DisableAxis(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 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 IOModule.h is not used because we didn't include any I/O control 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:

MoveAxisAbsolute: Target: 10000

MoveAxisRelative: Distance: 9000

MoveAxisAdditive: Distance: -7000