linuxcnc-hal-sys 0.1.5

Generated, unsafe Rust bindings to the LinuxCNC HAL submodule
Documentation
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
component plasmac;

description
"""

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

LinuxCNC V2.9 or later is required to use the "Paused Motion" feature.

.I VERSION:
.br
0.095 - 19 Jan 2020

.I SUMMARY:
.br
Usage of this component is demonstrated in the PlasmaC 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 failure timeout (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  float   axis_z_min_limit            "axis z minimum limit, connect to ini.z.min-limit";
pin in  float   axis_z_max_limit            "axis z maximum limit, connect to ini.z.max-limit";
pin in  float   axis_z_position             "current z axis position, connect to joint.N.pos-fb";
pin in  float   axis_x_position             "current x axis position, connect to axis.x.pos-cmd";
pin in  float   axis_y_position             "current y axis position, connect to axis.y.pos-cmd";
pin in  bit     breakaway                   "torch breakaway switch (optional, see float_switch)";
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  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, set to 0 to use feed rate from gcod file (machine units per minute)";
pin in  float   cut_height                  "cut height (machine units)";
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     external_estop              "external estop input";
pin in  float   feed_reduction              "reduce adaptive feed to this percentage (connect to motion.analog-out-03)";
pin in  float   feed_override               "feed override value from gui (connect to halui.feed-override.value)";
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   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     kerfcross_enable            "enable kerf crossing [mode 0 & mode 1]";
pin in  float   kerfcross_override          "kerf crossing threshold override as a percentage";
pin in  float   lowpass_frequency           "lowpass cutoff frequency for arc voltage output";
pin in  s32     mode                        "operating mode";
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                  "allows the use of multiple tools";
pin in  float   offset_current              "current z axis offset, connect to axis.z.eoffset";
pin in  bit     ohmic_probe_enable          "ohmic probe enable input";
pin in  float   ohmic_probe_offset          "Z axis offset for ohmic probe (machine units)";
pin in  s32     ohmic_max_attempts          "maximum ohmic probe attempts before fallback to float switch";
pin in  bit     ohmic_probe                 "ohmic probe input";
pin in  bit     ohmic_test                  "test for shorted torch";
pin in  float   paused_motion_speed         "multiplier for speed of motion when paused, from -1 to 1";
pin in  float   pause_at_end                "time to pause at end of cut";
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   pierce_height               "pierce height (machine units)";
pin in  float   probe_feed_rate             "probe down velocity (machine units per minute)";
pin in  bit     probe_test                  "probe test only";
pin in  float   probe_start_height          "probe starting height";
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";
pin in  float   requested_velocity          "requested velocity, set by a known requested velocity or connect to motion.requested-vel";
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     thc_enable                  "enable/disable thc and set the IHS skip type";
pin in  bit     thc_disable                 "thc disable";
pin in  float   thc_delay                   "delay from start of cut to THC enable (seconds)";
pin in  float   thc_feed_rate               "maximum feed rate for thc (machine units per minute)";
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  s32     x_y_velocity                "velocity for consumable change";
pin in  float   x_offset                    "offest to apply to axis x for consumable change";
pin in  float   y_offset                    "offest to apply to axis y for consumable change";

/* 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     cornerlock_is_locked        "corner locked indicator";
pin out float   cut_length                  "length of current cut job";
pin out float   cut_time                    "time of current cut job";
pin out bit     feed_hold                   "feed hold, connect to motion.feed_hold";
pin out bit     kerfcross_is_locked         "kerf crossing locked indicator [mode 0 & mode 1]";
pin out bit     led_down                    "thc move down indicator";
pin out bit     led_up                      "thc move up indicator";
pin out s32     offset_counts               "number of counts to offset, connect to axis.z.eoffset-counts";
pin out bit     offset_enable               "enable offsets, connect to axis.z.eoffset-enable";
pin out float   offset_scale                "offset scale, connect to axis.z.eoffset-scale";
pin out bit     ohmic_enable                "on only while probing";
pin out s32     pierce_count                "number of pierce attempts";
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 bit     safe_height_is_limited      "safe height is limited indicator";
pin out bit     scribe_arm                  "arm the scribe";
pin out bit     scribe_on                   "turn scribe on";
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 s32     x_offset_counts             "x offset for consumable change, connect to axis.x.eoffset-counts";
pin out s32     y_offset_counts             "y offset for consumable change, connect to axis.y.eoffset-counts";
pin out float   z_relative                  "distance of Z from last probed height";

/* VARIABLES */
variable double angle_x_y;                  /* angle for x/y velocity calcs in radians*/
variable int    arc_starts;                 /* number of attempts to start torch */
variable float  arc_fail_timer;             /* arc failure timer */
variable float  arc_voltage;                /* calculated arc voltage before lowpass filter*/
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 bool   auto_cut;                   /* auto cut mode is active */
variable float  spotting_timer;             /* spotting timer */
variable bool   spotting;                   /* spotting flag */
variable bool   consumable_changing;        /* consumables are being changed */
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 float  kerf_ratio;                 /* kerf crossing height to distance ratio */
variable float  kerf_threshold;             /* kerf crossing threshold voltage */
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_min;                 /* minimum allowed offset */
variable int    offset_max;                 /* maximum allowed offset */
variable int    ohmic_attempts;             /* current ohmic probe attempts */
variable bool   ohmic_detected;             /* true if ohmic probe detected */
variable int    setup_velocity;             /* velocity for setup moves */
variable float  pause_at_end_timer;         /* pause at end of cut timer */
variable bool   paused_motion;              /* paused motion flag */
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 float  pierce_timer;               /* pierce delay timer */
variable int    pierce_target;              /* pierce height target offset */
variable int    probe_force;                /* extra movement of probe after first contact */
variable int    probe_force_val;            /* distance for probe_force */
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 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  requested_feed_rate;        /* requested feed rate */
variable float  restart_timer;              /* time between torch on attempts*/
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 bool   scribe_pause;               /* scribe pause flag */
variable float  scribe_arm_timer;           /* scribe timer from arm to on*/
variable float  scribe_on_timer;            /* scribe timer from on to motion*/
variable bool   target_sensed;              /* sensed top of stock */
variable int    target_samples = 6;         /* number of samples for setting target_volts */
variable float  target_total;               /* total voltage of samples for setting target_volts */
variable float  target_volts;               /* target voltage for thc, set by arc voltage at cut height */
variable float  thc_delay_timer;            /* thc delay timer */
variable int    thc_velocity;               /* velocity for thc moves */
variable float  torch_off_timer;            /* arc off delay timer */
variable bool   torch_pulse;                /* torch pulse flag */
variable float  torch_pulse_timer;          /* torch pulse timer */
variable float  velocity_scale;             /* the velocity multipler */
variable int    x_velocity;                 /* velocity for x motion for consumable change */
variable int    y_velocity;                 /* velocity for y motion for consumable change */
variable int    zero_target;                /* zero height target offset */

/* DEBUGGING PINS */
pin out s32     state_out                   "current state";
pin out s32     stop_type_out               "current stop type";

function _;

author "Phillip A Carter";

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,
             PAUSE_AT_END,
             SAFE_HEIGHT,
             MAX_HEIGHT,
             FINISH,
             TORCHPULSE,
             PAUSED_MOTION,
             OHMIC_TEST,
             PROBE_TEST,
             SCRIBING,
             CONSUMABLE_CHANGE_ON,
             CONSUMABLE_CHANGE_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;

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;

FUNCTION(_) {

    /* do these first run only */
    if(!initialized && units_per_mm){
        offset_scale = units_per_mm * fperiod;
        velocity_scale = 1 / units_per_mm / 60;
        offset_enable = TRUE;
        adaptive_feed = 1;
        initialized = TRUE;
        probe_force_val = 0.125 * units_per_mm / offset_scale;
    }

    /* 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;
    }

    /* output the relative Z height */
    if(zero_target){
        z_relative = offset_current - zero_target * offset_scale;
    }else{
        z_relative = axis_z_position - axis_z_min_limit;
    }

    /* 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 ? 1:0);

    /* set THC status */
    thc_active = (state == CUT) && target_volts && thc_enabled && !cornerlock_is_locked && !kerfcross_is_locked ? 1:0;

    /* 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){
            adaptive_feed = 1;
        }else if(feed_reduction < 100 && state == CUT){
            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(!probe_test && (external_estop || program_is_idle) && cut_started && !program_run){
            auto_cut = FALSE;
            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(!external_estop){
                pause_at_end_timer = pause_at_end;
                state = PAUSE_AT_END;
            }else{
                state = MAX_HEIGHT;
            }
        }else if(!probe_test && state > IDLE && state <= CUT && 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 && ((!use_auto_volts || (use_auto_volts && target_volts)) || !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 > PIERCE_HEIGHT && 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 > PIERCE_HEIGHT && 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){ ;
                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 */
    arc_voltage = (arc_voltage_in - arc_voltage_offset) * arc_voltage_scale;
    if(lowpass_frequency){
        arc_voltage_out += (arc_voltage - arc_voltage_out) * (1 - pow(2.71828, -(2 * M_PI * lowpass_frequency) * fperiod));
    }else{
        arc_voltage_out = arc_voltage;
    }

    /* 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(mode > 0){
        arc_ok_out = arc_ok_in;
    }else if(arc_voltage_out >= arc_ok_low && arc_voltage_out <= arc_ok_high){
        arc_ok_out = TRUE;
    }else{
        arc_ok_out = FALSE;
    }

    /* reset program states */
    if(program_is_idle){
        program_stop = FALSE;
        program_resume = FALSE;
    }else if(program_is_paused){
        program_pause = FALSE;
    }else if(program_is_running){
        program_run = FALSE;
        program_resume = FALSE;
        auto_cut = TRUE;
    }

    /* if puddlejump height is 0 then set it to 100 */
    if(puddle_jump_height == 0){
        puddle_jump_percent = 100;
    }else{
        puddle_jump_percent = puddle_jump_height;
    }

/* state machine */
    switch(state){
        case IDLE:
            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){
                    state = PAUSED_MOTION;
                /* if we get a consumable change start request and we are paused */
                }else if(consumable_change && stop_type == PAUSE && !consumable_changing){
                    state = CONSUMABLE_CHANGE_ON;

                /* if we get a consumable change stop request and we are changing consumables */
                }else if(!consumable_change && stop_type == PAUSE && consumable_changing){
                        state = CONSUMABLE_CHANGE_OFF;

                /* 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_enabled && 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;
                    }
                    if(cut_offset && cut_offset < skip_ihs_distance && !probe_required){
                        if(thc_enabled){
                            cut_target = cut_target + cut_height_last - cut_height_first;
                            pierce_target = pierce_target + cut_height_last - cut_height_first;
                            puddle_jump_target = puddle_jump_target + cut_height_last - cut_height_first;
                            cut_height_first = cut_height_last;
                        }
                        cut_offset = 0;
                        state = PIERCE_HEIGHT;
                    }else{
                        ohmic_enable = TRUE;
                        if(ohmic_detected && !probe_inhibit){
                            ohmic_enable = FALSE;
                            probe_inhibit = TRUE;
                            if(probe_testing){
                                rtapi_print_msg(RTAPI_MSG_ERR,"ohmic probe detected before moving to probe height.");
                            }else{
                                stop_type = PAUSE;
                                program_pause = TRUE;
                                rtapi_print_msg(RTAPI_MSG_ERR,"ohmic probe detected before moving to probe height.\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 moving to probe height.");
                            }else{
                                stop_type = PAUSE;
                                program_pause = TRUE;
                                rtapi_print_msg(RTAPI_MSG_ERR,"float switch detected before moving to probe height.\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 moving to probe height.");
                            }else{
                                stop_type = PAUSE;
                                program_pause = TRUE;
                                rtapi_print_msg(RTAPI_MSG_ERR,"breakaway switch detected before moving to probe height.\n"
                                                              "program is paused.\n");
                            }
                        }else if (!program_is_paused){
                            if(!first_cut_finished){
                                if(probe_start_height){
                                    probe_start_target = offset_counts - ((axis_z_position - axis_z_min_limit - probe_start_height) / offset_scale);
                                }else{
                                    probe_start_target = offset_counts;
                                }
                            }
                            cut_offset = 0;
                            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;
                    }
                    feed_hold = TRUE;
                    stop_type = NONE;
                /* if torch pulse requested */
                }else if(torch_pulse_start){
                    feed_hold = TRUE;
                    state = TORCHPULSE;
                /* if ohmic probe shorted test requested */
                }else if(ohmic_test){
                    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:
            /* move to probe height at setup speed */
            if(probe_testing && !probe_test){
                state = PROBE_TEST;
                break;
            }
            if(float_switch && !float_detected){
                if(probe_testing){
                    rtapi_print_msg(RTAPI_MSG_ERR,"float switch detected while moving to probe height.");
                    state = PROBE_TEST;
                }else{
                    stop_type = PAUSE;
                    program_pause = TRUE;
                    state = MAX_HEIGHT;
                    rtapi_print_msg(RTAPI_MSG_ERR,"float switch detected while moving to probe height.\n"
                                                  "program is paused.\n");
                }
            }else if(ohmic_detected){
                if(probe_testing){
                    rtapi_print_msg(RTAPI_MSG_ERR,"ohmic probe detected while moving to probe height.");
                    state = PROBE_TEST;
                }else{
                    stop_type = PAUSE;
                    program_pause = TRUE;
                    state = MAX_HEIGHT;
                    rtapi_print_msg(RTAPI_MSG_ERR,"ohmic probe detected while moving to probe height.\n"
                                                  "program is paused.\n");
                }
            }else if(offset_counts - setup_velocity > probe_start_target){
                offset_counts -= setup_velocity;
            }else if(offset_counts + setup_velocity < probe_start_target){
                offset_counts += setup_velocity;
            }else if(offset_counts > probe_start_target || offset_counts < probe_start_target){
                offset_counts = probe_start_target;
            }else{
                if(float_detected){
                    if(!float_switch){
                        float_detected = FALSE;
                    }
                }else{
                    state = PROBE_DOWN;
                }
            }
            break;
        case PROBE_DOWN:
            /* probe down to top of stock at probe speed, then push a little further for good contact */
            feed_hold = TRUE;
            if(probe_testing && !probe_test){
                state = PROBE_TEST;
                break;
            }
            if(target_sensed){
                if(probe_force > 0){
                    probe_force -= 21;
                    offset_counts -= probe_velocity;
                }else{
                    target_sensed = FALSE;
                    if(float_switch || ohmic_detected){
                        state = PROBE_UP;
                    }else{
                        rtapi_print_msg(RTAPI_MSG_ERR,"probe trip error while probing.");
                        state = IDLE;
                    }
                }
            }else if(!float_switch && !ohmic_detected){
                if(axis_z_position - (probe_velocity * offset_scale) <= axis_z_min_limit){
                    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;
                    }else{
                        rtapi_print_msg(RTAPI_MSG_ERR,"bottom limit reached while probe testing.\n");
                    }
                    state = MAX_HEIGHT;
                }else{
                    offset_counts -= probe_velocity;
                }
            }else if(float_switch && !float_detected){
                if(ohmic_attempts == ohmic_max_attempts){
                    probe_type = FLOAT;
                    target_sensed = TRUE;
                    probe_force = probe_force_val;
                    ohmic_attempts = 0;
                    float_detected = FALSE;
                }else{
                    state = PROBE_HEIGHT;
                    ohmic_attempts += 1;
                    float_detected = TRUE;
                }
            }else if(ohmic_detected){
                probe_type = OHMIC;
                target_sensed = TRUE;
                probe_force = probe_force_val;
                ohmic_attempts = 0;
            }
            break;
        case PROBE_UP:
            /* probe up at minimum speed to find top of stock */
            if(probe_testing && !probe_test){
                state = PROBE_TEST;
                break;
            }
            if(float_switch || ohmic_detected){
                offset_counts += 1;
            }else{
                if(probe_type == OHMIC){
                    probe_offset = ohmic_probe_offset / offset_scale;
                }else{
                    probe_offset = float_switch_travel / offset_scale;
                }
                zero_target = offset_counts + probe_offset;
                cut_target = cut_height_first = offset_datum = offset_counts + probe_offset + (cut_height / offset_scale);
                pierce_target = offset_counts + probe_offset + (pierce_height / offset_scale);
                puddle_jump_target = offset_counts + probe_offset + (pierce_height * (puddle_jump_percent / 100) / offset_scale);
                safe_min = (pierce_height + (1 * units_per_mm)) / offset_scale;
                safe_preferred = safe_height / offset_scale;
                offset_min = offset_counts - ((axis_z_position - axis_z_min_limit) / offset_scale);
                offset_max = offset_counts + (axis_z_max_limit - axis_z_position - (1 * units_per_mm)) / offset_scale;
                if(safe_height == 0){
                    safe_target = offset_max;
                    ohmic_enable = FALSE;
//                    state = ZERO_HEIGHT;
                    state = PIERCE_HEIGHT;
                }else if(offset_counts + safe_min >= offset_max){
                    safe_target = offset_max;
                    rtapi_print_msg(RTAPI_MSG_ERR, "material too high for safe traverse.\n"
                                                   "program is paused.\n");
                    stop_type = PAUSE;
                    program_pause = TRUE;
                    state = MAX_HEIGHT;
                }else if(offset_counts + safe_preferred >= offset_max && !safe_height_is_limited){
                    rtapi_print_msg(RTAPI_MSG_ERR, "safe traverse height has been reduced.\n");
                    safe_target = offset_max;
                    safe_height_is_limited = TRUE;
                    ohmic_enable = FALSE;
//                    state = ZERO_HEIGHT;
                    state = PIERCE_HEIGHT;
                }else{
                    safe_target = offset_counts + safe_preferred;
                    ohmic_enable = FALSE;
//                    state = ZERO_HEIGHT;
                    state = PIERCE_HEIGHT;
                }
            }
            break;
        case ZERO_HEIGHT:
            probe_required = FALSE;
            if(probe_testing && !probe_test){
                state = PROBE_TEST;
                break;
            }
            if(offset_counts - setup_velocity > zero_target){
                offset_counts -= setup_velocity;
            }else if(offset_counts + setup_velocity < zero_target){
                offset_counts += setup_velocity;
            }else{
                offset_counts = zero_target;
                axis_x_start = axis_x_position;
                axis_y_start = axis_y_position;
                state = PIERCE_HEIGHT;
            }
            break;
        case PIERCE_HEIGHT:
            /* move up to pierce height */
            if(probe_testing && !probe_test){
                state = PROBE_TEST;
                break;
            }
            if(pierce_height && cut_height && (use_auto_volts || (!use_auto_volts && cut_volts))){
                feed_hold = TRUE;
                if(offset_counts - setup_velocity > pierce_target){
                    offset_counts -= setup_velocity;
                }else if(offset_counts + setup_velocity < pierce_target){
                    offset_counts += setup_velocity;
                }else{
                    offset_counts = pierce_target;
                    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){
                        state = TORCH_ON;
                    }
                }
            }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.");
            }
            break;
        case TORCH_ON:
            /* 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){
                program_pause = TRUE;
                restart_timer = 0;
                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;
                    state = ARC_OK;
                }
            }
            break;
        case ARC_OK:
            /* 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 >= 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 && tool != SPOTTING){
                    pierce_timer = pierce_delay;
                    state = PIERCE_DELAY;
            }
            break;
        case PIERCE_DELAY:
            /* wait for arc to pierce stock */
            feed_hold = TRUE;
            if(pierce_timer > 0){
                pierce_timer -= fperiod;
            }else{
                puddle_jump_timer = puddle_jump_delay;
                state = PUDDLE_JUMP;
            }
            break;
        case PUDDLE_JUMP:
            /* move to puddle_jump height */
            feed_hold = TRUE;
            if(offset_counts - setup_velocity > puddle_jump_target){
                offset_counts -= setup_velocity;
            }else if(offset_counts + setup_velocity < puddle_jump_target){
                offset_counts += setup_velocity;
            }else{
                offset_counts = puddle_jump_target;
                count = 0;
                if(puddle_jump_timer > 0){
                    feed_hold = FALSE;
                    puddle_jump_timer -= fperiod;
                }else{
                    puddle_jump_timer = 0;
                    feed_hold = TRUE;
                    state = CUT_HEIGHT;
                }
            }
            break;
        case CUT_HEIGHT:
            /* move to cut height */
            feed_hold = TRUE;
            if(offset_counts - setup_velocity > cut_target){
                offset_counts -= setup_velocity;
            }else if(offset_counts + setup_velocity < cut_target){
                offset_counts += setup_velocity;
            }else{
                offset_counts = cut_target;
                if((int)floor(offset_current / offset_scale) <= cut_target + 1 && 
                   (int)floor(offset_current / offset_scale) >= cut_target - 1){
                    count = 0;
                    /* set feed rate for this cut */
                    if(cut_feed_rate > 0){
                        requested_feed_rate = cut_feed_rate;
                    }else{
                        requested_feed_rate = requested_velocity;
                    }
                    feed_hold = FALSE;
                    thc_delay_timer = thc_delay;
                    state = CUT;
                }
            }
            break;
        case CUT:
            /* while cutting and it is not a dry run:
             * 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 kerf crossing is enabled, only allow THC if the voltage change is less than the threshold voltage (modes 0 & 1 only)
             * adjust torch height and target voltage to suit if height override requested (modes 0 & 1 only)*/
            if(torch_on){
//            if(torch_enable){
                /* thc control by arc voltage */
                if(mode < 2){
                    thc_delay_timer -= fperiod;
                    if(thc_delay_timer <= 0){
                        /* set target voltage */
                        if(target_volts == 0){
                            if(use_auto_volts){
                                /* wait until velocity is at least 99.9% of requested velocity before sampling arc voltage */
                                if(current_velocity * 60 > requested_feed_rate * feed_override * 0.999){
                                    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;
                                    }
                                }
                                if(target_volts == 0){
                                    cornerlock_is_locked = TRUE;
                                }else{
                                    cornerlock_is_locked = FALSE;
                                }
                            }else{
                                target_volts = cut_volts;
                            }
                        /* height override setup*/
                        }else if(fabs(height_override - height_ovr_old) > 0.05){
                            height_ovr_counts -= (height_override - height_ovr_old) / 10 * units_per_mm / offset_scale;
                            height_ovr_old = height_override;
                        /* height override z motion */
                        }else if(height_ovr_counts != 0){
                            if((setup_velocity) < height_ovr_counts){
                                offset_counts -= setup_velocity;
                                height_ovr_counts -= setup_velocity;
                            }else{
                                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 * feed_override * cornerlock_threshold * 0.01){
                                    cornerlock_is_locked = TRUE;
                                }else if(cornerlock_is_locked && current_velocity * 60 > requested_feed_rate * feed_override * 0.99){
                                    cornerlock_is_locked = FALSE;
                                }
                            }else{
                                cornerlock_is_locked = FALSE;
                            }
                            /* unlock kerfcross lock if voltage reduced to safe level  */
                            if(kerfcross_is_locked){
//                                if(arc_voltage_out < last_arc_voltage + (kerf_threshold * 0.05)){
                                if(arc_voltage_out < target_volts + (kerf_threshold * 0.5)){
                                    kerfcross_is_locked = FALSE;
                                    last_arc_voltage = arc_voltage_out;
                                }
                            /* set kerfcross lock if voltage change > kerfcross threshold volts */
                            }else if(kerfcross_enable){
                                if(thc_feed_rate < current_velocity * 60){
                                    kerf_ratio = thc_feed_rate / (current_velocity * 60);
                                }else{
                                    kerf_ratio = 1; /* 45 degree ramp */
                                }
                                kerf_threshold = ((kerf_ratio * current_velocity * fperiod) / height_per_volt) * (kerfcross_override * 0.01);
//                                if(arc_voltage_out > last_arc_voltage + kerf_threshold){
                                if(arc_voltage_out > target_volts + kerf_threshold){
                                    kerfcross_is_locked = TRUE;
                                }else{
                                    last_arc_voltage = arc_voltage_out;
                                }
                            }
                            /* do thc if ok to go */
                            if(!cornerlock_is_locked && !kerfcross_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;
                                pid_output += pid_error_now * pid_i_gain * fperiod;
                                pid_output += (pid_error_now - pid_error_old) * pid_d_gain / 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(offset_counts + pid_output <= offset_min || offset_counts + pid_output >= offset_max){
                                    torch_on = FALSE;
                                    stop_type = PAUSE;
                                    program_pause = TRUE;
                                    if(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;

                                }
                                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;
                    }
                }else{ /* thc control by move-up and move-down inputs (no kerf crossing in this mode) */
                    if(thc_enabled){
                        /* lock thc if velocity < requested velocity * cornerlock threshold percentage */
                        if(cornerlock_enable){
                            if(current_velocity * 60 < requested_feed_rate * feed_override * cornerlock_threshold * 0.01){
                                cornerlock_is_locked = TRUE;
                            }else if(cornerlock_is_locked && current_velocity * 60 > requested_feed_rate * feed_override * 0.99){
                                cornerlock_is_locked = FALSE;
                            }
                        }else{
                            cornerlock_is_locked = FALSE;
                        }
                        if(move_down && !cornerlock_is_locked){
                            if(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 */
                                offset_counts -= thc_velocity;
                                led_down = TRUE;
                            }
                        }else if(move_up && !cornerlock_is_locked){
                            if(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 */
                                offset_counts += thc_velocity;
                                led_up = TRUE;
                            }
                        }else{
                            led_down = FALSE;
                            led_up = FALSE;
                        }
                    }
                }
                /* check if safe height is below maximium offset */
                if(offset_counts > offset_datum){
                    safe_target += offset_counts - offset_datum;
                    offset_datum = offset_counts;
                    if(safe_target > offset_max){
                        safe_target = offset_max;
                        if(!safe_height_is_limited){
                            safe_height_is_limited = TRUE;
                            rtapi_print_msg(RTAPI_MSG_ERR, "safe traverse height has been reduced.");
                        }
                    }
                }
            }
            cut_height_last = 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:
            /* move to safe height */
            feed_hold = TRUE;
            if(!torch_off_timer || !torch_on){
                cornerlock_is_locked = FALSE;
                if(!probe_test){
                    if(offset_counts - setup_velocity > safe_target){
                        offset_counts -= setup_velocity;
                    }else if(offset_counts + setup_velocity < safe_target){
                        offset_counts += setup_velocity;
                    }else{
                        offset_counts = safe_target;
                        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 = FINISH;
                    }
                }
            }
            break;
        case MAX_HEIGHT:
            /* move to maximum height */
            feed_hold = TRUE;
            cornerlock_is_locked = FALSE;
            if(!probe_test){
                if(offset_counts - setup_velocity > offset_max){
                    offset_counts -= setup_velocity;
                }else if(offset_counts + setup_velocity < offset_max){
                    offset_counts += setup_velocity;
                }else{
                    offset_counts = offset_max;
                    if(stop_type == WAIT){
                        feed_hold = FALSE;
                    }
                    /* do height override here for remainder of job */
                    height_ovr_old = 0;
                    state = FINISH;
                }
            }
            break;
        case FINISH:
            /* clean up and return to idle state */
            target_volts = 0;
            cornerlock_is_locked = FALSE;
            kerfcross_is_locked = FALSE;
            led_down = FALSE;
            led_up = FALSE;
            ohmic_enable = FALSE;
            cut_length = 0;
            cut_time = 0;
            if(stop_type == STOP){
                auto_cut = FALSE;
                manual_cut = FALSE;
                program_run = FALSE;
                paused_motion = FALSE;
                adaptive_feed = 1;
                if(program_is_idle){
                    first_cut_finished = FALSE;
                    if(offset_counts - setup_velocity > 0){
                        offset_counts -= setup_velocity;
                    }else if(offset_counts + setup_velocity < 0){
                        offset_counts += setup_velocity;
                    }else{
                        offset_counts = 0;
                        stop_type = NONE;
                        safe_height_is_limited = FALSE;
                        cut_started = FALSE;
                        /* do height override here for one cut only */
                        /* height_ovr_old = 0; */
                        if(consumable_changing){
                            state = CONSUMABLE_CHANGE_OFF;
                        }
                    }
                }
            }else{
                state = IDLE;
            }
            break;
        case TORCHPULSE:
            /* single pulse the torch on and off */
            if(!torch_pulse){
                torch_pulse_timer = torch_pulse_time;
                if(torch_enable){
                    torch_on = TRUE;
                }
                torch_pulse = TRUE;
            }else{
                if(torch_pulse_timer > 0){
                    torch_pulse_timer -= fperiod;
                }else{
                    torch_on = FALSE;
                    if(!torch_pulse_start){
                        torch_pulse = FALSE;
                        state = IDLE;
                    }
                }
            }
            break;
        case PAUSED_MOTION:
            /* a bit kludgy but we need a timer here for a minimum run
             * time to give the GUI time to poll the status channel */
            if(paused_motion_speed){
                if(!paused_motion){
                    paused_motion_timer = 0.2; /* 0.2 seconds */
                    paused_motion = TRUE;
                    adaptive_feed = paused_motion_speed;
                    feed_hold = FALSE;
                    program_resume = TRUE;
                }else{
                    paused_motion_timer -= fperiod;
                }
            }else{
                paused_motion_timer -= fperiod;
                feed_hold = TRUE;
                program_pause = TRUE;
                if(program_is_paused && paused_motion_timer <= 0){
                    paused_motion = FALSE;
                    adaptive_feed = 1;
                    state = IDLE;
                }
            }
            break;
        case OHMIC_TEST:
            /* wait here until ohmic_test input released */
            if (!ohmic_test){
                ohmic_enable = FALSE;
                state = IDLE;
            }
            break;
        case PROBE_TEST:
            /* wait here until probe_test input released */
            if(!probe_test){
                probe_testing = FALSE;
                if(offset_counts - setup_velocity > 0){
                    offset_counts -= setup_velocity;
                }else if(offset_counts + setup_velocity < 0){
                    offset_counts += setup_velocity;
                }else{
                    offset_counts = 0;
                    state = FINISH;
                }
            }
            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:
            if(!consumable_changing){
                if(x_offset == 0 && y_offset == 0){
                    x_velocity = 0;
                    y_velocity = 0;
                }else if(x_offset == 0){
                    x_velocity = 0;
                    if(y_offset > 0){
                        y_velocity = x_y_velocity;
                    }else{
                        y_velocity = x_y_velocity * -1;
                    }
                }else if(y_offset == 0){
                    if(x_offset > 0){
                        x_velocity = x_y_velocity;
                    }else{
                        x_velocity = x_y_velocity * -1;
                    }
                }else{
                    angle_x_y = atan2(y_offset, x_offset);
                    x_velocity = x_y_velocity * cos(angle_x_y);
                    y_velocity = x_y_velocity * sin(angle_x_y);
                }
                consumable_changing = TRUE;
            }else{
                if(x_velocity && x_offset_counts != x_offset){
                    if(fabs(x_offset_counts) + fabs(x_velocity) < fabs(x_offset)){
                        x_offset_counts += x_velocity;
                    }else{
                        x_offset_counts = x_offset;
                    }
                }
                if(y_velocity && y_offset_counts != y_offset){
                    if(fabs(y_offset_counts) + fabs(y_velocity) < fabs(y_offset)){
                        y_offset_counts += y_velocity;
                    }else{
                        y_offset_counts = y_offset;
                    }
                }
            }
            if(x_offset_counts == x_offset && y_offset_counts == y_offset){
                state = IDLE;
            }
            break;
        case CONSUMABLE_CHANGE_OFF:
            if(x_velocity){
                if(fabs(x_offset_counts) - fabs(x_velocity) > 0){
                    x_offset_counts -= x_velocity;
                }else{
                    x_offset_counts = 0;
                }
            }
            if(y_velocity){
                if(fabs(y_offset_counts) - fabs(y_velocity) > 0){
                    y_offset_counts -= y_velocity;
                }else{
                    y_offset_counts = 0;
                }
            }
            if(!x_offset_counts && !y_offset_counts){
                consumable_changing = FALSE;
                state = IDLE;
            }
            break;
        case DEBUG:
            /* holding state for debugging */
            rtapi_print_msg(RTAPI_MSG_ERR, "I have no idea how we got here...\n");
            break;
    }

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

}