summaryrefslogtreecommitdiff
path: root/docs/src/gcode/m-code.txt
blob: 8c8e6fd5cbd013262326dfdd81beb0755e0bebdf (plain)
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
= M Codes

[[cha:m-codes]] (((M Codes)))

:ini: {basebackend@docbook:'':ini}
:hal: {basebackend@docbook:'':hal}
:ngc: {basebackend@docbook:'':ngc}

== M Code Quick Reference Table [[m-code-quick-reference]]
[width="60%", options="header", cols="2^,5<"]
|========================================
|Code                                          | Description
|<<sec:M0-M1,M0 M1>>                           | Program Pause
|<<sec:M2-M30,M2 M30>>                         | Program End
|<<sec:M60, M60>>                              | Pallet Change Pause
|<<sec:M3-M4-M5,M3 M4 M5>>                     | Spindle Control
|<<sec:M6-Tool-Change,M6>>                     | Tool Change
|<<sec:M7-M8-M9,M7 M8 M9>>                     | Coolant Control
|<<sec:M19,M19>>                               | Orient Spindle
|<<sec:M48-M49-Override,M48 M49>>              | Feed & Spindle Overrides Enable/Disable
|<<sec:M50-Feed-Override,M50>>                 | Feed Override Control
|<<sec:M51-Spindle-Override,M51>>              | Spindle Override Control
|<<sec:M52-Adaptive-Feed-Control,M52>>         | Adaptive Feed Control
|<<sec:M53-Feed-Stop-Control,M53>>             | Feed Stop Control
|<<sec:M61-Set-Current-Tool-Number,M61>>       | Set Current Tool Number
|<<sec:M62-M65,M62-M65>>                       | Output Control
|<<sec:M66-Input-Control,M66>>                 | Input Control
|<<sec:M67-Analog-Output,M67>>                 | Analog Output Control
|<<sec:M68-Analog-Output,M68>>                 | Analog Output Control
|<<sec:M70-Save-Modal-State,M70>>              | Save Modal State
|<<sec:M71-Invalidate-Stored-Modal-State,M71>> | Invalidate Stored Modal State
|<<sec:M72-Restore-Modal-State,M72>>           | Restore Modal State
|<<sec:M73-Save-Autorestore-Modal-State,M73>>  | Save Autorestore Modal State
|<<sec:M100-to-M199,M100-M199>>                | User Defined M-Codes 
|========================================


[[sec:M0-M1]]
== M0, M1 Program Pause
(((M0 Program Pause)))(((M1 Program Optional Pause)))

* 'M0' - pause a running program temporarily. LinuxCNC remains
         in the Auto Mode so MDI and other manual actions
         are not enabled. Pressing the cycle start button
         will restart the program at the following line.

* 'M1' - pause a running program temporarily if the optional
         stop switch is on. LinuxCNC remains in the Auto Mode
         so MDI and other manual actions are not enabled.
         Pressing the cycle start button
         will restart the program at the following line.

[NOTE]
It is OK to program 'M0' and 'M1' in MDI mode,
but the effect will probably not be noticeable,
because normal behavior in MDI mode is 
to stop after each line of input anyway.

[[sec:M2-M30]]
== M2, M30 Program End
(((M2 Program End)))(((M30 Program End)))

* 'M2' - end the program. Pressing cycle start will
         start the program at the beginning of the file.
         
* 'M30' - exchange pallet shuttles and end the program.
          Pressing cycle start will start the program
          at the beginning of the file.

Both of these commands have the following effects:

. Change from Auto mode to MDI mode.
. Origin offsets are set to the default (like 'G54').
. Selected plane is set to XY plane (like 'G17').
. Distance mode is set to absolute mode (like 'G90').
. Feed rate mode is set to units per minute (like 'G94').
. Feed and speed overrides are set to ON (like 'M48').
. Cutter compensation is turned off (like 'G40').
. The spindle is stopped (like 'M5').
. The current motion mode is set to feed (like 'G1').
. Coolant is turned off (like 'M9').

[NOTE]
Lines of code after M2/M30 will not be executed.
Pressing cycle start will start the program
at the beginning of the file.

[[sec:M60]]
== M60 Pallet Change Pause
(((M60 Pallet Change Pause)))

* 'M60' - exchange pallet shuttles and then pause a running program
          temporarily (regardless of the setting of the optional stop
          switch). Pressing the cycle start button
          will restart the program at the following line.

