Add more validation:
* ensure group is not empty * ensure axis group with same name is created twice * ensure group exists when adding axis
This commit is contained in:
@@ -37,13 +37,32 @@ int setCfgString(const char* cfgString) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
ecmcSS1SafetyGroup* getGroupFromName(const char *name) {
|
||||
// Find group by name
|
||||
for(std::vector<ecmcSS1SafetyGroup*>::iterator psafetyGroup = safetyGroups.begin(); psafetyGroup != safetyGroups.end(); ++psafetyGroup) {
|
||||
bool found = strcmp(name, (*psafetyGroup)->getName().c_str()) == 0;
|
||||
if(found) {
|
||||
return (*psafetyGroup);
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
int createSafetyGroup(const char *name,
|
||||
const char *ec_rampdown_cmd,
|
||||
const char *ec_standstill_status,
|
||||
int time_delay_ms) {
|
||||
|
||||
// ensure group does not already exist
|
||||
ecmcSS1SafetyGroup* safetyGroup = getGroupFromName(name);
|
||||
if( safetyGroup ) {
|
||||
printf("Safety: Error, group %s already defined. Plugin will unload.\n");
|
||||
throw std::runtime_error( "Safety: Error, group already defined.");
|
||||
}
|
||||
|
||||
// create new ecmcSS1SafetyGroup object
|
||||
ecmcSS1SafetyGroup* safetyGroup = NULL;
|
||||
safetyGroup = NULL;
|
||||
|
||||
// create asynport name for new object ()
|
||||
memset(portNameBuffer, 0, ECMC_PLUGIN_MAX_PORTNAME_CHARS);
|
||||
@@ -61,7 +80,6 @@ int createSafetyGroup(const char *name,
|
||||
if(safetyGroup) {
|
||||
delete safetyGroup;
|
||||
}
|
||||
return ECMC_PLUGIN_SAFETY_ERROR_CODE;
|
||||
}
|
||||
|
||||
safetyGroups.push_back(safetyGroup);
|
||||
@@ -74,16 +92,17 @@ int addAxisToSafetyGroup(const char *groupName,
|
||||
int axisId,
|
||||
double veloLimit,
|
||||
int standStillTimeMs) {
|
||||
// Find group by name
|
||||
for(std::vector<ecmcSS1SafetyGroup*>::iterator psafetyGroup = safetyGroups.begin(); psafetyGroup != safetyGroups.end(); ++psafetyGroup) {
|
||||
bool found = strcmp(groupName, (*psafetyGroup)->getName().c_str()) == 0;
|
||||
if(found) {
|
||||
(*psafetyGroup)->addAxis(axisId,veloLimit,standStillTimeMs);
|
||||
return asynSuccess;
|
||||
}
|
||||
|
||||
ecmcSS1SafetyGroup* grp = getGroupFromName(groupName);
|
||||
|
||||
if( !grp ) {
|
||||
printf("Safety: Error, group %s not found. Plugin will unload.\n",groupName);
|
||||
throw std::runtime_error( "Safety: Error, group not found.");
|
||||
}
|
||||
|
||||
return asynError;
|
||||
grp->addAxis(axisId,veloLimit,standStillTimeMs);
|
||||
|
||||
return asynSuccess;
|
||||
}
|
||||
|
||||
void deleteAllSafetyGroups() {
|
||||
@@ -98,17 +117,26 @@ void deleteAllSafetyGroups() {
|
||||
}
|
||||
|
||||
int validate() {
|
||||
int groupCount = 0;
|
||||
for(std::vector<ecmcSS1SafetyGroup*>::iterator psafetyGroup = safetyGroups.begin(); psafetyGroup != safetyGroups.end(); ++psafetyGroup) {
|
||||
if(*psafetyGroup) {
|
||||
try {
|
||||
(*psafetyGroup)->validate();
|
||||
groupCount++;
|
||||
}
|
||||
catch(std::exception& e) {
|
||||
printf("Exception: %s. Plugin will unload.\n",e.what());
|
||||
printf("Safety: %s. Plugin will unload.\n",e.what());
|
||||
return ECMC_PLUGIN_SAFETY_ERROR_CODE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Do not allow loaded plugin with no group configured
|
||||
if(groupCount == 0) {
|
||||
printf("Saftey: Error, group count 0. Plugin will unload.\n");
|
||||
return ECMC_PLUGIN_SAFETY_ERROR_CODE;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -120,7 +148,7 @@ int executeSafetyGroups() {
|
||||
(*psafetyGroup)->execute();
|
||||
}
|
||||
catch(std::exception& e) {
|
||||
printf("Exception: %s. Plugin will unload.\n",e.what());
|
||||
printf("Safety: %s. Plugin will unload.\n",e.what());
|
||||
return ECMC_PLUGIN_SAFETY_ERROR_CODE;
|
||||
}
|
||||
}
|
||||
@@ -165,7 +193,7 @@ int ecmcAddSS1SafetyGroup(const char* name, const char* ec_rampdown_cmd,const ch
|
||||
}
|
||||
if(time_delay_ms <= 0) {
|
||||
printf("Error: time_delay invalid.\n");
|
||||
exit (1);
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -173,7 +201,7 @@ int ecmcAddSS1SafetyGroup(const char* name, const char* ec_rampdown_cmd,const ch
|
||||
}
|
||||
catch(std::exception& e) {
|
||||
printf("Exception: %s. Add safety group failed.\n",e.what());
|
||||
exit (1);
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
return asynSuccess;
|
||||
@@ -224,24 +252,24 @@ int ecmcAddAxisToSafetyGroup(const char* name, int axis_id, double velo_lim, int
|
||||
|
||||
if(axis_id <= 0) {
|
||||
printf("Error: Invalid axis id.\n");
|
||||
exit (1);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if(velo_lim < 0) {
|
||||
printf("Error: Invalid velocity limit.\n");
|
||||
exit (1);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if(stand_still_time < 0) {
|
||||
printf("Error: Invalid stand still filter time.\n");
|
||||
exit (1);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
try {
|
||||
return addAxisToSafetyGroup(name,axis_id, velo_lim, stand_still_time);
|
||||
}
|
||||
catch(std::exception& e) {
|
||||
printf("Exception: %s. Add axis to safety group failed.\n",e.what());
|
||||
exit (1);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
return asynSuccess;
|
||||
|
||||
Reference in New Issue
Block a user