forked from grblHAL/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state_machine.c
847 lines (689 loc) · 32.1 KB
/
state_machine.c
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
/*
state_machine.c - An embedded CNC Controller with rs274/ngc (g-code) support
Main state machine
Part of grblHAL
Copyright (c) 2018-2023 Terje Io
Copyright (c) 2011-2016 Sungeun K. Jeon for Gnea Research LLC
Copyright (c) 2009-2011 Simen Svale Skogsrud
Grbl is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Grbl is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Grbl. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include "hal.h"
#include "motion_control.h"
#include "state_machine.h"
#include "override.h"
static void state_idle (uint_fast16_t new_state);
static void state_cycle (uint_fast16_t rt_exec);
static void state_await_hold (uint_fast16_t rt_exec);
static void state_noop (uint_fast16_t rt_exec);
static void state_await_motion_cancel (uint_fast16_t rt_exec);
static void state_await_resume (uint_fast16_t rt_exec);
static void state_await_toolchanged (uint_fast16_t rt_exec);
static void state_await_waypoint_retract (uint_fast16_t rt_exec);
static void state_restore (uint_fast16_t rt_exec);
static void state_await_resumed (uint_fast16_t rt_exec);
static void (* volatile stateHandler)(uint_fast16_t rt_exec) = state_idle;
typedef struct {
coolant_state_t coolant;
spindle_num_t spindle_num; // Active spindle
spindle_t spindle[N_SYS_SPINDLE];
} restore_condition_t;
static restore_condition_t restore_condition;
static sys_state_t pending_state = STATE_IDLE, sys_state = STATE_IDLE;
typedef union {
uint8_t value;
struct {
uint8_t active :1,
motion :1,
restart :1,
restoring :1,
unassigned :4;
};
} parking_flags_t;
typedef struct {
float target[N_AXIS];
float restore_target[N_AXIS];
float retract_waypoint;
volatile parking_flags_t flags;
plan_line_data_t plan_data;
} parking_data_t;
// Declare and initialize parking local variables
static parking_data_t park = {0};
static void state_spindle_restore (spindle_t *spindle)
{
if(spindle->hal)
spindle_restore(spindle->hal, spindle->state, spindle->rpm);
}
static void state_spindle_set_state (spindle_t *spindle)
{
if(spindle->hal)
spindle_set_state(spindle->hal, spindle->state, spindle->rpm);
}
static void state_restore_conditions (restore_condition_t *condition)
{
if (!settings.parking.flags.enabled || !park.flags.restart) {
spindle_num_t spindle_num = N_SYS_SPINDLE;
park.flags.restoring = On; //
do {
state_spindle_restore(&condition->spindle[--spindle_num]);
} while(spindle_num);
// Block if safety door re-opened during prior restore actions.
if (gc_state.modal.coolant.value != hal.coolant.get_state().value) {
// NOTE: Laser mode will honor this delay. An exhaust system is often controlled by this signal.
coolant_set_state(condition->coolant);
delay_sec(settings.safety_door.coolant_on_delay, DelayMode_SysSuspend);
}
park.flags.restoring = Off;
sys.override.spindle_stop.value = 0; // Clear spindle stop override states
}
}
static void enter_sleep (void)
{
st_go_idle();
spindle_all_off();
hal.coolant.set_state((coolant_state_t){0});
grbl.report.feedback_message(Message_SleepMode);
stateHandler = state_noop;
}
static bool initiate_hold (uint_fast16_t new_state)
{
spindle_ptrs_t *spindle;
spindle_num_t spindle_num = N_SYS_SPINDLE;
if (settings.parking.flags.enabled) {
plan_data_init(&park.plan_data);
park.plan_data.condition.system_motion = On;
park.plan_data.condition.no_feed_override = On;
park.plan_data.line_number = PARKING_MOTION_LINE_NUMBER;
}
plan_block_t *block = plan_get_current_block();
restore_condition.spindle_num = 0;
do {
if((spindle = spindle_get(--spindle_num))) {
if(block && block->spindle.hal == spindle) {
restore_condition.spindle_num = spindle_num;
restore_condition.spindle[spindle_num].hal = block->spindle.hal;
restore_condition.spindle[spindle_num].rpm = block->spindle.rpm;
restore_condition.spindle[spindle_num].state = block->spindle.state;
} else if(gc_state.spindle.hal == spindle) {
restore_condition.spindle_num = spindle_num;
restore_condition.spindle[spindle_num].hal = gc_state.spindle.hal;
restore_condition.spindle[spindle_num].rpm = gc_state.spindle.rpm;
restore_condition.spindle[spindle_num].state = gc_state.modal.spindle.state;
} else {
restore_condition.spindle[spindle_num].hal = spindle;
restore_condition.spindle[spindle_num].rpm = spindle->param->rpm;
restore_condition.spindle[spindle_num].state = spindle->param->state;
}
} else
restore_condition.spindle[spindle_num].hal = NULL;
} while(spindle_num);
if (block)
restore_condition.coolant.mask = block->condition.coolant.mask;
else
restore_condition.coolant.mask = gc_state.modal.coolant.mask | hal.coolant.get_state().mask;
if (restore_condition.spindle[restore_condition.spindle_num].hal->cap.laser && settings.flags.disable_laser_during_hold)
enqueue_spindle_override(CMD_OVERRIDE_SPINDLE_STOP);
if (sys_state & (STATE_CYCLE|STATE_JOG)) {
st_update_plan_block_parameters(); // Notify stepper module to recompute for hold deceleration.
sys.step_control.execute_hold = On; // Initiate suspend state with active flag.
stateHandler = state_await_hold;
}
if (new_state == STATE_HOLD)
sys.holding_state = Hold_Pending;
else {
sys.parking_state = Parking_Retracting;
park.flags.value = 0;
}
sys.suspend = !sys.flags.soft_limit;
pending_state = sys_state == STATE_JOG ? new_state : STATE_IDLE;
return sys_state == STATE_CYCLE;
}
bool state_door_reopened (void)
{
return settings.parking.flags.enabled && park.flags.restart;
}
void state_update (rt_exec_t rt_exec)
{
if((rt_exec & EXEC_SAFETY_DOOR) && sys_state != STATE_SAFETY_DOOR)
state_set(STATE_SAFETY_DOOR);
stateHandler(rt_exec);
}
ISR_CODE sys_state_t ISR_FUNC(state_get)(void)
{
return sys_state;
}
uint8_t state_get_substate (void)
{
uint8_t substate = 0;
switch(sys_state) {
case STATE_CYCLE:
if(sys.flags.feed_hold_pending)
substate = 1;
else if(sys.probing_state == Probing_Active || (hal.probe.get_state && hal.probe.get_state().triggered))
substate = 2;
break;
case STATE_HOLD:
substate = sys.holding_state - 1;
break;
case STATE_ESTOP:
case STATE_ALARM:
substate = sys.alarm;
break;
case STATE_SAFETY_DOOR:
substate = sys.parking_state;
break;
}
return substate;
}
void state_set (sys_state_t new_state)
{
if(new_state != sys_state) {
sys_state_t org_state = sys_state;
switch(new_state) { // Set up new state and handler
case STATE_IDLE:
sys.suspend = false; // Break suspend state.
sys.step_control.flags = 0; // Restore step control to normal operation.
sys.parking_state = Parking_DoorClosed;
sys.holding_state = Hold_NotHolding;
sys_state = pending_state = new_state;
park.flags.value = 0;
stateHandler = state_idle;
break;
case STATE_CYCLE:
if (sys_state == STATE_IDLE) {
// Start cycle only if queued motions exist in planner buffer and the motion is not canceled.
plan_block_t *block;
if ((block = plan_get_current_block())) {
sys_state = new_state;
sys.steppers_deenergize = false; // Cancel stepper deenergize if pending.
st_prep_buffer(); // Initialize step segment buffer before beginning cycle.
if (block->spindle.state.synchronized) {
uint32_t ms = hal.get_elapsed_ticks();
if (block->spindle.hal->reset_data)
block->spindle.hal->reset_data();
uint32_t index = block->spindle.hal->get_data(SpindleData_Counters)->index_count + 2;
while(index != block->spindle.hal->get_data(SpindleData_Counters)->index_count) {
if(hal.get_elapsed_ticks() - ms > 5000) {
system_raise_alarm(Alarm_Spindle);
return;
}
if(sys.rt_exec_state & (EXEC_RESET|EXEC_STOP)) {
system_set_exec_state_flag(EXEC_RESET);
return;
}
// TODO: allow real time reporting?
}
}
st_wake_up();
stateHandler = state_cycle;
}
}
break;
case STATE_JOG:
if (sys_state == STATE_TOOL_CHANGE)
pending_state = STATE_TOOL_CHANGE;
sys_state = new_state;
stateHandler = state_cycle;
break;
case STATE_TOOL_CHANGE:
sys_state = new_state;
stateHandler = state_await_toolchanged;
break;
case STATE_HOLD:
if (sys.override.control.sync && sys.override.control.feed_hold_disable)
sys.flags.feed_hold_pending = On;
if (!((sys_state & STATE_JOG) || sys.override.control.feed_hold_disable)) {
if (!initiate_hold(new_state)) {
sys.holding_state = Hold_Complete;
stateHandler = state_await_resume;
}
sys_state = new_state;
sys.flags.feed_hold_pending = Off;
}
break;
case STATE_SAFETY_DOOR:
if ((sys_state & (STATE_ALARM|STATE_ESTOP|STATE_SLEEP|STATE_CHECK_MODE)))
return;
grbl.report.feedback_message(Message_SafetyDoorAjar);
// no break
case STATE_SLEEP:
sys.parking_state = Parking_Retracting;
if (!initiate_hold(new_state)) {
if (pending_state != new_state) {
sys_state = new_state;
state_await_hold(EXEC_CYCLE_COMPLETE); // "Simulate" a cycle stop
}
} else
sys_state = new_state;
if(sys_state == STATE_SLEEP && stateHandler != state_await_waypoint_retract)
enter_sleep();
break;
case STATE_ALARM:
case STATE_ESTOP:
case STATE_HOMING:
case STATE_CHECK_MODE:
sys_state = new_state;
sys.suspend = false;
stateHandler = state_noop;
break;
}
if(!(sys_state & (STATE_ALARM|STATE_ESTOP)))
sys.alarm = Alarm_None;
if(sys_state != org_state && grbl.on_state_change)
grbl.on_state_change(sys_state);
}
}
// Suspend manager. Controls spindle overrides in hold states.
void state_suspend_manager (void)
{
if (stateHandler != state_await_resume || !gc_state.modal.spindle.state.on)
return;
if (sys.override.spindle_stop.value) {
// Handles beginning of spindle stop
if (sys.override.spindle_stop.initiate) {
sys.override.spindle_stop.value = 0; // Clear stop override state
spindle_set_state(restore_condition.spindle[restore_condition.spindle_num].hal, (spindle_state_t){0}, 0.0f); // De-energize
sys.override.spindle_stop.enabled = On; // Set stop override state to enabled, if de-energized.
if(grbl.on_override_changed)
grbl.on_override_changed(OverrideChanged_SpindleState);
}
// Handles restoring of spindle state
if (sys.override.spindle_stop.restore) {
grbl.report.feedback_message(Message_SpindleRestore);
if (restore_condition.spindle[restore_condition.spindle_num].hal->cap.laser) // When in laser mode, ignore spindle spin-up delay. Set to turn on laser when cycle starts.
sys.step_control.update_spindle_rpm = On;
else
state_spindle_set_state(&restore_condition.spindle[restore_condition.spindle_num]);
sys.override.spindle_stop.value = 0; // Clear stop override state
if(grbl.on_override_changed)
grbl.on_override_changed(OverrideChanged_SpindleState);
}
} else if (sys.step_control.update_spindle_rpm && restore_condition.spindle[0].hal->get_state().on) {
// Handles spindle state during hold. NOTE: Spindle speed overrides may be altered during hold state.
state_spindle_set_state(&restore_condition.spindle[restore_condition.spindle_num]);
sys.step_control.update_spindle_rpm = Off;
}
}
// **************
// State handlers
// **************
/*! /brief No operation handler.
*/
static void state_noop (uint_fast16_t rt_exec)
{
// Do nothing - state change requests are handled elsewhere or ignored.
}
/*! /brief Waits for idle actions and executes them by switching to the appropriate sys_state.
*/
static void state_idle (uint_fast16_t rt_exec)
{
if ((rt_exec & EXEC_CYCLE_START))
state_set(STATE_CYCLE);
if (rt_exec & EXEC_FEED_HOLD)
state_set(STATE_HOLD);
if ((rt_exec & EXEC_TOOL_CHANGE)) {
hal.stream.suspend_read(true); // Block reading from input stream until tool change state is acknowledged
state_set(STATE_TOOL_CHANGE);
}
if (rt_exec & EXEC_SLEEP)
state_set(STATE_SLEEP);
}
/*! /brief Waits for cycle actions and executes them by switching to the appropriate sys_state.
*/
static void state_cycle (uint_fast16_t rt_exec)
{
if (rt_exec == EXEC_CYCLE_START)
return; // no need to perform other tests...
if ((rt_exec & EXEC_TOOL_CHANGE))
hal.stream.suspend_read(true); // Block reading from input stream until tool change state is acknowledged
if (rt_exec & EXEC_CYCLE_COMPLETE)
state_set(gc_state.tool_change ? STATE_TOOL_CHANGE : STATE_IDLE);
if (rt_exec & EXEC_MOTION_CANCEL) {
st_update_plan_block_parameters(); // Notify stepper module to recompute for hold deceleration.
sys.suspend = true;
sys.step_control.execute_hold = On; // Initiate suspend state with active flag.
stateHandler = state_await_motion_cancel;
}
if ((rt_exec & EXEC_FEED_HOLD))
state_set(STATE_HOLD);
}
/*! /brief Waits for tool change cycle to end then restarts the cycle.
*/
static void state_await_toolchanged (uint_fast16_t rt_exec)
{
if (rt_exec & EXEC_CYCLE_START) {
if (!gc_state.tool_change) {
if (hal.stream.suspend_read)
hal.stream.suspend_read(false); // Tool change complete, restore "normal" stream input.
if(grbl.on_tool_changed)
grbl.on_tool_changed(gc_state.tool);
system_add_rt_report(Report_Tool);
}
pending_state = gc_state.tool_change ? STATE_TOOL_CHANGE : STATE_IDLE;
state_set(STATE_IDLE);
state_set(STATE_CYCLE);
// Force a status report to let the sender know tool change is completed.
system_set_exec_state_flag(EXEC_STATUS_REPORT);
}
}
/*! /brief Waits for motion to end to complete then executes actions depending on the current sys_state.
*/
static void state_await_motion_cancel (uint_fast16_t rt_exec)
{
if (rt_exec & EXEC_CYCLE_COMPLETE) {
if (sys_state == STATE_JOG) {
sys.step_control.flags = 0;
plan_reset();
st_reset();
sync_position();
sys.suspend = false;
}
state_set(pending_state);
if (gc_state.tool_change)
state_set(STATE_TOOL_CHANGE);
}
}
/*! /brief Waits for feed hold to complete then executes actions depending on the current sys_state.
*/
static void state_await_hold (uint_fast16_t rt_exec)
{
if (rt_exec & EXEC_CYCLE_COMPLETE) {
bool handler_changed = false;
plan_cycle_reinitialize();
sys.step_control.flags = 0;
if (sys.alarm_pending) {
system_set_exec_alarm(sys.alarm_pending);
sys.alarm_pending = Alarm_None;
}
switch (sys_state) {
case STATE_TOOL_CHANGE:
spindle_all_off(); // De-energize
hal.coolant.set_state((coolant_state_t){0}); // De-energize
break;
// Resume door state when parking motion has retracted and door has been closed.
case STATE_SLEEP:
case STATE_SAFETY_DOOR:
// Parking manager. Handles de/re-energizing, switch state checks, and parking motions for
// the safety door and sleep states.
// Handles retraction motions and de-energizing.
// Ensure any prior spindle stop override is disabled at start of safety door routine.
sys.override.spindle_stop.value = 0;
// Parking requires parking axis homed, the current location not exceeding the???
// parking target location, and laser mode disabled.
if (settings.parking.flags.enabled && !sys.override.control.parking_disable && settings.mode != Mode_Laser) {
// Get current position and store as restore location.
if (!park.flags.active) {
park.flags.active = On;
system_convert_array_steps_to_mpos(park.restore_target, sys.position);
}
// Execute slow pull-out parking retract motion if parking axis is homed and parking target is above restore target.
if (bit_istrue(sys.homed.mask, bit(settings.parking.axis)) && (park.restore_target[settings.parking.axis] < settings.parking.target)) {
bool await_motion;
handler_changed = true;
stateHandler = state_await_waypoint_retract;
// Copy current location to park target and calculate retract waypoint if not restarting.
if(park.flags.restart)
system_convert_array_steps_to_mpos(park.target, sys.position);
else {
memcpy(park.target, park.restore_target, sizeof(park.target));
park.retract_waypoint = settings.parking.pullout_increment + park.target[settings.parking.axis];
park.retract_waypoint = min(park.retract_waypoint, settings.parking.target);
}
// Retract by pullout distance. Ensure retraction motion moves away from
// the workpiece and waypoint motion doesn't exceed the parking target location.
if ((await_motion = park.target[settings.parking.axis] < park.retract_waypoint)) {
park.target[settings.parking.axis] = park.retract_waypoint;
park.plan_data.feed_rate = settings.parking.pullout_rate;
park.plan_data.condition.coolant = restore_condition.coolant; // Retain coolant state
park.plan_data.spindle.state = restore_condition.spindle[restore_condition.spindle_num].state; // Retain spindle state
park.plan_data.spindle.hal = restore_condition.spindle[restore_condition.spindle_num].hal;
park.plan_data.spindle.rpm = restore_condition.spindle[restore_condition.spindle_num].rpm;
await_motion = mc_parking_motion(park.target, &park.plan_data);
}
if(!park.flags.restart)
park.flags.motion = await_motion;
if (!await_motion)
stateHandler(EXEC_CYCLE_COMPLETE); // No motion, proceed to next step immediately.
} else {
// Parking motion not possible. Just disable the spindle and coolant.
// NOTE: Laser mode does not start a parking motion to ensure the laser stops immediately.
spindle_all_off(); // De-energize
if (!settings.safety_door.flags.keep_coolant_on || sys_state == STATE_SLEEP)
hal.coolant.set_state((coolant_state_t){0}); // De-energize
sys.parking_state = hal.control.get_state().safety_door_ajar ? Parking_DoorAjar : Parking_DoorClosed;
}
} else {
spindle_all_off(); // De-energize
if (!settings.safety_door.flags.keep_coolant_on || sys_state == STATE_SLEEP)
hal.coolant.set_state((coolant_state_t){0}); // De-energize
sys.parking_state = hal.control.get_state().safety_door_ajar ? Parking_DoorAjar : Parking_DoorClosed;
}
break;
default:
break;
}
if (!handler_changed) {
if(sys.flags.soft_limit)
state_set(STATE_IDLE);
else {
sys.holding_state = Hold_Complete;
stateHandler = state_await_resume;
}
}
}
}
/*! /brief Waits for action to execute when in feed hold state.
*/
static void state_await_resume (uint_fast16_t rt_exec)
{
if ((rt_exec & EXEC_CYCLE_COMPLETE) && settings.parking.flags.enabled) {
if (sys.step_control.execute_sys_motion) {
sys.step_control.execute_sys_motion = Off;
st_parking_restore_buffer(); // Restore step segment buffer to normal run state.
}
sys.parking_state = hal.control.get_state().safety_door_ajar ? Parking_DoorAjar : Parking_DoorClosed;
if(sys_state == STATE_SLEEP) {
enter_sleep();
return;
}
}
if (rt_exec & EXEC_SLEEP)
state_set(STATE_SLEEP);
if (rt_exec & EXEC_SAFETY_DOOR)
sys.parking_state = hal.control.get_state().safety_door_ajar ? Parking_DoorAjar : Parking_DoorClosed;
else if (rt_exec & EXEC_CYCLE_START) {
if (sys_state == STATE_HOLD && !sys.override.spindle_stop.value)
sys.override.spindle_stop.restore_cycle = On;
switch (sys_state) {
case STATE_TOOL_CHANGE:
break;
case STATE_SLEEP:
break;
case STATE_SAFETY_DOOR:
if (park.flags.restart || !hal.control.get_state().safety_door_ajar) {
bool await_motion = false;
stateHandler = state_restore;
sys.parking_state = Parking_Resuming;
// Resume door state when parking motion has retracted and door has been closed.
if (park.flags.motion) {
park.flags.restart = Off;
// Execute fast restore motion to the pull-out position.
// Check to ensure the motion doesn't move below pull-out position.
if (park.restore_target[settings.parking.axis] <= settings.parking.target) {
float target[N_AXIS];
memcpy(target, park.restore_target, sizeof(target));
target[settings.parking.axis] = park.retract_waypoint;
park.plan_data.feed_rate = settings.parking.rate;
await_motion = mc_parking_motion(target, &park.plan_data);
}
}
if (!await_motion) // No motion, proceed to next step immediately.
stateHandler(EXEC_CYCLE_COMPLETE);
}
break;
default:
if (!settings.flags.restore_after_feed_hold) {
if (!restore_condition.spindle[restore_condition.spindle_num].hal->get_state().on)
gc_spindle_off();
sys.override.spindle_stop.value = 0; // Clear spindle stop override states
} else {
if (restore_condition.spindle[restore_condition.spindle_num].state.on != restore_condition.spindle[restore_condition.spindle_num].hal->get_state().on) {
grbl.report.feedback_message(Message_SpindleRestore);
state_spindle_restore(&restore_condition.spindle[restore_condition.spindle_num]);
}
if (restore_condition.coolant.value != hal.coolant.get_state().value) {
// NOTE: Laser mode will honor this delay. An exhaust system is often controlled by coolant signals.
coolant_set_state(restore_condition.coolant);
delay_sec(settings.safety_door.coolant_on_delay, DelayMode_SysSuspend);
}
sys.override.spindle_stop.value = 0; // Clear spindle stop override states
grbl.report.feedback_message(Message_None);
}
break;
}
// Restart cycle
if (!(sys_state & (STATE_SLEEP|STATE_SAFETY_DOOR))) {
step_control_t step_control = sys.step_control;
state_set(STATE_IDLE);
sys.step_control = step_control;
state_set(STATE_CYCLE);
}
} else if ((rt_exec & EXEC_DOOR_CLOSED) && !hal.control.get_state().safety_door_ajar)
sys.parking_state = Parking_DoorClosed;
}
// ********************
// Safety door handlers
// ********************
/*! /brief Waits until plunge motion abort is completed then calls state_await_hold() to restart retraction.
state_await_hold() is set to handle the cycle complete event.
*/
static void state_await_restart_retract (uint_fast16_t rt_exec)
{
if (rt_exec & EXEC_CYCLE_COMPLETE) {
if (sys.step_control.execute_sys_motion) {
sys.step_control.execute_sys_motion = Off;
st_parking_restore_buffer(); // Restore step segment buffer to normal run state.
}
stateHandler = state_await_hold;
stateHandler(EXEC_CYCLE_COMPLETE);
}
}
/*! /brief Sets up a feed hold to abort plunge motion.
state_await_restart_retract() is set to handle the cycle complete event.
*/
static void restart_retract (void)
{
grbl.report.feedback_message(Message_SafetyDoorAjar);
stateHandler = state_await_restart_retract;
park.flags.restart = On;
sys.parking_state = Parking_Retracting;
if (sys.step_control.execute_sys_motion) {
st_update_plan_block_parameters(); // Notify stepper module to recompute for hold deceleration.
sys.step_control.execute_hold = On;
sys.step_control.execute_sys_motion = On;
} else // else NO_MOTION is active.
stateHandler(EXEC_CYCLE_COMPLETE);
}
/*! /brief Waits until slow plunge motion is completed then deenergize spindle and coolant and execute fast retract motion.
state_await_resume() is set to handle the cycle complete event.
*/
static void state_await_waypoint_retract (uint_fast16_t rt_exec)
{
if (rt_exec & EXEC_CYCLE_COMPLETE) {
bool await_motion = false;
if (sys.step_control.execute_sys_motion) {
sys.step_control.execute_sys_motion = Off;
st_parking_restore_buffer(); // Restore step segment buffer to normal run state.
}
// NOTE: Clear accessory state after retract and after an aborted restore motion.
park.plan_data.spindle.state.value = 0;
park.plan_data.spindle.rpm = 0.0f;
park.plan_data.spindle.hal->set_state(park.plan_data.spindle.state, 0.0f); // De-energize
if (!settings.safety_door.flags.keep_coolant_on) {
park.plan_data.condition.coolant.value = 0;
hal.coolant.set_state(park.plan_data.condition.coolant); // De-energize
}
stateHandler = state_await_resume;
// Execute fast parking retract motion to parking target location.
if (park.flags.motion && park.target[settings.parking.axis] < settings.parking.target) {
float target[N_AXIS];
memcpy(target, park.target, sizeof(target));
target[settings.parking.axis] = settings.parking.target;
park.plan_data.feed_rate = settings.parking.rate;
await_motion = mc_parking_motion(target, &park.plan_data);
}
if (!await_motion)
stateHandler(EXEC_CYCLE_COMPLETE);
}
}
/*! /brief Waits until fast plunge motion is completed then restore spindle and coolant and execute slow plunge motion.
state_await_resumed() is set to handle the cycle complete event.
Note: A safety door event during restoration or motion will halt it and restart the retract sequence.
*/
static void state_restore (uint_fast16_t rt_exec)
{
if (rt_exec & EXEC_SAFETY_DOOR) {
if(park.flags.restoring)
park.flags.restart = On;
else
restart_retract();
}
else if (rt_exec & EXEC_CYCLE_COMPLETE) {
bool await_motion = false;
if (sys.step_control.execute_sys_motion) {
sys.step_control.execute_sys_motion = Off;
st_parking_restore_buffer(); // Restore step segment buffer to normal run state.
}
park.flags.restart = Off;
stateHandler = state_await_resumed;
// Restart spindle and coolant, delay to power-up.
state_restore_conditions(&restore_condition);
if(park.flags.restart) {
// Restart flag was set by a safety door event during
// conditions restore so restart retract.
restart_retract();
return;
}
if (park.flags.motion) {
sys.parking_state = Parking_Resuming;
// Execute slow plunge motion from pull-out position to resume position.
// Regardless if the retract parking motion was a valid/safe motion or not, the
// restore parking motion should logically be valid, either by returning to the
// original position through valid machine space or by not moving at all.
park.plan_data.feed_rate = settings.parking.pullout_rate;
park.plan_data.condition.coolant = restore_condition.coolant;
park.plan_data.spindle.state = restore_condition.spindle[restore_condition.spindle_num].state;
park.plan_data.spindle.rpm = restore_condition.spindle[restore_condition.spindle_num].rpm;
await_motion = mc_parking_motion(park.restore_target, &park.plan_data);
}
if (!await_motion)
stateHandler(EXEC_CYCLE_COMPLETE); // No motion, proceed to next step immediately.
}
}
/*! /brief Waits until slow plunge motion is complete then restart the cycle.
Note: A safety door event during the motion will halt it and restart the retract sequence.
*/
static void state_await_resumed (uint_fast16_t rt_exec)
{
if (rt_exec & EXEC_SAFETY_DOOR)
restart_retract();
else if (rt_exec & EXEC_CYCLE_COMPLETE) {
sys.parking_state = Parking_DoorClosed;
park.flags.value = 0;
if (sys.step_control.execute_sys_motion) {
sys.step_control.flags = 0;
st_parking_restore_buffer(); // Restore step segment buffer to normal run state.
}
state_set(STATE_IDLE);
state_set(STATE_CYCLE);
}
}