[[sec:M3-M4-M5]]
== M3, M4, M5 Spindle Control
(((M3 Spindle CW)))(((M4 Spindle CCW)))(((M5 Spindle Stop)))

* 'M3' - start the spindle clockwise at the 'S' speed.
* 'M4' - start the spindle counterclockwise at the 'S' speed.
* 'M5' - stop the spindle.

It is OK to use 'M3' or 'M4' if the <<sec:S-spindle-speed,S>>
spindle speed is set to zero. If this is done
(or if the speed override switch is enabled and set to zero),
the spindle will not start turning.
If, later, the spindle speed is set above zero
(or the override switch is turned up),
the spindle will start turning.
It is OK to use 'M3' or 'M4' when the spindle is already
turning or to use 'M5' when the spindle is already stopped.

[[sec:M6-Tool-Change]]
== M6 Tool Change
(((M6-Tool-Change)))

=== Manual Tool Change

If the HAL component hal_manualtoolchange is loaded,
M6 will stop the spindle and prompt the user to change the tool
based on the last 'T-' number programmed.
For more information on hal_manualtoolchange see
the (<<sec:Manual-Tool-Change,Manual Tool Change>>) Section.

=== Tool Changer

To change a tool in the spindle from the tool currently in the spindle
to the tool most recently selected (using a T word - see Section
<<sec:T-Select-Tool,Select Tool>>), program 'M6'.
When the tool change is complete:

* The spindle will be stopped. 
* The tool that was selected (by a T word on the same line or on any
   line after the previous tool change) will be in the spindle.
* If the selected tool was not in the spindle before the tool change,
   the tool that was in the spindle (if there was one) will be placed
   back into the tool changer magazine.
* If configured in the .ini file some axis positions may move when a M6
   is issued. See the EMCIO section of the Integrator's Manual for more
   information on tool change options.
* No other changes will be made. For example, coolant will continue to
   flow during the tool change unless it has been turned off by an 'M9'.

[WARNING]
The tool length offset is not changed by 'M6', use 'G43' after the
'M6' to change the tool length offset.

The tool change may include axis motion. 
It is OK (but not useful) to program a change to the tool already in the spindle.
It is OK if there is no tool in the selected slot;
in that case, the spindle will be empty after the tool change.
If slot zero was last selected,
there will definitely be no tool in the spindle after a tool change. The tool
changer will have to be setup to perform the tool change in hal and possibly
classicladder.

[[sec:M7-M8-M9]]
== M7, M8, M9 Coolant Control
(((M7 Mist Coolant)))(((M8 Flood Coolant)))(((M9 Coolant Off)))

* 'M7' - turn mist coolant on.
* 'M8' - turn flood coolant on.
* 'M9' - turn all coolant off.

It is OK to use any of these commands, regardless of the current coolant
state.

[[sec:M19]]
== M19 Orient Spindle
(((M19 Orient Spindle)))

* 'M19 R- Q- [P-]'

* 'R' Position to rotate to from 0, valid range is 0-360 degrees

* 'Q' Number of seconds to wait until orient completes. If
      motion.spindle.is_oriented does not become true within Q timeout
      an error occurs.

* 'P' Direction to rotate to position.
** '0' rotate for smallest angular movement (default)
** '1' always rotate clockwise (same as M3 direction)
** '2' always rotate counterclockwise (same as M4 direction)


M19 is cleared by any of M3,M4,M5.

Spindle orientation requires a differential encoder with an index to sense the
spindle shaft position and direction of rotation.

INI Settings in the [RS274NGC] section.

ORIENT_OFFSET = 0-360 (fixed offset in degrees added to M19 R word)

HAL Pins

* 'motion.spindle-orient-angle' (out float)
Desired spindle orientation for M19. Value of the M19 R word parameter 
plus the value of the [RS274NGC]ORIENT_OFFSET ini parameter.

* 'motion.spindle-orient-mode' (out s32)
Desired spindle rotation mode. Reflects M19 P parameter word, Default = 0

* 'motion.spindle-orient' (out bit)
Indicates start of spindle orient cycle. Set by M19. Cleared by any of 
M3,M4,M5.
  If spindle-orient-fault is not zero during spindle-orient true, the 
M19 command fails with an error message.

* 'motion.spindle-is-oriented' (in bit)
Acknowledge pin for spindle-orient. Completes orient cycle. If 
spindle-orient was true when spindle-is-oriented
  was asserted, the spindle-orient pin is cleared and the spindle-locked 
