Add command to load config file: ecmcGrblLoadConfigFile()

This commit is contained in:
Anders Sandstrom
2022-02-08 09:28:23 +01:00
parent 652d4d21f5
commit b9d97d08bd
6 changed files with 567 additions and 996 deletions

View File

@@ -911,7 +911,7 @@ void ecmcGrbl::addCommand(std::string command) {
}
}
void ecmcGrbl::loadFile(std::string fileName, int append) {
void ecmcGrbl::loadGCodeFile(std::string fileName, int append) {
if(cfgDbgMode_){
printf("%s:%s:%d: file %s, append %d\n",__FILE__,__FUNCTION__,__LINE__,fileName.c_str(),append);
}
@@ -946,19 +946,24 @@ void ecmcGrbl::loadFile(std::string fileName, int append) {
}
void ecmcGrbl::addConfig(std::string command) {
if(cfgDbgMode_){
printf("%s:%s:%d:command %s\n",__FILE__,__FUNCTION__,__LINE__,command.c_str());
}
if (getEcmcEpicsIOCState() == 16) {
if(cfgDbgMode_){
printf("%s:%s:%d: GRBL: ERROR: Configuratoin can only be applied during startup:(0x%x)\n",
printf("%s:%s:%d: GRBL: ERROR: Configuratoin can only be applied during startup:(0x%x)\n",
__FILE__,__FUNCTION__,__LINE__,ECMC_PLUGIN_CONFIG_ERROR_CODE);
}
return;
}
if(cfgDbgMode_){
printf("%s:%s:%d:command %s\n",__FILE__,__FUNCTION__,__LINE__,command.c_str());
std::size_t found = command.find(ECMC_CONFIG_GRBL_CONFIG_CHAR);
if (found==std::string::npos) {
printf("%s:%s:%d: GRBL: ERROR: Configuration command not valid (0x%x)\n",
__FILE__,__FUNCTION__,__LINE__,ECMC_PLUGIN_CONFIG_ERROR_CODE);
return;
}
epicsMutexLock(grblConfigBufferMutex_);
grblConfigBuffer_.push_back(command.c_str());
epicsMutexUnlock(grblConfigBufferMutex_);
@@ -968,3 +973,37 @@ void ecmcGrbl::addConfig(std::string command) {
}
}
void ecmcGrbl::loadConfigFile(std::string fileName, int append) {
if(cfgDbgMode_){
printf("%s:%s:%d: file %s, append %d\n",__FILE__,__FUNCTION__,__LINE__,fileName.c_str(),append);
}
std::ifstream file;
file.open(fileName);
if (!file.good()) {
if(cfgDbgMode_){
printf("%s:%s:%d: GRBL: ERROR: File not found: %s (0x%x)\n",
__FILE__,__FUNCTION__,__LINE__,fileName.c_str(),ECMC_PLUGIN_LOAD_FILE_ERROR_CODE);
}
errorCode_ = ECMC_PLUGIN_LOAD_FILE_ERROR_CODE;
throw std::runtime_error("Error: File not found.");
return;
}
// Clear buffer (since not append)
if(!append) {
setExecute(0);
epicsMutexLock(grblConfigBufferMutex_);
grblConfigBuffer_.clear();
epicsMutexUnlock(grblConfigBufferMutex_);
}
std::string line, lineNoComments;
while (std::getline(file, line)) {
if(line.length()>0) {
addConfig(line);
}
}
}

View File

@@ -74,7 +74,8 @@ class ecmcGrbl : public asynPortDriver {
void doWriteWorker();
void addCommand(std::string command);
void addConfig(std::string command);
void loadFile(std::string filename, int append);
void loadGCodeFile(std::string filename, int append);
void loadConfigFile(std::string fileName, int append);
int enterRT();
int grblRTexecute(int ecmcError);
int setExecute(int exe);

View File

@@ -24,6 +24,7 @@
#define ECMC_PLUGIN_ASYN_PREFIX "plugin.grbl"
#define ECMC_CONFIG_FILE_COMMENT_CHAR "#"
#define ECMC_CONFIG_GRBL_CONFIG_CHAR "$"
#define ECMC_PLUGIN_GRBL_GENERAL_ERROR_CODE 0x100
#define ECMC_PLUGIN_GRBL_COMMAND_ERROR_CODE 0x101

View File

@@ -210,7 +210,7 @@ static void initCallFunc_0(const iocshArgBuf *args) {
void ecmcGrblLoadFilePrintHelp() {
printf("\n");
printf(" Use ecmcGrblLoadFile(<filename>,<append>)\n");
printf(" Use ecmcGrblLoadGCodeFile(<filename>,<append>)\n");
printf(" <filename> : Filename containg g-code.\n");
printf(" <append> : 0: reset grbl, clear all current commands in buffer before \n");
printf (" loading file (default).\n");
@@ -237,7 +237,7 @@ int ecmcGrblLoadFile(const char* filename, int append) {
}
try {
grbl->loadFile(filename, append);
grbl->loadGCodeFile(filename, append);
}
catch(std::exception& e) {
printf("Exception: %s. Load file command failed.\n",e.what());
@@ -255,13 +255,11 @@ static const iocshArg initArg1_1 =
static const iocshArg *const initArgs_1[] = { &initArg0_1,
&initArg1_1};
static const iocshFuncDef initFuncDef_1 = { "ecmcGrblLoadFile", 2, initArgs_1 };
static const iocshFuncDef initFuncDef_1 = { "ecmcGrblLoadGCodeFile", 2, initArgs_1 };
static void initCallFunc_1(const iocshArgBuf *args) {
ecmcGrblLoadFile(args[0].sval,args[1].ival);
}
/*
$11 - Junction deviation, mm
$12 Arc tolerance, mm
@@ -333,6 +331,62 @@ static void initCallFunc_2(const iocshArgBuf *args) {
ecmcGrblAddConfig(args[0].sval);
}
/**
* EPICS iocsh shell command: ecmcGrblLoadConfigFile
*/
void ecmcGrblLoadConfigFilePrintHelp() {
printf("\n");
printf(" Use ecmcGrblLoadConfigFile(<filename>,<append>)\n");
printf(" <filename> : Filename containg grbl configs.\n");
printf(" <append> : 0: clear all current configs in buffer before \n");
printf (" loading file (default).\n");
printf(" : 1: append commands in file last in buffer. (grbl is not reset)\n");
printf("\n");
}
int ecmcGrblLoadConfigFile(const char* filename, int append) {
if(!filename) {
printf("Error: filename.\n");
ecmcGrblLoadConfigFilePrintHelp();
return asynError;
}
if(strcmp(filename,"-h") == 0 || strcmp(filename,"--help") == 0 ) {
ecmcGrblLoadConfigFilePrintHelp();
return asynSuccess;
}
if(!grbl) {
printf("Plugin not initialized/loaded.\n");
return asynError;
}
try {
grbl->loadConfigFile(filename,append);
}
catch(std::exception& e) {
printf("Exception: %s. Load file command failed.\n",e.what());
return asynError;
}
return asynSuccess;
}
static const iocshArg initArg0_3 =
{ " Filename", iocshArgString };
static const iocshArg initArg1_3 =
{ " Append", iocshArgInt };
static const iocshArg *const initArgs_3[] = { &initArg0_3,
&initArg1_3};
static const iocshFuncDef initFuncDef_3 = { "ecmcGrblLoadConfigFile", 2, initArgs_3 };
static void initCallFunc_3(const iocshArgBuf *args) {
ecmcGrblLoadConfigFile(args[0].sval,args[1].ival);
}
///**
// * Register all functions
//*/
@@ -340,6 +394,7 @@ void ecmcGrblPluginDriverRegister(void) {
iocshRegister(&initFuncDef_0, initCallFunc_0); // ecmcGrblAddCommand
iocshRegister(&initFuncDef_1, initCallFunc_1); // ecmcGrblLoadFile
iocshRegister(&initFuncDef_2, initCallFunc_2); // ecmcGrblAddConfig
iocshRegister(&initFuncDef_3, initCallFunc_3); // ecmcGrblLoadConfigFile
}
epicsExportRegistrar(ecmcGrblPluginDriverRegister);

File diff suppressed because it is too large Load Diff

View File

@@ -26,7 +26,6 @@ $(ECMCCFG_INIT)$(SCRIPTEXEC) ${ecmccfg_DIR}startup.cmd, "IOC=$(IOC),ECMC_VER=ruc
# Hardware for X and Y in ESS crate
$(SCRIPTEXEC) $(ecmccfg_DIR)ecmcMCU1021_coupler.cmd
# Use EL7211 as spindle
epicsEnvSet("ECMC_EC_SLAVE_NUM", "11")
epicsEnvSet("ECMC_EC_SLAVE_NUM_SPINDLE", "$(ECMC_EC_SLAVE_NUM)")
@@ -76,6 +75,9 @@ $(SCRIPTEXEC) ($(ecmccfg_DIR)applyAxisSynchronization.cmd, CONFIG=./cfg/y.sax)
epicsEnvSet("DEV", "$(IOC)")
$(SCRIPTEXEC) ($(ecmccfg_DIR)configureAxis.cmd, CONFIG=./cfg/spindle.ax)
# Block motor driver printouts
asynSetTraceMask(MC_CPU1, -1, 0x0)
##############################################################################
## Load plugin:
epicsEnvSet("PLUGIN_VER" ,"develop")
@@ -87,8 +89,21 @@ ${SCRIPTEXEC} ${ecmccfg_DIR}loadPlugin.cmd, "PLUGIN_ID=0,FILE=${ECMC_PLUGIN_FILN
epicsEnvUnset(ECMC_PLUGIN_FILNAME)
epicsEnvUnset(ECMC_PLUGIN_CONFIG)
ecmcGrblLoadFile("./plc/gcode.nc",0)
asynSetTraceMask(MC_CPU1, -1, 0x0)
# Load grbl configs:
# $11 - Junction deviation, mm\n
# $12 Arc tolerance, mm\n
# $30 - Max spindle speed, RPM\n
# $31 - Min spindle speed, RPM\n
# $100, $101 and $102 [X,Y,Z] steps/mm\n
# $110, $111 and $112 [X,Y,Z] Max rate, mm/min\n
# $120, $121, $122 [X,Y,Z] Acceleration, mm/sec^2\n
ecmcGrblAddConfig("$120=1234");
ecmcGrblAddConfig("$121=1234");
ecmcGrblAddConfig("$122=1234");
# Load g-code
ecmcGrblLoadGCodeFile("./plc/gcode.nc",0)
#ecmcGrblAddCommand("G1X20Y20F360");
#ecmcGrblAddCommand("G4P2");
@@ -98,13 +113,6 @@ asynSetTraceMask(MC_CPU1, -1, 0x0)
#ecmcGrblAddCommand("G4P2");
#ecmcGrblAddCommand("G1X0Y0");
#ecmcGrblAddCommand("G2X0adadsdY0R20");
#ecmcGrblAddCommand("G1X0Y20");
#ecmcGrblAddCommand("G1X10Y0F360");
#ecmcGrblAddCommand("G4P1");
#ecmcGrblAddCommand("G1X50Y50F180");
#ecmcGrblAddCommand("G1X0Y0");
##############################################################################
## PLC 0
$(SCRIPTEXEC) $(ecmccfg_DIR)loadPLCFile.cmd, "PLC_ID=0, SAMPLE_RATE_MS=1000,FILE=./plc/grbl.plc")