-
Notifications
You must be signed in to change notification settings - Fork 0
/
MotionController.cpp
365 lines (325 loc) · 12.2 KB
/
MotionController.cpp
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include "MotionController.h"
#include "Arduino.h"
MotionController::MotionController()
{
//We need these bloat variables to deal with a bug in the linker.
//See https://github.com/arduino/Arduino/issues/1071 for more info.
//
for (int i = 0; i < 5; i++)
{
bloatVariable1[i] = i;
bloatVariable2[i] = i;
bloatVariable3[i] = i;
bloatVariable4[i] = i;
bloatVariable5[i] = i;
}
}
//This attaches our servo to the correct pin
void MotionController::attachServo( void )
{
#ifdef IO_APERTURE_SERVO
apertureServo_.attach(IO_APERTURE_SERVO);
apertureServo_.write(ApertureClose); //Close aperture
#endif
}
void MotionController::setFeedrate( float feeds[] )
{
//We know that we only have max 3 feedrates.
feedRates_[0] = feeds[0];
feedRates_[1] = feeds[1];
feedRates_[2] = feeds[2];
}
void MotionController::calculateStepDelay( void )
{
long stepsPerSecond[3];
for (int i = 0; i < 3; i++)
{
stepsPerSecond[i] = (long) (stepsPerMM_[i] * feedRates_[i]);
stepDelay_[i] = (int) (1000/stepsPerSecond[i]);
}
}
//This takes a char that was grabbed from DuellaSerial, and peforms actions
//depending on the char. This is essentially the Uncia's stock firmware.
void MotionController::pushCurrentCommandForStockUncia( char c )
{
char Byte = c;
switch(Byte)
{
case 'E': //Enable Stepper Motor
digitalWrite(Z_STEPPER_DISABLE, 0);
Serial.write('e');
break;
case 'D': //Disable Stepper Motor
digitalWrite(Z_STEPPER_DISABLE, 1);
Serial.write('d');
break;
case 'F': //Change Direction to forward, or away from the projector (1)
digitalWrite(Z_STEPPER_DIRECTION, 1);
Serial.write('f');
break;
case 'R': //Change Direction to Reverse, or towards the projector (0)
digitalWrite(Z_STEPPER_DIRECTION, 0);
Serial.write('r');
break;
case 'S': //Step the Stepper Motor
digitalWrite( Z_STEPPER_STEP, 1);
delay(1);
digitalWrite( Z_STEPPER_STEP, 0);
Serial.write('s');
break;
case 'P': //Ping Teensy 2.0
#ifdef STOCK_UNCIA
Serial.write('Sedgwick3D');
#endif
#ifndef STOCK_UNCIA
Serial.write('N'); //'N' for Not-Stock
#endif
break;
case 'O':
apertureServo_.write(ApertureOpen); //Open Aperture
Serial.write('O');
break;
case 'C':
apertureServo_.write(ApertureClose); //Close Aperture
Serial.write('C');
break;
default:
break;
}
}
void MotionController::pushCurrentCommandForGcode( String sBuffer )
{
String* parsedLine = gcodeParser_.parseLine(sBuffer);
int numberOfCommands = parsedLine[0].toInt(); //As set in our parseLine method
int commandCount = 0;
//String* parsedLineWithoutCount = parsedLine.substring(1);
String** commandArray = gcodeParser_.parseCommands(parsedLine);
for (int i = 1; i < numberOfCommands; i++) //We start at 1 since the 0 pos is the number of commands.
{
String* fullCommand = commandArray[i];
String commandLetterString = fullCommand[0];
commandCount++; //We're going to increase our counter to make sure we don't read the same command twice.
char commandLetter = commandLetterString.charAt(0);
String commandValueString = fullCommand[1];
float commandValue = float(gcodeParser_.stringToFloat(commandValueString));
// We now have both the letter and the value of the command! Time to move!
switch(commandLetter)
{
case 'G':
//If it's a G command, commandValue must be an int:
int commandValueInt = int(commandValue);
switch(commandValueInt)
{
case 1: //G1, simple move
//We know that the next three commands could possibly be X, Y, or Z.
if (commandCount < numberOfCommands)
{// So we don't try to grab a null
String* nextCommand = commandArray[i+1];
char nextCommandLetter = nextCommand[0].charAt(0);
float nextCommandValue = float(gcodeParser_.stringToFloat(nextCommand[1]));
if (nextCommandLetter == 'X' || nextCommandLetter == 'Y' || nextCommandLetter == 'Z')
{
switch(nextCommandLetter)
{
case 'X':
moveX( nextCommandValue );
break;
case 'Y':
moveX( nextCommandValue );
break;
case 'Z':
moveZ( nextCommandValue );
break;
}
commandCount++;
i++; //We want to increase our counter to make sure that we don't check this twice.
if(commandCount < (numberOfCommands - 1))
{ //Again, don't want to grab a null!
String* nextNextCommand = commandArray[i+2];
char nextNextCommandLetter = nextNextCommand[0].charAt(0);
float nextNextCommandValue = float(gcodeParser_.stringToFloat(nextNextCommand[1]));
if (nextNextCommandLetter == 'X' || nextNextCommandLetter == 'Y' || nextNextCommandLetter == 'Z')
{
switch(nextNextCommandLetter)
{
case 'X':
moveX( nextNextCommandValue );
break;
case 'Y':
moveX( nextNextCommandValue );
break;
case 'Z':
moveZ( nextNextCommandValue );
break;
}
commandCount++;
i++; //We want to increase our counter to make sure that we don't check this twice.
if(commandCount < (numberOfCommands - 2))
{ //Again, don't want to grab a null!
String* nextNextNextCommand = commandArray[i+3]; /// This is getting ridiculous...
char nextNextNextCommandLetter = nextNextNextCommand[0].charAt(0);
float nextNextNextCommandValue = float(gcodeParser_.stringToFloat(nextNextNextCommand[1]));
if (nextNextNextCommandLetter == 'X' || nextNextNextCommandLetter == 'Y' || nextNextNextCommandLetter == 'Z')
{
switch(nextNextNextCommandLetter)
{
case 'X':
moveX( nextNextNextCommandValue );
break;
case 'Y':
moveX( nextNextNextCommandValue );
break;
case 'Z':
moveZ( nextNextNextCommandValue );
break;
}
commandCount++;
i++; //We want to increase our counter to make sure that we don't check this twice.
//We know that it will be, at most, "G1 X_ Y_ Z_", so we can stop at 3 iterations. Thank god that this silliness is done.
}
}
}
}
}
}
break;
case 4: //G4, dwell.
//TODO
break;
case 28: // home the printer.
//TODO
break;
case 5:
//TODO
break;
}
break;
}
}
}
void MotionController::setAxisSteps(float steps[])
{
stepsPerMM_[0] = steps[0];
stepsPerMM_[1] = steps[1];
stepsPerMM_[2] = steps[2];
}
void MotionController::giveGcodeParser( GcodeParser parse )
{
gcodeParser_ = parse;
}
void MotionController::moveX( float amountToMove )
{
#ifdef WIPER_AXIS_STEPPER
//amountToMove is given in mm
if (amountToMove > 0) //we want to move in the negative direction
{
digitalWrite(X_STEPPER_DIRECTION, 0);
}
float stepsToTake = amountToMove * stepsPerMM_[0];
stepsToTake = abs(stepsToTake);
for (int i = 0; i < stepsToTake; i++)
{
if(!checkEndstopTriggered(0) )
{
digitalWrite( X_STEPPER_STEP, 1);
delayMicroseconds(500);
digitalWrite( X_STEPPER_STEP, 0);
if (stepDelay_[0] <= 500)
{
delayMicroseconds(500);
}else{
delayMicroseconds( stepDelay_[0] - 500 );
}
}
}
#endif
}
void MotionController::moveY( float amountToMove )
{
#ifdef Y_AXIS_STEPPER
//amountToMove is given in mm
if (amountToMove > 0) //we want to move in the negative direction
{
digitalWrite(Y_STEPPER_DIRECTION, 0);
}
float stepsToTake = amountToMove * stepsPerMM_[1];
stepsToTake = abs(stepsToTake);
for (int i = 0; i < stepsToTake; i++)
{
if(!checkEndstopTriggered(1) )
{
digitalWrite( Y_STEPPER_STEP, 1);
delayMicroseconds(500);
digitalWrite( Y_STEPPER_STEP, 0);
if (stepDelay_[1] <= 500)
{
delayMicroseconds(500);
}else{
delayMicroseconds( stepDelay_[1] - 500 );
}
}
}
#endif
}
void MotionController::moveZ( float amountToMove )
{
//amountToMove is given in mm
if (amountToMove > 0) //we want to move in the negative direction
{
digitalWrite(Z_STEPPER_DIRECTION, 0);
}
float stepsToTake = amountToMove * stepsPerMM_[2];
stepsToTake = abs(stepsToTake);
for (int i = 0; i < stepsToTake; i++)
{
if(!checkEndstopTriggered(2) )
{
digitalWrite(Z_STEPPER_STEP, 1);
delayMicroseconds(500);
digitalWrite(Z_STEPPER_STEP, 0);
if (stepDelay_[2] <= 500)
{
delayMicroseconds(500);
}else{
delayMicroseconds( stepDelay_[2] - 500 );
}
}
}
}
boolean MotionController::checkEndstopTriggered( int axis )
{
#ifdef Z_PRINT_LEVEL_ENDSTOP
if (axis == 2)
{
if( Z_MIN_ENDSTOP_INVERTING )
{
return !digitalRead(Z_PRINT_ENDSTOP_PIN);
}else {
return digitalRead(Z_PRINT_ENDSTOP_PIN);
}
}
#endif
#ifdef Y_AXIS_ENDSTOP
if (axis == 1)
{
if( Y_MIN_ENDSTOP_INVERTING )
{
return !digitalRead(Y_AXIS_ENDSTOP_PIN);
}else {
return digitalRead(Y_AXIS_ENDSTOP_PIN);
}
}
#endif
#ifdef WIPER_AXIS_ENDSTOP
if (axis == 0)
{
if( Z_MIN_ENDSTOP_INVERTING )
{
return !digitalRead(WIPER_AXIS_ENDSTOP_PIN);
}else {
return digitalRead(WIPER_AXIS_ENDSTOP_PIN);
}
}
#endif
return false;
}