pin is asserted. Also, the spindle-brake pin is asserted.

* 'motion.spindle-orient-fault' (in s32)
  Fault code input for orient cycle. Any value other than zero will 
cause the orient cycle to abort.

* 'motion.spindle-locked' (out bit)
Spindle orient complete pin. Cleared by any of M3,M4,M5.


[[sec:M48-M49-Override]]
== M48, M49 Speed and Feed Override Control
(((M48, M49 Override Control)))

* 'M48' - enable the spindle speed and feed rate override controls.
* 'M49' - disable both controls.

It is OK to enable or disable the controls when 
they are already enabled or disabled. 
See the <<sub:feed-rate,Feed Rate>> Section for more details.

[[sec:M50-Feed-Override]]
== M50 Feed Override Control
(((M50 Feed Override Control)))

* 'M50 <P1>' - enable the feed rate override control. The P1
               is optional. 
* 'M50 P0' - disable the feed rate control.
 
While disabled the feed override will have no influence,
and the motion will be executed at programmed feed rate.
(unless there is an adaptive feed rate override active).

[[sec:M51-Spindle-Override]]
== M51 Spindle Speed Override Control
(((M51 Spindle Speed Override)))

* 'M51 <P1>' - enable the spindle speed override control. The P1
               is optional. 
* 'M51 P0' - disable the spindle speed override control program.
             While disabled the spindle speed override will have
             no influence, and the spindle speed will have the
             exact program specified value of the S-word
             (described in <<sec:S-spindle-speed,Spindle Speed>> Section).

[[sec:M52-Adaptive-Feed-Control]]
== M52 Adaptive Feed Control
(((M52 Adaptive Feed Control)))

* 'M52 <P1>' - use an adaptive feed. The P1 is optional.
* 'M52 P0' - stop using adaptive feed.
 
When adaptive feed is enabled, some external input value is used together
with the user interface feed override value and the commanded feed rate
to set the actual feed rate. In LinuxCNC, the HAL pin 'motion.adaptive-feed'
is used for this purpose. Values on 'motion.adaptive-feed' should range
from 0 (feed hold) to 1 (full speed).

[[sec:M53-Feed-Stop-Control]]
== M53 Feed Stop Control
(((M53 Feed Stop Control)))

* 'M53 <P1>' - enable the feed stop switch. The P1 is optional.
               Enabling the feed stop switch will allow motion to be
               interrupted by means of the feed stop control. In LinuxCNC,
               the HAL pin 'motion.feed-hold' is used for this purpose. A 'true'
               value will cause the motion to stop when 'M53' is active.

* 'M53 P0' - disable the feed stop switch. The state of 'motion.feed-hold'
             will have no effect on feed when M53 is not active.


[[sec:M61-Set-Current-Tool-Number]]
== M61 Set Current Tool Number
(((M61 Set Current Tool Number)))

* 'M61 Q-' - change the current tool number while in MDI or Manual mode.
             One use is when you power up LinuxCNC with a tool currently in
             the spindle you can set that tool number without doing a tool change.

It is an error if:

* Q- is not 0 or greater

[[sec:M62-M65]]
== M62 to M65 Output Control
(((M62 to M65 Output Control)))

* 'M62 P-' - turn on digital output synchronized with motion.
             The P- word specifies the digital output number.

* 'M63 P-' - turn off digital output synchronized with motion.
             The P- word specifies the digital output number.

* 'M64 P-' - turn on digital output immediately.
             The P- word specifies the digital output number.

* 'M65 P-' - turn off digital output immediately.
             The P- word specifies the digital output number.

The P-word ranges from 0 to a default value of 3. If needed the the
number of I/O can be increased by using the num_dio parameter when loading
the motion controller. See the Integrator's Manual Configuration Section LinuxCNC and
HAL section for more information.

The M62 & M63 commands will be queued. Subsequent commands referring
to the same output number will overwrite the older settings. More than
one output change can be specified by issuing more than one M62/M63
command.

The actual change of the specified outputs will happen at the
beginning of the next motion command. If there is no subsequent motion
command, the queued output changes won't happen. It's best to always
program a motion G code (G0, G1, etc) right after the M62/63.

M64 & M65 happen immediately as they are received by the motion
controller. They are not synchronized with movement, and they will
break blending.

