Make Gcode from .txt data. It is 2D array, for Robot Path.
Dependents: WRS_mechanamu_test WRS2019_master
MakeSequencer.cpp@4:1e606e2301f9, 2018-10-17 (annotated)
- Committer:
- satoyuki1111
- Date:
- Wed Oct 17 08:39:42 2018 +0000
- Revision:
- 4:1e606e2301f9
- Parent:
- 3:09ee560a3007
- Child:
- 5:9b4fcc87af6a
ok
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
satoyuki1111 | 0:359eba7592ca | 1 | /* |
satoyuki1111 | 4:1e606e2301f9 | 2 | SAMPLE |
satoyuki1111 | 2:4fc4593498d7 | 3 | #include "mbed.h" |
satoyuki1111 | 2:4fc4593498d7 | 4 | #include "MakeSequencer.h" |
satoyuki1111 | 2:4fc4593498d7 | 5 | |
satoyuki1111 | 4:1e606e2301f9 | 6 | #define SIZE 5 |
satoyuki1111 | 4:1e606e2301f9 | 7 | #define ArraySize(array) (sizeof(array) / sizeof(array[0])) |
satoyuki1111 | 4:1e606e2301f9 | 8 | |
satoyuki1111 | 2:4fc4593498d7 | 9 | //A row is from 1 |
satoyuki1111 | 0:359eba7592ca | 10 | |
satoyuki1111 | 0:359eba7592ca | 11 | LocalFileSystem local("local"); |
satoyuki1111 | 4:1e606e2301f9 | 12 | int array[SIZE] = {}; |
satoyuki1111 | 0:359eba7592ca | 13 | |
satoyuki1111 | 0:359eba7592ca | 14 | int main() |
satoyuki1111 | 0:359eba7592ca | 15 | { |
satoyuki1111 | 0:359eba7592ca | 16 | FILE *fp = fopen( "/local/out.txt", "r"); |
satoyuki1111 | 0:359eba7592ca | 17 | MakeSequencer code(fp); |
satoyuki1111 | 4:1e606e2301f9 | 18 | code.getGcode(2,ArraySize(array),array);//getGcode(int row,int aSize, int* array) |
satoyuki1111 | 4:1e606e2301f9 | 19 | //array will get Gcode of a "int row" line |
satoyuki1111 | 4:1e606e2301f9 | 20 | for(int i = 0;i < ArraySize(array);i++) |
satoyuki1111 | 2:4fc4593498d7 | 21 | printf("%d,",array[i]); |
satoyuki1111 | 4:1e606e2301f9 | 22 | printf("\n\r%d\n\r",code.getGcodeSize()); //getGcodeSize() |
satoyuki1111 | 4:1e606e2301f9 | 23 | //return Gcode row |
satoyuki1111 | 0:359eba7592ca | 24 | } |
satoyuki1111 | 0:359eba7592ca | 25 | */ |
satoyuki1111 | 0:359eba7592ca | 26 | #include "MakeSequencer.h" |
satoyuki1111 | 0:359eba7592ca | 27 | MakeSequencer::MakeSequencer(FILE *fp) |
satoyuki1111 | 0:359eba7592ca | 28 | { |
satoyuki1111 | 0:359eba7592ca | 29 | _fp=fp; |
satoyuki1111 | 0:359eba7592ca | 30 | FileOpen(); |
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 | 2:4fc4593498d7 | 47 | void MakeSequencer::getGcode(int row,int aSize, int* array) |
satoyuki1111 | 0:359eba7592ca | 48 | { |
satoyuki1111 | 2:4fc4593498d7 | 49 | for(int i = 0;i < aSize;i++) |
satoyuki1111 | 0:359eba7592ca | 50 | { |
satoyuki1111 | 4:1e606e2301f9 | 51 | array[i] = _data[i][row]; |
satoyuki1111 | 0:359eba7592ca | 52 | } |
satoyuki1111 | 0:359eba7592ca | 53 | } |
satoyuki1111 | 0:359eba7592ca | 54 | |
satoyuki1111 | 1:4ebb30d54be7 | 55 | int MakeSequencer::getGcodeSize() |
satoyuki1111 | 1:4ebb30d54be7 | 56 | { |
satoyuki1111 | 2:4fc4593498d7 | 57 | return _codesize+1; |
satoyuki1111 | 1:4ebb30d54be7 | 58 | } |
satoyuki1111 | 1:4ebb30d54be7 | 59 | |
satoyuki1111 | 0:359eba7592ca | 60 | bool MakeSequencer::ReadGcode() |
satoyuki1111 | 0:359eba7592ca | 61 | { |
satoyuki1111 | 4:1e606e2301f9 | 62 | char * _buf = new char[BUF]; |
satoyuki1111 | 4:1e606e2301f9 | 63 | int *_tempnum = new int[ARRAY_EREMENT]; |
satoyuki1111 | 4:1e606e2301f9 | 64 | char **_element = new char*[ARRAY_EREMENT]; |
satoyuki1111 | 4:1e606e2301f9 | 65 | bool **_check = new bool*[ARRAY_EREMENT]; |
satoyuki1111 | 4:1e606e2301f9 | 66 | for (int y = 0; y < ARRAY_EREMENT; y++) |
satoyuki1111 | 4:1e606e2301f9 | 67 | { |
satoyuki1111 | 4:1e606e2301f9 | 68 | _element[y] = new char[CODE_LENGTH]; |
satoyuki1111 | 4:1e606e2301f9 | 69 | _check[y] = new bool[CODE_LENGTH]; |
satoyuki1111 | 4:1e606e2301f9 | 70 | } |
satoyuki1111 | 4:1e606e2301f9 | 71 | |
satoyuki1111 | 4:1e606e2301f9 | 72 | for(int b = 0;b < BUF;b++) |
satoyuki1111 | 4:1e606e2301f9 | 73 | _buf[b] = 0; |
satoyuki1111 | 4:1e606e2301f9 | 74 | |
satoyuki1111 | 4:1e606e2301f9 | 75 | for(int x = 0;x < ARRAY_EREMENT;x++) |
satoyuki1111 | 4:1e606e2301f9 | 76 | { |
satoyuki1111 | 4:1e606e2301f9 | 77 | _tempnum[x] = 0; |
satoyuki1111 | 4:1e606e2301f9 | 78 | for(int y = 0;y < CODE_LENGTH;y++) |
satoyuki1111 | 4:1e606e2301f9 | 79 | { |
satoyuki1111 | 4:1e606e2301f9 | 80 | _data [x][y] = 0; |
satoyuki1111 | 4:1e606e2301f9 | 81 | _element[x][y] = ' '; |
satoyuki1111 | 4:1e606e2301f9 | 82 | _check [x][y] = true; |
satoyuki1111 | 4:1e606e2301f9 | 83 | } |
satoyuki1111 | 4:1e606e2301f9 | 84 | } |
satoyuki1111 | 3:09ee560a3007 | 85 | _ele = 0; |
satoyuki1111 | 4:1e606e2301f9 | 86 | for(int i = 0;fgets(_buf,BUF, _fp) != NULL;i++) |
satoyuki1111 | 0:359eba7592ca | 87 | { |
satoyuki1111 | 2:4fc4593498d7 | 88 | _codesize = i; |
satoyuki1111 | 0:359eba7592ca | 89 | if(i > CODE_LENGTH) |
satoyuki1111 | 0:359eba7592ca | 90 | return false; |
satoyuki1111 | 2:4fc4593498d7 | 91 | else |
satoyuki1111 | 2:4fc4593498d7 | 92 | { |
satoyuki1111 | 4:1e606e2301f9 | 93 | string *_tempstr = new string[ARRAY_EREMENT]; |
satoyuki1111 | 4:1e606e2301f9 | 94 | for(int x = 0;x < ARRAY_EREMENT;x++) |
satoyuki1111 | 4:1e606e2301f9 | 95 | _tempstr[x] = " "; |
satoyuki1111 | 4:1e606e2301f9 | 96 | |
satoyuki1111 | 3:09ee560a3007 | 97 | _count = -1; |
satoyuki1111 | 0:359eba7592ca | 98 | |
satoyuki1111 | 2:4fc4593498d7 | 99 | for(int n = 0;_buf[n] != '\0';n++) |
satoyuki1111 | 0:359eba7592ca | 100 | { |
satoyuki1111 | 4:1e606e2301f9 | 101 | char _char = _buf[n]; |
satoyuki1111 | 2:4fc4593498d7 | 102 | if((_char >= 'a'&& _char <= 'z') || (_char >= 'A' && _char <= 'Z')) |
satoyuki1111 | 2:4fc4593498d7 | 103 | { |
satoyuki1111 | 2:4fc4593498d7 | 104 | _count++; |
satoyuki1111 | 2:4fc4593498d7 | 105 | if(_char != _element[_count][0] && i != 0) |
satoyuki1111 | 2:4fc4593498d7 | 106 | { |
satoyuki1111 | 2:4fc4593498d7 | 107 | for(int p = 0;p < _ele;p++) |
satoyuki1111 | 2:4fc4593498d7 | 108 | { |
satoyuki1111 | 2:4fc4593498d7 | 109 | if(_char == _element[p][0]) |
satoyuki1111 | 2:4fc4593498d7 | 110 | { |
satoyuki1111 | 2:4fc4593498d7 | 111 | _element[p][i] = _char; |
satoyuki1111 | 2:4fc4593498d7 | 112 | _count = p; |
satoyuki1111 | 2:4fc4593498d7 | 113 | _check[p][i] = false; |
satoyuki1111 | 2:4fc4593498d7 | 114 | break; |
satoyuki1111 | 2:4fc4593498d7 | 115 | } |
satoyuki1111 | 2:4fc4593498d7 | 116 | } |
satoyuki1111 | 2:4fc4593498d7 | 117 | } |
satoyuki1111 | 4:1e606e2301f9 | 118 | else |
satoyuki1111 | 4:1e606e2301f9 | 119 | _element[_count][i] = _char; |
satoyuki1111 | 2:4fc4593498d7 | 120 | if(i == 0) |
satoyuki1111 | 2:4fc4593498d7 | 121 | _ele++; |
satoyuki1111 | 2:4fc4593498d7 | 122 | } |
satoyuki1111 | 4:1e606e2301f9 | 123 | else if(_char >= '0'&& _char <= '9' || _char <= '-') |
satoyuki1111 | 2:4fc4593498d7 | 124 | { |
satoyuki1111 | 2:4fc4593498d7 | 125 | _tempstr[_count] = _tempstr[_count] + _char; |
satoyuki1111 | 2:4fc4593498d7 | 126 | } |
satoyuki1111 | 0:359eba7592ca | 127 | |
satoyuki1111 | 4:1e606e2301f9 | 128 | else if(_char == '\n' || _char == '\r') |
satoyuki1111 | 2:4fc4593498d7 | 129 | { |
satoyuki1111 | 2:4fc4593498d7 | 130 | } |
satoyuki1111 | 2:4fc4593498d7 | 131 | else |
satoyuki1111 | 2:4fc4593498d7 | 132 | return false; |
satoyuki1111 | 2:4fc4593498d7 | 133 | } |
satoyuki1111 | 4:1e606e2301f9 | 134 | //printf("#(%d) ",i); |
satoyuki1111 | 2:4fc4593498d7 | 135 | for(int n = 0;n < _ele;n++) |
satoyuki1111 | 2:4fc4593498d7 | 136 | { |
satoyuki1111 | 2:4fc4593498d7 | 137 | _tempnum[n] = atoi(_tempstr[n].c_str()); |
satoyuki1111 | 3:09ee560a3007 | 138 | _data[n][i] = (i != 0 && _check[n][i] && _element[n][i] != _element[n][0]) |
satoyuki1111 | 3:09ee560a3007 | 139 | ? _data[n][i-1] |
satoyuki1111 | 3:09ee560a3007 | 140 | : _tempnum[n]; |
satoyuki1111 | 4:1e606e2301f9 | 141 | |
satoyuki1111 | 4:1e606e2301f9 | 142 | _element[n][i] = (i != 0 && _check[n][i] && _element[n][i] != _element[n][0]) |
satoyuki1111 | 4:1e606e2301f9 | 143 | ? _element[n][0] |
satoyuki1111 | 4:1e606e2301f9 | 144 | : _element[n][i]; |
satoyuki1111 | 4:1e606e2301f9 | 145 | //printf("%c:%d",_element[n][i],_data[n][i]); |
satoyuki1111 | 2:4fc4593498d7 | 146 | } |
satoyuki1111 | 4:1e606e2301f9 | 147 | //printf("\n\r"); |
satoyuki1111 | 4:1e606e2301f9 | 148 | delete[] _tempstr; |
satoyuki1111 | 0:359eba7592ca | 149 | } |
satoyuki1111 | 0:359eba7592ca | 150 | } |
satoyuki1111 | 4:1e606e2301f9 | 151 | |
satoyuki1111 | 4:1e606e2301f9 | 152 | for (int y = 0; y < ARRAY_EREMENT;y++) |
satoyuki1111 | 4:1e606e2301f9 | 153 | { |
satoyuki1111 | 4:1e606e2301f9 | 154 | delete[] _check[y]; |
satoyuki1111 | 4:1e606e2301f9 | 155 | delete[] _element[y]; |
satoyuki1111 | 4:1e606e2301f9 | 156 | } |
satoyuki1111 | 4:1e606e2301f9 | 157 | delete[] _tempnum; |
satoyuki1111 | 4:1e606e2301f9 | 158 | delete[] _buf; |
satoyuki1111 | 4:1e606e2301f9 | 159 | delete[] _check; |
satoyuki1111 | 4:1e606e2301f9 | 160 | delete[] _element; |
satoyuki1111 | 4:1e606e2301f9 | 161 | |
satoyuki1111 | 0:359eba7592ca | 162 | return true; |
satoyuki1111 | 0:359eba7592ca | 163 | } |