Convert the unit of position

When you get the actual position from an axis, the unit of the position is count (pulse). You may want to use your unit so that you can identify the actual position easily. The axis must be disabled before you convert the unit.

You can convert the unit of position to any unit you want. In this guide, we convert count to degrees.

Code

To convert the unit of position, first declare an instance of the SlaveStatus structure and apply it to GetAxisByIndex. Next, use SetAxisCountsPerUnit to set a conversion ratio. Finally, use EnableAxisUnitConversion to enable the unit conversion.

In AxisConfiguration.cpp, add the following code.

Copy
VOID PositionUnitConversion(int Index)
{
    RtPrintf("Convert the unit to degrees.\n");

    int Resolution = 0;
    KsError nRet = errNoError;
    SlaveStatus Sts = { 0 };

    //Get the information from an axis.
    nRet = GetAxisByIndex(Index, &Sts, &Resolution, NULL, NULL);
    if (nRet != errNoError)
        RtPrintf("GetAxisByIndex failed: %x\n", nRet);

    //Set a conversion ratio.
    nRet = SetAxisCountsPerUnit(Index, Resolution, UNITS_PER_REVOLUTION, FALSE);
    if (nRet != errNoError)
        RtPrintf("SetAxisCountsPerUnit failed: %x\n", nRet);

    //Enable the unit conversion.
    nRet = EnableAxisUnitConversion(Index, TRUE);
    if (nRet != errNoError)
        RtPrintf("EnableAxisUnitConversion failed: %x\n", nRet);

    RtPrintf("\n");
}