Make Gcode from .txt data. It is 2D array, for Robot Path.
Dependents: WRS_mechanamu_test WRS2019_master
MakeSequencer.cpp@6:e513810fc3a6, 2018-11-14 (annotated)
- Committer:
- sgrsn
- Date:
- Wed Nov 14 02:20:38 2018 +0000
- Revision:
- 6:e513810fc3a6
- Parent:
- 5:9b4fcc87af6a
Change Parameter;
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 | 5:9b4fcc87af6a | 6 | #define SIZE 6 |
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 | 5:9b4fcc87af6a | 24 | |
satoyuki1111 | 5:9b4fcc87af6a | 25 | Solves arrays bug(Dynamic allocation,Initialize,release) |
satoyuki1111 | 5:9b4fcc87af6a | 26 | //out.txt |
satoyuki1111 | 5:9b4fcc87af6a | 27 | //x0y0z0a0b0c0 |
satoyuki1111 | 5:9b4fcc87af6a | 28 | //x10y10 |
satoyuki1111 | 5:9b4fcc87af6a | 29 | //x20a20c20 |
satoyuki1111 | 5:9b4fcc87af6a | 30 | //c30 |
satoyuki1111 | 5:9b4fcc87af6a | 31 | |
satoyuki1111 | 5:9b4fcc87af6a | 32 | //output |
satoyuki1111 | 5:9b4fcc87af6a | 33 | //20,10,0,20,0,20, (3 rows data output) |
satoyuki1111 | 5:9b4fcc87af6a | 34 | //4 (this code has 4 rows) |
satoyuki1111 | 0:359eba7592ca | 35 | */ |
satoyuki1111 | 5:9b4fcc87af6a | 36 | |
satoyuki1111 | 0:359eba7592ca | 37 | #include "MakeSequencer.h" |
satoyuki1111 | 0:359eba7592ca | 38 | MakeSequencer::MakeSequencer(FILE *fp) |
satoyuki1111 | 0:359eba7592ca | 39 | { |
satoyuki1111 | 0:359eba7592ca | 40 | _fp=fp; |
satoyuki1111 | 0:359eba7592ca | 41 | FileOpen(); |
satoyuki1111 | 0:359eba7592ca | 42 | } |
satoyuki1111 | 0:359eba7592ca | 43 | |
satoyuki1111 | 0:359eba7592ca | 44 | bool MakeSequencer::FileOpen() |
satoyuki1111 | 0:359eba7592ca | 45 | { |
satoyuki1111 | 0:359eba7592ca | 46 | if ( _fp == NULL ) |
satoyuki1111 | 0:359eba7592ca | 47 | { |
satoyuki1111 | 0:359eba7592ca | 48 | printf("Exception"); |
satoyuki1111 | 0:359eba7592ca | 49 | return false; |
satoyuki1111 | 0:359eba7592ca | 50 | } |
satoyuki1111 | 0:359eba7592ca | 51 | else |
satoyuki1111 | 0:359eba7592ca | 52 | ReadGcode(); |
satoyuki1111 | 0:359eba7592ca | 53 | |
satoyuki1111 | 0:359eba7592ca | 54 | fclose(_fp); |
satoyuki1111 | 0:359eba7592ca | 55 | return true; |
satoyuki1111 | 0:359eba7592ca | 56 | } |
satoyuki1111 | 0:359eba7592ca | 57 | |
satoyuki1111 | 2:4fc4593498d7 | 58 | void MakeSequencer::getGcode(int row,int aSize, int* array) |
satoyuki1111 | 0:359eba7592ca | 59 | { |
satoyuki1111 | 2:4fc4593498d7 | 60 | for(int i = 0;i < aSize;i++) |
satoyuki1111 | 0:359eba7592ca | 61 | { |
satoyuki1111 | 4:1e606e2301f9 | 62 | array[i] = _data[i][row]; |
satoyuki1111 | 0:359eba7592ca | 63 | } |
satoyuki1111 | 0:359eba7592ca | 64 | } |
satoyuki1111 | 0:359eba7592ca | 65 | |
satoyuki1111 | 1:4ebb30d54be7 | 66 | int MakeSequencer::getGcodeSize() |
satoyuki1111 | 1:4ebb30d54be7 | 67 | { |
satoyuki1111 | 2:4fc4593498d7 | 68 | return _codesize+1; |
satoyuki1111 | 1:4ebb30d54be7 | 69 | } |
satoyuki1111 | 1:4ebb30d54be7 | 70 | |
satoyuki1111 | 0:359eba7592ca | 71 | bool MakeSequencer::ReadGcode() |
satoyuki1111 | 0:359eba7592ca | 72 | { |
satoyuki1111 | 4:1e606e2301f9 | 73 | char * _buf = new char[BUF]; |
satoyuki1111 | 4:1e606e2301f9 | 74 | int *_tempnum = new int[ARRAY_EREMENT]; |
satoyuki1111 | 4:1e606e2301f9 | 75 | char **_element = new char*[ARRAY_EREMENT]; |
satoyuki1111 | 4:1e606e2301f9 | 76 | bool **_check = new bool*[ARRAY_EREMENT]; |
satoyuki1111 | 4:1e606e2301f9 | 77 | for (int y = 0; y < ARRAY_EREMENT; y++) |
satoyuki1111 | 4:1e606e2301f9 | 78 | { |
satoyuki1111 | 4:1e606e2301f9 | 79 | _element[y] = new char[CODE_LENGTH]; |
satoyuki1111 | 4:1e606e2301f9 | 80 | _check[y] = new bool[CODE_LENGTH]; |
satoyuki1111 | 4:1e606e2301f9 | 81 | } |
satoyuki1111 | 4:1e606e2301f9 | 82 | |
satoyuki1111 | 4:1e606e2301f9 | 83 | for(int b = 0;b < BUF;b++) |
satoyuki1111 | 4:1e606e2301f9 | 84 | _buf[b] = 0; |
satoyuki1111 | 4:1e606e2301f9 | 85 | |
satoyuki1111 | 4:1e606e2301f9 | 86 | for(int x = 0;x < ARRAY_EREMENT;x++) |
satoyuki1111 | 4:1e606e2301f9 | 87 | { |
satoyuki1111 | 4:1e606e2301f9 | 88 | _tempnum[x] = 0; |
satoyuki1111 | 4:1e606e2301f9 | 89 | for(int y = 0;y < CODE_LENGTH;y++) |
satoyuki1111 | 4:1e606e2301f9 | 90 | { |
satoyuki1111 | 4:1e606e2301f9 | 91 | _data [x][y] = 0; |
satoyuki1111 | 4:1e606e2301f9 | 92 | _element[x][y] = ' '; |
satoyuki1111 | 4:1e606e2301f9 | 93 | _check [x][y] = true; |
satoyuki1111 | 4:1e606e2301f9 | 94 | } |
satoyuki1111 | 4:1e606e2301f9 | 95 | } |
satoyuki1111 | 3:09ee560a3007 | 96 | _ele = 0; |
satoyuki1111 | 4:1e606e2301f9 | 97 | for(int i = 0;fgets(_buf,BUF, _fp) != NULL;i++) |
satoyuki1111 | 0:359eba7592ca | 98 | { |
satoyuki1111 | 2:4fc4593498d7 | 99 | _codesize = i; |
satoyuki1111 | 0:359eba7592ca | 100 | if(i > CODE_LENGTH) |
satoyuki1111 | 0:359eba7592ca | 101 | return false; |
satoyuki1111 | 2:4fc4593498d7 | 102 | else |
satoyuki1111 | 2:4fc4593498d7 | 103 | { |
satoyuki1111 | 4:1e606e2301f9 | 104 | string *_tempstr = new string[ARRAY_EREMENT]; |
satoyuki1111 | 4:1e606e2301f9 | 105 | for(int x = 0;x < ARRAY_EREMENT;x++) |
satoyuki1111 | 4:1e606e2301f9 | 106 | _tempstr[x] = " "; |
satoyuki1111 | 4:1e606e2301f9 | 107 | |
satoyuki1111 | 3:09ee560a3007 | 108 | _count = -1; |
satoyuki1111 | 0:359eba7592ca | 109 | |
satoyuki1111 | 2:4fc4593498d7 | 110 | for(int n = 0;_buf[n] != '\0';n++) |
satoyuki1111 | 0:359eba7592ca | 111 | { |
satoyuki1111 | 4:1e606e2301f9 | 112 | char _char = _buf[n]; |
satoyuki1111 | 2:4fc4593498d7 | 113 | if((_char >= 'a'&& _char <= 'z') || (_char >= 'A' && _char <= 'Z')) |
satoyuki1111 | 2:4fc4593498d7 | 114 | { |
satoyuki1111 | 2:4fc4593498d7 | 115 | _count++; |
satoyuki1111 | 2:4fc4593498d7 | 116 | if(_char != _element[_count][0] && i != 0) |
satoyuki1111 | 2:4fc4593498d7 | 117 | { |
satoyuki1111 | 2:4fc4593498d7 | 118 | for(int p = 0;p < _ele;p++) |
satoyuki1111 | 2:4fc4593498d7 | 119 | { |
satoyuki1111 | 2:4fc4593498d7 | 120 | if(_char == _element[p][0]) |
satoyuki1111 | 2:4fc4593498d7 | 121 | { |
satoyuki1111 | 2:4fc4593498d7 | 122 | _element[p][i] = _char; |
satoyuki1111 | 2:4fc4593498d7 | 123 | _count = p; |
satoyuki1111 | 2:4fc4593498d7 | 124 | _check[p][i] = false; |
satoyuki1111 | 2:4fc4593498d7 | 125 | break; |
satoyuki1111 | 2:4fc4593498d7 | 126 | } |
satoyuki1111 | 2:4fc4593498d7 | 127 | } |
satoyuki1111 | 2:4fc4593498d7 | 128 | } |
satoyuki1111 | 4:1e606e2301f9 | 129 | else |
satoyuki1111 | 4:1e606e2301f9 | 130 | _element[_count][i] = _char; |
satoyuki1111 | 2:4fc4593498d7 | 131 | if(i == 0) |
satoyuki1111 | 2:4fc4593498d7 | 132 | _ele++; |
satoyuki1111 | 2:4fc4593498d7 | 133 | } |
satoyuki1111 | 4:1e606e2301f9 | 134 | else if(_char >= '0'&& _char <= '9' || _char <= '-') |
satoyuki1111 | 2:4fc4593498d7 | 135 | { |
satoyuki1111 | 2:4fc4593498d7 | 136 | _tempstr[_count] = _tempstr[_count] + _char; |
satoyuki1111 | 2:4fc4593498d7 | 137 | } |
satoyuki1111 | 0:359eba7592ca | 138 | |
satoyuki1111 | 4:1e606e2301f9 | 139 | else if(_char == '\n' || _char == '\r') |
satoyuki1111 | 2:4fc4593498d7 | 140 | { |
satoyuki1111 | 2:4fc4593498d7 | 141 | } |
satoyuki1111 | 2:4fc4593498d7 | 142 | else |
satoyuki1111 | 2:4fc4593498d7 | 143 | return false; |
satoyuki1111 | 2:4fc4593498d7 | 144 | } |
satoyuki1111 | 2:4fc4593498d7 | 145 | for(int n = 0;n < _ele;n++) |
satoyuki1111 | 2:4fc4593498d7 | 146 | { |
satoyuki1111 | 2:4fc4593498d7 | 147 | _tempnum[n] = atoi(_tempstr[n].c_str()); |
satoyuki1111 | 3:09ee560a3007 | 148 | _data[n][i] = (i != 0 && _check[n][i] && _element[n][i] != _element[n][0]) |
satoyuki1111 | 3:09ee560a3007 | 149 | ? _data[n][i-1] |
satoyuki1111 | 3:09ee560a3007 | 150 | : _tempnum[n]; |
satoyuki1111 | 4:1e606e2301f9 | 151 | |
satoyuki1111 | 4:1e606e2301f9 | 152 | _element[n][i] = (i != 0 && _check[n][i] && _element[n][i] != _element[n][0]) |
satoyuki1111 | 4:1e606e2301f9 | 153 | ? _element[n][0] |
satoyuki1111 | 4:1e606e2301f9 | 154 | : _element[n][i]; |
satoyuki1111 | 2:4fc4593498d7 | 155 | } |
satoyuki1111 | 4:1e606e2301f9 | 156 | delete[] _tempstr; |
satoyuki1111 | 0:359eba7592ca | 157 | } |
satoyuki1111 | 0:359eba7592ca | 158 | } |
satoyuki1111 | 4:1e606e2301f9 | 159 | |
satoyuki1111 | 4:1e606e2301f9 | 160 | for (int y = 0; y < ARRAY_EREMENT;y++) |
satoyuki1111 | 4:1e606e2301f9 | 161 | { |
satoyuki1111 | 4:1e606e2301f9 | 162 | delete[] _check[y]; |
satoyuki1111 | 4:1e606e2301f9 | 163 | delete[] _element[y]; |
satoyuki1111 | 4:1e606e2301f9 | 164 | } |
satoyuki1111 | 4:1e606e2301f9 | 165 | delete[] _tempnum; |
satoyuki1111 | 4:1e606e2301f9 | 166 | delete[] _buf; |
satoyuki1111 | 4:1e606e2301f9 | 167 | delete[] _check; |
satoyuki1111 | 4:1e606e2301f9 | 168 | delete[] _element; |
satoyuki1111 | 4:1e606e2301f9 | 169 | |
satoyuki1111 | 0:359eba7592ca | 170 | return true; |
satoyuki1111 | 0:359eba7592ca | 171 | } |