PositionMarker_CAPI.cpp
This example shows how to use the position marker in continuous mode.
//===========================================================================
// INCLUDES
//===========================================================================
#include "labbCAN_Bus_API.h"
#include "labbCAN_MotionControl_API.h"
//===========================================================================
// STATIC DATA
//===========================================================================
static double CapturedPosArray[10];
//===========================================================================
// DATA TYPES
//===========================================================================
enum eAxisId
{
AXIS_X = 0,
AXIS_Y,
AXIS_Z
};
//===========================================================================
// Position marker example functions
//===========================================================================
TErrCode UsePositionMarker()
{
TErrCode Result;
long PosHistorySize;
dev_hdl hAxisX;
hAxisX = LCA_GetAxisHandle(hAxisSystem, AXIS_X);
if (hAxis <= 0)
{
// no axis found - handle error and return
return hAxisX;
}
PosHistorySize = LCA_GetPositionHistorySize(hAxisX);
if (PosHistorySize < 0)
{
// handle error properly here
return PosHistorySize;
}
else if (0 == PosHistorySize)
{
//
// No position history - return an error code here
//
}
else
{
// here you can evaluate the size of the position history
// normally it will be 3
}
//
// We need to configure position capturing for each axis separately because
// each axis captures it's own position. Here we configure position
// capturing for X - axis. The edge type will be rising edge - that means
// the position value will be captured as soon as a digital input signal
// occures.
//
{
// handle error properly here
return Result;
}
//
// We use continues trigger mode here. That means each time a trigger occures
// a new position value will be captured into position history buffer.
//
if (Result != ERR_NOERR)
{
// handle error properly here
return Result;
}
//
// Before we start our move we clear all captured positions and reset
// the edge counter to 0
//
Result = LCA_ClearCapturedPositions(hAxisX);
if (Result != ERR_NOERR)
{
// handle error properly here
return Result;
}
//
// Now we start the move of X - Axis. We move 500 millimeters with a
// speed of 50 millimeters per second.
//
Result = LCA_MoveDistance(hAxisX, 500, 50, 0);
if (Result != ERR_NOERR)
{
// handle error properly here
return Result;
}
//
// The axis is now moving. We loop until target position is reached and
// read the captured positions from the device. We need to check the
// position with 2 * frequency of trigger input. On our 500 mm move
// we have an intersting point each 10 millimeters. Because we move
// with 50 millimeters per second we have 5 trigger impulses per second
// and check the position counter 10 times per second.
//
uint16_t PosCounter = 0;
do
{
Sleep(100); // sleep 100 ms - this enables 10 checks per second
//
// Check position counter if something happened
//
uint16_t NewPosCounter;
Result = LCA_ReadPosMarkerCounter(hAxisX, &NewPosCounter);
//
// Check position counter - if position counter changed then new
// position are captured and we need to read them an store them into
// our position array
//
if (NewPosCounter > PosCounter)
{
Result = LCA_ReadCapturedPosition(hAxisX, 0, &CapturedPosArray[NewPosCounter]);
if (Result != ERR_NOERR)
{
// handle error properly here - we leave the while loop
break;
}
PosCounter = NewPosCounter;
}
Result = LCA_IsAxisTargetPosReached(hAxisX);
} while (Result != 0); // leave in case of error or if target pos reached
//
// Result > 0 indicates that target position was reached without any
// errors
//
if (Result >= 0)
{
// now we can process our array with captured positions
}
return ERR_NOERR;
}