aboutsummaryrefslogtreecommitdiffstats
path: root/src/hal/components/plasmac.comp
blob: b8002c98c54cad5c91f0dffe0c2e619a98ee2168 (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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
component plasmac "A plasma cutter controller";

description
"""

A plasma cutting table control component for use with the LinuxCNC V2.8 or later.

.I VERSION:
.br
1.235

.I SUMMARY:
.br
Usage of this component is demonstrated in the QtPlasmaC example configurations included with LinuxCNC.

.I DISCLAIMER:
.br
THE AUTHOR OF THIS SOFTWARE ACCEPTS ABSOLUTELY NO LIABILITY FOR ANY HARM OR LOSS RESULTING FROM ITS USE.

IT IS EXTREMELY UNWISE TO RELY ON SOFTWARE ALONE FOR SAFETY.

Any machinery capable of harming persons must have provisions for completely stopping all motors and moving parts etc. before persons enter any danger area.

All machinery must be designed to comply with local and national safety codes, and the author of this software can not, and does not, take any responsibility for such compliance.
""";

/* INPUT PINS */
pin in  float   arc_fail_delay              "arc start failure timeout (seconds)";
pin in  float   arc_lost_delay              "arc lost delay during a cut (seconds)";
pin in  float   arc_ok_high                 "maximum voltage level for Arc OK signal [mode 0] (volts)";
pin in  bit     arc_ok_in                   "external arc ok input signal [mode 1 & mode 2]";
pin in  float   arc_ok_low                  "minimum voltage level for Arc OK signal [mode 0] (volts)";
pin in  s32     arc_max_starts              "maximum attempts at starting the arc";
pin in  float   arc_voltage_in              "arc voltage input [mode 0 & mode 1] see Notes above";
pin in  float   arc_voltage_offset          "offset to set arc voltage to 0 at 0 volts";
pin in  float   arc_voltage_scale           "scale to convert arc_voltage input to actual volts";
pin in  bit     thc_auto                    "enable automatic thc activation";
pin in  float   axis_x_max_limit            "axis x maximum limit, connect to ini.x.max-limit";
pin in  float   axis_x_min_limit            "axis x minimum limit, connect to ini.x.min-limit";
pin in  float   axis_x_position             "current x axis position, connect to axis.x.pos-cmd";
pin in  float   axis_y_max_limit            "axis y maximum limit, connect to ini.y.max-limit";
pin in  float   axis_y_min_limit            "axis y minimum limit, connect to ini.y.min-limit";
pin in  float   axis_y_position             "current y axis position, connect to axis.y.pos-cmd";
pin in  float   axis_z_max_limit            "axis z maximum limit, connect to ini.z.max-limit";
pin in  float   axis_z_min_limit            "axis z minimum limit, connect to ini.z.min-limit";
pin in  float   axis_z_position             "current z axis position, connect to joint.N.pos-fb";
pin in  bit     breakaway                   "torch breakaway switch (optional, see float_switch)";
pin in  bit     consumable_change           "change consumables in torch";
pin in  bit     cornerlock_enable           "enable corner lock";
pin in  float   cornerlock_threshold        "corner lock threshold (% of requested feed rate), speeds below this disable THC";
pin in  float   current_velocity            "current machine velocity, connect to motion.current-vel";
pin in  float   cut_feed_rate               "cut feed rate from current material (machine units per minute)";
pin in  float   cut_height                  "cut height (machine units)";
pin in  bit     cut_recovery                "recover from cut error";
pin in  float   cut_volts                   "cut voltage (volts)";
pin in  bit     cutting_start               "start a new cut, connect to spindle.0.on";
pin in  bit     debug_print                 "if true will print state changes as a debug aid";
pin in  bit     external_estop              "external E-stop input";
pin in  float   feed_override               "feed override value from GUI (connect to halui.feed-override.value)";
pin in  float   feed_reduction              "reduce adaptive feed to this percentage (connect to motion.analog-out-03)";
pin in  bit     float_switch                "float switch input (can also act as breakaway if it actuates when torch breaks away)";
pin in  float   float_switch_travel         "float switch travel (machine units)";
pin in  float   gcode_scale = 1             "current G-code scale";
pin in  float   height_override             "height override adjustment (volts)";
pin in  float   height_per_volt             "torch height change per volt (machine units)";
pin in  bit     homed                       "machine is homed";
pin in  bit     ignore_arc_ok_0             "don't require arc ok for start or cutting";
pin in  bit     ignore_arc_ok_1             "don't require arc ok for start or cutting";
pin in  float   kerf_width                  "placeholder for better G-code portability between GUIs";
pin in  bit     override_jog                "override jog inhibit";
pin in  bit     offset_set_probe            "deploy probe for setting offsets";
pin in  bit     offset_set_scribe           "deploy scribe for setting offsets";
pin in  s32     laser_recovery_start        "start laser offset for cut recovery";
pin in  s32     laser_x_offset              "alignment laser x axis offset (scaled units)";
pin in  s32     laser_y_offset              "alignment laser y axis offset (scaled units)";
pin in  float   lowpass_frequency           "lowpass cutoff frequency for arc voltage output";
pin in  bit     machine_is_on               "machine is on signal";
pin in  s32     max_offset = 5              "maximum height offset";
pin in  bit     mesh_arc_ok = FALSE         "don't require arc ok for mesh mode";
pin in  bit     mesh_enable                 "enable mesh cutting mode";
pin in  s32     mode                        "operating mode";
pin in  s32     motion_type                 "motion type, connect to motion.motion-type";
pin in  bit     move_down                   "external thc down switch [mode 2]";
pin in  bit     move_up                     "external thc up switch [mode 2]";
pin in  bit     multi_tool = 1              "allows the use of multiple tools";
pin in  float   offset_probe_delay          "wait for probe to deploy (seconds)";
pin in  float   offset_probe_x              "X axis offset for offset probe (machine units)";
pin in  float   offset_probe_y              "Y axis offset for offset probe (machine units)";
pin in  bit     offsets_active              "offsets are active, connect to motion.eoffsets-active";
pin in  s32     ohmic_sense_on_delay = 3    "debounce cycles for ohmic sense on";
pin in  s32     ohmic_sense_off_delay = 3   "debounce cycles for ohmic sense off";
pin in  bit     ohmic_sense_in              "ohmic sense relay input";
pin in  s32     ohmic_max_attempts          "maximum ohmic probe attempts before fallback to float switch";
pin in  bit     ohmic_probe                 "ohmic probe input, from ohmic-sense-out or external component/pin";
pin in  bit     ohmic_probe_enable          "enable ohmic probe";
pin in  float   ohmic_probe_offset          "Z axis offset for ohmic probe (machine units)";
pin in  bit     ohmic_test                  "test for shorted torch";
pin in  s32     ok_sample_counts = 10       "arc_ok number of valid samples required [mode 0]";
pin in  float   ok_sample_threshold = 10    "arc_ok maximum arc voltage deviation allowed [mode 0]";
pin in  float   pause_at_end                "time to pause at end of cut";
pin in  float   paused_motion_speed         "multiplier for speed of motion when paused, from -1 to 1";
pin in  float   pid_d_gain                  "derivative gain input [mode 0 & mode 1]";
pin in  float   pid_i_gain                  "integral gain input [mode 0 & mode 1]";
pin in  float   pid_p_gain                  "proportional gain input [mode 0 & mode 1]";
pin in  float   pierce_delay                "time required to pierce stock (seconds)";
pin in  float   probe_feed_rate             "probe down velocity (machine units per minute)";
pin in  s32     probe_final_speed = 1       "final probe speed (steps per servo period)";
pin in  float   pierce_height               "pierce height (machine units)";
pin in  float   probe_start_height          "probe starting height";
pin in  bit     probe_test                  "probe test only";
pin in  bit     program_is_idle             "program is idle, connect to halui.program.is-idle";
pin in  bit     program_is_paused           "program is paused, connect to halui.program.is-paused";
pin in  bit     program_is_running          "program is running, connect to halui.program.is-running";
pin in  float   puddle_jump_delay           "Delay move from pierce height to cut height (seconds), leave disconnected if not required.";
pin in  float   puddle_jump_height          "Puddle jump height (percentage of pierce height), leave disconnected if not required.";
/* requested_velocity is deprecated in favour of feed_upm, the pin is kept so as not to break existing configs */
pin in  float   requested_velocity          "deprecated";
pin in  float   feed_upm                    "requested feed_rate, connect to motion.feed-upm to use as the default (G-code units per minute)";
pin in  s32     resolution = 100            "multiplier for resolution of the offset counts";
pin in  float   restart_delay               "time from arc failure till next restart attempt";
pin in  float   safe_height                 "requested safe traverse height (machine units)";
pin in  float   scribe_arm_delay            "delay from scribe arm to scribe on";
pin in  float   scribe_on_delay             "delay from scribe on to motion beginning";
pin in  bit     scribe_start                "start a new scribe, connect to spindle.1.on";
pin in  float   setup_feed_rate             "feed rate for moves to pierce and cut heights (machine units per minute)";
pin in  float   skip_ihs_distance           "skip IHS if less than this distance from last cut";
pin in  bit     spotting_start              "start a new spot, connect to spindle.2.on";
pin in  float   spotting_threshold          "threshold voltage to start spotting delay";
pin in  float   spotting_time               "torch off delay after spotting threshold reached";
pin in  float   thc_delay                   "delay from cut feed rate reached to THC activate (seconds) [non auto THC]";
pin in  bit     thc_disable                 "thc disable";
pin in  bit     thc_enable                  "enable/disable thc and set the IHS skip type";
pin in  float   thc_feed_rate               "maximum feed rate for thc (machine units per minute)";
pin in  s32     thc_sample_counts = 50      "thc number of valid samples required [auto THC]";
pin in  float   thc_sample_threshold = 1    "thc maximum arc voltage deviation allowed [auto THC]";
pin in  float   thc_threshold               "thc threshold (volts), changes below this have no effect";
pin in  bit     torch_enable                "enable torch";
pin in  bit     torch_off                   "turn torch off";
pin in  bit     torch_pulse_start           "torch pulse start";
pin in  float   torch_pulse_time            "torch pulse time (seconds)";
pin in  float   units_per_mm                "for scale calcs, connect to halui.machine.units-per-mm";
pin in  bit     use_auto_volts              "use calculated voltage for thc baseline";
pin in  bit     voidlock_enable             "enable voidlock [mode 0 & mode 1]";
pin in  s32     voidlock_on_cycles = 2      "number of sampling cycles to activate voidlock ";
pin in  s32     voidlock_off_cycles = 10    "number of sampling cycles to deactivate voidlock";
pin in  s32     voidlock_slope = 500        "voidlock slope in volts per second";
pin in  s32     x_offset                    "offset to apply to axis x for consumable change and cut recovery (scaled units)";
pin in  float   x_offset_current            "current x axis offset, connect to axis.x.eoffset";
pin in  float   xy_feed_rate                "feed-rate for consumable change";
pin in  s32     y_offset                    "offset to apply to axis y for consumable change and cut recovery (scaled units)";
pin in  float   y_offset_current            "current z axis offset, connect to axis.y.eoffset";
pin in  float   z_offset_current            "current z axis offset, connect to axis.z.eoffset";
pin in  float   zero_window = 0.1           "sets window that voltage fluctuations show as zero (-0.1 to 0.1 at default value)";

/* OUTPUT PINS */
pin out float   adaptive_feed               "for reverse-run, connect to motion.adaptive-feed";
pin out bit     arc_ok_out                  "arc ok output";
pin out float   arc_voltage_out             "arc voltage output [mode 0 & mode 1]";
pin out bit     consumable_changing         "consumables are being changed";
pin out bit     cornerlock_is_locked        "corner locked indicator";
pin out float   cut_length                  "length of current cut job";
pin out bit     cut_recovering              "recovering from cut error";
pin out float   cut_time                    "cut time of current job";
pin out bit     cutting_stop                "stop manual cut, connect to halui.spindle.0.stop";
pin out bit     feed_hold                   "feed hold, connect to motion.feed-hold";
pin out bit     jog_inhibit                 "jog inhibit, connect to motion.jog-inhibit";
pin out s32     laser_recovery_state        "laser recovery status";
pin out bit     led_down                    "thc move down indicator";
pin out bit     led_up                      "thc move up indicator";
pin out float   offset_scale                "offset scale, connect to axis.<x y z>.eoffset-scale";
pin out bit     ohmic_enable                "on only while probing";
pin out bit     ohmic_sense_out             "ohmic sense output state";
pin out bit     paused_motion               "paused motion flag, true when paused motion is active";
pin out float   paused_time                 "paused time during current job";
pin out s32     pierce_count                "number of pierce attempts (torch starts)";
pin out bit     probe_test_error            "minimum limit reached while probe testing";
pin out float   probe_time                  "probe time of current job";
pin out bit     program_pause               "pause the current program, connect to halui.program.pause";
pin out bit     program_resume              "resume the currently paused program, connect to halui.program.resume";
pin out bit     program_run                 "run the currently loaded program, connect to halui.program.run";
pin out bit     program_stop                "stop current program, connect to halui.program.stop";
pin out float   rapid_time                  "rapid motion time of current job";
pin out float   run_time                    "run time of current job";
pin out bit     safe_height_is_limited      "safe height is limited indicator";
pin out bit     sensor_active               "one of float, ohmic, or breakaway is detected";
pin out bit     scribe_arm                  "arm the scribe";
pin out bit     scribe_on                   "turn scribe on";
pin out s32     state_out                   "current state";
pin out s32     stop_type_out               "current stop type";
pin out bit     thc_active                  "thc status output";
pin out bit     thc_enabled                 "thc is enabled";
pin out bit     torch_on                    "turn torch on, connect to your torch on input";
pin out float   torch_time                  "torch on time of current job";
pin out bit     voidlock_is_locked          "voidlock is locked indicator [mode 0 & mode 1]";
pin out s32     x_offset_counts             "x offset for consumable change, connect to axis.x.eoffset-counts";
pin out bit     xy_offset_enable            "enable x and y offsets, connect to axis.<x & y>.eoffset-enable";
pin out s32     y_offset_counts             "y offset for consumable change, connect to axis.y.eoffset-counts";
pin out float   z_height                    "current z axis height relative to the probed zero height";
pin out s32     z_offset_counts             "z offset for height control, connect to axis.z.eoffset-counts";
pin out bit     z_offset_enable             "enable z offsets, connect to axis.z.eoffset-enable";
pin out float   z_relative                  "distance of Z from last probed height";

// temp for auto-thc testing
// check when we at "at speed"
pin out float   current_feed_rate           "current feed rate per minute";
pin out float   requested_feed_rate         "requested feed rate";

/* VARIABLES */
variable double angle_x_y;                  /* angle for x/y velocity calcs in radians*/
variable float  arc_fail_timer;             /* arc failure timer */
variable float  arc_lost_timer;             /* arc lost timer */
variable int    arc_starts;                 /* number of attempts to start torch */
variable float  arc_valid_timer;            /* timer to wait for valid arc_voltage_in */
variable float  arc_voltage_raw;            /* filtered arc voltage to be scaled */
variable int    arc_voltage_buffer_index;   /* arc voltage sampler write index */
variable int    ok_sampler_samples;         /* required number of arc ok samples [mode0] */
variable bool   auto_cut;                   /* auto cut mode is active */
variable float  axis_x_finish;              /* axis x position at end of cut */
variable float  axis_x_start;               /* axis x position at start of cut */
variable float  axis_y_finish;              /* axis y position at end of cut */
variable float  axis_y_start;               /* axis y position at start of cut */
variable int    cons_change_clear;          /* consumable change is clearing */
variable int    count;                      /* for counting */
variable int    cut_height_first;           /* cut height at start of cut */
variable int    cut_height_last;            /* cut height at end of cut */
variable float  cut_offset;                 /* offset from last cut end to this cut start */
variable bool   cut_started;                /* cut has started */
variable int    cut_target;                 /* cut height target offset */
variable bool   error_message;              /* 1 if error message has been sent */
variable bool   first_cut_finished;         /* first cut is complete */
variable bool   float_detected;             /* float switch detected */
variable int    height_ovr_counts;          /* number of counts to change height via override */
variable float  height_ovr_old;             /* old height override value */
variable bool   initialized;                /* initialization flag */
variable int    laser_x_target;             /* target count for laser recovery x offset */
variable int    laser_y_target;             /* target count for laser recovery y offset */
variable float  last_arc_voltage;           /* last sensed arc voltage */
variable bool   manual_cut;                 /* manual cut mode is active */
variable int    offset_datum;               /* datum for safe height calcs */
variable int    offset_max;                 /* maximum allowed offset */
variable int    offset_min;                 /* minimum allowed offset */
variable float  offset_probe_timer;         /* offset probe deployment delay time */
variable bool   offset_probing;             /* offset probing is enabled */
variable int    offset_res;                 /* resolution for comparing offset move to target*/
variable bool   offset_set;                 /* probe or scribe is being offset*/
variable int    ohmic_attempts;             /* current ohmic probe attempts */
variable bool   ohmic_detected;             /* true if ohmic probe detected */
variable int    ohmic_sense_counts;         /* ohmic sense debounce counter */
variable int    old_resolution;             /* previous resolution value */
variable int    op_x_start;                 /* x offset when offset probing started */
variable int    op_x_target;                /* target offset for x probe offset */
variable int    op_x_velocity;              /* velocity for x probe offset */
variable double op_xy_angle;                /* angle for x/y probe offset calcs in radians*/
variable int    op_y_start;                 /* y offset when offset probing started */
variable int    op_y_target;                /* target offset for y probe offset */
variable int    op_y_velocity;              /* velocity for y probe offset */
variable float  pause_at_end_timer;         /* pause at end of cut timer */
variable float  paused_motion_timer;        /* minimum run timer for paused motion */
variable float  pid_error_now;              /* current error for pid calcs */
variable float  pid_error_old;              /* old error for pid calcs */
variable float  pid_output;                 /* calculated pid output value */
variable int    pierce_target;              /* pierce height target offset */
variable float  pierce_timer;               /* pierce delay timer */
variable float  probe_at_bottom;            /* probe is at bottom limit */
variable bool   probe_inhibit;              /* inhibit probing */
variable int    probe_offset;               /* offset for active probe */
variable bool   probe_required = 1;         /* a probe sequence is required */
variable bool   probe_retry;                /* probe will retry after contact before probe height */
variable int    probe_start_target;         /* probe start height target */
variable bool   probe_testing;              /* probe test active */
variable int    probe_velocity;             /* probe down velocity */
variable int    puddle_jump_percent;        /* puddle jump height as percentage of pierce height */
variable int    puddle_jump_target;         /* puddle jump height target offset */
variable float  puddle_jump_timer;          /* puddle jump delay timer */
variable float  recovery_velocity;          /* cut recovery step velocity */
// temp for auto-thc testing
// created a out pin @ L204 to check when we at "at speed"
//variable float  requested_feed_rate;        /* requested feed rate */
variable int    res;                        /* resolution of offset move*/
variable float  restart_timer;              /* time between torch on attempts*/
variable int    safe_alarm;                 /* warn if safe_available falls below this value during a cut */
variable int    safe_available;             /* available safe height */
variable int    safe_min;                   /* minimum safe height allowed */
variable int    safe_preferred;             /* preferred safe height offset */
variable int    safe_target;                /* safe height target offset */
variable int    setup_velocity;             /* velocity for setup moves */
variable float  scribe_arm_timer;           /* scribe timer from arm to on*/
variable float  scribe_on_timer;            /* scribe timer from on to motion*/
variable bool   scribe_pause;               /* scribe pause flag */
variable bool   spotting;                   /* spotting flag */
variable float  spotting_timer;             /* spotting timer */
variable int    state_old = -1;             /* old state */
variable int    statistics_reset;           /* statistics are reset */
//temp for testing forum post #210558
//variable int    target_samples = 6;         /* number of samples for setting target_volts */
variable float  target_total;               /* total voltage of samples for setting target_volts */
//temp for testing forum post #210558
//variable float  target_volts;               /* target voltage for thc, set by arc voltage at cut height */
variable bool   thc_activated;              /* thc was activated */
variable float  thc_activated_timer;        /* timer to reset thc_activated - end of cut is detected from userspace :( */
variable float  thc_delay_timer;            /* thc delay timer [non auto THC] */
variable int    thc_sampler_samples;        /* required number of thc samples  [auto THC] */
variable int    thc_velocity;               /* velocity for thc moves */
variable float  torch_off_timer;            /* arc off delay timer */
variable float  torch_pulse_timer;          /* torch pulse timer */
variable float  velocity_scale;             /* the velocity multiplier */
variable float  voidlock_change;            /* voltage change this cycle */
variable float  voidlock_off_count;         /* current count of voidlock deactivate cycles */
variable int    voidlock_on_count;          /* current count of voidlock activate cycles */
variable float  voidlock_threshold;         /* voidlock threshold voltage per cycle */
variable int    x_velocity;                 /* velocity for x motion for consumable change */
variable int    y_velocity;                 /* velocity for y motion for consumable change */
variable int    z_max;                      /* max height for testing against pierce height*/
variable int    z_pierce;                   /* pierce height for testing against max height*/
variable int    zero_target;                /* zero height target offset */

// temp for testing forum post #210558
// https://forum.linuxcnc.org/plasmac/42690-is-this-qtplasmac-expected-behaviour-cycle-start-and-jog-disabled?start=0#210558
pin in  s32     low_cut_volts               "low cut voltage threshold while thc active";
pin in  s32     target_samples = 6          "number of samples for setting target_volts";
pin out float   target_volts                "target voltage for thc, set by arc voltage at cut height";


function _;

author "Phillip A Carter & Gregory D Carl";

license "GPLv2 or greater";

option singleton yes;

;;

#include "rtapi_math.h"

typedef enum{IDLE,
             PROBE_HEIGHT,
             PROBE_DOWN,
             PROBE_UP,
             ZERO_HEIGHT,
             PIERCE_HEIGHT,
             TORCH_ON,
             ARC_OK,
             PIERCE_DELAY,
             PUDDLE_JUMP,
             CUT_HEIGHT,
             CUT_MODE_01,
             CUT_MODE_2,
             PAUSE_AT_END,
             SAFE_HEIGHT,
             MAX_HEIGHT,
             END_CUT,
             END_JOB,
             TORCHPULSE,
             PAUSED_MOTION,
             OHMIC_TEST,
             PROBE_TEST,
             SCRIBING,
             CONSUMABLE_CHANGE_ON,
             CONSUMABLE_CHANGE_OFF,
             CUT_RECOVERY_ON,
             CUT_RECOVERY_OFF,
             DEBUG,
            } state_t;

typedef enum{NONE,
             STOP,
             WAIT,
             PAUSE,
            } stop_type_t;

typedef enum{ZERO,
             UP,
             DOWN,
            } move_direction_t;

typedef enum{FLOAT,
             OHMIC,
            } probe_type_t;

typedef enum{EMPTY,
             CUTTING,
             SCRIBE,
             SPOTTING,
            } tool_t;

typedef enum{OFF,
             SET,
             SETTING,
             ON,
             RESET,
             RESETTING,
         } laser_recovery_state_t;

state_t state = IDLE;
stop_type_t stop_type = NONE;
move_direction_t move_direction = ZERO;
probe_type_t probe_type = FLOAT;
tool_t tool = EMPTY;
laser_recovery_state_t laser_recovery = OFF;

/* setup the arc voltage ring buffer */
#define BUFFERSIZE 1000
#define READ_OK 0
#define READ_THC 1
float arc_voltage_buffer[BUFFERSIZE];

/* read the arc voltage buffer */
float read_arc_voltage_buffer(int caller, int size, int index, float target, float threshold){
    int reads = 0;
    double sum = 0;
    int errors = 0;
    while(reads < size){
        if (arc_voltage_buffer[index] < target - threshold || arc_voltage_buffer[index] > target + threshold){
            if(caller == READ_THC){
                return 0;
            }else{
                errors++;
            }
        }
        sum += arc_voltage_buffer[index];
        if(index == 0){
            index = BUFFERSIZE;
        }else{
            index--;
        }
        reads++;
    }
    if(caller == READ_THC){
        return(sum / reads);
    }else{
        return(errors);
    }
}

/* set an axis eoffset position at a set velocity */
int offset_move(int offset, int vel, int target){
    if(offset - vel > target){
        offset -= vel;
    }else if(offset + vel < target){
        offset += vel;
    }else{
        offset = target;
    }
    return offset;
}

FUNCTION(_) {

    /* set message level */
    if(!debug_print && rtapi_get_msg_level() != 1){
        rtapi_set_msg_level(1);
    }else if(debug_print && rtapi_get_msg_level() != 3){
        rtapi_set_msg_level(3);
    }

    /* validate ok sample size for mode 0 */
    if(mode == 0){
        if(ok_sample_counts < 1){
            ok_sampler_samples = 1;
        }else if(ok_sample_counts > BUFFERSIZE){
            ok_sampler_samples = BUFFERSIZE;
        }else{
            ok_sampler_samples = ok_sample_counts;
        }
    }

    /* validate thc sample size */
    if(mode < 2){
        if(thc_sample_counts < 1){
            thc_sampler_samples = 1;
        }else if(thc_sample_counts > BUFFERSIZE){
            thc_sampler_samples = BUFFERSIZE;
        }else{
            thc_sampler_samples = thc_sample_counts;
        }
    }

    /* wait until arc_voltage_in is valid */
    if(isinf(arc_voltage_in) || isnan(arc_voltage_in)){
        arc_valid_timer += fperiod;
        if(arc_valid_timer > 1){
            rtapi_print_msg(RTAPI_MSG_ERR,"invalid arc-voltage-in\n");
            arc_valid_timer = 0;
        }
        return;
    }

    /* set scaling */
    if(resolution > 1000){
        res = 1000;
    }else if(resolution < 1){
        res = 1;
    }else{
        res = resolution;
    }
    if(units_per_mm == 1){
        offset_res = res;
    }else{
        offset_res = res * 10;
    }
    offset_scale = units_per_mm * fperiod / res;
    velocity_scale = res / units_per_mm / 60;
    recovery_velocity = cut_feed_rate * velocity_scale * 0.5;

    /* set the active tool */
    if(cutting_start){ //  this allows M3 for cutting as well as M3 $0
        tool = CUTTING;
    }else if(multi_tool && scribe_start && !cutting_start && !spotting_start){
        tool = SCRIBE;
    }else if(multi_tool && spotting_start && !cutting_start && !scribe_start){
        tool = SPOTTING;
    }else{
        tool = EMPTY;
    }

    /* check for active sensor */
    if(breakaway || float_switch || ohmic_detected){
        sensor_active = TRUE;
    }else{
        sensor_active = FALSE;
    }


// I don't recall why z_relative was originally created!!!
// commit # b29d1af 24 Oct 2019
//    /* output the relative Z height */
//    if(zero_target){
//        z_relative = z_offset_current - zero_target * offset_scale;
//    }else{
//        z_relative = axis_z_position - axis_z_min_limit;
//    }

// this height calculation is really only the target z height, we may not have reached the target yet
    /* output current z height */
//    z_height = (z_offset_counts - zero_target) * offset_scale;

// this is probably more correct
// we don't use z_relative anywhere so we could probably just rename it to z_height and remove the z_relative pin.
    /* output the relative Z height */
    if(zero_target){
        z_height = z_offset_current - (zero_target * offset_scale);
    }else{
        z_height = axis_z_position - axis_z_min_limit;
    }

// temp for auto-thc testing
//  check when we at "at speed"
    current_feed_rate = current_velocity * 60;

    /* convert feed rates to velocity */
    setup_velocity = setup_feed_rate * velocity_scale;
    if(probe_feed_rate < setup_feed_rate){
        probe_velocity = probe_feed_rate * velocity_scale;
    }else{
        probe_velocity = setup_velocity;
    }
    if(mode == 2){
        thc_velocity = thc_feed_rate * velocity_scale * pid_p_gain / 100;
    }else{
        thc_velocity = thc_feed_rate * velocity_scale;
    }
    if(thc_velocity < 1){
        thc_velocity = 1;
    }

    /* turn torch off if torch off timer completed */
    if(torch_off_timer > 0){
        torch_off_timer -= fperiod;
        if(torch_off_timer <= 0){
            torch_on = FALSE;
            torch_off_timer = 0;
        }
    }

    /* turn torch off from external input */
    if(torch_off){
        torch_on = FALSE;
    }

    /* set THC state */
    thc_enabled = (thc_enable && !thc_disable && !mesh_enable && !ignore_arc_ok_0 && !ignore_arc_ok_1 && (arc_lost_timer > arc_lost_delay - 0.000001) ? 1:0);

    /* set THC status */
    thc_active = (state == CUT_MODE_01 || state == CUT_MODE_2) && ((mode < 2 && target_volts) || (mode == 2 && arc_ok_out)) && thc_enabled && !cornerlock_is_locked && !voidlock_is_locked ? 1:0;
    if(thc_active){
        thc_activated = TRUE;
        thc_activated_timer = 0.25;
    }else if(thc_activated && (state == CUT_MODE_01 || state == CUT_MODE_2)){
        thc_activated_timer -= fperiod;
        if(thc_activated_timer <= 0){
            thc_activated = FALSE;
        }
    }else if(probe_required){
        thc_activated = FALSE;
    }

    /* set ohmic probe state */
    ohmic_detected = ohmic_probe && ohmic_probe_enable;

    /* set adaptive feed reduction if no paused motion */
    if(state != PAUSED_MOTION){
        if(feed_reduction < 10 && (state == CUT_MODE_01 || state == CUT_MODE_2)){
            adaptive_feed = 1;
        }else if(feed_reduction < 100 && (state == CUT_MODE_01 || state == CUT_MODE_2)){
            adaptive_feed = feed_reduction * 0.01;
        }else{
            adaptive_feed = 1;
        }
    }

    /* check for a manual cut */
    if(tool == CUTTING && !auto_cut){
        manual_cut = TRUE;
    }

    /* check for an abort */
    /* or for a pause or wait while active */
    if(auto_cut){
        if(state == IDLE && sensor_active && !program_is_idle){
            torch_on = FALSE;
            if(!program_is_paused && !program_pause){
                if(breakaway){
                    rtapi_print_msg(RTAPI_MSG_ERR,"breakaway switch activated\n"
                                                  "program is paused.\n");
                }else if(float_switch){
                    rtapi_print_msg(RTAPI_MSG_ERR,"float switch activated\n"
                                                  "program is paused.\n");
                }else if(ohmic_detected){
                    rtapi_print_msg(RTAPI_MSG_ERR,"ohmic probe activated\n"
                                                  "program is paused.\n");
                }
            }
            program_pause = TRUE;
            if(stop_type != WAIT){
                stop_type = PAUSE;
            }
            probe_required = TRUE;
            probe_inhibit = TRUE;
            state = MAX_HEIGHT;
        }else if(!probe_test && program_is_idle && cut_started && !program_run){
            if(consumable_changing){
                state = CONSUMABLE_CHANGE_OFF;
            }else if(cut_recovering){
                state = CUT_RECOVERY_OFF;
            }else{
                if(!pause_at_end){
                    torch_on = FALSE;
                }
                stop_type = STOP;
                program_stop = TRUE;
                cut_started = FALSE;
                probe_required = TRUE;
                axis_x_finish = 0;
                axis_y_finish = 0;
                if(pause_at_end_timer){
                    state = PAUSE_AT_END;
                }else{
                    state = MAX_HEIGHT;
                }
            }
        }else if(!probe_test && state > IDLE && state <= CUT_MODE_2 && stop_type == NONE && cut_started){
            if(program_is_paused){
                torch_on = FALSE;
                stop_type = PAUSE;
                probe_required = TRUE;
                pause_at_end_timer = pause_at_end;
                state = MAX_HEIGHT;
            }else if(tool == EMPTY){
                if(!pause_at_end){
                    torch_on = FALSE;
                }
                stop_type = WAIT;
                if(thc_enabled || !torch_enable){
                    axis_x_finish = axis_x_position;
                    axis_y_finish = axis_y_position;
                }else{
                    axis_x_finish = 0;
                    axis_y_finish = 0;
                }
                pause_at_end_timer = pause_at_end;
                state = PAUSE_AT_END;
            }else if(breakaway){
                torch_on = FALSE;
                program_pause = TRUE;
                stop_type = PAUSE;
                probe_required = TRUE;
                rtapi_print_msg(RTAPI_MSG_ERR,"breakaway switch activated\n"
                                              "program is paused.\n");
                probe_inhibit = TRUE;
                state = MAX_HEIGHT;
            }else if(state > ARC_OK && float_switch){
                torch_on = FALSE;
                program_pause = TRUE;
                stop_type = PAUSE;
                probe_required = TRUE;
                rtapi_print_msg(RTAPI_MSG_ERR,"float switch activated\n"
                                              "program is paused.\n");
                probe_inhibit = TRUE;
                state = MAX_HEIGHT;
            }else if(state > ARC_OK && ohmic_detected){
                torch_on = FALSE;
                program_pause = TRUE;
                stop_type = PAUSE;
                probe_required = TRUE;
                rtapi_print_msg(RTAPI_MSG_ERR,"ohmic probe activated\n"
                                              "program is paused.\n");
                probe_inhibit = TRUE;
                state = MAX_HEIGHT;
            }else if(state > ARC_OK && !arc_ok_out && torch_enable && !torch_off && !(mesh_enable || ignore_arc_ok_0 || ignore_arc_ok_1)){ ;
                torch_on = FALSE;
                program_pause = TRUE;
                stop_type = PAUSE;
                probe_required = TRUE;
                rtapi_print_msg(RTAPI_MSG_ERR,"valid arc lost\n"
                                              "program is paused.\n");
                state = MAX_HEIGHT;
            }
        }
    }else if(manual_cut){
        if(tool == EMPTY){
            manual_cut = FALSE;
            torch_on = FALSE;
            stop_type = STOP;
            program_stop = TRUE;
            cut_started = FALSE;
            probe_required = TRUE;
            axis_x_finish = 0;
            axis_y_finish = 0;
            state = MAX_HEIGHT;
        }
    }

    /* calculate arc voltage */
    if(lowpass_frequency){
        arc_voltage_raw += (arc_voltage_in - arc_voltage_raw) * (1 - exp(-(2 * M_PI * fabs(lowpass_frequency)) * fperiod));
    }else{
        arc_voltage_raw = arc_voltage_in;
    }
    arc_voltage_out = round(((fabs(arc_voltage_raw) - arc_voltage_offset) * fabs(arc_voltage_scale)) * 1000) / 1000;
    if(arc_voltage_out < zero_window && arc_voltage_out > zero_window * -1){
        arc_voltage_out = 0;
    }

    /* temporary for forum issue #210558 */
    if(thc_active && low_cut_volts && arc_voltage_out < low_cut_volts){
        torch_on = FALSE;
        program_pause = TRUE;
        stop_type = PAUSE;
        probe_required = TRUE;
        rtapi_print_msg(RTAPI_MSG_ERR,"arc voltage fell below safe level while THC active\n"
                                      "target voltage = %f\n"
                                      "low cut volts = %d\n"
                                      "arc voltage = %f\n"
                                      "program is paused.\n", target_volts, low_cut_volts, arc_voltage_out);
        probe_inhibit = TRUE;
        state = MAX_HEIGHT;
    }

    /* ohmic sensing */
    if(ohmic_enable){
        if(ohmic_sense_in){
            if(ohmic_sense_counts < ohmic_sense_on_delay){
                ohmic_sense_counts += 1;
            }else{
                ohmic_sense_out = TRUE;
                ohmic_sense_counts = 0;
            }
        }else if(!ohmic_sense_in && ohmic_sense_out){
            if(ohmic_sense_counts < ohmic_sense_off_delay){
                ohmic_sense_counts += 1;
            }else{
                ohmic_sense_out = FALSE;
                ohmic_sense_counts = 0;
            }
        }else{
            ohmic_sense_counts = 0;
        }
    }else{
        ohmic_sense_out = FALSE;
        ohmic_sense_counts = 0;
    }

    /* offset probing deployment timer*/
    if(ohmic_probe_enable && (offset_probe_x || offset_probe_y)){
        offset_probing = TRUE;
    }else{
        offset_probing = FALSE;
    }
    if(offset_probing && (ohmic_enable || state == PIERCE_HEIGHT)){
        if(offset_probe_timer < offset_probe_delay){
            offset_probe_timer += fperiod;
        }
    }else{
        offset_probe_timer = 0;
    }

    /* set arc ok from either arc ok input of from actual arc voltage
     * if using arc ok input, set arc_ok_low_in and/or arc_ok_high_in to 0 */
    if(torch_on){
        torch_time += fperiod;
        /* add the current arc voltage to the arc voltage buffer */
        arc_voltage_buffer[arc_voltage_buffer_index] = arc_voltage_out;
        if (arc_voltage_buffer_index == BUFFERSIZE) {
            arc_voltage_buffer_index = 0;
        }else{
            arc_voltage_buffer_index++;
        }
        /* set from arc ok input */
        if(mode > 0){
            if(arc_ok_in){
                arc_ok_out = TRUE;
                arc_lost_timer = arc_lost_delay;
            }else if(arc_lost_timer > 0){
                arc_lost_timer -= fperiod;
            }else{
                arc_ok_out = FALSE;
            }
        /* synthesised from arc voltage */
        }else{
            /* stable voltages required to set arc_ok */
            if(!arc_ok_out){
                if(arc_voltage_out >= arc_ok_low &&
                   arc_voltage_out <= arc_ok_high &&
                    !read_arc_voltage_buffer(READ_OK, ok_sampler_samples, arc_voltage_buffer_index - 1, arc_voltage_out, ok_sample_threshold)){
                arc_ok_out = TRUE;
                arc_lost_timer = arc_lost_delay;
                }
            /* stable voltages required to maintain arc_ok */
            }else if((arc_voltage_out < arc_ok_low || arc_voltage_out > arc_ok_high) &&
                    read_arc_voltage_buffer(READ_OK, ok_sampler_samples, arc_voltage_buffer_index - 1, target_volts, ok_sample_threshold) >= ok_sampler_samples){
                if(arc_lost_timer <= 0){
                    arc_ok_out = FALSE;
                }else{
                    arc_lost_timer -= fperiod;
                }
            }
        }
    /* unset arc ok */
    }else{
        arc_lost_timer = arc_lost_delay;
        arc_ok_out = FALSE;
    }

    /* reset program states */
    if(program_is_idle){
        program_stop = FALSE;
        program_resume = FALSE;
        statistics_reset = FALSE;
    }else if(program_is_paused){
        program_pause = FALSE;
        paused_time += fperiod;
        run_time += fperiod;
    }else if(program_is_running){
        program_run = FALSE;
        program_resume = FALSE;
        auto_cut = TRUE;
        run_time += fperiod;
        if(motion_type == 1){
            rapid_time += fperiod;
        }
        if(!statistics_reset){
            cut_length = 0;
            cut_time = 0;
            paused_time = 0;
            pierce_count = 0;
            probe_time = 0;
            rapid_time = 0;
            run_time = 0;
            torch_time = 0;
            statistics_reset = TRUE;
        }
    }

    /* if puddlejump height is 0 then set it to 100 */
    puddle_jump_percent = puddle_jump_height == 0 ? 100:puddle_jump_height;

    /* set jog inhibit if required */
    if(sensor_active && program_is_idle && !(override_jog || state == PROBE_DOWN || state == PROBE_UP)){
        jog_inhibit = TRUE;
    }else{
        jog_inhibit = FALSE;
    }

    /* probe and scribe deployment for setting offsets */
    if(offset_set_probe && !offset_set && homed && program_is_idle && state == IDLE){
        offset_set = TRUE;
        ohmic_enable = TRUE;
    }else if(offset_set_scribe && !offset_set && homed && program_is_idle && state == IDLE){
        offset_set = TRUE;
        scribe_arm = TRUE;
    }else if(offset_set && !offset_set_probe && !offset_set_scribe){
        offset_set = FALSE;
        ohmic_enable = FALSE;
        scribe_arm = FALSE;
    }

    if(!machine_is_on){
        /* if machine is off */
        x_offset_counts = 0;
        y_offset_counts = 0;
        z_offset_counts = 0;
        stop_type = NONE;
        cut_started = FALSE;
        probe_required = TRUE;
        program_stop = FALSE;
        program_resume = FALSE;
        program_pause = FALSE;
        torch_on = FALSE;
        torch_pulse_timer = 0;
        state = IDLE;
    }else{
        /* if machine is on */
        switch(state){
            case IDLE:
//                if(!consumable_changing){
//                    xy_offset_enable = FALSE;
//                }
//                if(!offsets_active && program_is_idle){
//                    z_offset_enable = FALSE;
//                }
                if(probe_inhibit && !float_switch && !breakaway && !ohmic_detected){
                    probe_inhibit = FALSE;
                }else if(!probe_inhibit){
                    /* if we get a paused motion request and we are paused */
                    if(paused_motion_speed && (stop_type == PAUSE || stop_type == WAIT)){
                        state = PAUSED_MOTION;
                    /* if we get a consumable change start request and we are paused */
                    }else if(consumable_change && (stop_type == PAUSE || stop_type == WAIT) && !consumable_changing){
                        state = MAX_HEIGHT;
                        /* if we get a consumable change stop request and we are changing consumables */
                    }else if(!consumable_change && (stop_type == PAUSE || stop_type == WAIT) && consumable_changing){
                        state = CONSUMABLE_CHANGE_OFF;
                    /* if we get a cut recovery start request and we are paused */
                    }else if(cut_recovery && stop_type == PAUSE && (!cut_recovering || laser_recovery_start)){
                        state = CUT_RECOVERY_ON;
                    /* if we get a torch start request and we are stopped or waiting for a restart */
                    }else if((tool == CUTTING || tool == SPOTTING || probe_test) && (stop_type == NONE || stop_type == WAIT) && homed){
                        feed_hold = TRUE;
                        stop_type = NONE;
                        //touchdown = FALSE;
                        if(!probe_test){
                            cut_started = TRUE;
                        }else{
                            probe_testing = TRUE;
                        }
                        if(!thc_activated && first_cut_finished){
                            cut_offset = sqrt(pow(axis_x_start - axis_x_position, 2) + pow(axis_y_start - axis_y_position, 2));
                        }else if((axis_x_finish || axis_y_finish) && first_cut_finished){
                            cut_offset = sqrt(pow(axis_x_finish - axis_x_position, 2) + pow(axis_y_finish - axis_y_position, 2));
                            axis_x_finish = 0;
                            axis_y_finish = 0;
                        }
                        thc_activated = FALSE;
                        if(skip_ihs_distance && cut_offset < skip_ihs_distance && !probe_required){
                            if(thc_enabled){
                                pierce_target = pierce_target + cut_height_last - cut_target;
                                puddle_jump_target = puddle_jump_target + cut_height_last - cut_target;
                                cut_target = cut_height_last;
                                cut_height_first = cut_target;
                            }
                            cut_offset = 0;
                            state = PIERCE_HEIGHT;
                        }else{
                            if(ohmic_detected && !probe_inhibit){
                                probe_inhibit = TRUE;
                                if(probe_testing){
                                    rtapi_print_msg(RTAPI_MSG_ERR,"ohmic probe detected before probing.\n"
                                                                  "probe test aborted.\n");
                                    state = PROBE_TEST;
                                    probe_test_error = TRUE;
                                }else{
                                    stop_type = PAUSE;
                                    program_pause = TRUE;
                                    rtapi_print_msg(RTAPI_MSG_ERR,"ohmic probe detected before probing.\n"
                                                                  "program is paused.\n");
                                }
                            }else if(float_switch && !probe_inhibit){
                                probe_inhibit = TRUE;
                                if(probe_testing){
                                    rtapi_print_msg(RTAPI_MSG_ERR,"float switch detected before probing.\n"
                                                                  "probe test aborted.");
                                    state = PROBE_TEST;
                                    probe_test_error = TRUE;
                                }else{
                                    stop_type = PAUSE;
                                    program_pause = TRUE;
                                    rtapi_print_msg(RTAPI_MSG_ERR,"float switch detected before probing.\n"
                                                                  "program is paused.\n");
                                }
                            }else if(breakaway && !probe_inhibit){
                                probe_inhibit = TRUE;
                                if(probe_testing){
                                    rtapi_print_msg(RTAPI_MSG_ERR,"breakaway switch detected before probing.\n"
                                                                  "probe test aborted.\n");
                                    state = PROBE_TEST;
                                    probe_test_error = TRUE;
                                }else{
                                    stop_type = PAUSE;
                                    program_pause = TRUE;
                                    rtapi_print_msg(RTAPI_MSG_ERR,"breakaway switch detected before probing.\n"
                                                                  "program is paused.\n");
                                }
                            }else if (!program_is_paused && !program_pause){
                                if(!first_cut_finished){
                                    if(probe_start_height){
                                        probe_start_target = z_offset_counts - ((axis_z_position - axis_z_min_limit - probe_start_height) / offset_scale);
                                    }else{
                                        probe_start_target = z_offset_counts;
                                    }
                                }
                                cut_offset = 0;
                                /* offset probing setup if required */
                                if(offset_probing){
                                    op_xy_angle = atan2(-offset_probe_y, -offset_probe_x);
                                    op_x_velocity = fabs(setup_velocity * cos(op_xy_angle));
                                    op_y_velocity = fabs(setup_velocity * sin(op_xy_angle));
                                    op_x_target = x_offset_counts + (offset_probe_x / offset_scale);
                                    op_y_target = y_offset_counts + (offset_probe_y / offset_scale);
                                    op_x_start = x_offset_counts;
                                    op_y_start = y_offset_counts;
                                    if(axis_x_position + (op_x_target * offset_scale) <= axis_x_min_limit){
                                        rtapi_print_msg(RTAPI_MSG_ERR,"probe offset would violate axis X minimum limit.\n"
                                                                      "program is paused.\n");
                                        stop_type = PAUSE;
                                        program_pause = TRUE;
                                        break;
                                    }
                                    if(axis_y_position + (op_y_target * offset_scale) <= axis_y_min_limit){
                                        rtapi_print_msg(RTAPI_MSG_ERR,"probe offset would violate axis Y minimum limit.\n"
                                                                      "program is paused.\n");
                                        stop_type = PAUSE;
                                        program_pause = TRUE;
                                        break;
                                    }
                                    if(axis_x_position + (op_x_target * offset_scale) >= axis_x_max_limit){
                                        rtapi_print_msg(RTAPI_MSG_ERR,"probe offset would violate axis X maximum limit.\n"
                                                                      "program is paused.\n");
                                        stop_type = PAUSE;
                                        program_pause = TRUE;
                                        break;
                                    }
                                    if(axis_y_position + (op_y_target * offset_scale) >= axis_y_max_limit){
                                        rtapi_print_msg(RTAPI_MSG_ERR,"probe offset would violate axis Y maximum limit.\n"
                                                                      "program is paused.\n");
                                        stop_type = PAUSE;
                                        program_pause = TRUE;
                                        break;
                                    }
                                }
                                /* clear any laser recovey offset */
                                if(laser_recovery_state > OFF && laser_recovery_state < RESET){
                                    laser_recovery_state = RESET;
                                }else if(laser_recovery_state == RESET){
                                    angle_x_y = atan2(laser_y_offset, laser_x_offset);
                                    x_velocity = fabs(cut_feed_rate * velocity_scale * cos(angle_x_y));
                                    y_velocity = fabs(cut_feed_rate * velocity_scale * sin(angle_x_y));
                                    laser_recovery_state = RESETTING;
                                }else if(laser_recovery_state == RESETTING){
                                    if(x_offset_counts == x_offset && y_offset_counts == y_offset){
                                        laser_recovery_state = OFF;
                                    }else{
                                        if(x_offset_counts != x_offset){
                                            x_offset_counts = offset_move(x_offset_counts, x_velocity, x_offset);
                                        }
                                        if(y_offset_counts != y_offset){
                                            y_offset_counts = offset_move(y_offset_counts, y_velocity, y_offset);
                                        }
                                    }
                                /* laser recovery offset complete if required */
                                }else{
                                    state = PROBE_HEIGHT;
                                }
                            }
                        }
                    /* if we get a resume request and we are paused */
                    }else if(!program_is_paused && stop_type == PAUSE){
                        if(consumable_changing){
                            state = CONSUMABLE_CHANGE_OFF;
                        }else if(motion_type == 1){
                            feed_hold = FALSE;
                        }else{
                            feed_hold = TRUE;
                        }
                        stop_type = NONE;
                    /* if torch pulse requested */
                    }else if(torch_pulse_start && torch_enable && !breakaway && !float_switch && !ohmic_probe){
                        feed_hold = TRUE;
                        state = TORCHPULSE;
                    /* if ohmic probe shorted test requested */
                    }else if(ohmic_test && ohmic_probe_enable){
                        feed_hold = TRUE;
                        ohmic_enable = TRUE;
                        state = OHMIC_TEST;
                    /* if we get a air-scribe start request */
                    }else if(tool == SCRIBE && !probe_test && homed){
                        state = SCRIBING;
                        scribe_arm_timer = scribe_arm_delay;
                        scribe_pause = FALSE;
                    }else if(!cut_started){
                        feed_hold = FALSE;
                    }
                }
                break;
            case PROBE_HEIGHT:
                z_offset_enable = TRUE;
                stop_type = NONE;
                if(ohmic_probe_enable){
                    ohmic_enable = TRUE;
                }
                if(probe_testing && !probe_test){
                    state = PROBE_TEST;
                    break;
                }
                if(probe_testing && breakaway){
                    rtapi_print_msg(RTAPI_MSG_ERR,"breakaway switch detected during probe test\n");
                    probe_test_error = TRUE;
                    state = PROBE_TEST;
                    break;
                }
                /* move probe to x/y offsets if required */
                if(offset_probing){
                    xy_offset_enable = TRUE;
                    if(x_offset_counts != op_x_target || y_offset_counts != op_y_target){
                        if(x_offset_counts != op_x_target){
                            x_offset_counts = offset_move(x_offset_counts, op_x_velocity, op_x_target);
                        }
                        if(y_offset_counts != op_y_target){
                            y_offset_counts = offset_move(y_offset_counts, op_y_velocity, op_y_target);
                        }
                    }
                }
                if((float_switch && !float_detected) || ohmic_detected){
                    z_pierce = z_offset_counts + probe_offset + ((pierce_height + (1 * units_per_mm))/ offset_scale);
                    z_max = z_offset_counts + (axis_z_max_limit - axis_z_position - (max_offset * units_per_mm)) / offset_scale;
                    if(z_pierce > z_max){
                        if(probe_testing){
                          state = PROBE_TEST;
                          probe_test_error = TRUE;
                          if(float_switch){
                            rtapi_print_msg(RTAPI_MSG_ERR,"pierce height would exceed Z axis maximum limit\n"
                                                          "condition found while moving to probe height during float switch probe testing");
                          }else{
                            rtapi_print_msg(RTAPI_MSG_ERR,"pierce height would exceed Z axis maximum limit\n"
                                                          "condition found while moving to probe height during ohmic probe testing");
                          }
                        }else{
                          stop_type = PAUSE;
                          program_pause = TRUE;
                          state = MAX_HEIGHT;
                          if(float_switch){
                              rtapi_print_msg(RTAPI_MSG_ERR,"pierce height would exceed Z axis maximum limit\n"
                                                            "condition found while moving to probe height during float switch probing\n");
                          }else{
                              rtapi_print_msg(RTAPI_MSG_ERR,"pierce height would exceed Z axis maximum limit\n"
                                                            "condition found while moving to probe height during ohmic probing\n");
                          }
                        }
                        break;
                    }else if(ohmic_probe_enable && (x_offset_counts != op_x_target || y_offset_counts != op_y_target)){
                        stop_type = PAUSE;
                        program_pause = TRUE;
                        rtapi_print_msg(RTAPI_MSG_ERR,"probe contact detected before probe offset reached\n"
                                                      "try increasing PROBE HEIGHT parameter\n");
                        state = MAX_HEIGHT;
                        break;
                    }
                    probe_retry = TRUE;
                    state = PROBE_UP;
                /* move to probe height at setup velocity */
                }else if(z_offset_counts != probe_start_target){
                    z_offset_counts = offset_move(z_offset_counts, setup_velocity, probe_start_target);
                }else{
                    if(float_detected){
                        if(!float_switch){
                            float_detected = FALSE;
                        }
                    }else{
                        /* test if probing offset is reached */
                        if(offset_probing){
                            if(x_offset_counts == op_x_target && y_offset_counts == op_y_target &&
                               z_offset_current - probe_start_target * offset_scale < 0.001){
                                state = PROBE_DOWN;
                            }
                        }else{
                            state = PROBE_DOWN;
                        }
                    }
                }
                probe_time += fperiod;
                break;
            case PROBE_DOWN:
                z_offset_enable = TRUE;
                stop_type = NONE;
                feed_hold = TRUE;
                if(ohmic_probe_enable){
                    ohmic_enable = TRUE;
                }
                if(probe_testing && !probe_test){
                    state = PROBE_TEST;
                    break;
                }
                if(probe_testing && breakaway){
                    rtapi_print_msg(RTAPI_MSG_ERR,"breakaway switch detected during probe test\n");
                    probe_test_error = TRUE;
                    state = PROBE_TEST;
                    break;
                }
                if(ohmic_probe_enable && ohmic_probe_enable && offset_probe_timer < offset_probe_delay){
                    break;
                }
                if(float_switch && !float_detected){
                    if(ohmic_attempts >= ohmic_max_attempts || !ohmic_probe_enable){
                        z_pierce = z_offset_counts + probe_offset + ((pierce_height + (1 * units_per_mm))/ offset_scale);
                        z_max = z_offset_counts + (axis_z_max_limit - axis_z_position - (max_offset * units_per_mm)) / offset_scale;
                        if(z_pierce > z_max && !probe_testing){
                            rtapi_print_msg(RTAPI_MSG_ERR,"pierce height would exceed Z axis maximum safe height\n"
                                                          "condition found while float switch probing\n");
                            stop_type = PAUSE;
                            program_pause = TRUE;
                            state = MAX_HEIGHT;
                            break;
                        }
                        probe_type = FLOAT;
                        ohmic_attempts = 0;
                        float_detected = FALSE;
                        state = PROBE_UP;
                    }else{
                        ohmic_attempts += 1;
                        float_detected = TRUE;
                        state = PROBE_HEIGHT;
                    }
                }else if(ohmic_detected){
                    z_pierce = z_offset_counts + probe_offset + ((pierce_height + (1 * units_per_mm))/ offset_scale);
                    z_max = z_offset_counts + (axis_z_max_limit - axis_z_position - (max_offset * units_per_mm)) / offset_scale;
                    if(z_pierce > z_max && !probe_testing){
                        rtapi_print_msg(RTAPI_MSG_ERR,"pierce height would exceed Z axis maximum safe height\n"
                                                      "condition found while ohmic probing\n");
                        stop_type = PAUSE;
                        program_pause = TRUE;
                        state = MAX_HEIGHT;
                        break;
                    }
                    probe_type = OHMIC;
                    ohmic_attempts = 0;
                    state = PROBE_UP;
                }else if(!float_switch && !ohmic_detected){
                    if(axis_z_position - (probe_velocity * offset_scale) <= axis_z_min_limit + (0.25 * units_per_mm )){
                        if(probe_at_bottom - z_offset_current < 0.0001){
                            probe_at_bottom = 0;
                            if(!probe_test){
                                rtapi_print_msg(RTAPI_MSG_ERR,"bottom limit reached while probing down.\n"
                                                              "program is paused.\n");
                                stop_type = PAUSE;
                                program_pause = TRUE;
                                state = MAX_HEIGHT;
                            }else{
                                rtapi_print_msg(RTAPI_MSG_ERR,"bottom limit reached while probe testing.\n");
                                state = PROBE_TEST;
                                probe_test_error = TRUE;
                            }
                        }else{
                            probe_at_bottom = z_offset_current;
                        }
                    }else{
                        z_offset_counts -= probe_velocity;
                    }
                }
                probe_time += fperiod;
                break;
            case PROBE_UP:
                z_offset_enable = TRUE;
                stop_type = NONE;
                offset_probe_timer = 0;
                if(probe_testing && !probe_test){
                    state = PROBE_TEST;
                    break;
                }
                /* probe up at minimum speed to find top of stock */
                if(float_switch || ohmic_detected){
                    if(probe_retry){
                        z_offset_counts += 10 * res;
                    }else{
                        if(probe_final_speed > 10){
                            z_offset_counts += 10 * res;
                        }else if(probe_final_speed < 1){
                            z_offset_counts += 1 * res;
                        }else{
                            z_offset_counts += probe_final_speed * res;
                        }
                    }
                }else{
                    if(probe_retry){
                        probe_retry = FALSE;
                        state = PROBE_DOWN;
                        break;
                    }
                    ohmic_enable = FALSE;
                    if(probe_type == OHMIC){
                        probe_offset = ohmic_probe_offset / offset_scale;
                    }else{
                        probe_offset = float_switch_travel / offset_scale;
                    }
                    zero_target = z_offset_counts + probe_offset;
                    cut_target = offset_datum = zero_target + (cut_height / offset_scale);
                    pierce_target = zero_target + (pierce_height / offset_scale);
                    puddle_jump_target = zero_target + ((pierce_height * puddle_jump_percent * 0.01) / offset_scale );
                    safe_min = pierce_target + ((1 * units_per_mm) / offset_scale);
                    safe_preferred = zero_target + (safe_height / offset_scale);
                    offset_min = z_offset_counts - ((axis_z_position - axis_z_min_limit) / offset_scale);
                    offset_max = z_offset_counts + (axis_z_max_limit - axis_z_position - (max_offset * units_per_mm)) / offset_scale;
                    if(!safe_available){
                        safe_available = safe_preferred - zero_target;
                    }
                    if(units_per_mm == 1){
                        safe_alarm = offset_max - (13 / offset_scale);
                    }else{
                        safe_alarm = offset_max - (0.5 / offset_scale);
                    }
                    if(!cut_height_first){
                        cut_height_first = cut_target;
                    }
                    if(safe_height == 0){
                        safe_target = offset_max;
                            axis_x_start = axis_x_position;
                            axis_y_start = axis_y_position;
                            state = ZERO_HEIGHT;
                    }else if(safe_min > offset_max && !probe_testing){
                        safe_target = offset_max;
                        rtapi_print_msg(RTAPI_MSG_ERR, "material is too high for safe traverse.\n"
                                                       "program is paused.\n");
                        stop_type = PAUSE;
                        program_pause = TRUE;
                        state = MAX_HEIGHT;
                    }else if(safe_preferred >= offset_max && !probe_testing){
                        safe_target = offset_max;
                        // warn if safe_height is less than available safe height{
                        if(offset_max - zero_target < safe_available){
                            if(units_per_mm == 1){
                                rtapi_print_msg(RTAPI_MSG_ERR, "safe traverse height has been reduced to %0.1fmm.\n",
                                                                (offset_max - zero_target) * offset_scale);
                            }else{
                                rtapi_print_msg(RTAPI_MSG_ERR, "safe traverse height has been reduced to %0.2fin.\n",
                                                                (offset_max - zero_target) * offset_scale);
                            }
                            safe_available = offset_max - zero_target;
                        }
                        axis_x_start = axis_x_position;
                        axis_y_start = axis_y_position;
                        state = ZERO_HEIGHT;
                    }else{
                        safe_target = safe_preferred;
                        axis_x_start = axis_x_position;
                        axis_y_start = axis_y_position;
                        state = ZERO_HEIGHT;
                    }
                }
                probe_time += fperiod;
                break;
            case ZERO_HEIGHT:
                /* this case is not used at present it is just a waypoint*/
                if(z_offset_counts == zero_target){
                    if((int)(z_offset_current * offset_res) == (int)(zero_target * offset_scale * offset_res)){
                        state = PIERCE_HEIGHT;
                    }
                /* move to zero height at setup velocity*/
                }else{
                    z_offset_counts = offset_move(z_offset_counts, setup_velocity, zero_target);
                }
                break;
            case PIERCE_HEIGHT:
                z_offset_enable = TRUE;
                stop_type = NONE;
                probe_required = FALSE;
                if(probe_testing && !probe_test){
                    state = PROBE_TEST;
                    break;
                }
                /* clear x/y probe offsets if required */
                if(offset_probing){
                    xy_offset_enable = TRUE;
                    if(x_offset_counts != op_x_start){
                        x_offset_counts = offset_move(x_offset_counts, op_x_velocity, op_x_start);
                    }
                    if(y_offset_counts != op_y_start){
                        y_offset_counts = offset_move(y_offset_counts, op_y_velocity, op_y_start);
                    }
                    if(ohmic_probe_enable && offset_probe_timer < offset_probe_delay){
                        break;
                    }
                }
                if(pierce_height && cut_height && (use_auto_volts || (!use_auto_volts && cut_volts))){
                    feed_hold = TRUE;
                    if(z_offset_counts == pierce_target){
                        if((int)(z_offset_current * offset_res) == (int)(pierce_target * offset_scale * offset_res) &&
                           (((offset_probing && x_offset_counts == op_x_start && y_offset_counts == op_y_start)) || !offset_probing)){
                            arc_starts = 0;
                            if(probe_testing){
                                state = PROBE_TEST;
                            }else if(!torch_enable){
                                pierce_timer = pierce_delay;
                                state = PIERCE_DELAY;
                            }else if((tool == CUTTING || tool == SPOTTING) &&
                                     (!offset_probing || (offset_probing &&
                                     (int)(x_offset_current * offset_res) == (int)(op_x_start * offset_scale * offset_res) &&
                                     (int)(y_offset_current * offset_res) == (int)(op_y_start * offset_scale * offset_res)))){
                                state = TORCH_ON;
                            }
                        }
                    /* move to pierce height at setup velocity*/
                    }else{
                        z_offset_counts = offset_move(z_offset_counts, setup_velocity, pierce_target);
                    }
                }else if(!probe_testing){
                    stop_type = PAUSE;
                    program_pause = TRUE;
                    state = MAX_HEIGHT;
                    rtapi_print_msg(RTAPI_MSG_ERR,"invalid pierce height.\n"
                                                  "or invalid cut height.\n"
                                                  "or invalid cut volts.\n"
                                                  "program is paused.\n");
                }
                break;
            case TORCH_ON:
                stop_type = NONE;
                /* turn torch on and start arc fail timer
                 * if too many attempts then turn torch off, pause program and return to idle state */
                feed_hold = TRUE;
                if(arc_starts > arc_max_starts - 1){
                    restart_timer = 0;
                    if(program_is_idle){
                        cutting_stop = 1;
                        state = MAX_HEIGHT;
                        if (!error_message){
                            rtapi_print_msg(RTAPI_MSG_ERR,"no arc detected after %d start attempts\nmanual cut is stopped.\n", arc_max_starts);
                            error_message = 1;
                        }
                    }else{
                        program_pause = TRUE;
                        if (!error_message){
                            rtapi_print_msg(RTAPI_MSG_ERR,"no arc detected after %d start attempts\nprogram is paused.\n", arc_max_starts);
                            error_message = 1;
                        }
                    }
                }else{
                    error_message = 0;
                    restart_timer -= fperiod;
                    if(restart_timer <= 0){
                        restart_timer = 0;
                        arc_fail_timer = arc_fail_delay;
                        if(torch_enable){
                            torch_on = TRUE;
                            pierce_count += 1;
                        }
                        spotting = FALSE;
                        /* clear arc voltage buffer */
                        memset(arc_voltage_buffer, 0, BUFFERSIZE * sizeof(*arc_voltage_buffer));
                        state = ARC_OK;
                    }
                }
                break;
            case ARC_OK:
                stop_type = NONE;
                /* wait for arc ok
                 * if timeout occurs turn torch off then return to TORCH_ON for another attempt */
                feed_hold = TRUE;
                if(tool == SPOTTING){
                    if(!spotting){
                        if(arc_voltage_out >= spotting_threshold){
                            spotting = TRUE;
                            spotting_timer = spotting_time * 0.001;
                        }
                    }else{
                        spotting_timer -= fperiod;
                        if(spotting_timer <= 0){
                            spotting = FALSE;
                            spotting_timer = 0;
                            torch_on = FALSE;
                            stop_type = WAIT;
                            state = SAFE_HEIGHT;
                        }
                    }
                }
                arc_fail_timer -= fperiod;
                if(arc_fail_timer <= 0){
                    torch_on = FALSE;
                    restart_timer = restart_delay;
                    arc_starts += 1;
                    state = TORCH_ON;
                }else if((arc_ok_out || ignore_arc_ok_0 || ignore_arc_ok_1) && tool != SPOTTING){
                        pierce_timer = pierce_delay;
                        state = PIERCE_DELAY;
                }
                break;
            case PIERCE_DELAY:
                stop_type = NONE;
                /* wait for arc to pierce stock */
                feed_hold = TRUE;
                if(pierce_timer > 0){
                    pierce_timer -= fperiod;
                }else if((puddle_jump_delay || puddle_jump_percent != 100) && !cut_recovering){
                    puddle_jump_timer = puddle_jump_delay;
                    state = PUDDLE_JUMP;
                }else{
                    state = CUT_HEIGHT;
                }
                break;
            case PUDDLE_JUMP:
                z_offset_enable = TRUE;
                feed_hold = FALSE;
                if(z_offset_counts == puddle_jump_target){
                    if((int)(z_offset_current * offset_res) == (int)(puddle_jump_target * offset_scale * offset_res)){
                        count = 0;
                        if(puddle_jump_timer > 0){
                            puddle_jump_timer -= fperiod;
                        }else{
                            puddle_jump_timer = 0;
                            state = CUT_HEIGHT;
                        }
                    }
                /* move to puddle_jump height at setup velocity*/
                }else{
                    z_offset_counts = offset_move(z_offset_counts, setup_velocity, puddle_jump_target);
                }
                break;
            case CUT_HEIGHT:
                z_offset_enable = TRUE;
                stop_type = NONE;
//                /* hold x/y motion until cut height reached */
//                if(!cut_recovering){
//                    feed_hold = FALSE;
//                }
                if(z_offset_counts == cut_target){
                    if((int)(z_offset_current * offset_res) == (int)(cut_target * offset_scale * offset_res)){
                        count = 0;
                        if(cut_recovering){
                            angle_x_y = atan2(-y_offset_counts, -x_offset_counts);
                            x_velocity = fabs(cut_feed_rate * velocity_scale * cos(angle_x_y));
                            y_velocity = fabs(cut_feed_rate * velocity_scale * sin(angle_x_y));
                            state = CUT_RECOVERY_OFF;
                        }else{
                            feed_hold = FALSE;
                            thc_delay_timer = thc_delay;
                            if(mode < 2){
                                voidlock_on_count = 0;
                                state = CUT_MODE_01;
                            }else{
                                state = CUT_MODE_2;
                            }
                        }
                    }
                /* move to cut height at setup velocity*/
                }else{
                    z_offset_counts = offset_move(z_offset_counts, setup_velocity, cut_target);
                }
                break;
            case CUT_MODE_01:
                /* thc control for modes 0 & 1 by arc voltage */
                /* if program is idle and spindle.0 is off then we shouldn't be here */
                if(program_is_idle && !cutting_start){
                    state = IDLE;
                }
                z_offset_enable = TRUE;
                /* use feed_upm if it has a value */
                if(feed_upm){
                    if(gcode_scale == 1){
                        requested_feed_rate = feed_upm / adaptive_feed;
                    }else{
                        requested_feed_rate = feed_upm / adaptive_feed / gcode_scale;
                    }
                /* otherwise fall back to cut feed rate parameter */
                }else{
                    if(gcode_scale == 1){
                        requested_feed_rate = cut_feed_rate * feed_override;
                    }else{
                        requested_feed_rate = cut_feed_rate * feed_override / gcode_scale;
                    }
                }
                /* while cutting and it is not a dry run and mesh mode is not enabled:
                 * if thc is enabled then vary the torch height to keep the arc voltage constant
                 * if corner lock enabled, only allow THC if current velocity is greater than the threshold percentage of requested velocity
                 * if voidlock is enabled, only allow THC if the voltage change is less than the threshold voltage
                 * adjust torch height and target voltage to suit if height override requested */
                if(torch_on && !mesh_enable && !ignore_arc_ok_0 && !ignore_arc_ok_1){
                    if(target_volts == 0){
                        cornerlock_is_locked = TRUE;
                        /* wait until velocity reaches 99.9% of requested velocity */
                        if(current_velocity * 60 > requested_feed_rate * 0.999){
                            if(thc_auto && use_auto_volts){
                                /* test the arc voltage buffer for a stable voltage */
                                double target = read_arc_voltage_buffer(READ_THC, thc_sampler_samples, arc_voltage_buffer_index - 1, arc_voltage_out, thc_sample_threshold);
                                if(target){
// should we use average voltage or current voltage???
//                                    target_volts = arc_voltage_out; // current voltage
                                    target_volts = target;            // average voltage
                                }
                            }else{
                                /* wait until thc delay is completed before setting target voltage */
                                if(thc_delay_timer <= 0){
                                    if(use_auto_volts){
                                        count += 1;
                                        target_total += arc_voltage_out;
                                        if(count == target_samples){
                                            target_volts = target_total / target_samples;
                                            last_arc_voltage = target_volts;
                                            count = 0;
                                            target_total = 0;
                                        }
                                    }else{
                                        target_volts = cut_volts;
                                    }
                                }else{
                                    thc_delay_timer -= fperiod;
                                }
                            }
                        }
                    /* height override setup*/
                    }else if(fabs(height_override - height_ovr_old) > 0.005){
                        height_ovr_counts -= (height_override - height_ovr_old) * height_per_volt / offset_scale;
                        height_ovr_old = height_override;
                    /* height override z motion */
                    }else if(height_ovr_counts != 0){
                        if((setup_velocity) < height_ovr_counts){
                            z_offset_counts -= setup_velocity;
                            height_ovr_counts -= setup_velocity;
                        }else{
                            z_offset_counts -= height_ovr_counts;
                            height_ovr_counts = 0;
                        }
                    /* torch height control */
                    }else if(thc_enabled){
                        /* lock thc if velocity < requested velocity * cornerlock threshold percentage */
                        if(cornerlock_enable){
                            if(current_velocity * 60 < requested_feed_rate * cornerlock_threshold * 0.01){
                                cornerlock_is_locked = TRUE;
                            }else if(cornerlock_is_locked && current_velocity * 60 > requested_feed_rate * 0.99){
                                cornerlock_is_locked = FALSE;
                            }
                        }else{
                            cornerlock_is_locked = FALSE;
                        }
                        if(voidlock_enable){
                            voidlock_threshold = (voidlock_slope * voidlock_on_cycles * fperiod);
                            voidlock_change = fabs(arc_voltage_out - last_arc_voltage);
                            last_arc_voltage = arc_voltage_out;
                            if(voidlock_change > voidlock_threshold){
                                if(voidlock_on_count >= voidlock_on_cycles){
                                    voidlock_off_count = 0;
                                    voidlock_is_locked = TRUE;
                                }else{
                                    voidlock_on_count += 1;
                                }
                            }else if(voidlock_is_locked){
                                voidlock_off_count += 1;
                            }else{
                                voidlock_on_count = 0;
                            }
                            if(voidlock_is_locked && voidlock_off_count >= voidlock_off_cycles){
                                    voidlock_is_locked = FALSE;
                            }
                        }else{
                            voidlock_is_locked = FALSE;
                            voidlock_on_count = 0;
                            last_arc_voltage = arc_voltage_out;
                        }
                        /* do thc if ok to go */
                        if(!cornerlock_is_locked && !voidlock_is_locked){
                            pid_error_now = (target_volts + height_override - arc_voltage_out) * 0.1;
                            if(fabs(pid_error_now) < fabs(thc_threshold * 0.1)){
                                pid_error_now = 0;
                            }
                            pid_output = pid_error_now * pid_p_gain * res;
                            pid_output += pid_error_now * pid_i_gain * res * fperiod;
                            pid_output += (pid_error_now - pid_error_old) * pid_d_gain * res / fperiod;
                            pid_error_old = pid_error_now;
                            if(pid_output > thc_velocity){
                                pid_output = thc_velocity;
                            }else if(pid_output < -thc_velocity){
                                pid_output = -thc_velocity;
                            }
                            /* if we hit a soft limit during thc*/
                            if(z_offset_counts + pid_output <= offset_min || z_offset_counts + pid_output >= offset_max){
                                torch_on = FALSE;
                                stop_type = PAUSE;
                                program_pause = TRUE;
                                if(z_offset_counts + pid_output <= offset_min){
                                    rtapi_print_msg(RTAPI_MSG_ERR,"bottom limit reached while THC moving down.\n"
                                                                  "program is paused.\n");
                                }else{
                                    rtapi_print_msg(RTAPI_MSG_ERR,"top limit reached while THC moving up.\n"
                                                                  "program is paused.\n");
                                }
                                pid_output = 0;
                                state = MAX_HEIGHT;

                            }
                            z_offset_counts += pid_output;
                        }
                    }
                    if(pid_output > 0){
                        led_up = TRUE;
                    }else if((pid_output) < 0){
                        led_down = TRUE;
                    }else{
                        led_down = FALSE;
                        led_up = FALSE;
                    }
                    pid_output = 0;
                    /* check if safe height is below maximum offset */
                    if(z_offset_counts > offset_datum){
                        safe_target += z_offset_counts - offset_datum;
                        offset_datum = z_offset_counts;
                        if(safe_target > offset_max){
                            safe_target = offset_max;
                            // warn if safe height less than 10mm (0.4")
                            if(!safe_height_is_limited && z_offset_counts > safe_alarm){
                                safe_height_is_limited = TRUE;
                                if(units_per_mm == 1){
                                    rtapi_print_msg(RTAPI_MSG_ERR, "safe traverse height has been reduced to less than 13mm.\n");
                                }else{
                                    rtapi_print_msg(RTAPI_MSG_ERR, "safe traverse height has been reduced to less than 0.5in.\n");
                                }
                                safe_available = offset_max - z_offset_counts;
                            }
                        }
                    }
                }
                cut_height_last = z_offset_counts;
                cut_length = cut_length + current_velocity * fperiod;
                cut_time = cut_time + fperiod;
                break;
            case CUT_MODE_2:
                /* thc control for mode 2 by move-up and move-down inputs (no void lock in this mode) */
                /* if program is idle and spindle.0 is off then we shouldn't be here */
                if(program_is_idle && !cutting_start){
                    state = IDLE;
                }
                z_offset_enable = TRUE;
                /* use feed_upm if it has a value */
                if(feed_upm){
                    if(gcode_scale == 1){
                        requested_feed_rate = feed_upm / adaptive_feed;
                    }else{
                        requested_feed_rate = feed_upm / adaptive_feed / gcode_scale;
                    }
                /* otherwise fall back to cut feed rate parameter */
                }else{
                    if(gcode_scale == 1){
                        requested_feed_rate = cut_feed_rate * feed_override;
                    }else{
                        requested_feed_rate = cut_feed_rate * feed_override / gcode_scale;
                    }
                }
                /* while cutting and it is not a dry run and mesh mode is not enabled:
                 * if thc is enabled then vary the torch height to keep the arc voltage constant
                 * if corner lock enabled, only allow THC if current velocity is greater than the threshold percentage of requested velocity */
                if(torch_on && !mesh_enable && !ignore_arc_ok_0 && !ignore_arc_ok_1){
                    if(thc_enabled){
                        /* lock thc if velocity < requested velocity * cornerlock threshold percentage */
                        if(cornerlock_enable){
                            if(current_velocity * 60 < requested_feed_rate * cornerlock_threshold * 0.01){
                                cornerlock_is_locked = TRUE;
                            }else if(cornerlock_is_locked && current_velocity * 60 > requested_feed_rate * 0.99){
                                cornerlock_is_locked = FALSE;
                            }
                        }else{
                            cornerlock_is_locked = FALSE;
                        }
                        if(move_down && !cornerlock_is_locked){
                            if(z_offset_counts - thc_velocity <= offset_min){
                                torch_on = FALSE;
                                stop_type = PAUSE;
                                program_pause = TRUE;
                                rtapi_print_msg(RTAPI_MSG_ERR,"bottom limit reached while THC moving down.\n"
                                                              "program is paused.\n");
                                state = MAX_HEIGHT;
                            }else{ /* move down at requested velocity */
                                z_offset_counts -= thc_velocity;
                                led_down = TRUE;
                            }
                        }else if(move_up && !cornerlock_is_locked){
                            if(z_offset_counts + thc_velocity + safe_min >= offset_max){
                                torch_on = FALSE;
                                stop_type = PAUSE;
                                program_pause = TRUE;
                                rtapi_print_msg(RTAPI_MSG_ERR,"top limit reached while THC moving up.\n"
                                                              "program is paused.\n");
                                state = MAX_HEIGHT;
                            }else{ /* move up at requested velocity */
                                z_offset_counts += thc_velocity;
                                led_up = TRUE;
                            }
                        }else{
                            led_down = FALSE;
                            led_up = FALSE;
                        }
                    }
                    /* check if safe height is below maximum offset */
                    if(z_offset_counts > offset_datum){
                        safe_target += z_offset_counts - offset_datum;
                        offset_datum = z_offset_counts;
                        if(safe_target > offset_max){
                            safe_target = offset_max;
                            // warn if safe height less than 10mm (0.4")
                            if(!safe_height_is_limited && z_offset_counts > safe_alarm){
                                safe_height_is_limited = TRUE;
                                if(units_per_mm == 1){
                                    rtapi_print_msg(RTAPI_MSG_ERR, "safe traverse height has been reduced to less than 13mm.\n");
                                }else{
                                    rtapi_print_msg(RTAPI_MSG_ERR, "safe traverse height has been reduced to less than 0.5in.\n");
                                }
                                safe_available = offset_max - z_offset_counts;
                            }
                        }
                    }
                }
                cut_height_last = z_offset_counts;
                cut_length = cut_length + current_velocity * fperiod;
                cut_time = cut_time + fperiod;
                break;
            case PAUSE_AT_END:
                feed_hold = TRUE;
                pause_at_end_timer -= fperiod;
                if(pause_at_end_timer <= 0){
                    pause_at_end_timer = 0;
                    torch_on = FALSE;
                    if(program_is_idle){
                        state = MAX_HEIGHT;
                    }else{
                        state = SAFE_HEIGHT;
                    }
                }
                break;
            case SAFE_HEIGHT:
                z_offset_enable = TRUE;
                feed_hold = TRUE;
                torch_on = FALSE;
                if(!torch_off_timer || !torch_on){
                    cornerlock_is_locked = FALSE;
                    if(!probe_test || probe_test_error){
                        if(z_offset_counts == safe_target){
                            if((int)(z_offset_current * offset_res) == (int)(safe_target * offset_scale * offset_res)){
                                if(stop_type == WAIT){
                                    feed_hold = FALSE;
                                }
                                first_cut_finished = TRUE;
                                /* do height override here for for remainder of job */
                                height_ovr_old = 0;
                                state = END_CUT;
                            }
                        /* move to safe height at setup velocity*/
                        }else{
                            z_offset_counts = offset_move(z_offset_counts, setup_velocity, safe_target);
                        }
                    }
                }
                break;
            case MAX_HEIGHT:
                z_offset_enable = TRUE;
                feed_hold = TRUE;
                torch_on = FALSE;
                cornerlock_is_locked = FALSE;
                if(!probe_test || probe_test_error){
                    if(z_offset_counts == 0){
                        if((int)(z_offset_current * offset_res) == 0){
                            if(stop_type == WAIT){
                                feed_hold = FALSE;
                            }
                            /* do height override here for remainder of job */
                            height_ovr_old = 0;
                            if(consumable_change){
                                state = CONSUMABLE_CHANGE_ON;
                            }else if(cut_recovery && stop_type != STOP){
                                state = CUT_RECOVERY_ON;
                            /* clear x/y probe offsets if required */
                            }else if((offset_probe_x || offset_probe_y) && (x_offset_counts || y_offset_counts)){
                                if(x_offset_counts != 0){
                                    x_offset_counts = offset_move(x_offset_counts, op_x_velocity, 0);
                                }
                                if(y_offset_counts != 0){
                                    y_offset_counts = offset_move(y_offset_counts, op_y_velocity, 0);
                                }
                            }else{
                                    state = END_CUT;
                            }
                        }
                    /* move to maximum height at setup velocity */
                    }else{
                        z_offset_counts = offset_move(z_offset_counts, setup_velocity, 0);
                    }
                }
                break;
            case END_CUT:
                /* clean up and return to idle state */
                target_volts = 0;
                cornerlock_is_locked = FALSE;
                voidlock_is_locked = FALSE;
                led_down = FALSE;
                led_up = FALSE;
                ohmic_enable = FALSE;
                probe_test_error = FALSE;
                cutting_stop = 0;
                if(program_is_idle){
                    state = END_JOB;
                }else{
                    state = IDLE;
                }
                break;
            case END_JOB:
                auto_cut = FALSE;
                manual_cut = FALSE;
                program_run = FALSE;
                paused_motion = FALSE;
                adaptive_feed = 1;
                cut_height_first = 0;
                cut_height_last = 0;
                if(program_is_idle){
                    z_offset_enable = TRUE;
                    if (z_offset_counts == 0){
                        if(!offsets_active){
                            first_cut_finished = FALSE;
                            stop_type = NONE;
                            safe_height_is_limited = FALSE;
                            safe_available = 0;
                            cut_started = FALSE;
                            /* do height override here for one cut only */
                            /* height_ovr_old = 0; */
                            if(consumable_changing){
                                state = CONSUMABLE_CHANGE_OFF;
                            }else if(cut_recovering){
                                angle_x_y = atan2(-y_offset_counts, -x_offset_counts);
                                x_velocity = fabs(cut_feed_rate * velocity_scale * cos(angle_x_y));
                                y_velocity = fabs(cut_feed_rate * velocity_scale * sin(angle_x_y));
                                state = CUT_RECOVERY_OFF;
                                cut_recovering = FALSE;
                                laser_recovery_state = OFF;
                            }else{
                                state = IDLE;
                            }
                        }
                    /* clear z offset at setup velocity */
                    }else{
                        z_offset_counts = offset_move(z_offset_counts, setup_velocity, 0);
                    }
                }
                break;
            case TORCHPULSE:
                /* single pulse the torch on and off */
                if(!torch_on){
                    torch_pulse_timer = torch_pulse_time;
                    if(torch_enable){
                        torch_on = TRUE;
                    }
                }else{
                    if(torch_pulse_time == 0){
                        torch_pulse_timer = 0;
                    }
                    if(torch_pulse_timer > 0){
                        torch_pulse_timer -= fperiod;
                    }else{
                        torch_on = FALSE;
                        if(!torch_pulse_start){
                            if(cut_recovering){
                                state = CUT_RECOVERY_ON;
                            }else{
                                state = IDLE;
                            }
                        }
                    }
                }
                break;
            case PAUSED_MOTION:
                if(paused_motion_speed){
                    paused_motion = TRUE;
                    adaptive_feed = paused_motion_speed;
                    program_resume = TRUE;
                    feed_hold = FALSE;
                }else if(program_is_running){
                    program_pause = TRUE;
                    feed_hold = TRUE;
                    adaptive_feed = 1;
                }else if(program_is_paused && feed_hold){
                    paused_motion = FALSE;
                    state = CUT_RECOVERY_ON;
                }
                break;
            case OHMIC_TEST:
                /* wait here until ohmic_test input released */
                if (!ohmic_test){
                    ohmic_enable = FALSE;
                    if(cut_recovering){
                        state = CUT_RECOVERY_ON;
                    }else{
                        state = IDLE;
                        }
                }
                break;
            case PROBE_TEST:
                z_offset_enable = TRUE;
                /* wait here until probe_test input released */
                if(!probe_test){
                    /* clear z offset at setup velocity */
                    z_offset_counts = offset_move(z_offset_counts, setup_velocity, 0);
                    if((offset_probe_x || offset_probe_y) && (x_offset_counts || y_offset_counts)){
                        x_offset_counts = offset_move(x_offset_counts, op_x_velocity, 0);
                        y_offset_counts = offset_move(y_offset_counts, op_y_velocity, 0);
                    }
                    if(!x_offset_counts && !y_offset_counts && !z_offset_counts){
                        probe_inhibit = FALSE;
                        probe_testing = FALSE;
                        probe_required = TRUE;
                        state = END_CUT;
                    }
                }
                break;
            case SCRIBING:
                if(tool == SCRIBE){
                    if(!program_is_paused){
                        scribe_arm = TRUE;

                        if(scribe_arm_timer){
                            feed_hold = TRUE;
                            scribe_arm_timer -= fperiod;
                            if(scribe_arm_timer < 0){
                                scribe_arm_timer = 0;
                            }
                        }else if(scribe_on_timer){
                            feed_hold = TRUE;
                            scribe_on_timer -= fperiod;
                            if(scribe_on_timer < 0){
                                scribe_on_timer = 0;
                            }
                        }else if(scribe_arm && !scribe_arm_timer && !scribe_on){
                            feed_hold = TRUE;
                            scribe_on_timer = scribe_on_delay;
                            scribe_on = TRUE;
                        }else if(scribe_on && !scribe_on_timer){
                            feed_hold = FALSE;
                        }
                    }else{
                        scribe_arm = FALSE;
                        scribe_on = FALSE;
                        scribe_arm_timer = scribe_arm_delay;
                        scribe_on_timer = 0;
                    }
                }else{
                    scribe_arm = FALSE;
                    scribe_on = FALSE;
                    scribe_arm_timer = 0;
                    scribe_on_timer = 0;
                    state = IDLE;
                }
                break;
            case CONSUMABLE_CHANGE_ON:
                xy_offset_enable = TRUE;
                feed_hold = TRUE;
                if(sensor_active){
                    if(!error_message){
                        error_message = TRUE;
                        rtapi_print_msg(RTAPI_MSG_ERR,
                                        "breakaway, float, or ohmic activated during consumable change, motion paused.\n"
                                        "WARNING: MOTION WILL RESUME IMMEDIATELY UPON RESOLVING THIS CONDITION!\n");
                    }
                    break;
                }
                error_message = FALSE;
                if(!consumable_changing){
                    if(x_offset == 0 && y_offset == 0){
                        x_velocity = 0;
                        y_velocity = 0;
                    }else if(x_offset == 0){
                        x_velocity = 0;
                        y_velocity = xy_feed_rate * velocity_scale;
                    }else if(y_offset == 0){
                        x_velocity = xy_feed_rate * velocity_scale;
                    }else{
                        angle_x_y = atan2(y_offset, x_offset);
                        x_velocity = fabs(xy_feed_rate * velocity_scale * cos(angle_x_y));
                        y_velocity = fabs(xy_feed_rate * velocity_scale * sin(angle_x_y));
                    }
                    consumable_changing = TRUE;
                }else{
                    if(((int)(x_offset_current * offset_res) == (int)(x_offset * offset_scale * offset_res) &&
                      (int)(y_offset_current * offset_res) == (int)(y_offset * offset_scale * offset_res)) ||
                      !consumable_change){
                        state = IDLE;
                    }else if(consumable_change){
                        if(x_velocity && x_offset_counts != x_offset){
                            if(x_offset > 0){
                                if(x_offset_counts + x_velocity < x_offset){
                                    x_offset_counts += x_velocity;
                                }else{
                                    x_offset_counts = x_offset;
                                }
                            }else{
                                if(x_offset_counts - x_velocity > x_offset){
                                    x_offset_counts -= x_velocity;
                                }else{
                                    x_offset_counts = x_offset;
                                }
                            }
                        }
                        if(y_velocity && y_offset_counts != y_offset){
                            if(y_offset > 0){
                                if(y_offset_counts + y_velocity < y_offset){
                                    y_offset_counts += y_velocity;
                                }else{
                                    y_offset_counts = y_offset;
                                }
                            }else{
                                if(y_offset_counts - y_velocity > y_offset){
                                    y_offset_counts -= y_velocity;
                                }else{
                                    y_offset_counts = y_offset;
                                }
                            }
                        }
                    }
                }
                break;
            case CONSUMABLE_CHANGE_OFF:
                xy_offset_enable = TRUE;
                if(sensor_active){
                    if(!error_message){
                        error_message = TRUE;
                        rtapi_print_msg(RTAPI_MSG_ERR,
                                        "breakaway, float, or ohmic activated during consumable change, motion paused.\n"
                                        "WARNING: MOTION WILL RESUME IMMEDIATELY UPON RESOLVING THIS CONDITION!\n");
                    }
                    break;
                }
                error_message = FALSE;
                if(x_offset_counts == 0 && y_offset_counts == 0){
                    if((int)(x_offset_current * offset_res) == 0 && (int)(y_offset_current * offset_res) == 0){
                        consumable_changing = FALSE;
                        cons_change_clear = FALSE;
                        feed_hold = FALSE;
                        state = IDLE;
                    }
                }else{
                    if(x_velocity && x_offset_counts != 0){
                        if(x_offset_counts > 0){
                            if(x_offset_counts - x_velocity > 0){
                                x_offset_counts -= x_velocity;
                            }else{
                                x_offset_counts = 0;
                            }
                        }else{
                            if(x_offset_counts + x_velocity < 0){
                                x_offset_counts += x_velocity;
                            }else{
                                x_offset_counts = 0;
                            }
                        }
                    }
                    if(y_velocity && y_offset_counts != 0){
                        if(y_offset_counts > 0){
                            if(y_offset_counts - y_velocity > 0){
                                y_offset_counts -= y_velocity;
                            }else{
                                y_offset_counts = 0;
                            }
                        }else{
                            if(y_offset_counts + y_velocity < 0){
                                y_offset_counts += y_velocity;
                            }else{
                                y_offset_counts = 0;
                            }
                        }
                    }
                }
                break;
            case CUT_RECOVERY_ON:
                xy_offset_enable = TRUE;
                if(program_is_running){
                    state = IDLE;
                }
                if(!cut_recovering){
                    cut_recovering = TRUE;
                    if(puddle_jump_delay || puddle_jump_percent != 100){
                        rtapi_print_msg(RTAPI_MSG_ERR, "Puddle Jump is disabled during Cut Recovery.\n");
                    }
                }
                if(torch_pulse_start && torch_enable && !breakaway && !float_switch && !ohmic_probe){
                    feed_hold = TRUE;
                    state = TORCHPULSE;
                }
                if(ohmic_test && ohmic_probe_enable){
                    feed_hold = TRUE;
                    ohmic_enable = TRUE;
                    state = OHMIC_TEST;
                }
                /* offset for laser recovery if required */
                if(laser_recovery_start && laser_recovery_state == OFF){
                    laser_recovery_state = SET;
                }else if(laser_recovery_state == SET){
                    angle_x_y = atan2(laser_y_offset, laser_x_offset);
                    x_velocity = fabs(cut_feed_rate * velocity_scale * cos(angle_x_y));
                    y_velocity = fabs(cut_feed_rate * velocity_scale * sin(angle_x_y));
                    laser_x_target = x_offset_counts + laser_x_offset;
                    laser_y_target = y_offset_counts + laser_y_offset;
                    laser_recovery_state = SETTING;
                }else if(laser_recovery_state == SETTING){
                    if(x_offset_counts == laser_x_target && y_offset_counts == laser_y_target){
                        laser_recovery_state = ON;
                    }else{
                        if(x_offset_counts != laser_x_target){
                            x_offset_counts = offset_move(x_offset_counts, x_velocity, laser_x_target);
                        }
                        if(y_offset_counts != laser_y_target){
                            y_offset_counts = offset_move(y_offset_counts, y_velocity, laser_y_target);
                        }
                    }
                /* laser recovery offset is clear */
                }else if(cut_recovery){
                    /* move x axis to recovery position at recovery velocity */
                    if(x_offset_counts != x_offset + laser_x_offset * (laser_recovery_state > 1)){
                        x_offset_counts = offset_move(x_offset_counts, recovery_velocity, x_offset + laser_x_offset * (laser_recovery_state > 1));
                    }
                    /* move y axis to recovery position at recovery velocity */
                    if(y_offset_counts != y_offset + laser_y_offset * (laser_recovery_state > 1)){
                        y_offset_counts = offset_move(y_offset_counts, recovery_velocity, y_offset + laser_y_offset * (laser_recovery_state > 1));
                    }
                }else{
                    state = CUT_RECOVERY_OFF;
                }
                if(paused_motion_speed){
                    state = PAUSED_MOTION;
                }
                break;
            case CUT_RECOVERY_OFF:
                xy_offset_enable = TRUE;
                angle_x_y = atan2(-y_offset_counts, -x_offset_counts);
                if(program_is_running){
                    x_velocity = fabs(cut_feed_rate * velocity_scale * cos(angle_x_y));
                    y_velocity = fabs(cut_feed_rate * velocity_scale * sin(angle_x_y));
                }else{
                    x_velocity = fabs(recovery_velocity * cos(angle_x_y));
                    y_velocity = fabs(recovery_velocity * sin(angle_x_y));
                }
                if(x_offset_counts == 0 && y_offset_counts == 0){
                    if((int)(x_offset_current * offset_res) == 0 && (int)(y_offset_current * offset_res) == 0){
                        cut_recovering = FALSE;
                        laser_recovery_state = OFF;
                        if(cut_recovery){
                            feed_hold = FALSE;
                            thc_delay_timer = thc_delay;
                            if(mode < 2){
                                voidlock_on_count = 0;
                                state = CUT_MODE_01;
                            }else{
                                state = CUT_MODE_2;
                            }
                        }else{
                            state = IDLE;
                        }
                    }
                }else{
                    /* clear x axis offset at recovery velocity */
                    if(x_offset_counts != 0){
                        x_offset_counts = offset_move(x_offset_counts, x_velocity, 0);
                    }
                    /* clear y axis offset at recovery velocity */
                    if(y_offset_counts != 0){
                        y_offset_counts = offset_move(y_offset_counts, y_velocity, 0);
                    }
                }
                break;
            case DEBUG:
                /* holding state for debugging */
                rtapi_print_msg(RTAPI_MSG_ERR, "We should never end up here...\n");
                break;
        }
    }

    /* set status pins */
    state_out = state;
    stop_type_out = stop_type;

    /* debug print */
    if(debug_print && state_old != state){
        state_old = state;
        switch(state){
            case 0:
                rtapi_print_msg(RTAPI_MSG_INFO, "IDLE\n");
                break;
            case 1:
                rtapi_print_msg(RTAPI_MSG_INFO, "PROBE_HEIGHT\n");
                break;
            case 2:
                rtapi_print_msg(RTAPI_MSG_INFO, "PROBE_DOWN\n");
                break;
            case 3:
                rtapi_print_msg(RTAPI_MSG_INFO, "PROBE_UP\n");
                break;
            case 4:
                rtapi_print_msg(RTAPI_MSG_INFO, "ZERO_HEIGHT\n");
                break;
            case 5:
                rtapi_print_msg(RTAPI_MSG_INFO, "PIERCE_HEIGHT\n");
                break;
            case 6:
                rtapi_print_msg(RTAPI_MSG_INFO, "TORCH_ON\n");
                break;
            case 7:
                rtapi_print_msg(RTAPI_MSG_INFO, "ARC_OK\n");
                break;
            case 8:
                rtapi_print_msg(RTAPI_MSG_INFO, "PIERCE_DELAY\n");
                break;
            case 9:
                rtapi_print_msg(RTAPI_MSG_INFO, "PUDDLE_JUMP\n");
                break;
            case 10:
                rtapi_print_msg(RTAPI_MSG_INFO, "CUT_HEIGHT\n");
                break;
            case 11:
                rtapi_print_msg(RTAPI_MSG_INFO, "CUT_MODE_01\n");
                break;
            case 12:
                rtapi_print_msg(RTAPI_MSG_INFO, "CUT_MODE_2\n");
                break;
            case 13:
                rtapi_print_msg(RTAPI_MSG_INFO, "PAUSE_AT_END\n");
                break;
            case 14:
                rtapi_print_msg(RTAPI_MSG_INFO, "SAFE_HEIGHT\n");
                break;
            case 15:
                rtapi_print_msg(RTAPI_MSG_INFO, "MAX_HEIGHT\n");
                break;
            case 16:
                rtapi_print_msg(RTAPI_MSG_INFO, "END_CUT\n");
                break;
            case 17:
                rtapi_print_msg(RTAPI_MSG_INFO, "END_JOB\n\n");
                break;
            case 18:
                rtapi_print_msg(RTAPI_MSG_INFO, "TORCHPULSE\n");
                break;
            case 19:
                rtapi_print_msg(RTAPI_MSG_INFO, "PAUSED_MOTION\n");
                break;
            case 20:
                rtapi_print_msg(RTAPI_MSG_INFO, "OHMIC_TEST\n");
                break;
            case 21:
                rtapi_print_msg(RTAPI_MSG_INFO, "PROBE_TEST\n");
                break;
            case 22:
                rtapi_print_msg(RTAPI_MSG_INFO, "SCRIBING\n");
                break;
            case 23:
                rtapi_print_msg(RTAPI_MSG_INFO, "CONSUMABLE_CHANGE_ON\n");
                break;
            case 24:
                rtapi_print_msg(RTAPI_MSG_INFO, "CONSUMABLE_CHANGE_OFF\n");
                break;
            case 25:
                rtapi_print_msg(RTAPI_MSG_INFO, "CUT_RECOVERY_ON\n");
                break;
            case 26:
                rtapi_print_msg(RTAPI_MSG_INFO, "CUT_RECOVERY_OFF\n");
                break;
            case 27:
                rtapi_print_msg(RTAPI_MSG_INFO, "DEBUG\n");
                break;
        }
    }

}
bues.ch cgit interface