[NOTE]
M62-65 will not function unless the appropriate motion.digital-out-nn pins are
connected in your hal file to outputs.

[[sec:M66-Input-Control]]
== M66 Wait on Input
(((M66 Input Control)))

----
M66 P- | E- <L->
----
* 'P-' - specifies the digital input number from 0 to 3.
* 'E-' - specifies the analog input number from 0 to 3.
* 'L-' - specifies the wait mode type.
**   'Mode 0: IMMEDIATE' - no waiting, returns immediately.
       The current value of the input is stored in parameter #5399
**   'Mode 1: RISE' - waits for the selected input to perform a rise event.
**   'Mode 2: FALL' - waits for the selected input to perform a fall event.
**   'Mode 3: HIGH' - waits for the selected input to go to the HIGH state.
**   'Mode 4: LOW' - waits for the selected input to go to the LOW state.
* 'Q-' - specifies the timeout in seconds for waiting. If the timeout is
         exceeded, the wait is interrupt, and the variable #5399 will be holding
         the value -1. The Q value is ignored if the L-word is zero (IMMEDIATE).
         A Q value of zero is an error if the L-word is non-zero.

* Mode 0 is the only one permitted for an analog input.

.M66 Example Lines
----
M66 P0 L3 Q5 (wait up to 5 seconds for digital input 0 to turn on)
----

M66 wait on an input stops further execution of the program, until the
selected event (or the programmed timeout) occurs.

It is an error to program M66 with both a P-word and an E-word (thus
selecting both an analog and a digital input).  In LinuxCNC these
inputs are not monitored in real time and thus should not be used for
timing-critical applications.

The number of I/O can be increased by using the num_dio or num_aio parameter
when loading the motion controller. See the Integrator's Manual, Core 
Components Section, Motion subsection, for more information.

[NOTE]
M66 will not function unless the appropriate motion.digital-in-nn pins or
motion.analog-in-nn pins are connected in your hal file to an input.

.Example HAL Connection
----
net signal-name motion.digital-in-00 <= parport.0.pin10-in
----

[[sec:M67-Analog-Output]]
== M67 Synchronized Analog Output
(((M67 Analog Motion Output Control)))

----
M67 E- Q-
----
* 'M67' - set an analog output synchronized with motion.
* 'E-' - output number ranging from 0 to 3.
* 'Q-' - is the value to set (set to 0 to turn off).

The actual change of the specified outputs will happen at the
beginning of the next motion command. If there is no subsequent motion
command, the queued output changes won't happen. It's best to always
program a motion G code (G0, G1, etc) right after the M67. M67 functions
the same as M62-63.

The number of I/O can be increased by using the num_dio or num_aio parameter
when loading the motion controller. See the Integrator's Manual, Core 
Components Section, Motion subsection, for more information. 

[NOTE]
M67 will not function unless the appropriate motion.analog-out-nn pins are
connected in your hal file to outputs.

[[sec:M68-Analog-Output]]
== M68 Analog Output
(((M68 Analog Aux Output Control)))

----
M68 E- Q-
----
* 'M68' - set an analog output immediately.
* 'E-' - output number ranging from 0 to 3.
* 'Q-' - is the value to set (set to 0 to turn off).

M68 output happen immediately as they are received by the motion
controller. They are not synchronized with movement, and they will
break blending. M68 functions the same as M64-65.

The number of I/O can be increased by using the num_dio or num_aio parameter
when loading the motion controller. See the Integrator's Manual, Core 
Components Section, Motion subsection, for more information.

[NOTE]
M68 will not function unless the appropriate motion.analog-out-nn pins are
connected in your hal file to outputs.

== M70 Save Modal State[[sec:M70-Save-Modal-State]]
(((M70 Save Modal State)))

To explicitly save the modal state at the current call level, program
'M70'. Once modal state has been saved with 'M70', it can be restored
to exactly that state by executing an 'M72'.

A pair of 'M70' and 'M72' instructions will typically be used to
protect a program against inadvertant modal changes within
subroutines.

[[saved_state_by_M70]]
The state saved consists of:

