SynqNetOk.cpp
Demonstrates verifying the SynqNet network.
#include <string>
#include <iostream>
#include <sstream>
#include "rsi.h"
using namespace RSI::RapidCode::SynqNet;
using namespace std;
bool IsSynqNetOkAtStartup(void);
bool IsSynqNetOk(void);
MotionController* motionController;
const int EXPECTED_NODE_COUNT = (5);
RSINodeType expectedNodeTypes[EXPECTED_NODE_COUNT] =
{
RSINodeTypeMEI_SLICE,
RSINodeTypeMEI_RMB,
RSINodeTypeYASKAWA_SGDS,
RSINodeTypeTRUST_TA805,
RSINodeTypeKOLLMORGEN_S200,
};
#ifndef RELEASE
#define RSILOG(msg) RSILog log(msg, __FUNCTION__, __LINE__)
#define RSIERR(msg) RSILog log(msg, __FUNCTION__, __LINE__, true)
#endif
class RSILog
{
public:
RSILog(char* message, char* function, long lineNumber)
{
printf("%s: %s Line %d\n\n", function, message, lineNumber);
}
RSILog(char* message, char* function, long lineNumber, bool error)
{
printf("Error: ");
RSILog(message, function, lineNumber);
}
};
bool CheckSynqNetState()
{
bool result = true;
RSISynqNetState state = motionController->SynqNetStateGet();
if(state != ::RSISynqNetStateSYNQ)
{
RSILOG("SynqNet Network not in the required SYNQ state.");
result = false;
}
return result;
}
bool CheckToFindFailedSqNode()
{
bool result = true;
long failedNodeMask = motionController->SynqNetFailedNodeMaskGet();
if(failedNodeMask != 0)
{
for(int i = 0; i < MEIXmpMAX_Axes; i++)
{
if( (1<<i) & failedNodeMask)
{
stringstream temp;
temp << "SynqNet Node " << i << " is not functioning";
RSILOG((char*)temp.str().c_str());
result = false;
}
}
}
return result;
}
void CheckToFindBadCableNumber()
{
stringstream temp;
temp << "Cable " << motionController->SynqNetIdleCableNumberGet() << " has failed check Link LEDs";
RSILOG((char*)temp.str().c_str());
}
bool CheckIdleCableStatus()
{
bool result = true;
if(motionController->SynqNetIdleCableStatusGet() != ::RSISynqNetCableStatusGOOD)
{
result = false;
}
if(result == false)
{
CheckToFindBadCableNumber();
}
return result;
}
bool CheckSynqNetNetworkType()
{
bool result = true;
RSINetworkType type = motionController->SynqNetNetworkTypeGet();
if(type != ::RSINetworkTypeRING)
{
RSILOG("SynqNet Network not in the required Ring topology.");
result = false;
}
return result;
}
bool CheckSynqNetRecoveryEvent()
{
bool result = true;
result = !(motionController->SynqNetRecoveryEventGet()) ;
if(result == false)
{
RSILOG("SynqNet Network had to recover from a network/cable error.");
CheckToFindBadCableNumber();
CheckToFindFailedSqNode();
}
return result;
}
bool CheckSynqNetNodeCount()
{
bool result = true;
int nodeCount = motionController->SynqNetNodeCountGet();
if(EXPECTED_NODE_COUNT != nodeCount)
{
stringstream temp;
temp << "ExpectedCount: " << EXPECTED_NODE_COUNT << " does not match actual Node count: " << nodeCount;
RSILOG((char*)temp.str().c_str());
result = false;
}
return result;
}
bool CheckSynqNetNodeTypes()
{
bool result = true;
for(int i = 0; i < EXPECTED_NODE_COUNT; i++)
{
if(expectedNodeTypes[i] != motionController->IOGet(i)->SqNode->TypeGet())
{
stringstream temp;
temp << "ExpectedType on Node " << i << " does not match actual Node type";
RSILOG((char*)temp.str().c_str());
result = false;
}
}
return result;
}
void LogMotionControllerErrors(RapidCodeObject *rsiObject)
{
RsiError *err;
while(rsiObject->ErrorLogCountGet() > 0)
{
err = rsiObject->ErrorLogGet();
RSIERR(err->text);
}
}
bool IsSynqNetOkAtStartup()
{
bool synqNetOk = IsSynqNetOk();
bool nodeCountResult = CheckSynqNetNodeCount();
bool nodeTypesResult = CheckSynqNetNodeTypes();
return synqNetOk && nodeCountResult && nodeTypesResult;
}
bool IsSynqNetOk()
{
bool stateResult = CheckSynqNetState();
bool typeResult = CheckSynqNetNetworkType();
bool recoveryResult = CheckSynqNetRecoveryEvent();
bool idleCableResult = CheckIdleCableStatus();
return stateResult && typeResult && recoveryResult && idleCableResult;
}
int SynqNetOkMain()
{
motionController = MotionController::CreateFromBoard(0);
LogMotionControllerErrors(motionController);
try
{
bool synqNetOK = IsSynqNetOkAtStartup();
if(synqNetOK == false)
{
RSILOG("SynqNet Network initialization had problems, going to reset and retry now.");
motionController->Reset();
synqNetOK = IsSynqNetOkAtStartup();
}
if(synqNetOK)
{
while(1)
{
motionController->OS->Sleep(1000);
if(IsSynqNetOk())
{
RSILOG("SynqNet is ok...(Ctrl-C to exit)");
}
else
{
break;
}
}
}
}
catch (RsiError *err)
{
RSIERR(err->text);
return -1;
}
return 0;
}