-
Notifications
You must be signed in to change notification settings - Fork 1
/
g_code.ino
238 lines (195 loc) · 6.38 KB
/
g_code.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
The MIT License (MIT)
Copyright (c) 2014 Jellyfush
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#define BAUD (57600)
#define MIN_FEEDRATE (20)
#define MAX_FEEDRATE (800)
#define MIN_STEPS_PER_MIN (4000)
#define MAX_STEPS_PER_MIN (80000)
#define IO_LED (11)
#define STEPPER_DISABLE (4)
#define STEPPER_STEP (1)
#define STEPPER_DIRECTION (0)
#define STEPPER_RESET (3)
#define STEPPER_SLEEP (2)
String sBuffer;
int stepsPerUnit = 87;
int feedRate = 100;
int stepDelay = 0;
void setup(){
pinMode(STEPPER_DISABLE, OUTPUT);
digitalWrite(STEPPER_DISABLE, 1); // disable stepper
pinMode(STEPPER_STEP, OUTPUT);
digitalWrite(STEPPER_STEP, 0);
pinMode(STEPPER_DIRECTION, OUTPUT);
digitalWrite(STEPPER_DIRECTION, 0);
pinMode(STEPPER_RESET, OUTPUT); // I dont get why these are attached
digitalWrite(STEPPER_RESET, 1);
pinMode(STEPPER_SLEEP, OUTPUT);
digitalWrite(STEPPER_SLEEP, 1);
setAxisSteps(80); // default steps per mm
setFeedRate(100); // default feed rate
Serial.begin(BAUD); // open coms
printCommands();
printReady();
}
void loop(){
while(Serial.available() > 0) {
char c = Serial.read();
sBuffer += c;
if(c == '\n' || c == '\r'){
pushCurrentCommand();
resetBuffer();
printReady();
break;
}
}
}
void resetBuffer(){
sBuffer = "";
}
void printCommands(){
Serial.println(F("Commands:"));
Serial.println(F("G01 Z(units) [F(feedrate)]; - relative move"));
Serial.println(F("G04 P(seconds); - delay (not implemented)")); // TODO, not needed yet
Serial.println(F("G91; - relative mode"));
Serial.println(F("M17; - enable motors"));
Serial.println(F("M18; - disable motors"));
Serial.println(F("M91 Z(steps); - Set axis steps per unit"));
Serial.println(F("M100; - display commands"));
Serial.println(F("M114; - report position and feedrate"));
}
void printReady(){
Serial.print(F("\n\rok\n\r>"));
}
boolean pushCurrentCommand(){
if(sBuffer.length() < 3)// no commands are less than 3 char long
return false;
String results[5];
String attr;
int resultSize = getCommandArray(results);
char commandType = sBuffer[0];
int id = getCommandAttr(results, resultSize, commandType).toInt();
switch(commandType){
case 'G':
Serial.print(F("G command"));
switch(id){
case 01:
Serial.print(F(" Feed"));
attr = getCommandAttr(results, resultSize, 'F');
if(attr != "")
setFeedRate(attr.toFloat());
attr = getCommandAttr(results, resultSize, 'Z');
if(attr != "")
return startMovement(attr.toFloat());
return true;
case 04:
Serial.print(F(" Delay"));
return true;
case 91:
Serial.print(F(" Relative mode"));
return true;
}
return false;
case 'M':
Serial.print(F("M command"));
switch(id){
case 17:
Serial.print(F(" Enable"));
digitalWrite(STEPPER_DISABLE, 0);
return true;
case 18:
Serial.print(F(" Disable"));
digitalWrite(STEPPER_DISABLE, 1);
return true;
case 91:
attr = getCommandAttr(results, resultSize, 'Z');
if(attr != "")
return setAxisSteps(attr.toInt());
return true;
case 100:
printCommands();
return true;
case 114:
attr = getCommandAttr(results, resultSize, 'Z');
if(attr != "")
return setAxisSteps(attr.toInt());
return false;
}
return true;
default:
Serial.print(F("Unknown command"));
return false;
}
}
String getCommandAttr(String *attr, int attrSize, char prefix){
for(int i = 0; i < attrSize; i++){
if(attr[i].startsWith(prefix)){
return attr[i].substring(1, attr[i].length());
}
}
return "";
}
int getCommandArray(String *subCommands){
int e[] = {sBuffer.indexOf(";"), sBuffer.indexOf("\n"), sBuffer.indexOf("\r")};
int commandEnd =
e[0] != -1 ? e[0] :
e[1] != -1 ? e[1] :
e[2];
String mainCommand = sBuffer.substring(0, commandEnd);
int i = 0, j = 0, stIdx = 0, endIdx = -1;
while(1){
endIdx = mainCommand.indexOf(" ", i);
if(endIdx == -1){
subCommands[j] = mainCommand.substring(stIdx, mainCommand.length());
j++;
break;
}
subCommands[j] = mainCommand.substring(stIdx, endIdx);
if(subCommands[j] != " " && subCommands[j] != "")
j++;
stIdx = endIdx+1;
i++;
}
return j;
}
boolean setAxisSteps(int steps){
stepsPerUnit = steps;
setFeedRate(feedRate);
}
// unit(mm)/m
boolean setFeedRate(float _feedRate){
// feedrate = 100
// stepsPerUnit * 100 = steps needed to take
// steps needed to take / 60000000 = time each step will take aka delay
feedRate = constrain(_feedRate, MIN_FEEDRATE, MAX_FEEDRATE);
long stepsPerMin = (long)stepsPerUnit * feedRate;
stepDelay = 60000000l / constrain(stepsPerMin ,MIN_STEPS_PER_MIN ,MAX_STEPS_PER_MIN);
}
boolean startMovement(float pos){
float stepsToTake = stepsPerUnit * pos; // kinda shitty, probably bad
stepsToTake = abs(stepsToTake);
digitalWrite(STEPPER_DIRECTION, pos < 0);
for(int j = 0; j < stepsToTake; j++){
digitalWrite(STEPPER_STEP, 1);
delayMicroseconds(500);
digitalWrite(STEPPER_STEP, 0);
delayMicroseconds(stepDelay-500);
}
}