Information and States complete code
This page contains the complete code of the Information and States.
In AxisConfiguration.cpp
, your Information and States code should be as follows:
Copy
#include "RT_Project_01.h"
#include "AxisConfiguration.h"
using namespace std;
VOID PrintDeviceInformation(int Index)
{
int Resolution = 0;
KsError nRet = errNoError;
SlaveStatus Sts = { 0 };
nRet = GetAxisByIndex(Index, &Sts, &Resolution, NULL, NULL);
if (nRet != errNoError)
RtPrintf("GetAxisByIndex failed: %x\n", nRet);
RtPrintf("Device Index: %d, Device Name: %s, Vendor ID: 0x%x, Product Code: 0x%x, Revision Number: 0x%x, Serial Number: 0x%x\n",
Index, Sts.Name, Sts.VendorId, Sts.ProductCode, Sts.RevisionNumber, Sts.SerialNumber);
RtPrintf("Slave ID: %d, Physical Address: %d, Alias Address: %d\n", Sts.SlaveId, Sts.PhysAddress, Sts.AliasAddress);
RtPrintf("Input Length: %d, Output Length: %d, Variable Index Offset: 0x%x\n", Sts.InputLength, Sts.OutputLength, Sts.VariableIndexOffset);
RtPrintf("EtherCAT State: %d, Cycle Time: %d\n", Sts.State, Sts.CycleTime);
RtPrintf("Resolution: %d\n", Resolution);
RtPrintf("\n");
}
VOID PrintAxisInformation(int Index)
{
BOOL HomeAbsSwitch = FALSE;
BOOL LimitSwitchPos = FALSE;
BOOL LimitSwitchNeg = FALSE;
BOOL Simulation = FALSE;
BOOL CommunicationReady = FALSE;
BOOL ReadyForPowerOn = FALSE;
BOOL PowerOn = FALSE;
BOOL IsHomed = FALSE;
BOOL AxisWarning = FALSE;
KsError nRet = errNoError;
nRet = GetAxisInfo(Index, &HomeAbsSwitch, &LimitSwitchPos, &LimitSwitchNeg, &Simulation, &CommunicationReady, &ReadyForPowerOn, &PowerOn, &IsHomed, &AxisWarning);
if (nRet != errNoError)
RtPrintf("GetAxisInfo failed: %x\n", nRet);
RtPrintf("Switches:\n");
RtPrintf("Home Switch: %d, Positive Limit Switch: %d, Negative Limit Switch: %d\n\n", HomeAbsSwitch, LimitSwitchPos, LimitSwitchNeg);
RtPrintf("States:\n");
RtPrintf("States: Simulation: %d, Communication Ready: %d, Ready for Power On: %d, Power On: %d, Is Homed: %d, Axis Warning: %d\n",
Simulation, CommunicationReady, ReadyForPowerOn, PowerOn, IsHomed, AxisWarning);
RtPrintf("\n");
}
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");
}