-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvirtualmachine.cpp
338 lines (273 loc) · 7.78 KB
/
virtualmachine.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
#include "virtualmachine.h"
#include "vmstate.h"
#include "codeblock.h"
#include "word.h"
#include "straightmove.h"
#include <QDebug>
VirtualMachine::VirtualMachine(double rapidRate) :
_rapidRate(rapidRate)
{
commands[QString("G93")] = &VirtualMachine::g93;
commands[QString("G94")] = &VirtualMachine::g94;
commands[QString("M6")] = &VirtualMachine::m6;
commands[QString("M61")] = &VirtualMachine::m61;
commands[QString("M3")] = &VirtualMachine::m3;
commands[QString("M4")] = &VirtualMachine::m4;
commands[QString("M5")] = &VirtualMachine::m5;
commands[QString("M7")] = &VirtualMachine::m7;
commands[QString("M8")] = &VirtualMachine::m8;
commands[QString("M9")] = &VirtualMachine::m9;
commands[QString("G4")] = &VirtualMachine::g4;
commands[QString("G20")] = &VirtualMachine::g20;
commands[QString("G21")] = &VirtualMachine::g21;
commands[QString("G90")] = &VirtualMachine::g90;
commands[QString("G91")] = &VirtualMachine::g91;
commands[QString("G92")] = &VirtualMachine::g92;
commands[QString("G0")] = &VirtualMachine::g0;
commands[QString("G1")] = &VirtualMachine::g1;
commands[QString("G2")] = &VirtualMachine::g2;
commands[QString("G3")] = &VirtualMachine::g3;
}
const VMState&
VirtualMachine::previousState() const
{
return _previousState;
}
const VMState&
VirtualMachine::currentState() const
{
return _currentState;
}
const VMState&
VirtualMachine::doBlock(CodeBlock* block)
{
_previousState = _currentState;
QMap<Word*, char>::const_iterator it = block->commandsBegin();
for(; it != block->commandsEnd(); it++)
{
Word* cmd = it.key();
// Speed Feed & Tool always
cmd->parameterAsDouble(QChar('F'), &_currentState._feedRate);
cmd->parameterAsDouble(QChar('S'), &_currentState._spindleSpeed);
cmd->parameterAsInt(QChar('T'), &_currentState._nextTool);
CommandFn fn = commands.value(cmd->toString());
if (!fn) continue;
(this->*fn)(cmd);
}
return _currentState;
}
void
VirtualMachine::reset()
{
_moves.clear();
_previousState = _currentState = VMState();
}
double convertToMM(double inches)
{
// Yes, by the latest treaties 1 inch is EXACTLY 25.4 mm
return (inches * 25.40);
}
// Set Inverse Time Mode
void
VirtualMachine::g93(Word* word)
{
Q_UNUSED(word);
qDebug("G93 not implemented. Feeds will be wrong.");
}
// Set Units Per Minute feed mode
void
VirtualMachine::g94(Word* word)
{
Q_UNUSED(word);
qDebug("G94 not implemented. Feeds will be wrong.");
}
// Manual tool change. Tx was set either on this line or a previous
// one
void
VirtualMachine::m6(Word* word) // change tool
{
Q_UNUSED(word);
_currentState._spindleOn = false;
_currentState._currentTool = _currentState._nextTool;
}
// Force the tool to be Q as in M61 Q
// This isn't actually going to work at all because Q will not be
// seen as a real value....
void
VirtualMachine::m61(Word* word) // set tool number
{
Q_UNUSED(word);
qWarning("M61 needs an un-worded number that is unsupported right now");
}
// Start the spindle clockwise
void
VirtualMachine::m3(Word* word)
{
Q_UNUSED(word);
_currentState._spindleOn = true;
}
// start the spindle counterclockwise
void
VirtualMachine::m4(Word* word)
{
Q_UNUSED(word);
_currentState._spindleOn = true;
}
// Stop the spindle
void
VirtualMachine::m5(Word* word) //
{
Q_UNUSED(word);
_currentState._spindleOn = false;
}
// Mist coolant on
void
VirtualMachine::m7(Word* word) //
{
Q_UNUSED(word);
_currentState._coolantOn = true;
}
// Flood coolant on
void
VirtualMachine::m8(Word* word) //
{
Q_UNUSED(word);
_currentState._coolantOn = true;
}
// All coolant off
void
VirtualMachine::m9(Word* word) //
{
Q_UNUSED(word);
_currentState._coolantOn = false;
}
// Dwell for P seconds
void
VirtualMachine::g4(Word* word)
{
double p;
if (word->parameterAsDouble(QChar('P'),&p) && p > 0)
{
_currentState._seconds += p;
}
}
// Set inches length units
void
VirtualMachine::g20(Word* word)
{
Q_UNUSED(word);
_currentState._mmUnits = false;
}
// Set mm length units
void
VirtualMachine::g21(Word* word)
{
Q_UNUSED(word);
_currentState._mmUnits = true;
}
// G54-G59
// Set Absolute distance mode
void
VirtualMachine::g90(Word* word)
{
Q_UNUSED(word);
_currentState._movesAreRelative = false;
}
// Set relative distance mode
void
VirtualMachine::g91(Word* word)
{
Q_UNUSED(word);
_currentState._movesAreRelative = true;
}
// G28, G30 & G10
// Set the current point as the given value. Moves all axis
void
VirtualMachine::g92(Word* word)
{
// We really should handle this a little differently, but for now this works.
// All coordinate systems are supposed to have their own offsets that need updating,
// but since we don't do anything other than the one set of offsets this works-ish
// The offsets will be added to a point to convert from '92' to absolute space.
// They are subtracted from absolute space to get '92' space
double np;
if (word->parameterAsDouble(QChar('X'), &np))
{
if (!_currentState._mmUnits) np = convertToMM(np);
_currentState._off92MM._x = _currentState._absMM._x - np;
}
if (word->parameterAsDouble(QChar('Y'), &np))
{
if (!_currentState._mmUnits) np = convertToMM(np);
_currentState._off92MM._y = _currentState._absMM._y - np;
}
if (word->parameterAsDouble(QChar('Z'), &np))
{
if (!_currentState._mmUnits) np = convertToMM(np);
_currentState._off92MM._z = _currentState._absMM._z - np;
}
}
void
VirtualMachine::getXYZ(Word* word, Position& delta)
{
word->parameterAsDouble(QChar('X'), &delta._x);
word->parameterAsDouble(QChar('Y'), &delta._y);
word->parameterAsDouble(QChar('Z'), &delta._z);
if (!_currentState._mmUnits)
{
delta._x = convertToMM(delta._x);
delta._y = convertToMM(delta._y);
delta._z = convertToMM(delta._z);
}
// Coordinates are always expressed in '92' space
// We sort of presumably could decide that G53 (machine coordinates) are not effected by
// these offsets, but I _think_ G53 is more relevant when you are switching fixture offsets
// using G54, G55, etc. In other words, I think G53 is kind of equivalent to a 'base' set
// of NO fixture offsets, but G92 still applies.
// Anyway, always apply G92 and ignore G53-G59.3 for now
delta += _currentState._off92MM;
}
void
VirtualMachine::straightMoveAt(Word* word, double rate)
{
Position delta;
getXYZ(word, delta);
// delta is returned in absolute coordinate space and scaled to proper units
// If it's relative add the current position to it to get a final destination
if (_currentState._movesAreRelative) delta += _currentState._absMM;
// Delta is actually our new position, but we need to know the distance from the old
// so that we know how long it will take to get there at our max feed rate
double distance = delta.distanceFrom(_currentState._absMM);
double time = distance / rate;
// Store the straight move
_moves.append(new StraightMove(_currentState._absMM, delta, time));
_currentState._absMM = delta;
_currentState._seconds += time;
}
// Rapid move to a new coordinate
void
VirtualMachine::g0(Word* word)
{
straightMoveAt(word, _rapidRate);
}
// Current rate move to a new coordinate
void
VirtualMachine::g1(Word* word)
{
straightMoveAt(word, _currentState._feedRate);
}
void
VirtualMachine::g2(Word* word)
{
// For now, straighten all the curves. Sad.
qDebug("G2 not supported yet. Straightening a curve");
g1(word);
}
void
VirtualMachine::g3(Word* word)
{
// For now, straighten all the curves. Sad.
qDebug("G2 not supported yet. Straightening a curve");
g1(word);
}
// stops?