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