* current G20/G21 settings (imperial/metric)
* selected plane (G17/G18/G19 G17.1,G18.1,G19.1)
* status of cutter compensation (G40,G41,G42,G41.1,G42,1)
* distance mode - relative/absolute (G90/G91)
* feed mode (G93/G94,G95)
* current coordinate system (G54-G59.3)
* tool length compensation status (G43,G43.1,G49)
* retract mode (G98,G99)
* spindle mode (G96-css or G97-RPM)
* arc distance mode (G90.1, G91.1)
* lathe radius/diameter mode (G7,G8)
* path control mode (G61, G61.1, G64)
* current feed and speed ('F' and 'S' values)
* spindle status (M3,M4,M5) - on/off and direction
* mist (M7) and flood (M8) status
* speed override (M51) and feed override (M50) settings
* adaptive feed setting (M52)
* feed hold setting (M53)

Note that in particular, the motion mode (G1 etc) is NOT restored.

'current call level' means either:

 * executing in the main program. There is a single storage location
 for state at the main program level; if several 'M70' instructions
 are executed in turn, only the most recently saved state is restored
 when an 'M72' is executed.

 * executing within a G-code subroutine. The state saved with 'M70'
 within a subroutine behaves exactly like a local named parameter - it
 can be referred to only within this subroutine invocation with an
 'M72' and when the subroutine exits, the paramter goes away.

A recursive invocation of a subroutine introduces a new call level.

== M71 Invalidate Stored Modal State[[sec:M71-Invalidate-Stored-Modal-State]]
(((M71 Invalidate Stored Modal State)))

<<saved_state_by_M70,Modal state saved with an 'M70'>> or by an
<<sec:M73-Save-Autorestore-Modal-State, 'M73'>> at the current call
level is invalidated (cannot be restored from anymore).

A subsequent 'M72' at the same call level will fail.

If executed in a subroutine which protects modal state by an 'M73', a
subsequent return or endsub will *not* restore modal state.

The usefulness of this feature is dubious. It should not be relied upon as it might
go away.

== M72 Restore Modal State[[sec:M72-Restore-Modal-State]]
(((M72 Restore Modal State)))

<<saved_state_by_M70,Modal state saved with an 'M70'>> code can be
restored by executing an 'M72'.

The handling of G20/G21 is specially treated as feeds are interpreted
differently depending on G20/G21: if length units (mm/in) are about to
be changed by the restore operation, 'M72 'will restore the distance
mode first, and then all other state including feed to make sure the
feed value is interpreted in the correct unit setting.

It is an error to execute an 'M72' with no previous 'M70' save
operation at that level.

The following example demonstrates saving and explicitely restoring
modal state around a subroutine call using 'M70' and 'M72'. Note that
the 'imperialsub' subroutine is not "aware" of the M7x features and can be
used unmodified:

