Make Gcode from .txt data. It is 2D array, for Robot Path.

Dependents:   WRS_mechanamu_test WRS2019_master

Committer:
satoyuki1111
Date:
Wed Oct 10 05:34:43 2018 +0000
Revision:
1:4ebb30d54be7
Parent:
0:359eba7592ca
Child:
2:4fc4593498d7
MakeSequencer v1.1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
satoyuki1111 0:359eba7592ca 1 /*
satoyuki1111 0:359eba7592ca 2 sample code
satoyuki1111 0:359eba7592ca 3
satoyuki1111 0:359eba7592ca 4 LocalFileSystem local("local");
satoyuki1111 0:359eba7592ca 5 int array[3] = {};
satoyuki1111 0:359eba7592ca 6
satoyuki1111 0:359eba7592ca 7 int main()
satoyuki1111 0:359eba7592ca 8 {
satoyuki1111 0:359eba7592ca 9 FILE *fp = fopen( "/local/out.txt", "r");
satoyuki1111 0:359eba7592ca 10 MakeSequencer code(fp);
satoyuki1111 0:359eba7592ca 11
satoyuki1111 0:359eba7592ca 12 code.getGcode(1,array);
satoyuki1111 0:359eba7592ca 13 }
satoyuki1111 0:359eba7592ca 14 */
satoyuki1111 0:359eba7592ca 15
satoyuki1111 0:359eba7592ca 16
satoyuki1111 0:359eba7592ca 17
satoyuki1111 0:359eba7592ca 18 #include "MakeSequencer.h"
satoyuki1111 0:359eba7592ca 19
satoyuki1111 0:359eba7592ca 20
satoyuki1111 0:359eba7592ca 21 MakeSequencer::MakeSequencer(FILE *fp)
satoyuki1111 0:359eba7592ca 22 {
satoyuki1111 0:359eba7592ca 23 _fp=fp;
satoyuki1111 0:359eba7592ca 24 FileOpen();
satoyuki1111 0:359eba7592ca 25 }
satoyuki1111 0:359eba7592ca 26
satoyuki1111 0:359eba7592ca 27 bool MakeSequencer::inherit(int _rows,int _columns,int _data[][CODE_LENGTH])
satoyuki1111 0:359eba7592ca 28 {
satoyuki1111 0:359eba7592ca 29 _data[_rows][_columns] = (_columns == 0) ? 0 : _data[_rows][_columns-1];
satoyuki1111 0:359eba7592ca 30 return false;
satoyuki1111 0:359eba7592ca 31 }
satoyuki1111 0:359eba7592ca 32
satoyuki1111 0:359eba7592ca 33 bool MakeSequencer::FileOpen()
satoyuki1111 0:359eba7592ca 34 {
satoyuki1111 0:359eba7592ca 35 if ( _fp == NULL )
satoyuki1111 0:359eba7592ca 36 {
satoyuki1111 0:359eba7592ca 37 printf("Exception");
satoyuki1111 0:359eba7592ca 38 return false;
satoyuki1111 0:359eba7592ca 39 }
satoyuki1111 0:359eba7592ca 40 else
satoyuki1111 0:359eba7592ca 41 ReadGcode();
satoyuki1111 0:359eba7592ca 42
satoyuki1111 0:359eba7592ca 43 fclose(_fp);
satoyuki1111 0:359eba7592ca 44 return true;
satoyuki1111 0:359eba7592ca 45 }
satoyuki1111 0:359eba7592ca 46
satoyuki1111 0:359eba7592ca 47 void MakeSequencer::getGcode(int _seq,int* _array)
satoyuki1111 0:359eba7592ca 48 {
satoyuki1111 0:359eba7592ca 49 for(int _i = 0;_i < ARRAY_EREMENT;_i++)
satoyuki1111 0:359eba7592ca 50 {
satoyuki1111 0:359eba7592ca 51 _array[_i] = data[_i][_seq];
satoyuki1111 0:359eba7592ca 52 }
satoyuki1111 0:359eba7592ca 53 }
satoyuki1111 0:359eba7592ca 54
satoyuki1111 1:4ebb30d54be7 55 int MakeSequencer::getGcodeSize()
satoyuki1111 1:4ebb30d54be7 56 {
satoyuki1111 1:4ebb30d54be7 57 return codesize;
satoyuki1111 1:4ebb30d54be7 58 }
satoyuki1111 1:4ebb30d54be7 59
satoyuki1111 0:359eba7592ca 60 bool MakeSequencer::ReadGcode()
satoyuki1111 0:359eba7592ca 61 {
satoyuki1111 0:359eba7592ca 62 for(int i = 0;fgets(buf,sizeof(buf), _fp) != NULL;i++)
satoyuki1111 0:359eba7592ca 63 {
satoyuki1111 1:4ebb30d54be7 64 codesize = i+1;
satoyuki1111 0:359eba7592ca 65 if(i > CODE_LENGTH)
satoyuki1111 0:359eba7592ca 66 return false;
satoyuki1111 0:359eba7592ca 67
satoyuki1111 0:359eba7592ca 68 for(int n = 0;buf[n] != '\0';n++)
satoyuki1111 0:359eba7592ca 69 code[0][i] = code[0][i]+buf[n];
satoyuki1111 0:359eba7592ca 70
satoyuki1111 0:359eba7592ca 71 int pos[3] = {code[0][i].find("x"),code[0][i].find("y"),code[0][i].find("z")};
satoyuki1111 0:359eba7592ca 72
satoyuki1111 0:359eba7592ca 73 for(int p = 0;p < sizeof(pos)/sizeof(int);p++)
satoyuki1111 0:359eba7592ca 74 find[p] = (pos[p] != code[0][i].npos) ? true:inherit(p,i,data);
satoyuki1111 0:359eba7592ca 75
satoyuki1111 0:359eba7592ca 76 for(int p = 0;p < sizeof(pos)/sizeof(int);p++)
satoyuki1111 0:359eba7592ca 77 {
satoyuki1111 0:359eba7592ca 78 char trans[1] = {};
satoyuki1111 0:359eba7592ca 79 string sw_code = find[p] ? code[0][i].substr(pos[p],1) : "I";
satoyuki1111 0:359eba7592ca 80 sw_code.copy(trans,1);
satoyuki1111 0:359eba7592ca 81 switch(trans[0])
satoyuki1111 0:359eba7592ca 82 {
satoyuki1111 0:359eba7592ca 83 case 'x':
satoyuki1111 0:359eba7592ca 84 if(find[1])
satoyuki1111 0:359eba7592ca 85 data[0][i] = atoi(code[0][i].substr(pos[0]+1,pos[1] - 1).c_str());
satoyuki1111 0:359eba7592ca 86 else if(find[2])
satoyuki1111 0:359eba7592ca 87 data[0][i] = atoi(code[0][i].substr(pos[0]+1,pos[2] - 1).c_str());
satoyuki1111 0:359eba7592ca 88 else
satoyuki1111 0:359eba7592ca 89 data[0][i] = atoi(code[0][i].substr(pos[0]+1,code[0][i].size() - 1).c_str());
satoyuki1111 0:359eba7592ca 90 break;
satoyuki1111 0:359eba7592ca 91
satoyuki1111 0:359eba7592ca 92 case 'y':
satoyuki1111 0:359eba7592ca 93 if(find[2])
satoyuki1111 0:359eba7592ca 94 data[1][i] = atoi(code[0][i].substr(pos[1]+1,pos[2] - 1).c_str());
satoyuki1111 0:359eba7592ca 95 else
satoyuki1111 0:359eba7592ca 96 data[1][i] = atoi(code[0][i].substr(pos[1]+1,code[0][i].size() - 1).c_str());
satoyuki1111 0:359eba7592ca 97 break;
satoyuki1111 0:359eba7592ca 98
satoyuki1111 0:359eba7592ca 99 case 'z':
satoyuki1111 0:359eba7592ca 100 data[2][i] = atoi(code[0][i].substr(pos[2]+1,code[0][i].size() - 1).c_str());
satoyuki1111 0:359eba7592ca 101 break;
satoyuki1111 0:359eba7592ca 102
satoyuki1111 0:359eba7592ca 103 default:
satoyuki1111 0:359eba7592ca 104 break;
satoyuki1111 0:359eba7592ca 105 }
satoyuki1111 0:359eba7592ca 106 }
satoyuki1111 0:359eba7592ca 107 printf("x=%d ,y=%d ,z=%d \n\r",data[0][i],data[1][i],data[2][i]);
satoyuki1111 0:359eba7592ca 108 }
satoyuki1111 0:359eba7592ca 109 return true;
satoyuki1111 0:359eba7592ca 110 }