Motion states

KINGSTAR provides a series of motion states that describes different behavior of an axis. When an axis is moved or stopped by motion commands, it falls into one of the states. For more information about the meaning of each state, see Axis states.

To get the state of an axis, use GetAxisState; to get the motion state and direction of an axis, use GetAxisMotionState. The states we get are values. To make them easy to be identified, we use string arrays to store the name of each state and use c_str to convert the value to its corresponding name.

Since the string data type is used, you must include the string library in RT_Project_01.h (the project header file). Moreover, because string is a standard library of C++, if you need to declare string variables, you need to type either using namespace std; in the beginning of AxisConfiguration.cpp, or declare the variable using the form of std::string variableName.

  1. In RT_Project_01.h, make sure #include <string> is added because we are going to use strings in our code.
  2. Copy
    #include <string>

    NOTE:  The original code may be written as #include <string.h>. Change it as per above.

  3. In AxisConfiguration.cpp, add the following code:
  4. Copy
    #include "RT_Project_01.h"
    #include "AxisConfiguration.h"
    using namespace std;
  5. Under using namespace std;, add the following code:
  6. Copy
    VOID PrintMotionStates(int Index)
    {
        AxisState axisState = axisOffline;
        KsError nRet = errNoError;
        McMotionState motionState = mcHalted;
        McDirection axisDirection = mcPositiveDirection;
        string axisStates[10] = { "axisOffline", "axisCommunicationError", "axisMotionError", "axisDisabled", "axisLocked", "axisStandStill",
        "axisHoming", "axisDiscreteMotion", "axisContinuousMotion", "axisSynchronizedMotion" };
        string motionStates[4] = { "mcConstantVelocity", "mcAccelerating", "mcDecelrating", "mcHalted" };
        string axisDirections[4] = { "mcPositiveDirection", "mcShortestWay", "mcNegativeDirection", "mcCurrentDirection" };

        //Get the state of an axis.
        nRet = GetAxisState(Index, &axisState);
        if (nRet != errNoError)
            RtPrintf("GetAxisState failed: %x\n", nRet);

        //Reset nRet to errNoError if it has received an error.
        nRet = errNoError;

        //Get the motion state of an axis.
        nRet = GetAxisMotionState(Index, &motionState, &axisDirection);
        if (nRet != errNoError)
            RtPrintf("GetAxisState failed: %x\n", nRet);

        /*Use c_str() to convert a string object to a C_style string.
          You can see the states in text instead of seeing them in number.*/
        for (int i = 0; i < 10; i++)
        {
            if (axisState == i)
                RtPrintf("Axis %d state: %s\n", Index, axisStates[i].c_str());

            if (motionState == i)
                RtPrintf("Axis %d motion state: %s\n", Index, motionStates[i].c_str());

            if (axisDirection == i)
                RtPrintf("Axis %d direction: %s\n", Index, axisDirections[i].c_str());
        }

        RtPrintf("\n");
    }