1- import _ from 'lodash' ;
21import { GCodeInterpreter } from 'gcode-interpreter' ;
32
43// from in to mm
@@ -10,7 +9,7 @@ const noop = () => {};
109const translatePosition = ( position , newPosition , relative ) => {
1110 relative = ! ! relative ;
1211 newPosition = Number ( newPosition ) ;
13- if ( _ . isNaN ( newPosition ) ) {
12+ if ( Number . isNaN ( newPosition ) ) {
1413 return position ;
1514 }
1615 return relative ? ( position + newPosition ) : newPosition ;
@@ -40,7 +39,9 @@ class GCodeToolpath {
4039 handlers = {
4140 // G0: Rapid Linear Move
4241 'G0' : ( params ) => {
43- this . setModalState ( { 'motion' : 'G0' } ) ;
42+ if ( this . modalState . motion !== 'G0' ) {
43+ this . setModalState ( { 'motion' : 'G0' } ) ;
44+ }
4445
4546 let v1 = {
4647 x : this . position . x ,
@@ -74,7 +75,9 @@ class GCodeToolpath {
7475 // G1 X90.6 Y13.8 E22.4 (Move to 90.6mm on the X axis and 13.8mm on the Y axis while extruding 22.4mm of material)
7576 //
7677 'G1' : ( params ) => {
77- this . setModalState ( { 'motion' : 'G1' } ) ;
78+ if ( this . modalState . motion !== 'G1' ) {
79+ this . setModalState ( { 'motion' : 'G1' } ) ;
80+ }
7881
7982 let v1 = {
8083 x : this . position . x ,
@@ -112,7 +115,9 @@ class GCodeToolpath {
112115 // http://linuxcnc.org/docs/2.5/html/gcode/gcode.html#sec:G2-G3-Arc
113116 // https://github.com/grbl/grbl/issues/236
114117 'G2' : ( params ) => {
115- this . setModalState ( { 'motion' : 'G2' } ) ;
118+ if ( this . modalState . motion !== 'G2' ) {
119+ this . setModalState ( { 'motion' : 'G2' } ) ;
120+ }
116121
117122 let v1 = {
118123 x : this . position . x ,
@@ -176,7 +181,9 @@ class GCodeToolpath {
176181 this . setPosition ( targetPosition . x , targetPosition . y , targetPosition . z ) ;
177182 } ,
178183 'G3' : ( params ) => {
179- this . setModalState ( { 'motion' : 'G3' } ) ;
184+ if ( this . modalState . motion !== 'G3' ) {
185+ this . setModalState ( { 'motion' : 'G3' } ) ;
186+ }
180187
181188 let v1 = {
182189 x : this . position . x ,
@@ -262,77 +269,113 @@ class GCodeToolpath {
262269 // G17..19: Plane Selection
263270 // G17: XY (default)
264271 'G17' : ( params ) => {
265- this . setModalState ( { 'plane' : 'G17' } ) ;
272+ if ( this . modalState . plane !== 'G17' ) {
273+ this . setModalState ( { 'plane' : 'G17' } ) ;
274+ }
266275 } ,
267276 // G18: XZ
268277 'G18' : ( params ) => {
269- this . setModalState ( { 'plane' : 'G18' } ) ;
278+ if ( this . modalState . plane !== 'G18' ) {
279+ this . setModalState ( { 'plane' : 'G18' } ) ;
280+ }
270281 } ,
271282 // G19: YZ
272283 'G19' : ( params ) => {
273- this . setModalState ( { 'plane' : 'G19' } ) ;
284+ if ( this . modalState . plane !== 'G19' ) {
285+ this . setModalState ( { 'plane' : 'G19' } ) ;
286+ }
274287 } ,
275288 // G20: use inches for length units
276289 'G20' : ( params ) => {
277- this . setModalState ( { 'units' : 'G20' } ) ;
290+ if ( this . modalState . units !== 'G20' ) {
291+ this . setModalState ( { 'units' : 'G20' } ) ;
292+ }
278293 } ,
279294 // G21: use millimeters for length units
280295 'G21' : ( params ) => {
281- this . setModalState ( { 'units' : 'G21' } ) ;
296+ if ( this . modalState . units !== 'G21' ) {
297+ this . setModalState ( { 'units' : 'G21' } ) ;
298+ }
282299 } ,
283300 // G38.x Straight Probe
284301 // G38.2 probe toward workpiece, stop on contact, signal error if failure
285302 'G38.2' : ( params ) => {
286- this . setModalState ( { 'motion' : 'G38.2' } ) ;
303+ if ( this . modalState . motion !== 'G38.2' ) {
304+ this . setModalState ( { 'motion' : 'G38.2' } ) ;
305+ }
287306 } ,
288307 // G38.3 probe toward workpiece, stop on contact
289308 'G38.3' : ( params ) => {
290- this . setModalState ( { 'motion' : 'G38.3' } ) ;
309+ if ( this . modalState . motion !== 'G38.3' ) {
310+ this . setModalState ( { 'motion' : 'G38.3' } ) ;
311+ }
291312 } ,
292313 // G38.4 probe away from workpiece, stop on loss of contact, signal error if failure
293314 'G38.4' : ( params ) => {
294- this . setModalState ( { 'motion' : 'G38.4' } ) ;
315+ if ( this . modalState . motion !== 'G38.4' ) {
316+ this . setModalState ( { 'motion' : 'G38.4' } ) ;
317+ }
295318 } ,
296319 // G38.5 probe away from workpiece, stop on loss of contact
297320 'G38.5' : ( params ) => {
298- this . setModalState ( { 'motion' : 'G38.5' } ) ;
321+ if ( this . modalState . motion !== 'G38.5' ) {
322+ this . setModalState ( { 'motion' : 'G38.5' } ) ;
323+ }
299324 } ,
300325 // G54..59: Coordinate System Select
301326 'G54' : ( ) => {
302- this . setModalState ( { 'coordinate' : 'G54' } ) ;
327+ if ( this . modalState . coordinate !== 'G54' ) {
328+ this . setModalState ( { 'coordinate' : 'G54' } ) ;
329+ }
303330 } ,
304331 'G55' : ( ) => {
305- this . setModalState ( { 'coordinate' : 'G55' } ) ;
332+ if ( this . modalState . coordinate !== 'G55' ) {
333+ this . setModalState ( { 'coordinate' : 'G55' } ) ;
334+ }
306335 } ,
307336 'G56' : ( ) => {
308- this . setModalState ( { 'coordinate' : 'G56' } ) ;
337+ if ( this . modalState . coordinate !== 'G56' ) {
338+ this . setModalState ( { 'coordinate' : 'G56' } ) ;
339+ }
309340 } ,
310341 'G57' : ( ) => {
311- this . setModalState ( { 'coordinate' : 'G57' } ) ;
342+ if ( this . modalState . coordinate !== 'G57' ) {
343+ this . setModalState ( { 'coordinate' : 'G57' } ) ;
344+ }
312345 } ,
313346 'G58' : ( ) => {
314- this . setModalState ( { 'coordinate' : 'G58' } ) ;
347+ if ( this . modalState . coordinate !== 'G58' ) {
348+ this . setModalState ( { 'coordinate' : 'G58' } ) ;
349+ }
315350 } ,
316351 'G59' : ( ) => {
317- this . setModalState ( { 'coordinate' : 'G59' } ) ;
352+ if ( this . modalState . coordinate !== 'G59' ) {
353+ this . setModalState ( { 'coordinate' : 'G59' } ) ;
354+ }
318355 } ,
319356 // G80: Cancel Canned Cycle
320357 'G80' : ( ) => {
321- this . setModalState ( { 'motion' : 'G80' } ) ;
358+ if ( this . modalState . motion !== 'G80' ) {
359+ this . setModalState ( { 'motion' : 'G80' } ) ;
360+ }
322361 } ,
323362 // G90: Set to Absolute Positioning
324363 // Example
325364 // G90
326365 // All coordinates from now on are absolute relative to the origin of the machine.
327366 'G90' : ( ) => {
328- this . setModalState ( { 'distance' : 'G90' } ) ;
367+ if ( this . modalState . distance !== 'G90' ) {
368+ this . setModalState ( { 'distance' : 'G90' } ) ;
369+ }
329370 } ,
330371 // G91: Set to Relative Positioning
331372 // Example
332373 // G91
333374 // All coordinates from now on are relative to the last position.
334375 'G91' : ( ) => {
335- this . setModalState ( { 'distance' : 'G91' } ) ;
376+ if ( this . modalState . distance !== 'G91' ) {
377+ this . setModalState ( { 'distance' : 'G91' } ) ;
378+ }
336379 } ,
337380 // G92: Set Position
338381 // Parameters
@@ -365,15 +408,19 @@ class GCodeToolpath {
365408 // [one divided by the F number] minutes.
366409 // For example, if the F number is 2.0, the move should be completed in half a minute.
367410 'G93' : ( ) => {
368- this . setModalState ( { 'feedrate' : 'G93' } ) ;
411+ if ( this . modalState . feedrate !== 'G93' ) {
412+ this . setModalState ( { 'feedrate' : 'G93' } ) ;
413+ }
369414 } ,
370415 // G94: start the units per minute mode
371416 // In units per minute feed rate mode, an F word on the line is interpreted to
372417 // mean the controlled point should move at a certain number of inches per minute,
373418 // millimeters per minute or degrees per minute, depending upon what length units
374419 // are being used and which axis or axes are moving.
375420 'G94' : ( ) => {
376- this . setModalState ( { 'feedrate' : 'G94' } ) ;
421+ if ( this . modalState . feedrate !== 'G94' ) {
422+ this . setModalState ( { 'feedrate' : 'G94' } ) ;
423+ }
377424 }
378425 } ;
379426
@@ -382,19 +429,25 @@ class GCodeToolpath {
382429 // @param {function } [options.addLine]
383430 // @param {function } [options.addArcCurve]
384431 constructor ( options ) {
385- options = options || { } ;
386-
387- this . setModalState ( options . modalState ) ;
388- this . fn = {
389- addLine : options . addLine || noop ,
390- addArcCurve : options . addArcCurve || noop
391- } ;
432+ const { modalState, addLine = noop , addArcCurve = noop } = { ...options } ;
433+ const nextModalState = { } ;
434+ Object . keys ( { ...modalState } ) . forEach ( key => {
435+ if ( ! this . modalState . hasOwnProperty ( key ) ) {
436+ return ;
437+ }
438+ nextModalState [ key ] = modalState [ key ] ;
439+ } ) ;
440+ this . setModalState ( nextModalState ) ;
441+ this . fn = { addLine, addArcCurve } ;
392442
393443 return new GCodeInterpreter ( { handlers : this . handlers } ) ;
394444 }
395445 setModalState ( modalState ) {
396- modalState = _ . pick ( modalState , _ . keys ( this . modalState ) )
397- this . modalState = _ . merge ( { } , this . modalState , modalState ) ;
446+ this . modalState = {
447+ ...this . modalState ,
448+ ...modalState
449+ } ;
450+
398451 return this . modalState ;
399452 }
400453 isMetricUnits ( ) { // mm
@@ -418,16 +471,10 @@ class GCodeToolpath {
418471 isYZPlane ( ) {
419472 return this . modalState . plane === 'G19' ;
420473 }
421- isInverseTimeFeedrateMode ( ) {
422- return this . modalState . feedrate === 'G93' ;
423- }
424- isUnitsPerMinuteFeedrateMode ( ) {
425- return this . modalState . feedrate === 'G94' ;
426- }
427474 setPosition ( x , y , z ) {
428- this . position . x = _ . isNumber ( x ) ? x : this . position . x ;
429- this . position . y = _ . isNumber ( y ) ? y : this . position . y ;
430- this . position . z = _ . isNumber ( z ) ? z : this . position . z ;
475+ this . position . x = ( typeof x === 'number' ) ? x : this . position . x ;
476+ this . position . y = ( typeof y === 'number' ) ? y : this . position . y ;
477+ this . position . z = ( typeof z === 'number' ) ? z : this . position . z ;
431478 }
432479 translateX ( x , relative ) {
433480 if ( x !== undefined ) {
@@ -467,7 +514,7 @@ class GCodeToolpath {
467514 }
468515 translateR ( r ) {
469516 r = Number ( r ) ;
470- if ( _ . isNaN ( r ) ) {
517+ if ( Number . isNaN ( r ) ) {
471518 return 0 ;
472519 }
473520 return this . isImperialUnits ( ) ? in2mm ( r ) : r ;
0 commit comments