forked from LinuxCNC/linuxcnc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemctaskmain.cc
More file actions
3593 lines (3152 loc) · 107 KB
/
emctaskmain.cc
File metadata and controls
3593 lines (3152 loc) · 107 KB
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
/********************************************************************
* Description: emctaskmain.cc
* Main program for EMC task level
*
* Derived from a work by Fred Proctor & Will Shackleford
*
* Author:
* License: GPL Version 2
* System: Linux
*
* Copyright (c) 2004 All rights reserved.
*
********************************************************************/
/*
Principles of operation:
1. The main program calls emcTaskPlan() and emcTaskExecute() cyclically.
2. emcTaskPlan() reads the new command, and decides what to do with
it based on the mode (manual, auto, mdi) or state (estop, on) of the
machine. Many of the commands just go out immediately to the
subsystems (motion and IO). In auto mode, the interpreter is called
and as a result the interp_list is appended with NML commands.
3. emcTaskExecute() executes a big switch on execState. If it's done,
it gets the next item off the interp_list, and sets execState to the
preconditions for that. These preconditions include waiting for motion,
waiting for IO, etc. Once they are satisfied, it issues the command, and
sets execState to the postconditions. Once those are satisfied, it gets
the next item off the interp_list, and so on.
4. preconditions and postconditions are only looked at in conjunction
with commands on the interp_list. Immediate commands won't have any
pre- or postconditions associated with them looked at.
5. At this point, nothing in this file adds anything to the interp_list.
This could change, for example, when defining pre- and postconditions for
jog or home commands. If this is done, make sure that the corresponding
abort command clears out the interp_list.
6. Single-stepping is handled in checkPreconditions() as the first
condition. If we're in single-stepping mode, as indicated by the
variable 'stepping', we set the state to waiting-for-step. This
polls on the variable 'steppingWait' which is reset to zero when a
step command is received, and set to one when the command is
issued.
*/
#include <stdio.h> // vsprintf()
#include <string.h> // strcpy()
#include <stdarg.h> // va_start()
#include <stdlib.h> // exit()
#include <signal.h> // signal(), SIGINT
#include <float.h> // DBL_MAX
#include <sys/types.h> // pid_t
#include <unistd.h> // fork()
#include <sys/wait.h> // waitpid(), WNOHANG, WIFEXITED
#include <ctype.h> // isspace()
#include <libintl.h>
#include <locale.h>
#include "usrmotintf.h"
#include <rtapi_string.h> // rtapi_strlcpy()
#include "tooldata.hh"
#if 0
// Enable this to niftily trap floating point exceptions for debugging
#include <fpu_control.h>
fpu_control_t __fpu_control = _FPU_IEEE & ~(_FPU_MASK_IM | _FPU_MASK_ZM | _FPU_MASK_OM);
#endif
#include "config.h"
#include "rcs.hh" // NML classes, nmlErrorFormat()
#include "emc.hh" // EMC NML
#include "emc_nml.hh"
#include "canon.hh" // CANON_TOOL_TABLE stuff
#include "inifile.hh" // INIFILE
#include "interpl.hh" // NML_INTERP_LIST, interp_list
#include "emcglb.h" // EMC_INIFILE,NMLFILE, EMC_TASK_CYCLE_TIME
#include "interp_return.hh" // public interpreter return values
#include "interp_internal.hh" // interpreter private definitions
#include "rcs_print.hh"
#include "timer.hh"
#include "nml_oi.hh"
#include "task.hh" // emcTaskCommand etc
#include "taskclass.hh"
#include "motion.h" // EMCMOT_ORIENT_*
#include "inihal.hh"
static emcmot_config_t emcmotConfig;
/* time after which the user interface is declared dead
* because it wouldn't read any more messages
*/
#define DEFAULT_EMC_UI_TIMEOUT 5.0
// NML channels
static RCS_CMD_CHANNEL *emcCommandBuffer = 0;
static RCS_STAT_CHANNEL *emcStatusBuffer = 0;
static NML *emcErrorBuffer = 0;
// NML command channel data pointer
static RCS_CMD_MSG *emcCommand = 0;
// global EMC status
EMC_STAT *emcStatus = 0;
// timer stuff
static RCS_TIMER *timer = 0;
// flag signifying that INI file [TASK] CYCLE_TIME is <= 0.0, so
// we should not delay at all between cycles. This means also that
// the EMC_TASK_CYCLE_TIME global will be set to the measured cycle
// time each cycle, in case other code references this.
static int emcTaskNoDelay = 0;
// flag signifying that on the next loop, there should be no delay.
// this is set when transferring trajectory data from userspace to kernel
// space, and reset otherwise.
static int emcTaskEager = 0;
static int no_force_homing = 0; // forces the user to home first before allowing MDI and Program run
//can be overridden by [TRAJ]NO_FORCE_HOMING=1
static double EMC_TASK_CYCLE_TIME_ORIG = 0.0;
// delay counter
static double taskExecDelayTimeout = 0.0;
// emcTaskIssueCommand issues command immediately
static int emcTaskIssueCommand(NMLmsg * cmd);
// pending command to be sent out by emcTaskExecute()
std::unique_ptr<NMLmsg> emcTaskCommand;
// signal handling code to stop main loop
volatile int done;
static int emctask_shutdown(void);
extern void backtrace(int signo);
int _task = 1; // control preview behaviour when remapping
static int joints = 0;
// for operator display on iocontrol signalling a toolchanger fault if io.fault is set
// %d receives io.reason
static const char *io_error = "toolchanger error %d";
extern void setup_signal_handlers(); // backtrace, gdb-in-new-window supportx
int all_homed(void) {
for (int i = 0; i < emcStatus->motion.traj.joints; i++) {
if(!emcStatus->motion.joint[i].homed) // XXX
return 0;
}
return 1;
}
bool jogging_is_active(void) {
return emcStatus->motion.jogging_active;
}
void emctask_quit(int sig)
{
// set main's done flag
done = 1;
// restore signal handler
signal(sig, emctask_quit);
}
/* make sure at least space bytes are available on
* error channel; wait a bit to drain if needed
*/
int emcErrorBufferOKtoWrite(int space, const char *caller)
{
// check channel for validity
if (emcErrorBuffer == NULL)
return -1;
if (!emcErrorBuffer->valid())
return -1;
double send_errorchan_timout = etime() + DEFAULT_EMC_UI_TIMEOUT;
while (etime() < send_errorchan_timout) {
if (emcErrorBuffer->get_space_available() < space) {
esleep(0.01);
continue;
} else {
break;
}
}
if (etime() >= send_errorchan_timout) {
if (emc_debug & EMC_DEBUG_TASK_ISSUE) {
rcs_print("timeout waiting for error channel to drain, caller=`%s' request=%d\n", caller,space);
}
return -1;
} else {
// printf("--- %d bytes available after %f seconds\n", space, etime() - send_errorchan_timout + DEFAULT_EMC_UI_TIMEOUT);
}
return 0;
}
// implementation of EMC error logger
int emcOperatorError(const char *fmt, ...)
{
EMC_OPERATOR_ERROR error_msg;
va_list ap;
if ( emcErrorBufferOKtoWrite(sizeof(error_msg) * 2, "emcOperatorError"))
return -1;
if (NULL == fmt) {
return -1;
}
if (0 == *fmt) {
return -1;
}
// prepend error code, leave off 0 ad-hoc code
error_msg.error[0] = 0;
// append error string
va_start(ap, fmt);
vsnprintf(&error_msg.error[strlen(error_msg.error)],
sizeof(error_msg.error) - strlen(error_msg.error), fmt, ap);
va_end(ap);
// force a NULL at the end for safety
error_msg.error[LINELEN - 1] = 0;
// write it
rcs_print("%s\n", error_msg.error);
return emcErrorBuffer->write(error_msg);
}
int emcOperatorText(const char *fmt, ...)
{
EMC_OPERATOR_TEXT text_msg;
va_list ap;
if ( emcErrorBufferOKtoWrite(sizeof(text_msg) * 2, "emcOperatorText"))
return -1;
// write args to NML message (ignore int text code)
va_start(ap, fmt);
vsnprintf(text_msg.text, sizeof(text_msg.text), fmt, ap);
va_end(ap);
// force a NULL at the end for safety
text_msg.text[LINELEN - 1] = 0;
// write it
return emcErrorBuffer->write(text_msg);
}
int emcOperatorDisplay(const char *fmt, ...)
{
EMC_OPERATOR_DISPLAY display_msg;
va_list ap;
if ( emcErrorBufferOKtoWrite(sizeof(display_msg) * 2, "emcOperatorDisplay"))
return -1;
// write args to NML message (ignore int display code)
va_start(ap, fmt);
vsnprintf(display_msg.display, sizeof(display_msg.display), fmt, ap);
va_end(ap);
// force a NULL at the end for safety
display_msg.display[LINELEN - 1] = 0;
// write it
return emcErrorBuffer->write(display_msg);
}
/*
handling of EMC_SYSTEM_CMD
*/
/* convert string to arg/argv set */
static int argvize(const char *src, char *dst, char *argv[], int len)
{
char *bufptr;
int argvix;
char inquote;
char looking;
snprintf(dst, len, "%s", src);
bufptr = dst;
inquote = 0;
argvix = 0;
looking = 1;
while (0 != *bufptr) {
if (*bufptr == '"') {
*bufptr = 0;
if (inquote) {
inquote = 0;
looking = 1;
} else {
inquote = 1;
}
} else if (isspace(*bufptr) && !inquote) {
looking = 1;
*bufptr = 0;
} else if (looking) {
looking = 0;
argv[argvix] = bufptr;
argvix++;
}
bufptr++;
}
argv[argvix] = 0; // null-terminate the argv list
return argvix;
}
static pid_t emcSystemCmdPid = 0;
int emcSystemCmd(char *s)
{
char buffer[EMC_SYSTEM_CMD_LEN];
char *argv[EMC_SYSTEM_CMD_LEN / 2 + 1];
if (0 != emcSystemCmdPid) {
// something's already running, and we can only handle one
if (emc_debug & EMC_DEBUG_TASK_ISSUE) {
rcs_print
("emcSystemCmd: abandoning process %d, running ``%s''\n",
emcSystemCmdPid, s);
}
}
emcSystemCmdPid = fork();
if (-1 == emcSystemCmdPid) {
// we're still the parent, with no child created
if (emc_debug & EMC_DEBUG_TASK_ISSUE) {
rcs_print("system command ``%s'' can't be executed\n", s);
}
return -1;
}
if (0 == emcSystemCmdPid) {
// we're the child
// convert string to argc/argv
argvize(s, buffer, argv, EMC_SYSTEM_CMD_LEN);
execvp(argv[0], argv);
// if we get here, we didn't exec
if (emc_debug & EMC_DEBUG_TASK_ISSUE) {
rcs_print("emcSystemCmd: can't execute ``%s''\n", s);
}
exit(-1);
}
// else we're the parent
return 0;
}
// shorthand typecasting ptrs
static EMC_JOINT_HALT *joint_halt_msg;
static EMC_JOINT_HOME *home_msg;
static EMC_JOINT_UNHOME *unhome_msg;
static EMC_JOG_CONT *jog_cont_msg;
static EMC_JOG_STOP *jog_stop_msg;
static EMC_JOG_INCR *jog_incr_msg;
static EMC_JOG_ABS *jog_abs_msg;
static EMC_JOINT_SET_BACKLASH *set_backlash_msg;
static EMC_JOINT_SET_HOMING_PARAMS *set_homing_params_msg;
static EMC_JOINT_SET_FERROR *set_ferror_msg;
static EMC_JOINT_SET_MIN_FERROR *set_min_ferror_msg;
static EMC_JOINT_SET_MAX_POSITION_LIMIT *set_max_limit_msg;
static EMC_JOINT_SET_MIN_POSITION_LIMIT *set_min_limit_msg;
static EMC_JOINT_OVERRIDE_LIMITS *joint_lim_msg;
//static EMC_JOINT_SET_OUTPUT *axis_output_msg;
static EMC_JOINT_LOAD_COMP *joint_load_comp_msg;
//static EMC_AXIS_SET_STEP_PARAMS *set_step_params_msg;
static EMC_TRAJ_SET_SCALE *emcTrajSetScaleMsg;
static EMC_TRAJ_SET_RAPID_SCALE *emcTrajSetRapidScaleMsg;
static EMC_TRAJ_SET_MAX_VELOCITY *emcTrajSetMaxVelocityMsg;
static EMC_TRAJ_SET_SPINDLE_SCALE *emcTrajSetSpindleScaleMsg;
static EMC_TRAJ_SET_VELOCITY *emcTrajSetVelocityMsg;
static EMC_TRAJ_SET_ACCELERATION *emcTrajSetAccelerationMsg;
static EMC_TRAJ_LINEAR_MOVE *emcTrajLinearMoveMsg;
static EMC_TRAJ_CIRCULAR_MOVE *emcTrajCircularMoveMsg;
static EMC_TRAJ_DELAY *emcTrajDelayMsg;
static EMC_TRAJ_SET_TERM_COND *emcTrajSetTermCondMsg;
static EMC_TRAJ_SET_SPINDLESYNC *emcTrajSetSpindlesyncMsg;
// These classes are commented out because the compiler
// complains that they are "defined but not used".
//static EMC_MOTION_SET_AOUT *emcMotionSetAoutMsg;
//static EMC_MOTION_SET_DOUT *emcMotionSetDoutMsg;
static EMC_SPINDLE_SPEED *spindle_speed_msg;
static EMC_SPINDLE_ORIENT *spindle_orient_msg;
static EMC_SPINDLE_WAIT_ORIENT_COMPLETE *wait_spindle_orient_complete_msg;
static EMC_SPINDLE_ON *spindle_on_msg;
static EMC_SPINDLE_OFF *spindle_off_msg;
static EMC_SPINDLE_BRAKE_ENGAGE *spindle_brake_engage_msg;
static EMC_SPINDLE_BRAKE_RELEASE *spindle_brake_release_msg;
static EMC_SPINDLE_INCREASE *spindle_increase_msg;
static EMC_SPINDLE_DECREASE *spindle_decrease_msg;
static EMC_SPINDLE_CONSTANT *spindle_constant_msg;
static EMC_TOOL_PREPARE *tool_prepare_msg;
static EMC_TOOL_LOAD_TOOL_TABLE *load_tool_table_msg;
static EMC_TOOL_SET_OFFSET *emc_tool_set_offset_msg;
static EMC_TOOL_SET_NUMBER *emc_tool_set_number_msg;
static EMC_TASK_SET_MODE *mode_msg;
static EMC_TASK_SET_STATE *state_msg;
static EMC_TASK_PLAN_RUN *run_msg;
static EMC_TASK_PLAN_EXECUTE *execute_msg;
static EMC_TASK_PLAN_OPEN *open_msg;
static EMC_TASK_PLAN_SET_OPTIONAL_STOP *os_msg;
static EMC_TASK_PLAN_SET_BLOCK_DELETE *bd_msg;
static EMC_AUX_INPUT_WAIT *emcAuxInputWaitMsg;
static int emcAuxInputWaitType = 0;
static int emcAuxInputWaitIndex = -1;
// commands we compose here
static EMC_TASK_PLAN_RUN taskPlanRunCmd; // 16-Aug-1999 FMP
//static EMC_TASK_PLAN_INIT taskPlanInitCmd;
//static EMC_TASK_PLAN_SYNCH taskPlanSynchCmd;
extern void emcTaskQueueTaskPlanSynchCmd();
static EMC_TASK_INTERP interpResumeState = EMC_TASK_INTERP::IDLE;
static int programStartLine = 0; // which line to run program from
// how long the interp list can be
int stepping = 0;
int steppingWait = 0;
static int steppedLine = 0;
// Variables to handle MDI call interrupts
// Depth of call level before interrupted MDI call
static int mdi_execute_level = -1;
// Schedule execute(0) command
static int mdi_execute_next = 0;
// Wait after interrupted command
static int mdi_execute_wait = 0;
// Side queue to store MDI commands
static NML_INTERP_LIST mdi_execute_queue;
// MDI input queue
static NML_INTERP_LIST mdi_input_queue;
#define MAX_MDI_QUEUE 10
static int max_mdi_queued_commands = MAX_MDI_QUEUE;
/*
checkInterpList(NML_INTERP_LIST *il, EMC_STAT *stat) takes a pointer
to an interpreter list and a pointer to the EMC status, pops each NML
message off the list, and checks it against limits, resource availability,
etc. in the status.
It returns 0 if all messages check out, -1 if any of them fail. If one
fails, the rest of the list is not checked.
*/
static int checkInterpList(NML_INTERP_LIST * il, EMC_STAT * /*stat*/)
{
while (il->len() > 0) {
auto cmd = il->get();
switch (cmd->_type) {
case EMC_OPERATOR_ERROR_TYPE: {
auto error_msg = static_cast<EMC_OPERATOR_ERROR*>(cmd.get());
emcOperatorError("%s", error_msg->error);
break;
}
//FIXME: there was limit checking tests below, see if they were needed
case EMC_TRAJ_LINEAR_MOVE_TYPE:
break;
case EMC_TRAJ_CIRCULAR_MOVE_TYPE:
break;
default:
break;
}
}
return 0;
}
extern int emcTaskMopup();
void readahead_reading(void)
{
int readRetval;
int execRetval;
if (interp_list.len() <= emc_task_interp_max_len) {
int count = 0;
interpret_again:
if (emcTaskPlanIsWait()) {
// delay reading of next line until all is done
if (interp_list.len() == 0 &&
emcTaskCommand == 0 &&
emcStatus->task.execState ==
EMC_TASK_EXEC::DONE) {
emcTaskPlanClearWait();
}
} else {
readRetval = emcTaskPlanRead();
/*! \todo MGS FIXME
This if() actually evaluates to if (readRetval != INTERP_OK)...
*** Need to look at all calls to things that return INTERP_xxx values! ***
MGS */
if (readRetval > INTERP_MIN_ERROR
|| readRetval == INTERP_ENDFILE
|| readRetval == INTERP_EXIT
|| readRetval == INTERP_EXECUTE_FINISH) {
/* emcTaskPlanRead retval != INTERP_OK
Signal to the rest of the system that that the interp
is now in a paused state. */
/*! \todo FIXME The above test *should* be reduced to:
readRetVal != INTERP_OK
(N.B. Watch for negative error codes.) */
emcStatus->task.interpState =
EMC_TASK_INTERP::WAITING;
} else {
// got a good line
// record the line number and command
emcStatus->task.readLine = emcTaskPlanLine();
emcTaskPlanCommand((char *) &emcStatus->task.
command);
// and execute it
execRetval = emcTaskPlanExecute(0);
// line number may need update after
// returns from subprograms in external
// files
emcStatus->task.readLine = emcTaskPlanLine();
if (execRetval > INTERP_MIN_ERROR) {
emcStatus->task.interpState =
EMC_TASK_INTERP::WAITING;
interp_list.clear();
emcAbortCleanup(EMC_ABORT::INTERPRETER_ERROR,
"interpreter error");
} else if (execRetval == -1
|| execRetval == INTERP_EXIT ) {
emcStatus->task.interpState =
EMC_TASK_INTERP::WAITING;
} else if (execRetval == INTERP_EXECUTE_FINISH) {
// INTERP_EXECUTE_FINISH signifies
// that no more reading should be done until
// everything
// outstanding is completed
emcTaskPlanSetWait();
// and resynch interp WM
//emcTaskQueueCommand(&taskPlanSynchCmd);
emcTaskQueueTaskPlanSynchCmd();
} else if (execRetval != 0) {
// end of file
emcStatus->task.interpState =
EMC_TASK_INTERP::WAITING;
emcStatus->task.motionLine = 0;
emcStatus->task.readLine = 0;
} else {
// executed a good line
}
// throw the results away if we're supposed to
// read
// through it
if ( programStartLine != 0 &&
emcTaskPlanLevel() == 0 &&
( programStartLine < 0 ||
emcTaskPlanLine() <= programStartLine )) {
// we're stepping over lines, so check them
// for
// limits, etc. and clear then out
if (0 != checkInterpList(&interp_list,
emcStatus)) {
// problem with actions, so do same as we
// did
// for a bad read from emcTaskPlanRead()
// above
emcStatus->task.interpState =
EMC_TASK_INTERP::WAITING;
}
// and clear it regardless
interp_list.clear();
}
if (emcStatus->task.readLine < programStartLine &&
emcTaskPlanLevel() == 0) {
//update the position with our current position, as the other positions are only skipped through
CANON_UPDATE_END_POINT(emcStatus->motion.traj.actualPosition.tran.x,
emcStatus->motion.traj.actualPosition.tran.y,
emcStatus->motion.traj.actualPosition.tran.z,
emcStatus->motion.traj.actualPosition.a,
emcStatus->motion.traj.actualPosition.b,
emcStatus->motion.traj.actualPosition.c,
emcStatus->motion.traj.actualPosition.u,
emcStatus->motion.traj.actualPosition.v,
emcStatus->motion.traj.actualPosition.w);
if ((emcStatus->task.readLine + 1 == programStartLine) &&
(emcTaskPlanLevel() == 0)) {
emcTaskPlanSynch();
// reset programStartLine so we don't fall into our stepping routines
// if we happen to execute lines before the current point later (due to subroutines).
programStartLine = 0;
}
}
if (count++ < emc_task_interp_max_len
&& emcStatus->task.interpState == EMC_TASK_INTERP::READING
&& interp_list.len() <= emc_task_interp_max_len * 2/3) {
goto interpret_again;
}
} // else read was OK, so execute
} // else not emcTaskPlanIsWait
} // if interp len is less than max
}
static void mdi_execute_abort(void)
{
int queued_mdi_commands;
// XXX: Reset needed?
if (mdi_execute_wait || mdi_execute_next)
emcTaskPlanReset();
mdi_execute_level = -1;
mdi_execute_wait = 0;
mdi_execute_next = 0;
queued_mdi_commands = mdi_execute_queue.len();
if (queued_mdi_commands > 0) {
rcs_print("mdi_execute_abort: dropping %d queued MDI commands\n", queued_mdi_commands);
}
mdi_execute_queue.clear();
emcStatus->task.queuedMDIcommands = 0;
emcStatus->task.interpState = EMC_TASK_INTERP::IDLE;
}
static void mdi_execute_hook(void)
{
if (mdi_execute_wait && emcTaskPlanIsWait()) {
// delay reading of next line until all is done
if (interp_list.len() == 0 &&
emcTaskCommand == 0 &&
emcStatus->task.execState ==
EMC_TASK_EXEC::DONE) {
emcTaskPlanClearWait();
mdi_execute_wait = 0;
mdi_execute_hook();
}
return;
}
if (
(mdi_execute_level < 0)
&& (mdi_execute_wait == 0)
&& (mdi_execute_queue.len() > 0)
&& (interp_list.len() == 0)
&& (emcTaskCommand == NULL)
) {
interp_list.append(mdi_execute_queue.get());
emcStatus->task.queuedMDIcommands = mdi_execute_queue.len();
return;
}
// determine when a MDI command actually finishes normally.
if (interp_list.len() == 0 &&
emcTaskCommand == 0 &&
emcStatus->task.execState == EMC_TASK_EXEC::DONE &&
emcStatus->task.interpState != EMC_TASK_INTERP::IDLE &&
emcStatus->motion.traj.queue == 0 &&
emcStatus->io.status == RCS_STATUS::DONE &&
!mdi_execute_wait &&
!mdi_execute_next) {
// finished. Check for dequeuing of queued MDI command is done in emcTaskPlan().
if (emc_debug & EMC_DEBUG_TASK_ISSUE)
rcs_print("mdi_execute_hook: MDI command '%s' done (remaining: %d)\n",
emcStatus->task.command, mdi_input_queue.len());
emcStatus->task.command[0] = 0;
emcStatus->task.interpState = EMC_TASK_INTERP::IDLE;
}
if (!mdi_execute_next) return;
if (interp_list.len() > emc_task_interp_max_len) return;
mdi_execute_next = 0;
auto msg = std::make_unique<EMC_TASK_PLAN_EXECUTE>();
msg->command[0] = (char) 0xff;
interp_list.append(std::move(msg));
}
void readahead_waiting(void)
{
// now handle call logic
// check for subsystems done
if (interp_list.len() == 0 &&
emcTaskCommand == 0 &&
emcStatus->motion.traj.queue == 0 &&
emcStatus->io.status == RCS_STATUS::DONE)
// finished
{
int was_open = taskplanopen;
if (was_open) {
emcTaskPlanClose();
if ((emc_debug & EMC_DEBUG_INTERP) && was_open) {
rcs_print
("emcTaskPlanClose() called at %s:%d\n",
__FILE__, __LINE__);
}
// then resynch interpreter
emcTaskQueueTaskPlanSynchCmd();
} else {
emcStatus->task.interpState = EMC_TASK_INTERP::IDLE;
}
emcStatus->task.readLine = 0;
} else {
// still executing
}
}
static bool allow_while_idle_type() {
// allow for EMC_TASK_MODE::AUTO, EMC_TASK_MODE::MDI
// expect immediate command
RCS_CMD_MSG *emcCommand;
emcCommand = emcCommandBuffer->get_address();
switch(emcCommand->_type) {
case EMC_JOG_CONT_TYPE:
case EMC_JOG_INCR_TYPE:
case EMC_JOG_STOP_TYPE:
case EMC_JOG_ABS_TYPE:
case EMC_TRAJ_SET_TELEOP_ENABLE_TYPE:
return 1;
break;
}
return 0;
}
/*
emcTaskPlan()
Planner for NC code or manual mode operations
*/
static int emcTaskPlan(void)
{
NMLTYPE type;
int retval = 0;
// check for new command
if (emcCommand->serial_number != emcStatus->echo_serial_number) {
// flag it here locally as a new command
type = emcCommand->_type;
} else {
// no new command-- reset local flag
type = 0;
}
// Always display messages
switch (type) {
case EMC_OPERATOR_ERROR_TYPE:
case EMC_OPERATOR_TEXT_TYPE:
case EMC_OPERATOR_DISPLAY_TYPE:
retval = emcTaskIssueCommand(emcCommand);
return retval;
}
// handle any new command
switch (emcStatus->task.state) {
case EMC_TASK_STATE::OFF:
case EMC_TASK_STATE::ESTOP:
case EMC_TASK_STATE::ESTOP_RESET:
// now switch on the mode
switch (emcStatus->task.mode) {
case EMC_TASK_MODE::MANUAL:
case EMC_TASK_MODE::AUTO:
case EMC_TASK_MODE::MDI:
// now switch on the command
switch (type) {
case 0:
case EMC_NULL_TYPE:
// no command
break;
// immediate commands
case EMC_JOINT_SET_BACKLASH_TYPE:
case EMC_JOINT_SET_HOMING_PARAMS_TYPE:
case EMC_JOINT_SET_FERROR_TYPE:
case EMC_JOINT_SET_MIN_FERROR_TYPE:
case EMC_JOINT_LOAD_COMP_TYPE:
case EMC_JOINT_UNHOME_TYPE:
case EMC_TRAJ_SET_SCALE_TYPE:
case EMC_TRAJ_SET_RAPID_SCALE_TYPE:
case EMC_TRAJ_SET_MAX_VELOCITY_TYPE:
case EMC_TRAJ_SET_SPINDLE_SCALE_TYPE:
case EMC_TRAJ_SET_FO_ENABLE_TYPE:
case EMC_TRAJ_SET_FH_ENABLE_TYPE:
case EMC_TRAJ_SET_SO_ENABLE_TYPE:
case EMC_TRAJ_SET_VELOCITY_TYPE:
case EMC_TRAJ_SET_ACCELERATION_TYPE:
case EMC_TASK_SET_MODE_TYPE:
case EMC_TASK_SET_STATE_TYPE:
case EMC_TASK_PLAN_INIT_TYPE:
case EMC_TASK_PLAN_OPEN_TYPE:
case EMC_TASK_PLAN_CLOSE_TYPE:
case EMC_TASK_PLAN_SET_OPTIONAL_STOP_TYPE:
case EMC_TASK_PLAN_SET_BLOCK_DELETE_TYPE:
case EMC_TASK_ABORT_TYPE:
case EMC_TASK_PLAN_SYNCH_TYPE:
case EMC_TRAJ_CLEAR_PROBE_TRIPPED_FLAG_TYPE:
case EMC_TRAJ_PROBE_TYPE:
case EMC_AUX_INPUT_WAIT_TYPE:
case EMC_MOTION_SET_DOUT_TYPE:
case EMC_MOTION_ADAPTIVE_TYPE:
case EMC_MOTION_SET_AOUT_TYPE:
case EMC_TRAJ_RIGID_TAP_TYPE:
case EMC_TRAJ_SET_TELEOP_ENABLE_TYPE:
case EMC_SET_DEBUG_TYPE:
retval = emcTaskIssueCommand(emcCommand);
break;
// one case where we need to be in manual mode
case EMC_JOINT_OVERRIDE_LIMITS_TYPE:
retval = 0;
if (emcStatus->task.mode == EMC_TASK_MODE::MANUAL) {
retval = emcTaskIssueCommand(emcCommand);
}
break;
case EMC_TOOL_LOAD_TOOL_TABLE_TYPE: {
// send to IO
emcTaskQueueCommand(std::make_unique<EMC_TOOL_LOAD_TOOL_TABLE>(*static_cast<EMC_TOOL_LOAD_TOOL_TABLE*>(emcCommand)));
// signify no more reading
emcTaskPlanSetWait();
// then resynch interpreter
emcTaskQueueTaskPlanSynchCmd();
break;
}
case EMC_TOOL_SET_OFFSET_TYPE: {
// send to IO
emcTaskQueueCommand(std::make_unique<EMC_TOOL_SET_OFFSET>(*static_cast<EMC_TOOL_SET_OFFSET*>(emcCommand)));
// signify no more reading
emcTaskPlanSetWait();
// then resynch interpreter
emcTaskQueueTaskPlanSynchCmd();
break;
}
case EMC_TOOL_SET_NUMBER_TYPE: {
// send to IO
emcTaskQueueCommand(std::make_unique<EMC_TOOL_SET_NUMBER>(*static_cast<EMC_TOOL_SET_NUMBER*>(emcCommand)));
// signify no more reading
emcTaskPlanSetWait();
// then resynch interpreter
emcTaskQueueTaskPlanSynchCmd();
break;
}
default:
emcOperatorError(_("command (%s) cannot be executed until the machine is out of E-stop and turned on"), emc_symbol_lookup(type));
retval = -1;
break;
} // switch (type)
default:
// invalid mode
break;
} // switch (mode)
break; // case EMC_TASK_STATE::OFF,ESTOP,ESTOP_RESET
case EMC_TASK_STATE::ON:
/* we can do everything (almost) when the machine is on, so let's
switch on the execution mode */
switch (emcStatus->task.mode) {
case EMC_TASK_MODE::MANUAL: // ON, MANUAL
switch (type) {
case 0:
case EMC_NULL_TYPE:
// no command
break;
// immediate commands
case EMC_JOINT_SET_BACKLASH_TYPE:
case EMC_JOINT_SET_HOMING_PARAMS_TYPE:
case EMC_JOINT_SET_FERROR_TYPE:
case EMC_JOINT_SET_MIN_FERROR_TYPE:
case EMC_JOINT_SET_MAX_POSITION_LIMIT_TYPE:
case EMC_JOINT_SET_MIN_POSITION_LIMIT_TYPE:
case EMC_JOINT_HALT_TYPE:
case EMC_JOINT_HOME_TYPE:
case EMC_JOINT_UNHOME_TYPE:
case EMC_JOG_CONT_TYPE:
case EMC_JOG_INCR_TYPE:
case EMC_JOG_ABS_TYPE:
case EMC_JOG_STOP_TYPE:
case EMC_JOINT_OVERRIDE_LIMITS_TYPE:
case EMC_TRAJ_PAUSE_TYPE:
case EMC_TRAJ_RESUME_TYPE:
case EMC_TRAJ_ABORT_TYPE:
case EMC_TRAJ_SET_SCALE_TYPE:
case EMC_TRAJ_SET_RAPID_SCALE_TYPE:
case EMC_TRAJ_SET_MAX_VELOCITY_TYPE:
case EMC_TRAJ_SET_SPINDLE_SCALE_TYPE:
case EMC_TRAJ_SET_FO_ENABLE_TYPE:
case EMC_TRAJ_SET_FH_ENABLE_TYPE:
case EMC_TRAJ_SET_SO_ENABLE_TYPE:
case EMC_SPINDLE_SPEED_TYPE:
case EMC_SPINDLE_ON_TYPE:
case EMC_SPINDLE_OFF_TYPE:
case EMC_SPINDLE_BRAKE_RELEASE_TYPE:
case EMC_SPINDLE_BRAKE_ENGAGE_TYPE:
case EMC_SPINDLE_INCREASE_TYPE:
case EMC_SPINDLE_DECREASE_TYPE:
case EMC_SPINDLE_CONSTANT_TYPE:
case EMC_COOLANT_MIST_ON_TYPE:
case EMC_COOLANT_MIST_OFF_TYPE:
case EMC_COOLANT_FLOOD_ON_TYPE:
case EMC_COOLANT_FLOOD_OFF_TYPE:
case EMC_TASK_SET_MODE_TYPE:
case EMC_TASK_SET_STATE_TYPE:
case EMC_TASK_ABORT_TYPE:
case EMC_TASK_PLAN_OPEN_TYPE:
case EMC_TASK_PLAN_CLOSE_TYPE:
case EMC_TASK_PLAN_PAUSE_TYPE:
case EMC_TASK_PLAN_REVERSE_TYPE:
case EMC_TASK_PLAN_FORWARD_TYPE:
case EMC_TASK_PLAN_RESUME_TYPE:
case EMC_TASK_PLAN_INIT_TYPE:
case EMC_TASK_PLAN_SYNCH_TYPE:
case EMC_TASK_PLAN_SET_OPTIONAL_STOP_TYPE:
case EMC_TASK_PLAN_SET_BLOCK_DELETE_TYPE:
case EMC_TASK_PLAN_OPTIONAL_STOP_TYPE:
case EMC_TRAJ_CLEAR_PROBE_TRIPPED_FLAG_TYPE:
case EMC_TRAJ_PROBE_TYPE:
case EMC_AUX_INPUT_WAIT_TYPE:
case EMC_MOTION_SET_DOUT_TYPE:
case EMC_MOTION_SET_AOUT_TYPE:
case EMC_MOTION_ADAPTIVE_TYPE:
case EMC_TRAJ_RIGID_TAP_TYPE:
case EMC_TRAJ_SET_TELEOP_ENABLE_TYPE:
case EMC_SET_DEBUG_TYPE:
retval = emcTaskIssueCommand(emcCommand);
break;
// queued commands
case EMC_TASK_PLAN_EXECUTE_TYPE:
// resynch the interpreter, since we may have moved
// externally
emcTaskQueueTaskPlanSynchCmd();
// and now call for interpreter execute
retval = emcTaskIssueCommand(emcCommand);
break;
case EMC_TOOL_LOAD_TOOL_TABLE_TYPE: {
// send to IO
emcTaskQueueCommand(std::make_unique<EMC_TOOL_LOAD_TOOL_TABLE>(*static_cast<EMC_TOOL_LOAD_TOOL_TABLE*>(emcCommand)));
// signify no more reading
emcTaskPlanSetWait();
// then resynch interpreter
emcTaskQueueTaskPlanSynchCmd();
break;
}
case EMC_TOOL_SET_OFFSET_TYPE: {
// send to IO
emcTaskQueueCommand(std::make_unique<EMC_TOOL_SET_OFFSET>(*static_cast<EMC_TOOL_SET_OFFSET*>(emcCommand)));
// signify no more reading
emcTaskPlanSetWait();
// then resynch interpreter
emcTaskQueueTaskPlanSynchCmd();
break;
}
case EMC_TOOL_SET_NUMBER_TYPE: {
// send to IO
emcTaskQueueCommand(std::make_unique<EMC_TOOL_SET_NUMBER>(*static_cast<EMC_TOOL_SET_NUMBER*>(emcCommand)));
// signify no more reading
emcTaskPlanSetWait();
// then resynch interpreter
emcTaskQueueTaskPlanSynchCmd();
break;
}
case EMC_TASK_PLAN_RUN_TYPE:
if (GET_EXTERNAL_OFFSET_APPLIED()) {
// err here, fewer err reports
retval = -1;
}