[source,{ngc}]
---------------------------------------------------------------------
O<showstate> sub
(DEBUG, imperial=#<_imperial> absolute=#<_absolute> feed=#<_feed> rpm=#<_rpm>)
O<showstate> endsub

O<imperialsub> sub
g20 (imperial)
g91 (relative mode)
F5 (low feed)
S300 (low rpm)
(debug, in subroutine, state now:)
o<showstate> call
O<imperialsub> endsub

; main program
g21 (metric)
g90 (absolute)
f200 (fast speed)
S2500 (high rpm)

(debug, in main, state now:)
o<showstate> call

M70 (save caller state in at global level)
O<imperialsub> call
M72 (explicitely restore state)

(debug, back in main, state now:)
o<showstate> call
m2
---------------------------------------------------------------------

== M73 Save and Autorestore Modal State[[sec:M73-Save-Autorestore-Modal-State]]
(((M73 Save and Autorestore Modal State)))

To save modal state within a subroutine, and restore state on
subroutine 'endsub' or any 'return' path, program 'M73'.

Aborting a running program in a subroutine which has an 'M73'
operation will *not* restore state .

Also, the normal end ('M2') of a main program which contains an 'M73'
will *not* restore state.

The suggested use is at the beginning of a O-word subroutine as in the
following example. Using 'M73' this way enables designing subroutines
which need to modify modal state but will protect the calling program
against inadvertant modal changes. Note the use of
<<sec:Predefined-Named-Parameters, predefined named parameters>> in
the 'showstate' subroutine.

[source,{ngc}]
---------------------------------------------------------------------
O<showstate> sub
(DEBUG, imperial=#<_imperial> absolute=#<_absolute> feed=#<_feed> rpm=#<_rpm>)
O<showstate> endsub

O<imperialsub> sub
M73 (save caller state in current call context, restore on return or endsub)
g20 (imperial)
g91 (relative mode)
F5 (low feed)
S300 (low rpm)
(debug, in subroutine, state now:)
o<showstate> call

; note - no M72 is needed here - the following endsub or an
; explicit 'return' will restore caller state
O<imperialsub> endsub

; main program
g21 (metric)
g90 (absolute)
f200 (fast speed)
S2500 (high rpm)
(debug, in main, state now:)
o<showstate> call
o<imperialsub> call
(debug, back in main, state now:)
o<showstate> call
m2
---------------------------------------------------------------------

=== Selectively restoring modal state by testing predefined parameters [[sec:Selectively-restoring-modal-state]]

Executing an 'M72' or returning from a subroutine which contains an
'M73' will restore <<saved_state_by_M70,*all* modal state saved>>.

If only some aspects of modal state should be preserved, an
alternative is the usage of <<sec:Predefined-Named-Parameters,
predefined named parameters>>, local parameters and conditional
statements. The idea is to remember the modes to be restored at the
beginning of the subroutine, and restore these before exiting. Here is
an example, based on snippet of 'nc_files/tool-length-probe.ngc':

[source,{ngc}]
---------------------------------------------------------------------
O<measure> sub   (measure reference tool)
;
#<absolute> = #<_absolute>  (remember in local variable if G90 was set)
;
g30 (above switch)
g38.2 z0 f15 (measure)
g91 g0z.2 (off the switch)
#1000=#5063 (save reference tool length)
(print,reference length is #1000)
;
O<restore_abs> if [#<absolute>]
    g90 (restore G90 only if it was set on entry:)
O<restore_abs> endif
;
O<measure> endsub

---------------------------------------------------------------------

[[sec:M100-to-M199]]
== M100 to M199 User Defined Commands
(((M100 to M199 User Defined Commands)))(((User Defined Commands M100-M199)))

----
M1-- <P- Q->
----

* 'M1--' - an integer in the range of 100 - 199.
* 'P-' - a number passed to the file as the first parameter.
* 'Q-' - a number passed to the file as the second parameter.

[NOTE]
After creating a new 'M1nn' file you must restart the GUI so it is aware
of the new file, otherwise you will get an 'Unkown m code' error.

The external program named 'M100' through 'M199' (no extension and a capitol M)
is executed with the optional P and Q values as its two arguments.
Execution of the G code file pauses until the external program exits.
Any valid executable file can be used. The file must be located in the search
path specificed in the ini file configuration. See the ini config section of
the Integrators Manual for more information on search paths.

[WARNING]
Do not use a word processor to create or edit the files. A word processor
will leave unseen codes that will cause problems and may prevent a bash or
python file from working. Use a text editor like Gedit in Ubuntu or Notepad++
in other operating systems to create or edit the files.

The error 'Unknown M code used' denotes one of the following

* The specified User Defined Command does not exist
* The file is not an executable file
* The file name has an extension
* The file name does not follow this format M1nn where nn = 00 through 99
* The file name used a lower case M

For example to open and close a collet closer that is controlled by a
parallel port pin using a bash script file using M101 and M102. Create two
files named M101 and M102. Set them as executable files (typically
right click/properties/permissions) before running LinuxCNC. Make sure the
parallel port pin is not connected to anything in a HAL file.

.M101 Example File
----
#!/bin/bash
# file to turn on parport pin 14 to open the collet closer
halcmd setp parport.0.pin-14-out True
exit 0
----

.M102 Example File
----
#!/bin/bash
# file to turn off parport pin 14 to open the collet closer
halcmd setp parport.0.pin-14-out False
exit 0
----

To pass a variable to a M1nn file you use the P and Q option like this:

----
M100 P123.456 Q321.654
----

.M100 Example file
----
#!/bin/bash
voltage=$1
feedrate=$2
halcmd setp thc.voltage $voltage
halcmd setp thc.feedrate $feedrate
exit 0
----

To display a graphic message and stop until the message window is closed
use a graphic display program like Eye of Gnome to display the graphic
file. When you close it the program will resume.

.M110 Example file
----
#!/bin/bash
eog /home/john/linuxcnc/nc_files/message.png
exit 0
----

To display a graphic message and continue processing the G code file 
suffix an ampersand to the command.

.M110 Example display and keep going
----
#!/bin/bash
eog /home/john/linuxcnc/nc_files/message.png &
exit 0
----

// vim: set syntax=asciidoc: