Make Gcode from .txt data. It is 2D array, for Robot Path.
Dependents: WRS_mechanamu_test WRS2019_master
MakeSequencer.cpp
- Committer:
- satoyuki1111
- Date:
- 2018-10-09
- Revision:
- 0:359eba7592ca
- Child:
- 1:4ebb30d54be7
File content as of revision 0:359eba7592ca:
/* sample code LocalFileSystem local("local"); int array[3] = {}; int main() { FILE *fp = fopen( "/local/out.txt", "r"); MakeSequencer code(fp); code.getGcode(1,array); } */ #include "MakeSequencer.h" MakeSequencer::MakeSequencer(FILE *fp) { _fp=fp; FileOpen(); } bool MakeSequencer::inherit(int _rows,int _columns,int _data[][CODE_LENGTH]) { _data[_rows][_columns] = (_columns == 0) ? 0 : _data[_rows][_columns-1]; return false; } bool MakeSequencer::FileOpen() { if ( _fp == NULL ) { printf("Exception"); return false; } else ReadGcode(); fclose(_fp); return true; } void MakeSequencer::getGcode(int _seq,int* _array) { for(int _i = 0;_i < ARRAY_EREMENT;_i++) { _array[_i] = data[_i][_seq]; } } bool MakeSequencer::ReadGcode() { for(int i = 0;fgets(buf,sizeof(buf), _fp) != NULL;i++) { if(i > CODE_LENGTH) return false; for(int n = 0;buf[n] != '\0';n++) code[0][i] = code[0][i]+buf[n]; int pos[3] = {code[0][i].find("x"),code[0][i].find("y"),code[0][i].find("z")}; for(int p = 0;p < sizeof(pos)/sizeof(int);p++) find[p] = (pos[p] != code[0][i].npos) ? true:inherit(p,i,data); for(int p = 0;p < sizeof(pos)/sizeof(int);p++) { char trans[1] = {}; string sw_code = find[p] ? code[0][i].substr(pos[p],1) : "I"; sw_code.copy(trans,1); switch(trans[0]) { case 'x': if(find[1]) data[0][i] = atoi(code[0][i].substr(pos[0]+1,pos[1] - 1).c_str()); else if(find[2]) data[0][i] = atoi(code[0][i].substr(pos[0]+1,pos[2] - 1).c_str()); else data[0][i] = atoi(code[0][i].substr(pos[0]+1,code[0][i].size() - 1).c_str()); break; case 'y': if(find[2]) data[1][i] = atoi(code[0][i].substr(pos[1]+1,pos[2] - 1).c_str()); else data[1][i] = atoi(code[0][i].substr(pos[1]+1,code[0][i].size() - 1).c_str()); break; case 'z': data[2][i] = atoi(code[0][i].substr(pos[2]+1,code[0][i].size() - 1).c_str()); break; default: break; } } printf("x=%d ,y=%d ,z=%d \n\r",data[0][i],data[1][i],data[2][i]); } return true; }