4949
5050typedef enum {
5151 Plasma_ModeOff = 0 ,
52- Plasma_ModeVoltage = 1 ,
53- Plasma_ModeUpDown = 2
52+ Plasma_ModeVoltage ,
53+ Plasma_ModeUpDown ,
54+ Plasma_ModeArcOK
5455} plasma_mode_t ;
5556
5657typedef union {
@@ -115,9 +116,10 @@ static void state_idle (void);
115116static void state_thc_delay (void );
116117static void state_thc_pid (void );
117118static void state_thc_adjust (void );
119+ static void state_arc_monitor (void );
118120static void state_vad_lock (void );
119121
120- static bool set_feed_override = false, updown_enabled = false, init_ok = false;
122+ static bool set_feed_override = false, updown_enabled = false, init_ok = false, thc_disabled = false ;
121123static uint8_t n_ain , n_din ;
122124static uint8_t port_arc_ok , port_arc_voltage ;
123125static uint_fast8_t feed_override , segment_id = 0 ;
@@ -129,10 +131,11 @@ static float fr_pgm, fr_actual, fr_thr_99, fr_thr_vad;
129131static thc_signals_t thc = {0 };
130132static pidf_t pid ;
131133static nvs_address_t nvs_address ;
132- static char thc_modes [] = "Off,Voltage,Up/down" ;
134+ static char thc_modes [] = "Off,Voltage,Up/down,Arc ok " ;
133135static plasma_settings_t plasma ;
134136static st2_motor_t * z_motor ;
135137static void (* volatile stateHandler )(void ) = state_idle ;
138+ static plasma_mode_t mode = Plasma_ModeOff ;
136139static xbar_t arc_ok , cutter_down , cutter_up , parc_voltage ;
137140
138141static settings_changed_ptr settings_changed ;
@@ -260,10 +263,13 @@ static void enumeratePins (bool low_level, pin_info_ptr pin_info, void *data)
260263static void digital_out (uint8_t portnum , bool on )
261264{
262265 if (portnum == PLASMA_THC_DISABLE_PORT ) {
263- if (!(thc .enabled = !on ))
264- stateHandler = state_idle ;
265- else if (thc .arc_ok )
266- stateHandler = plasma .mode == Plasma_ModeUpDown ? state_thc_adjust : state_thc_pid ;
266+ thc_disabled = on ;
267+ if (thc .arc_ok && mode != Plasma_ModeArcOK ) {
268+ if (!(thc .enabled = !thc_disabled ))
269+ stateHandler = state_arc_monitor ;
270+ else
271+ stateHandler = mode == Plasma_ModeUpDown ? state_thc_adjust : state_thc_pid ;
272+ }
267273 } else if (portnum == PLASMA_TORCH_DISABLE_PORT ) {
268274 // PLASMA_TORCH_DISABLE_PORT:
269275 // TODO
@@ -377,12 +383,12 @@ static void pause_on_error (void)
377383
378384static void state_idle (void )
379385{
380- if (plasma . mode == Plasma_ModeVoltage )
386+ if (mode == Plasma_ModeVoltage )
381387 arc_voltage = parc_voltage .get_value (& parc_voltage ) * plasma .arc_voltage_scale - plasma .arc_voltage_offset ;
382388
383389 if (plasma .option .sync_pos && state_get () == STATE_IDLE ) {
384390
385- if (plasma . mode != Plasma_ModeUpDown )
391+ if (mode != Plasma_ModeUpDown )
386392 step_count = (uint32_t )st2_get_position (z_motor );
387393
388394 if (step_count && state_get () == STATE_IDLE ) {
@@ -397,8 +403,10 @@ static void state_idle (void)
397403static void state_thc_delay (void )
398404{
399405 if (hal .get_elapsed_ticks () >= thc_delay ) {
400- thc .enabled = On ;
401- if (plasma .mode == Plasma_ModeUpDown ) {
406+
407+ if (!(thc .enabled = !(thc_disabled || mode == Plasma_ModeArcOK )))
408+ stateHandler = state_arc_monitor ;
409+ else if (mode == Plasma_ModeUpDown ) {
402410 step_count = 0 ;
403411 stateHandler = state_thc_adjust ;
404412 } else {
@@ -411,6 +419,12 @@ static void state_thc_delay (void)
411419 }
412420}
413421
422+ static void state_arc_monitor (void )
423+ {
424+ if (!(thc .arc_ok = arc_ok .get_value (& arc_ok ) == 1.0f ))
425+ pause_on_error ();
426+ }
427+
414428static void state_thc_adjust (void )
415429{
416430 if ((thc .arc_ok = arc_ok .get_value (& arc_ok ) == 1.0f )) {
@@ -550,7 +564,7 @@ static void arcSetState (spindle_ptrs_t *spindle, spindle_state_t state, float r
550564 spindle_set_state_ (spindle , state , rpm );
551565 thc .torch_on = On ;
552566 report_message ("arc on" , Message_Plain );
553- if ((thc .arc_ok = rport .wait_on_input (Port_Digital , port_arc_ok , WaitMode_High , plasma .arc_fail_timeout ) != -1 )) {
567+ if ((thc .arc_ok = hal . port .wait_on_input (Port_Digital , port_arc_ok , WaitMode_High , plasma .arc_fail_timeout ) != -1 )) {
554568 report_message ("arc ok" , Message_Plain );
555569 retries = 0 ;
556570 thc_delay = hal .get_elapsed_ticks () + (uint32_t )ceilf (1000.0f * plasma .thc_delay ); // handle overflow!
@@ -647,31 +661,6 @@ static void onRealtimeReport (stream_write_ptr stream_write, report_tracking_fla
647661 stream_write (buf );
648662
649663 thc .report_up = thc .report_down = Off ;
650- /*
651- if(thc.value) {
652- *append++ = ',';
653- if(thc.arc_ok)
654- *append++ = 'A';
655- if(thc.enabled)
656- *append++ = 'E';
657- if(thc.active)
658- *append++ = 'R';
659- if(thc.torch_on)
660- *append++ = 'T';
661- if(thc.ohmic_probe)
662- *append++ = 'O';
663- if(thc.velocity_lock)
664- *append++ = 'V';
665- if(thc.void_lock)
666- *append++ = 'H';
667- if(thc.report_down)
668- *append++ = 'D';
669- if(thc.report_up)
670- *append++ = 'U';
671- thc.report_up = thc.report_down = Off;
672- }
673- *append = '\0';
674- */
675664
676665 if (on_realtime_report )
677666 on_realtime_report (stream_write , report );
@@ -682,7 +671,10 @@ static void onSpindleSelected (spindle_ptrs_t *spindle)
682671 spindle_set_state_ = spindle -> set_state ;
683672
684673 spindle -> set_state = arcSetState ;
685- spindle -> cap .at_speed = Off ; // TODO: only disable if PWM spindle active?
674+ // TODO: only change caps if PWM spindle active?
675+ spindle -> cap .at_speed = Off ;
676+ //?? spindle->cap.laser = Off;
677+ spindle -> cap .torch = On ;
686678
687679 if (on_spindle_selected )
688680 on_spindle_selected (spindle );
@@ -697,6 +689,9 @@ static void plasma_setup (settings_t *settings, settings_changed_flags_t changed
697689 driver_reset = hal .driver_reset ;
698690 hal .driver_reset = reset ;
699691
692+ on_spindle_selected = grbl .on_spindle_selected ;
693+ grbl .on_spindle_selected = onSpindleSelected ;
694+
700695 on_realtime_report = grbl .on_realtime_report ;
701696 grbl .on_realtime_report = onRealtimeReport ;
702697
@@ -869,7 +864,7 @@ static void plasma_settings_save (void)
869864
870865static void plasma_settings_restore (void )
871866{
872- plasma .mode = updown_enabled ? Plasma_ModeUpDown : Plasma_ModeVoltage ;
867+ plasma .mode = mode ;
873868 plasma .option .flags = 0 ;
874869 plasma .thc_delay = 3.0f ;
875870 plasma .thc_threshold = 1.0f ;
@@ -903,7 +898,7 @@ static bool plasma_claim_digital_in (xbar_t *target, uint8_t port, const char *d
903898 xbar_t * p ;
904899 bool ok = false;
905900
906- if ((p = ioport_get_info (Port_Digital , Port_Input , port )) && p -> get_value && !p -> mode .claimed ) {
901+ if (port != 255 && (p = ioport_get_info (Port_Digital , Port_Input , port )) && p -> get_value && !p -> mode .claimed ) {
907902 memcpy (target , p , sizeof (xbar_t ));
908903 if ((ok = ioport_claim (Port_Digital , Port_Input , & port , description )) && target == & arc_ok )
909904 port_arc_ok = port ;
@@ -919,45 +914,47 @@ static void plasma_settings_load (void)
919914 plasma_settings_restore ();
920915 }
921916
922- port_arc_voltage = plasma .port_arc_voltage ;
917+ if ((mode = plasma .mode ) == Plasma_ModeOff )
918+ return ;
923919
924- if (!(init_ok = n_ain == 0 )) {
920+ if (!(init_ok = mode != Plasma_ModeVoltage )) {
925921
926- xbar_t * p ;
922+ if ( plasma . port_arc_voltage != 255 ) {
927923
928- if (port_arc_voltage == 255 )
929- plasma .port_arc_voltage = port_arc_voltage = 0 ;
924+ xbar_t * p ;
930925
931- if ((p = ioport_get_info (Port_Analog , Port_Input , port_arc_voltage )) && p -> get_value && !p -> mode .claimed ) {
932- memcpy (& parc_voltage , p , sizeof (xbar_t ));
933- init_ok = ioport_claim (Port_Analog , Port_Input , & port_arc_voltage , "Arc voltage" );
926+ if ((p = ioport_get_info (Port_Analog , Port_Input , port_arc_voltage )) && p -> get_value && !p -> mode .claimed ) {
927+ memcpy (& parc_voltage , p , sizeof (xbar_t ));
928+ init_ok = ioport_claim (Port_Analog , Port_Input , & port_arc_voltage , "Arc voltage" );
929+ }
934930 }
935931
936- // init_ok = ioport_claim(Port_Analog, Port_Input, &port_arc_voltage, "Arc voltage");
932+ if (!init_ok ) {
933+ init_ok = true;
934+ mode = Plasma_ModeUpDown ;
935+ }
937936 }
938937
939938 init_ok = init_ok && plasma_claim_digital_in (& arc_ok , plasma .port_arc_ok , "Arc ok" );
940- if (init_ok && n_din > 2 && plasma .port_cutter_down != 255 ) {
941- init_ok = plasma_claim_digital_in (& cutter_down , plasma .port_cutter_down , "Cutter down" );
942- init_ok = init_ok && plasma_claim_digital_in (& cutter_up , plasma .port_cutter_up , "Cutter up" );
939+
940+ if (init_ok && mode == Plasma_ModeUpDown ) {
941+ if (!(plasma_claim_digital_in (& cutter_down , plasma .port_cutter_down , "Cutter down" ) &&
942+ plasma_claim_digital_in (& cutter_up , plasma .port_cutter_up , "Cutter up" )))
943+ mode = Plasma_ModeArcOK ;
943944 }
944945
945946 if (init_ok ) {
946947
947- if (n_ain == 0 && plasma .mode == Plasma_ModeVoltage )
948- plasma .mode = Plasma_ModeUpDown ;
949-
950- if (n_din < 3 && plasma .mode == Plasma_ModeUpDown )
951- plasma .mode = n_ain >= 1 ? Plasma_ModeVoltage : Plasma_ModeOff ;
952-
953- updown_enabled = plasma .mode == Plasma_ModeUpDown ;
948+ updown_enabled = mode == Plasma_ModeUpDown ;
954949
955950 settings_changed = hal .settings_changed ;
956951 hal .settings_changed = plasma_setup ;
957952
953+ if (plasma .mode != mode )
954+ protocol_enqueue_foreground_task (report_warning , "Plasma mode changed due to lack of inputs!" );
955+
958956 if (plasma .option .virtual_ports )
959957 protocol_enqueue_foreground_task (add_virtual_ports , NULL );
960-
961958 } else
962959 protocol_enqueue_foreground_task (report_warning , "Plasma mode failed to initialize!" );
963960}
@@ -971,9 +968,25 @@ static void onReportOptions (bool newopt)
971968{
972969 on_report_options (newopt );
973970
974- if (!newopt )
975- report_plugin ("PLASMA" , "0.17" );
976- else if (driver_reset ) // non-null when successfully enabled
971+ if (!newopt ) {
972+
973+ plasma_mode_t i = Plasma_ModeOff ;
974+ char buf [30 ] = "PLASMA (" , * s1 = & buf [8 ], * s2 = thc_modes , c ;
975+
976+ while ((c = * s2 ++ )) {
977+ if (i == mode ) {
978+ if (c == ',' )
979+ break ;
980+ * s1 ++ = c ;
981+ } else if (c == ',' )
982+ i ++ ;
983+ }
984+ * s1 ++ = ')' ;
985+ * s1 = '\0' ;
986+
987+ report_plugin (buf , "0.18" );
988+
989+ } else if (mode != Plasma_ModeOff )
977990 hal .stream .write (",THC" );
978991}
979992
@@ -1018,19 +1031,16 @@ void plasma_init (void)
10181031 on_report_options = grbl .on_report_options ;
10191032 grbl .on_report_options = onReportOptions ;
10201033
1021- on_spindle_selected = grbl .on_spindle_selected ;
1022- grbl .on_spindle_selected = onSpindleSelected ;
1023-
10241034/*
10251035 control_interrupt_callback = hal.control_interrupt_callback;
10261036 hal.control_interrupt_callback = trap_control_interrupts;
10271037*/
10281038
10291039 if (n_ain == 0 )
1030- setting_remove_elements (Setting_THC_Mode , 0b101 );
1040+ setting_remove_elements (Setting_THC_Mode , 0b1101 );
10311041
10321042 if (n_din < 3 )
1033- setting_remove_elements (Setting_THC_Mode , 0b011 );
1043+ setting_remove_elements (Setting_THC_Mode , 0b1011 );
10341044
10351045 } else
10361046 protocol_enqueue_foreground_task (report_warning , "Plasma mode failed to initialize!" );
0 commit comments