-
Notifications
You must be signed in to change notification settings - Fork 301
/
Copy pathplan_exec.cpp
1159 lines (1056 loc) · 56 KB
/
plan_exec.cpp
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
/*
* plan_exec.cpp - execution function for acceleration managed lines
* This file is part of the g2core project
*
* Copyright (c) 2010 - 2018 Alden S. Hart, Jr.
* Copyright (c) 2012 - 2018 Rob Giseburt
*
* This file ("the software") is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2 as published by the
* Free Software Foundation. You should have received a copy of the GNU General Public
* License, version 2 along with the software. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, you may use this file as part of a software library without
* restriction. Specifically, if other files instantiate templates or use macros or
* inline functions from this file, or you compile this file and link it with other
* files to produce an executable, this file does not by itself cause the resulting
* executable to be covered by the GNU General Public License. This exception does not
* however invalidate any other reasons why the executable file might be covered by the
* GNU General Public License.
*
* THE SOFTWARE IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY
* WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
* SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "g2core.h"
#include "config.h"
#include "controller.h"
#include "planner.h"
#include "kinematics.h"
#include "stepper.h"
#include "encoder.h"
#include "report.h"
#include "util.h"
#include "spindle.h"
#include "xio.h" // DIAGNOSTIC
// execute routines (NB: These are all called from the LO interrupt)
static stat_t _exec_aline_head(mpBuf_t *bf); // passing bf because body might need it, and it might call body
static stat_t _exec_aline_body(mpBuf_t *bf); // passing bf so that body can extend itself if the exit velocity rises.
static stat_t _exec_aline_tail(mpBuf_t *bf);
static stat_t _exec_aline_segment(void);
static void _exec_aline_normalize_block(mpBlockRuntimeBuf_t *b);
static stat_t _exec_aline_feedhold(mpBuf_t *bf);
static void _init_forward_diffs(float v_0, float v_1);
/****************************************************************************************
* mp_forward_plan() - plan commands and moves ahead of exec; call ramping for moves
*
**** WARNING ****
* mp_forward_plan() should NOT be called directly!
* Instead call st_request_forward_plan(), which mediates access.
*
* mp_forward_plan() performs just-in-time forward planning immediately before
* moves and commands are queued to the move execution runtime (exec).
* Unlike back planning, buffers are only forward planned once.
*
* mp_forward_plan() is called aggressively via st_request_forward_plan().
* It has a relatively low interrupt level to call its own.
* See also: Planner Background and Overview notes in planner.h
*
* It examines the currently running buffer and its adjacent buffers to:
* - Stop the system from re-planning or planning something that's not prepped
* - Plan the next available ALINE (movement) block past the COMMAND blocks
* - Skip past/ or pre-plan COMMAND blocks while labeling them as FULLY_PLANNED
*
* Returns:
* - STAT_OK if exec should be called to kickstart (or continue) movement
* - STAT_NOOP to exit with no action taken (do not call exec)
*/
/*
* --- Forward Planning Processing and Cases ---
*
* These cases describe all possible sequences of buffers in the planner queue starting
* with the currently executing (or about to execute) Run buffer, looking forward
* to more recently arrived buffers. In most cases only one or two buffers need to
* be examined, but contiguous groups of commands may need to be processed.
*
* 'Running' cases are where the run buffer state is RUNNING. Bootstrap handles all other cases.
* 'Bootstrap' occurs during the startup phase where moves are collected before starting movement.
* Conditions that are impossible based on this definition are not listed in the tables below.
*
* See planner.h / bufferState enum for shorthand used in the descriptions.
* All cases assume a mix of moves and commands, as noted in the shorthand.
* All cases assume 2 'blocks' - Run block (r) & Plan block (p). These cases
* will need to be revisited and generalized if more blocks are used in the future
* (i.e. deeper forward planning).
*
* 'NOT_PLANNED' means the block has not been back planned or forward planned
* This refers to any state below BACK_PLANNED, i.e. < MP_BUFFER_BACK_PLANNED
* 'NOT_PLANNED' can be either a move or command, we don't care so it's not specified.
*
* 'BACK_PLANNED' means the block has been back planned but not forward planned
* 'FULLY_PLANNED' means the block is back planned and forward planned, is ready for execution
* 'RUNNING' means the move is executing in the runtime. The bf is "locked" during this phase
*
* 'COMMAND' or 'COMMAND(s)' refers to one command or a contiguous group of command buffers
* that may be in BACK_PLANNED or FULLY_PLANNED states. Processing is always the same;
* plan all BACK_PLANNED commands and skip past all FULLY_PLANNED commands.
*
* Note 1: For MOVEs use the exit velocity of the Run block (mr->r->exit_velocity)
* as the entry velocity of the next adjacent move.
*
* Note 1a: In this special COMMAND case we trust mr->r->exit_velocity because the
* back planner has already handled this case for us.
*
* Note 2: For COMMANDs use the entry velocity of the current runtime (mr->entry_velocity)
* as the entry velocity for the next adjacent move. mr->entry_velocity is almost always 0,
* but could be non-0 in a race condition.
* FYI: mr->entry_velocity is set at the end of the last running block in mp_exec_aline().
*
* CASE:
* 0. Nothing to do
*
* run_buffer
* ----------
* a. <no buffer> Run buffer has not yet been initialized (prep null buffer and return NOOP)
* b. NOT_BACK_PLANNED No moves or commands in run buffer. Exit with no action
*
* 1. Bootstrap cases (buffer state < RUNNING)
*
* run_buffer next N bufs terminal buf Actions
* ---------- ----------- ------------ ----------------------------------
* a. BACK_PLANNED/MOVE <don't care> <don't care> plan move, exit OK
* b. FULLY_PLANNED/MOVE NOT_PLANNED <don't care> exit NOOP
* c. FULLY_PLANNED/MOVE BACK_PLANNED/MOVE <don't care> exit NOOP (don't plan past a PLANNED buffer)
* d. FULLY_PLANNED/MOVE FULLY_PLANNED/MOVE <don't care> trap illegal condition, exit NOOP
* e. FULLY_PLANNED/MOVE COMMAND(s) <don't care> exit NOOP
* f. BACK_PLANNED/COMMAND NOT_PREPPED <don't care> plan command, exit OK
* g. BACK_PLANNED/COMMAND BACK_PLANNED/MOVE <don't care> plan command, plan move (Note 2), exit OK
* h. BACK_PLANNED/COMMAND FULLY_PLANNED/MOVE <don't care> trap illegal condition, exit NOOP
* i. BACK_PLANNED/COMMAND NOT_PLANNED <don't care> skip command, exit OK
* j. BACK_PLANNED/COMMAND BACK_PLANNED/MOVE <don't care> skip command, plan move (Note 2), exit OK
* k. BACK_PLANNED/COMMAND FULLY_PLANNED/MOVE <don't care> exit NOOP
*
* 2. Running cases (buffer state == RUNNING)
*
* run_buffer next N bufs terminal buf Actions
* ---------- ----------- ------------ ----------------------------------
* a. RUNNING/MOVE BACK_PLANNED/MOVE <don't care> plan move, exit OK
* b. RUNNING/MOVE FULLY_PLANNED/MOVE <don't care> exit NOOP
* c. RUNNING/MOVE COMMAND(s) NOT_PLANNED skip/plan command(s), exit OK
* d. RUNNING/MOVE COMMAND(s) BACK_PLANNED/MOVE skip/plan command(s), plan move, exit OK
* e. RUNNING/MOVE BACK_PLANNED(s) FULLY_PLANNED-MOVE exit NOOP
* f. RUNNING/COMMAND BACK_PLANNED/MOVE <don't care> plan move, exit OK
* g. RUNNING/COMMAND FULLY_PLANNED/MOVE <don't care> exit NOOP
* h. RUNNING/COMMAND COMMAND(s) NOT_PLANNED skip/plan command(s), exit OK
* i. RUNNING/COMMAND COMMAND(s) BACK_PLANNED/MOVE skip/plan command(s), plan move (Note 1a), exit OK
* j. RUNNING/COMMAND COMMAND(s) FULLY_PLANNED/MOVE skip command(s), exit NOOP
*
* (Note: all COMMAND(s) in 2j. should be in PLANNED state)
*/
/*
* _plan_aline() - mp_forward_plan() helper
*
* Calculate ramps for the current planning block and the next PREPPED buffer
* The PREPPED buffer will be set to PLANNED later...
*
* Pass in the bf buffer that will "link" with the planned block
* The block and the buffer are implicitly linked for exec_aline()
*
* Note that that can only be one PLANNED move at a time.
* This is to help sync mr->p to point to the next planned mr->bf
* mr->p is only advanced in mp_exec_aline(), after mp.r = mr->p.
* This code aligns the buffers and the blocks for exec_aline().
*/
static stat_t _plan_aline(mpBuf_t *bf, float entry_velocity)
{
mpBlockRuntimeBuf_t* block = mr->p; // set a local planning block so pointer doesn't change on you
mp_calculate_ramps(block, bf, entry_velocity); // (which it will if you don't do this)
debug_trap_if_true((block->exit_velocity > block->cruise_velocity),
"_plan_line() exit velocity > cruise velocity after calculate_ramps()");
debug_trap_if_true((block->head_length < 0.00001 && block->body_length < 0.00001 && block->tail_length < 0.00001),
"_plan_line() zero or negative length block after calculate_ramps()");
bf->buffer_state = MP_BUFFER_FULLY_PLANNED; //...here
bf->plannable = false;
return (STAT_OK); // report that we planned something...
}
stat_t mp_forward_plan()
{
mpBuf_t *bf = mp_get_run_buffer();
float entry_velocity;
// Case 0: Examine current running buffer for early exit conditions
if (bf == NULL) { // case 0a: NULL means nothing is running - this is OK
st_prep_null();
return (STAT_NOOP);
}
if (bf->buffer_state < MP_BUFFER_BACK_PLANNED) { // case 0b: nothing to do. get outta here.
return (STAT_NOOP);
}
// Case 2: Running cases - move bf past run buffer so it acts like case 1
if (bf->buffer_state == MP_BUFFER_RUNNING) {
bf = bf->nx;
entry_velocity = mr->r->exit_velocity; // set Note 1 entry_velocity (move cases)
} else {
entry_velocity = mr->entry_velocity; // set Note 2 entry velocity (command cases)
}
// bf points to a command block; start cases 1f, 1g, 1h, 1i, 1j, 1k, 2c, 2d, 2e, 2h, 2i, 2j
bool planned_something = false;
if (bf->block_type != BLOCK_TYPE_ALINE) { // meaning it's a COMMAND
while (bf->block_type >= BLOCK_TYPE_COMMAND) {
if (bf->buffer_state == MP_BUFFER_BACK_PLANNED) {
bf->buffer_state = MP_BUFFER_FULLY_PLANNED; // "planning" is just setting the state (for now)
planned_something = true;
}
bf = bf->nx;
}
// Note: bf now points to the first non-command buffer past the command(s)
if ((bf->block_type == BLOCK_TYPE_ALINE) && (bf->buffer_state > MP_BUFFER_BACK_PLANNED )) { // case 1i
entry_velocity = mr->r->exit_velocity; // set entry_velocity for Note 1a
}
}
// bf will always be on a non-command at this point - either a move or empty buffer
// process move
if (bf->block_type == BLOCK_TYPE_ALINE) { // do cases 1a - 1e; finish cases 1f - 1k
if (bf->buffer_state == MP_BUFFER_BACK_PLANNED) {// do 1a; finish 1f, 1j, 2d, 2i
_plan_aline(bf, entry_velocity);
planned_something = true;
}
}
return (planned_something ? STAT_OK : STAT_NOOP);
}
/*************************************************************************
* mp_exec_move() - execute runtime functions to prep move for steppers
*
* Dequeues the buffer queue and executes the move continuations.
* Manages run buffers and other details
*/
stat_t mp_exec_move()
{
mpBuf_t *bf;
// It is possible to try to try to exec from a priming planner if coming off a hold
// This occurs if new p1 commands (and were held back) arrived while in a hold
// if (mp->planner_state <= MP_BUFFER_BACK_PLANNED) {
// st_prep_null();
// return (STAT_NOOP);
// }
// Run an out of band dwell. It was probably set in the previous st_load_move()
if (mr->out_of_band_dwell_flag) {
mr->out_of_band_dwell_flag = false;
st_prep_out_of_band_dwell(mr->out_of_band_dwell_seconds * 1000000);
return (STAT_OK);
}
// Getting a NULL buffer means nothing's running in the queue - this is OK
if ((bf = mp_get_run_buffer()) == NULL) {
st_prep_null();
return (STAT_NOOP);
}
if (bf->block_type == BLOCK_TYPE_ALINE) { // cycle auto-start for lines only
// first-time operations
if (bf->buffer_state != MP_BUFFER_RUNNING) {
if ((bf->buffer_state < MP_BUFFER_BACK_PLANNED) && (cm->motion_state == MOTION_RUN)) {
// debug_trap("mp_exec_move() buffer is not prepped. Starvation"); // IMPORTANT: can't rpt_exception from here!
st_prep_null();
return (STAT_NOOP);
}
if ((bf->nx->buffer_state < MP_BUFFER_BACK_PLANNED) && (bf->nx->buffer_state > MP_BUFFER_EMPTY)) {
// This detects buffer starvation, but also can be a single-line "jog" or command
// rpt_exception(42, "mp_exec_move() next buffer is empty");
// ^^^ CAUSES A CRASH. We can't rpt_exception from here!
debug_trap("mp_exec_move() no buffer prepped - starvation");
}
if (bf->buffer_state == MP_BUFFER_BACK_PLANNED) {
debug_trap_if_true((cm->motion_state == MOTION_RUN), "mp_exec_move() buffer prepped but not planned");
// IMPORTANT: can't rpt_exception from here!
// We need to have it planned. We don't want to do this here,
// as it might already be happening in a lower interrupt.
st_request_forward_plan();
return (STAT_NOOP);
}
if (bf->buffer_state == MP_BUFFER_FULLY_PLANNED) {
bf->buffer_state = MP_BUFFER_RUNNING; // must precede mp_planner_time_acccounting()
} else {
return (STAT_NOOP);
}
mp_planner_time_accounting();
}
// Go ahead and *ask* for a forward planning of the next move.
// This won't call mp_plan_move until we leave this function
// (and have called mp_exec_aline via bf->bf_func).
// This also allows mp_exec_aline to advance mr->p first.
if (bf->nx->buffer_state >= MP_BUFFER_BACK_PLANNED) {
st_request_forward_plan();
}
}
if (bf->bf_func == NULL) {
return(cm_panic(STAT_INTERNAL_ERROR, "mp_exec_move()")); // never supposed to get here
}
return (bf->bf_func(bf)); // run the move callback in the planner buffer
}
/*************************************************************************/
/**** ALINE EXECUTION ROUTINES *******************************************/
/*************************************************************************
* ---> Everything here fires from interrupts and must be interrupt safe
*
* _exec_aline() - acceleration line main routine
* _exec_aline_head() - helper for acceleration section
* _exec_aline_body() - helper for cruise section
* _exec_aline_tail() - helper for deceleration section
* _exec_aline_segment() - helper for running a segment
*
* Returns:
* STAT_OK move is done
* STAT_EAGAIN move is not finished - has more segments to run
* STAT_NOOP would cause no operation to the steppers - do not load the move
* STAT_xxxxx fatal error. Ends the move and frees the bf buffer
*
* This routine is called from the (LO) interrupt level. The interrupt sequencing
* relies on the behaviors of the routines being exactly correct. Each call to
* _exec_aline() must execute and prep **one and only one** segment. If the segment
* is the not the last segment in the bf buffer the _aline() must return STAT_EAGAIN.
* If it's the last segment it must return STAT_OK. If it encounters a fatal error
* that would terminate the move it should return a valid error code. Failure to
* obey this will introduce subtle and very difficult to diagnose bugs (trust us on this).
*
* Note 1: Returning STAT_OK ends the move and frees the bf buffer.
* Returning STAT_OK at this point does NOT advance the position vector,
* meaning any position error will be compensated by the next move.
*
* Note 2: BF/MR sequencing solves a potential race condition where the current move
* ends but the new move has not started because the previous move is still
* being run by the steppers. Planning can overwrite the new move.
*/
/* --- State transitions - hierarchical state machine ---
*
* bf->block_state transitions:
* from _NEW to _RUN on first call (sub_state set to _OFF)
* from _RUN to _OFF on final call
* or just remains _OFF
*
* mr->block_state transitions on first call from _OFF to one of _HEAD, _BODY, _TAIL
* Within each section state may be
* _NEW - trigger initialization
* _RUN1 - run the first part
* _RUN2 - run the second part
*
* Important distinction to note:
* - mp_plan move() is called for every type of move (bf block)
* - mp_exec_move() is called for every type of move
* - mp_exec_aline() is only called for alines
*/
/* Synchronization of run BUFFER and run BLOCK
*
* Note first: mp_exec_aline() makes a huge assumption: When it comes time to get a
* new run block (mr->r) it assumes the planner block (mr->p) has been fully planned
* via the JIT forward planning and is ready for use as the new run block.
*
* The runtime uses 2 structures for the current move or commend, the run BUFFER
* from the planner queue (mb.r, aka bf), and the run BLOCK from the runtime
* singleton (mr->r). These structures are synchronized implicitly, but not
* explicitly referenced, as pointers can lead to race conditions.
* See plan_zoid.cpp / mp_calculate_ramps() for more details
*
* When mp_exec_aline() needs to grab a new planner buffer for a new move or command
* (i.e. block state is inactive) it swaps (rolls) the run and planner BLOCKS so that
* mr->p (planner block) is now the mr->r (run block), and the old mr->r block becomes
* available for planning; it becomes mr->p block.
*
* At the same time, it's when finished with its current run buffer (mb.r), it has already
* advanced to the next buffer. mp_exec_move() does this at the end of previous move.
* Or in the bootstrap case, there never was a previous mb.r, so the current one is OK.
*
* As if by magic, the new mb.r aligns with the run block that was just moved in from
* the planning block.
*/
/**** NOTICE ** NOTICE ** NOTICE ****
**
** mp_exec_aline() is called in
** --INTERRUPT CONTEXT!!--
**
** Things we MUST NOT do (even indirectly):
** mp_plan_buffer()
** mp_plan_block_list()
** printf()
**
**** NOTICE ** NOTICE ** NOTICE ****/
stat_t mp_exec_aline(mpBuf_t *bf)
{
// don't run the block if the machine is not in cycle
if (cm_get_machine_state() != MACHINE_CYCLE) {
return (STAT_NOOP);
}
// don't run the block if the block is inactive
if (bf->block_state == BLOCK_INACTIVE) {
return (STAT_NOOP);
}
stat_t status;
// Initialize all new blocks, regardless of normal or feedhold operation
if (mr->block_state == BLOCK_INACTIVE) {
// ASSERTIONS
// Zero length moves (and other too-short moves) should have already been removed earlier
// But let's still alert the condition should it ever occur
debug_trap_if_zero(bf->length, "mp_exec_aline() zero length move");
// These equalities in the assertions must be true for this to work:
// entry_velocity <= cruise_velocity
// exit_velocity <= cruise_velocity
//
// NB: Even if the move is head or tail only, cruise velocity needs to be valid.
// This is because a "head" is *always* entry->cruise, and a "tail" is *always* cruise->exit,
// even if there are no other sections in the move. (This is a significant time savings.)
debug_trap_if_true((mr->entry_velocity > mr->r->cruise_velocity),
"mp_exec_aline() mr->entry_velocity > mr->r->cruise_velocity");
debug_trap_if_true((mr->r->exit_velocity > mr->r->cruise_velocity),
"mp_exec_aline() mr->exit_velocity > mr->r->cruise_velocity");
// Start a new move by setting up the runtime singleton (mr)
memcpy(&mr->gm, &(bf->gm), sizeof(GCodeState_t)); // copy in the gcode model state
bf->block_state = BLOCK_ACTIVE; // note that this buffer is running
mr->block_state = BLOCK_INITIAL_ACTION; // note the planner doesn't look at block_state
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// !!! THIS IS THE ONLY PLACE WHERE mr->r AND mr->p ARE ALLOWED TO BE CHANGED !!!
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// Swap P and R blocks
mr->r = mr->p; // we are now going to run the planning block
mr->p = mr->p->nx; // re-use the old running block as the new planning block
// Check to make sure no sections are less than MIN_SEGMENT_TIME & adjust if necessary
_exec_aline_normalize_block(mr->r);
// transfer move parameters from planner buffer to the runtime
copy_vector(mr->unit, bf->unit);
copy_vector(mr->target, bf->gm.target);
copy_vector(mr->axis_flags, bf->axis_flags);
mr->run_bf = bf; // DIAGNOSTIC: points to running bf
mr->plan_bf = bf->nx; // DIAGNOSTIC: points to next bf to forward plan
// characterize the move for starting section - head/body/tail
mr->section_state = SECTION_NEW;
mr->section = SECTION_HEAD;
if (fp_ZERO(mr->r->head_length)) {
mr->section = SECTION_BODY;
if (fp_ZERO(mr->r->body_length)) {
mr->section = SECTION_TAIL;
}
}
// generate the way points for position correction at section ends
for (uint8_t axis=0; axis<AXES; axis++) {
mr->waypoint[SECTION_HEAD][axis] = mr->position[axis] + mr->unit[axis] * mr->r->head_length;
mr->waypoint[SECTION_BODY][axis] = mr->position[axis] + mr->unit[axis] * (mr->r->head_length + mr->r->body_length);
mr->waypoint[SECTION_TAIL][axis] = mr->position[axis] + mr->unit[axis] * (mr->r->head_length + mr->r->body_length + mr->r->tail_length);
}
}
// Feed Override Processing - We need to handle the following cases (listed in rough sequence order):
// Feedhold Processing - We need to handle the following cases (listed in rough sequence order):
if (cm->hold_state != FEEDHOLD_OFF) {
// if running actions, or in HOLD state, or exiting with actions
if (cm->hold_state >= FEEDHOLD_MOTION_STOPPED) { // handles _exec_aline_feedhold_processing case (7)
return (STAT_NOOP); // VERY IMPORTANT to exit as a NOOP. Do not load another move
}
// STAT_OK terminates aline execution for this move
// STAT_NOOP terminates execution and does not load another move
status = _exec_aline_feedhold(bf);
if ((status == STAT_OK) || (status == STAT_NOOP)) {
return (status);
}
}
mr->block_state = BLOCK_ACTIVE;
// NB: from this point on the contents of the bf buffer do not affect execution
//**** main dispatcher to process segments ***
status = STAT_OK;
if (mr->section == SECTION_HEAD) { status = _exec_aline_head(bf); }
else if (mr->section == SECTION_BODY) { status = _exec_aline_body(bf); }
else if (mr->section == SECTION_TAIL) { status = _exec_aline_tail(bf); }
else { return(cm_panic(STAT_INTERNAL_ERROR, "exec_aline()"));} // never supposed to get here
// Conditionally set the move to be unplannable. We can't use the if/else block above,
// since the head may call a body or a tail, and a body call tail, so we wait till after.
//
// Conditions are:
// - Allow 3 segments: 1 segment isn't enough, because there's one running as we execute,
// so it has to be the next one. There's a slight possibility we'll miss that, since we
// didn't necessarily start at the beginning, so three.
// - If it's a head/tail move and we've started the head we can't replan it anyway as
// the head can't be interrupted, and the tail is already as sharp as it can be (or there'd be a body)
// - ...so if you are in a body mark the body unplannable if we are too close to its end.
if ((mr->section == SECTION_TAIL) || ((mr->section == SECTION_BODY) && (mr->segment_count < 3))) {
bf->plannable = false;
}
// Feedhold Case (3): Look for the end of the deceleration to transition HOLD states
// This code sets states used by _exec_feedhold_processing() helper.
if (cm->hold_state == FEEDHOLD_DECEL_TO_ZERO) {
if ((status == STAT_OK) || (status == STAT_NOOP)) {
cm->hold_state = FEEDHOLD_DECEL_COMPLETE;
bf->block_state = BLOCK_INITIAL_ACTION; // reset bf so it can restart the rest of the move
}
}
// Perform motion state transition. Also sets active model to RUNTIME
if (cm->motion_state != MOTION_RUN) {
cm_set_motion_state(MOTION_RUN);
}
// There are 4 things that can happen here depending on return conditions:
// status bf->block_state Description
// ----------- -------------- ----------------------------------------
// STAT_EAGAIN <don't care> mr buffer has more segments to run
// STAT_OK BLOCK_ACTIVE mr and bf buffers are done
// STAT_OK BLOCK_INITIAL_ACTION mr done; bf must be run again (it's been reused)
// STAT_NOOP <don't care> treated as a STAT_OK
if (status == STAT_EAGAIN) {
sr_request_status_report(SR_REQUEST_TIMED); // continue reporting mr buffer
// Note that that'll happen in a lower interrupt level.
} else {
mr->block_state = BLOCK_INACTIVE; // invalidate mr buffer (reset)
mr->section_state = SECTION_OFF;
mp->run_time_remaining = 0.0; // it's done, so time goes to zero
mr->entry_velocity = mr->r->exit_velocity; // feed the old exit into the entry.
if (bf->block_state == BLOCK_ACTIVE) {
if (mp_free_run_buffer()) { // returns true of the buffer is empty
if (cm->hold_state == FEEDHOLD_OFF) {
cm_set_motion_state(MOTION_STOP); // also sets active model to RUNTIME
cm_cycle_end(); // free buffer & end cycle if planner is empty
}
} else {
st_request_forward_plan();
}
}
}
return (status);
}
/*
* Forward difference math explained:
*
* We are using a quintic (fifth-degree) Bezier polynomial for the velocity curve.
* This gives us a "linear pop" velocity curve; with pop being the sixth derivative of position:
* velocity - 1st, acceleration - 2nd, jerk - 3rd, snap - 4th, crackle - 5th, pop - 6th
*
* The Bezier curve takes the form:
*
* V(t) = P_0 * B_0(t) + P_1 * B_1(t) + P_2 * B_2(t) + P_3 * B_3(t) + P_4 * B_4(t) + P_5 * B_5(t)
*
* Where 0 <= t <= 1, and V(t) is the velocity. P_0 through P_5 are the control points, and B_0(t)
* through B_5(t) are the Bernstein basis as follows:
*
* B_0(t) = (1-t)^5 = -t^5 + 5t^4 - 10t^3 + 10t^2 - 5t + 1
* B_1(t) = 5(1-t)^4 * t = 5t^5 - 20t^4 + 30t^3 - 20t^2 + 5t
* B_2(t) = 10(1-t)^3 * t^2 = -10t^5 + 30t^4 - 30t^3 + 10t^2
* B_3(t) = 10(1-t)^2 * t^3 = 10t^5 - 20t^4 + 10t^3
* B_4(t) = 5(1-t) * t^4 = -5t^5 + 5t^4
* B_5(t) = t^5 = t^5
* ^ ^ ^ ^ ^ ^
* | | | | | |
* A B C D E F
*
*
* We use forward-differencing to calculate each position through the curve.
* This requires a formula of the form:
*
* V_f(t) = A*t^5 + B*t^4 + C*t^3 + D*t^2 + E*t + F
*
* Looking at the above B_0(t) through B_5(t) expanded forms, if we take the coefficients of t^5
* through t of the Bezier form of V(t), we can determine that:
*
* A = -P_0 + 5*P_1 - 10*P_2 + 10*P_3 - 5*P_4 + P_5
* B = 5*P_0 - 20*P_1 + 30*P_2 - 20*P_3 + 5*P_4
* C = -10*P_0 + 30*P_1 - 30*P_2 + 10*P_3
* D = 10*P_0 - 20*P_1 + 10*P_2
* E = - 5*P_0 + 5*P_1
* F = P_0
*
* Now, since we will (currently) *always* want the initial acceleration and jerk values to be 0,
* We set P_i = P_0 = P_1 = P_2 (initial velocity), and P_t = P_3 = P_4 = P_5 (target velocity),
* which, after simplification, resolves to:
*
* A = - 6*P_i + 6*P_t
* B = 15*P_i - 15*P_t
* C = -10*P_i + 10*P_t
* D = 0
* E = 0
* F = P_i
*
* Given an interval count of I to get from P_i to P_t, we get the parametric "step" size of h = 1/I.
* We need to calculate the initial value of forward differences (F_0 - F_5) such that the inital
* velocity V = P_i, then we iterate over the following I times:
*
* V += F_5
* F_5 += F_4
* F_4 += F_3
* F_3 += F_2
* F_2 += F_1
*
* See http://www.drdobbs.com/forward-difference-calculation-of-bezier/184403417 for an example of
* how to calculate F_0 - F_5 for a cubic bezier curve. Since this is a quintic bezier curve, we
* need to extend the formulas somewhat. I'll not go into the long-winded step-by-step here,
* but it gives the resulting formulas:
*
* a = A, b = B, c = C, d = D, e = E, f = F
* F_5(t+h)-F_5(t) = (5ah)t^4 + (10ah^2 + 4bh)t^3 + (10ah^3 + 6bh^2 + 3ch)t^2 +
* (5ah^4 + 4bh^3 + 3ch^2 + 2dh)t + ah^5 + bh^4 + ch^3 + dh^2 + eh
*
* a = 5ah
* b = 10ah^2 + 4bh
* c = 10ah^3 + 6bh^2 + 3ch
* d = 5ah^4 + 4bh^3 + 3ch^2 + 2dh
*
* (After substitution, simplification, and rearranging):
* F_4(t+h)-F_4(t) = (20ah^2)t^3 + (60ah^3 + 12bh^2)t^2 + (70ah^4 + 24bh^3 + 6ch^2)t +
* 30ah^5 + 14bh^4 + 6ch^3 + 2dh^2
*
* a = (20ah^2)
* b = (60ah^3 + 12bh^2)
* c = (70ah^4 + 24bh^3 + 6ch^2)
*
* (After substitution, simplification, and rearranging):
* F_3(t+h)-F_3(t) = (60ah^3)t^2 + (180ah^4 + 24bh^3)t + 150ah^5 + 36bh^4 + 6ch^3
*
* (You get the picture...)
* F_2(t+h)-F_2(t) = (120ah^4)t + 240ah^5 + 24bh^4
* F_1(t+h)-F_1(t) = 120ah^5
*
* Normally, we could then assign t = 0, use the A-F values from above, and get out initial F_* values.
* However, for the sake of "averaging" the velocity of each segment, we actually want to have the initial
* V be be at t = h/2 and iterate I-1 times. So, the resulting F_* values are (steps not shown):
*
* F_5 = (121Ah^5)/16 + 5Bh^4 + (13Ch^3)/4 + 2Dh^2 + Eh
* F_4 = (165Ah^5)/2 + 29Bh^4 + 9Ch^3 + 2Dh^2
* F_3 = 255Ah^5 + 48Bh^4 + 6Ch^3
* F_2 = 300Ah^5 + 24Bh^4
* F_1 = 120Ah^5
*
* Note that with our current control points, D and E are actually 0.
*/
// Total time: 147us
static void _init_forward_diffs(const float v_0, const float v_1)
{
// Times from *here*
/* Full formulation:
const float fifth_T = T * 0.2; //(1/5) T
const float two_fifths_T = T * 0.4; //(2/5) T
const float twentienth_T_2 = T * T * 0.05; // (1/20) T^2
const float P_0 = v_0;
const float P_1 = v_0 + fifth_T*a_0;
const float P_2 = v_0 + two_fifths_T*a_0 + twentienth_T_2*j_0;
const float P_3 = v_1 - two_fifths_T*a_1 + twentienth_T_2*j_1;
const float P_4 = v_1 - fifth_T*a_1;
const float P_5 = v_1;
const float A = 5*( P_1 - P_4 + 2*(P_3 - P_2) ) + P_5 - P_0;
const float B = 5*( P_0 + P_4 - 4*(P_3 + P_1) + 6*P_2 );
const float C = 10*( P_3 - P_0 + 3*(P_1 - P_2) );
const float D = 10*( P_0 + P_2 - 2*P_1 );
const float E = 5*( P_1 - P_0 );
//const float F = P_0;
*/
float A = -6.0*v_0 + 6.0*v_1;
float B = 15.0*v_0 - 15.0*v_1;
float C = -10.0*v_0 + 10.0*v_1;
// D = 0
// E = 0
// F = Vi
const float h = 1/(mr->segments);
const float h_2 = h * h;
const float h_3 = h_2 * h;
const float h_4 = h_3 * h;
const float h_5 = h_4 * h;
const float Ah_5 = A * h_5;
const float Bh_4 = B * h_4;
const float Ch_3 = C * h_3;
const float const1 = 7.5625; // (121.0/16.0)
const float const2 = 3.25; // ( 13.0/ 4.0)
const float const3 = 82.5; // (165.0/ 2.0)
/*
* F_5 = (121/16)A h^5 + 5 B h^4 + (13/4) C h^3 + 2 D h^2 + Eh
* F_4 = (165/2)A h^5 + 29 B h^4 + 9 C h^3 + 2 D h^2
* F_3 = 255 A h^5 + 48 B h^4 + 6 C h^3
* F_2 = 300 A h^5 + 24 B h^4
* F_1 = 120 A h^5
*/
mr->forward_diff_5 = const1*Ah_5 + 5.0*Bh_4 + const2*Ch_3;
mr->forward_diff_4 = const3*Ah_5 + 29.0*Bh_4 + 9.0*Ch_3;
mr->forward_diff_3 = 255.0*Ah_5 + 48.0*Bh_4 + 6.0*Ch_3;
mr->forward_diff_2 = 300.0*Ah_5 + 24.0*Bh_4;
mr->forward_diff_1 = 120.0*Ah_5;
// Calculate the initial velocity by calculating V(h/2)
const float half_h = h * 0.5; // h/2
const float half_h_3 = half_h * half_h * half_h;
const float half_h_4 = half_h_3 * half_h;
const float half_h_5 = half_h_4 * half_h;
const float half_Ch_3 = C * half_h_3;
const float half_Bh_4 = B * half_h_4;
const float half_Ah_5 = A * half_h_5;
mr->segment_velocity = half_Ah_5 + half_Bh_4 + half_Ch_3 + v_0;
}
/*********************************************************************************************
* _exec_aline_head()
*/
static stat_t _exec_aline_head(mpBuf_t *bf)
{
bool first_pass = false;
if (mr->section_state == SECTION_NEW) { // INITIALIZATION
first_pass = true;
if (fp_ZERO(mr->r->head_length)) { // Needed here as feedhold may have changed the block
mr->section = SECTION_BODY;
return(_exec_aline_body(bf)); // skip ahead to the body generator
}
mr->segments = ceil(uSec(mr->r->head_time) / NOM_SEGMENT_USEC);// # of segments for the section
mr->segment_count = (uint32_t)mr->segments;
mr->segment_time = mr->r->head_time / mr->segments; // time to advance for each segment
if (mr->segment_count == 1) {
// We will only have one segment, simply average the velocities
mr->segment_velocity = mr->r->head_length / mr->segment_time;
} else {
_init_forward_diffs(mr->entry_velocity, mr->r->cruise_velocity); // sets initial segment_velocity
}
if (mr->segment_time < MIN_SEGMENT_TIME) {
debug_trap("mr->segment_time < MIN_SEGMENT_TIME (head)");
return (STAT_OK); // exit without advancing position, say we're done
}
// If this trap ever fires put this statement back in: mr->section = SECTION_HEAD;
debug_trap_if_true(mr->section != SECTION_HEAD, "exec_aline() Not section head");
mr->section_state = SECTION_RUNNING;
} else {
mr->segment_velocity += mr->forward_diff_5;
}
if (_exec_aline_segment() == STAT_OK) { // set up for second half
if ((fp_ZERO(mr->r->body_length)) && (fp_ZERO(mr->r->tail_length))) {
return (STAT_OK); // ends the move
}
mr->section = SECTION_BODY; // advance to body
mr->section_state = SECTION_NEW;
}
else if (!first_pass) {
mr->forward_diff_5 += mr->forward_diff_4;
mr->forward_diff_4 += mr->forward_diff_3;
mr->forward_diff_3 += mr->forward_diff_2;
mr->forward_diff_2 += mr->forward_diff_1;
}
return (STAT_EAGAIN);
}
/*********************************************************************************************
* _exec_aline_body()
*
* The body is broken into little segments even though it is a straight line
* so that feed holds can happen in the middle of a line with minimum latency
*/
static stat_t _exec_aline_body(mpBuf_t *bf)
{
if (mr->section_state == SECTION_NEW) {
if (fp_ZERO(mr->r->body_length)) { // Needed here as feedhold may have changed the block
mr->section = SECTION_TAIL;
return(_exec_aline_tail(bf)); // skip ahead to tail generator
}
float body_time = mr->r->body_time;
mr->segments = ceil(uSec(body_time) / NOM_SEGMENT_USEC);
mr->segment_time = body_time / mr->segments;
mr->segment_velocity = mr->r->cruise_velocity;
mr->segment_count = (uint32_t)mr->segments;
if (mr->segment_time < MIN_SEGMENT_TIME) {
debug_trap("mr->segment_time < MIN_SEGMENT_TIME (body)");
return (STAT_OK); // exit without advancing position, say we're done
}
// If this trap ever fires put this statement back in: mr->section = SECTION_BODY;
debug_trap_if_true(mr->section != SECTION_BODY, "exec_aline() Not section body");
mr->section_state = SECTION_RUNNING; // uses PERIOD_2 so last segment detection works
}
if (_exec_aline_segment() == STAT_OK) { // OK means this section is done
if (fp_ZERO(mr->r->tail_length)) {
return (STAT_OK); // ends the move
}
mr->section = SECTION_TAIL; // advance to tail
mr->section_state = SECTION_NEW;
}
return (STAT_EAGAIN);
}
/*********************************************************************************************
* _exec_aline_tail()
*/
static stat_t _exec_aline_tail(mpBuf_t *bf)
{
bool first_pass = false;
if (mr->section_state == SECTION_NEW) { // INITIALIZATION
first_pass = true;
bf->plannable = false; // Mark the block as unplannable
if (fp_ZERO(mr->r->tail_length)) { // Needed here as feedhold may have changed the block
return(STAT_OK); // end the move
}
mr->segments = ceil(uSec(mr->r->tail_time) / NOM_SEGMENT_USEC);// # of segments for the section
mr->segment_count = (uint32_t)mr->segments;
mr->segment_time = mr->r->tail_time / mr->segments; // time to advance for each segment
if (mr->segment_count == 1) {
mr->segment_velocity = mr->r->tail_length / mr->segment_time;
} else {
_init_forward_diffs(mr->r->cruise_velocity, mr->r->exit_velocity); // sets initial segment_velocity
}
if (mr->segment_time < MIN_SEGMENT_TIME) {
debug_trap("mr->segment_time < MIN_SEGMENT_TIME (tail)");
return (STAT_OK); // exit without advancing position, say we're done
}
// If this trap ever fires put this statement back in: mr->section = SECTION_TAIL;
debug_trap_if_true(mr->section != SECTION_TAIL, "exec_aline() Not section tail");
mr->section_state = SECTION_RUNNING;
} else {
mr->segment_velocity += mr->forward_diff_5;
}
if (_exec_aline_segment() == STAT_OK) {
return (STAT_OK); // STAT_OK completes the move
}
else if (!first_pass) {
mr->forward_diff_5 += mr->forward_diff_4;
mr->forward_diff_4 += mr->forward_diff_3;
mr->forward_diff_3 += mr->forward_diff_2;
mr->forward_diff_2 += mr->forward_diff_1;
}
return (STAT_EAGAIN);
}
/*********************************************************************************************
* _exec_aline_segment() - segment runner helper
*
* NOTES ON STEP ERROR CORRECTION:
*
* The commanded_steps are the target_steps delayed by one more segment.
* This lines them up in time with the encoder readings so a following error can be generated
*
* The following_error term is positive if the encoder reading is greater than (ahead of)
* the commanded steps, and negative (behind) if the encoder reading is less than the
* commanded steps. The following error is not affected by the direction of movement -
* it's purely a statement of relative position. Examples:
*
* Encoder Commanded Following Err
* 100 90 +10 encoder is 10 steps ahead of commanded steps
* -90 -100 +10 encoder is 10 steps ahead of commanded steps
* 90 100 -10 encoder is 10 steps behind commanded steps
* -100 -90 -10 encoder is 10 steps behind commanded steps
*/
static stat_t _exec_aline_segment()
{
float travel_steps[MOTORS];
// Set target position for the segment
// If the segment ends on a section waypoint synchronize to the head, body or tail end
// Otherwise if not at a section waypoint compute target from segment time and velocity
// Don't do waypoint correction if you are going into a hold.
if ((--mr->segment_count == 0) && (cm->hold_state == FEEDHOLD_OFF)) {
copy_vector(mr->gm.target, mr->waypoint[mr->section]);
} else {
float segment_length = mr->segment_velocity * mr->segment_time;
// See https://en.wikipedia.org/wiki/Kahan_summation_algorithm
// for the summation compensation description
for (uint8_t a=0; a<AXES; a++) {
float to_add = (mr->unit[a] * segment_length) - mr->gm.target_comp[a];
float target = mr->position[a] + to_add;
mr->gm.target_comp[a] = (target - mr->position[a]) - to_add;
mr->gm.target[a] = target;
// the above replaces this line:
// mr->gm.target[a] = mr->position[a] + (mr->unit[a] * segment_length);
}
}
if (cm_is_laser_tool() && spindle.direction == SPINDLE_CCW) {
spindle_update_laser_override(mr->segment_velocity);
}
// Convert target position to steps
// Bucket-brigade the old target down the chain before getting the new target from kinematics
//
// Very small travels of less than 0.01 step are truncated to zero. This is to correct a condition
// where a rounding error in kinematics could reverse the direction of a move in the extreme head or tail.
// Truncating the move contributes to positional error, but this is corrected by encoder feedback should
// it ever accumulate to more than one step.
//
// NB: The direct manipulation of steps to compute travel_steps only works for Cartesian kinematics.
// Other kinematics may require transforming travel distance as opposed to simply subtracting steps.
for (uint8_t m=0; m<MOTORS; m++) {
mr->commanded_steps[m] = mr->position_steps[m]; // previous segment's position, delayed by 1 segment
mr->position_steps[m] = mr->target_steps[m]; // previous segment's target becomes position
mr->encoder_steps[m] = en_read_encoder(m); // get current encoder position (time aligns to commanded_steps)
mr->following_error[m] = mr->encoder_steps[m] - mr->commanded_steps[m];
}
kn_inverse_kinematics(mr->gm.target, mr->target_steps); // now determine the target steps...
for (uint8_t m=0; m<MOTORS; m++) { // and compute the distances to be traveled
travel_steps[m] = mr->target_steps[m] - mr->position_steps[m];
if (fabs(travel_steps[m]) < 0.01) { // truncate very small moves to deal with rounding errors
travel_steps[m] = 0;
}
}
// Update the mb->run_time_remaining -- we know it's missing the current segment's time before it's loaded, that's ok.
mp->run_time_remaining -= mr->segment_time;
if (mp->run_time_remaining < 0) {
mp->run_time_remaining = 0.0;
}
// Call the stepper prep function
ritorno(st_prep_line(travel_steps, mr->following_error, mr->segment_time));
copy_vector(mr->position, mr->gm.target); // update position from target
if (mr->segment_count == 0) {
return (STAT_OK); // this section has run all its segments
}
return (STAT_EAGAIN); // this section still has more segments to run
}
/*********************************************************************************************
* _exec_aline_normalize_block() - re-organize block to eliminate minimum time segments
*
* Check to make sure no sections are less than MIN_SEGMENT_TIME & adjust if necessary
*/
static void _exec_aline_normalize_block(mpBlockRuntimeBuf_t *b)
{
if ((b->head_length > 0) && (b->head_time < MIN_SEGMENT_TIME)) {
// Compute the new body time. head_time !== body_time
b->body_length += b->head_length;
b->body_time = b->body_length / b->cruise_velocity;
b->head_length = 0;
b->head_time = 0;
}
if ((b->tail_length > 0) && (b->tail_time < MIN_SEGMENT_TIME)) {
// Compute the new body time. tail_time !== body_time
b->body_length += b->tail_length;
b->body_time = b->body_length / b->cruise_velocity;
b->tail_length = 0;
b->tail_time = 0;
}
// At this point, we've already possibly merged head and/or tail into the body.
// If the body is still too "short" (brief) we *might* be able to add it to a head or tail.
// If there's still a head or a tail, we will add the body to whichever there is, maybe both.
// We saved it for last since it's the most expensive.
if ((b->body_length > 0) && (b->body_time < MIN_SEGMENT_TIME)) {
// We'll add the time to either the head or the tail or split it
if (b->tail_length > 0) {
if (b->head_length > 0) { // Split the body to the head and tail
b->head_length += b->body_length * 0.5;
b->tail_length += b->body_length * 0.5; // let the compiler optimize out one of these *
b->head_time = (2.0 * b->head_length) / (mr->entry_velocity + b->cruise_velocity);
b->tail_time = (2.0 * b->tail_length) / (b->cruise_velocity + b->exit_velocity);