-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathavr_dualsteppers.c
2574 lines (2239 loc) · 98.2 KB
/
avr_dualsteppers.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
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
#include <avr/io.h>
#include <avr/interrupt.h>
#include <math.h>
#include <util/twi.h>
#include <stdint.h>
#define DEBUG
#ifndef STEPPER_I2C_ADDRESS
#define STEPPER_I2C_ADDRESS 0x14
#endif
#ifndef M_PI
#define M_PI 3.14159265358979323846264338327950288
#endif
/*
If the following preprocessor directive is defined the steppers
will be put in sleep mode upon reset (after initial power-up)
*/
#define STEPPER_DISABLE_STARTUP 1
#ifndef __cplusplus
typedef int bool;
#define true 1
#define false 0
#endif
/*
Pin usage:
pinDir1 2 PD2 OUT
pinStep1 3 PD3 OUT
pinFault1 16 PC2 IN PCINT10 (PCI1)
pinDir2 14 PC0 OUT
pinStep2 15 PC1 OUT
pinFault2 17 PC3 IN PCINT11 (PCI1)
pinSleep 4 PD4 OUT
pinReset 5 PD5 OUT
pinMode2 6 PD6 OUT
pinMode1 7 PD7 OUT
pinMode0 8 PB0 OUT
pinEnable 9 PB1 OUT
I/O bank configuration:
PB: 0 OUT LOW (Fullstep)
1 OUT HIGH (ENABLE)
2 IN no pull
3 IN no pull
4 IN no pull
5 IN no pull
6 IN no pull
7 IN no pull
DDRB = 0x03;
PORTB = 0x02;
PC: 0 OUT LOW
1 OUT LOW
2 IN no pull
3 IN no pull
4 IN no pull
5 IN no pull
6 IN no pull
7 IN no pull
DDRC = 0x03;
PORTC = 0x00;
PD: 0 IN no pull
1 IN no pull
2 OUT LOW (Dir)
3 OUT LOW (Step)
4 OUT LOW (SLEEP)
5 OUT LOW (RESET)
6 OUT LOW (Fullstep)
7 OUT LOW (Fullstep)
DDRD = 0xFC;
PORTD = 0x00;
*/
/*
Master clock source will be TIMER2
Running with one of the following settings:
Prescaler Frequency OCR1A Effective Pulse Frequency
/64 250 kHz 2 62.5 kHz
/256 62.5 kHz 1 31.2 kHz
/1024 15.625 kHz 1 7.812 kHz
*/
#ifndef STEPPER_COMMANDQUEUELENGTH
#define STEPPER_COMMANDQUEUELENGTH 10
#endif
#define STEPPER_TIMERTICK_FRQ 7812/2
#define STEPPER_TIMERTICK_PRESCALER 0x06
#define STEPPER_TIMERTICK_OVERFLOWVAL 0x01
#define STEPPER_WAKEUP_SLEEP_TICKS 10 /* More than 1.2 ms! */
/*
#define STEPPER_INITIAL_ALPHA (0.01 * M_PI)
#define STEPPER_INITIAL_VMAX (83.0 * M_PI)
#define STEPPER_INITIAL_ACCELERATION (18.0 * M_PI)
#define STEPPER_INITIAL_DECELERATION (-18.0 * M_PI)
*/
#define STEPPER_INITIAL_ALPHA (0.01 * M_PI)
#define STEPPER_INITIAL_VMAX 6
#define STEPPER_INITIAL_ACCELERATION 1 /* 6 */
#define STEPPER_INITIAL_DECELERATION -1 /* -6 */
#define STEPPER_INITIAL_MICROSTEPPING 0
/*
Min and max configureable values
Alpha: Step size in radians
V_max: Maximum speed in radians per second
Note: Since max. 250 kHz stepping frequency
is supported by DRV8825 we use this as
the limit together with alpha.
(Alpha * 250 000)
a, d Acceleration and deceleration in radians per
squaresecond
*/
#define STEPPER_MAX_ALPHA (0.01 * M_PI)
#define STEPPER_MIN_ALPHA ((0.01 * M_PI)/128.0)
#define STEPPER_MAX_VMAX 100 /* (STEPPER_MAX_ALPHA * 250000.0) */
#define STEPPER_MIN_VMAX 0.01
// #define STEPPER_MIN_ACCELERATION 0.01 /* Currently arbitrary! */
// #define STEPPER_MAX_ACCELERATION ((STEPPER_MAX_VMAX * STEPPER_MAX_VMAX)/(STEPPER_MIN_ALPHA*4294967296.0))
#define STEPPER_MIN_ACCELERATION ((STEPPER_MAX_VMAX * STEPPER_MAX_VMAX)/(STEPPER_MIN_ALPHA*4294967296.0))
#define STEPPER_MAX_ACCELERATION 1000 /* Currently arbitrary */
#define STEPPER_MIN_DECELERATION (-1.0 * ((STEPPER_MAX_VMAX * STEPPER_MAX_VMAX)/(STEPPER_MIN_ALPHA*4294967296.0)))
#define STEPPER_MAX_DECELERATION -0.01 /* Currently arbitrary! */
#define STEPPER_MIN_RELATIVEDISTANCE STEPPER_MIN_ALPHA
#define STEPPER_MAX_RELATIVEDISTANCE (4294967295.0 * STEPPER_MIN_ALPHA)
/*
We use our own systick implementation
*/
unsigned long int millis();
unsigned long int micros();
void delay(unsigned long millisecs);
void delayMicros(unsigned int microDelay);
enum stepperCommandType {
stepperCommand_AccelerateStopToStop,
stepperCommand_ConstantSpeed,
stepperCommand_Stop,
stepperCommand_Disable,
stepperCommand_AccelerateToSpeed,
stepperCommand_DecelerateToSpeed,
};
struct stepperCommand {
enum stepperCommandType cmdType;
int forward;
union {
struct {
uint32_t nA;
uint32_t nC;
uint32_t nD;
double c7, c8, c9; /* Calculation constants for our specific acceleration and deceleration. */
double initialDelayTicks;
} acceleratedStopToStop;
struct {
uint32_t cConst; /* In case of constant speed mode we supply the tick count per constant step */
} constantSpeed;
struct {
uint32_t nA;
uint32_t nD;
double c8, c9;
double cEnd;
double cStart;
} accelerateDecelerateToConstSpeed;
} data;
};
/*
Command queue is a ringbuffer. Head points always to the next entry
that is available for writing. If head == tail the queue is EMPTY!
Tail is only ever advanced by TIMER2_COMPA_vect, head only ever by
the motion planner.
If Head + 1 == Tail the queue is currently FULL and nothing can
be enqueued.
*/
struct stepperState {
double c_i; /* The previously loaded c_i (used during calculation and advancing) */
uint32_t counterCurrent; /* Counter reduced every time until we reach zero */
struct stepperCommand cmdQueue[STEPPER_COMMANDQUEUELENGTH];
unsigned int cmdQueueHead;
unsigned int cmdQueueTail;
#ifdef ENABLE_ABSOLUTEPOSITION
long int currentPosition; /* Current position in "steps" i.e. currentPosition * alpha = angular Position */
#endif
/* Cache for precalculated constants - see documentation */
struct {
double c1;
double c2;
double c3;
double c4;
double c5;
double c7;
double c8;
double c9;
double c10;
} constants;
struct {
double acceleration; /* in rad/sec */
double deceleration; /* in rad/sec */
double alpha; /* step size in rad */
double vmax; /* in rad/sec */
} settings;
};
#define STEPPER_COUNT 2
/*
Declare state for both steppers
*/
static struct stepperState state[STEPPER_COUNT];
static uint8_t stateMicrostepping;
volatile static uint8_t stateFault;
static uint8_t drvEnableState;
static uint8_t drvRealEnabled;
bool bResetRun = false;
static volatile bool bIntTriggered = false;
/*@
requires \valid(&PORTC) && \valid(&PORTD);
assigns drvRealEnabled;
assigns bIntTriggered;
assigns PORTC, PORTD;
ensures
\forall integer iStep; 0 <= iStep < STEPPER_COUNT
==> (state[iStep].cmdQueueHead >= 0) && (state[iStep].cmdQueueHead < STEPPER_COMMANDQUEUELENGTH);
ensures
\forall integer iStep; 0 <= iStep < STEPPER_COUNT
==> (state[iStep].cmdQueueTail >= 0) && (state[iStep].cmdQueueTail < STEPPER_COMMANDQUEUELENGTH);
*/
static void handleTimer2Interrupt() {
if(!bIntTriggered) {
return;
}
bIntTriggered = false;
/*
Re-enable counter. This is used to delay the
next stepper commands after re-awaking from
sleep.
*/
if((drvRealEnabled & 0x7F) != 0) {
drvRealEnabled = drvRealEnabled - 1;
return;
}
/*
Interrupt handler executed with 2 * STEPPER_TIMERTICK_FRQ frequency
either use to disable step pins or advance state machine depending on
out state. We have to finish in way less then 1024 ticks ...
*/
int stepperIdx;
/*@
loop assigns PORTD, PORTC;
loop assigns drvRealEnabled, drvEnableState;
loop assigns state[0 .. (STEPPER_COUNT-1)];
loop invariant 0 <= stepperIdx < STEPPER_COUNT;
*/
for(stepperIdx = 0; stepperIdx < STEPPER_COUNT; stepperIdx = stepperIdx + 1) {
if(bResetRun) {
if(stepperIdx == 0) {
// Pulse PD2 low
PORTD = PORTD & (~0x08);
} else {
// Pulse PC1 low
PORTC = PORTC & (~0x02);
}
if((drvEnableState == 0) && ((drvRealEnabled & 0x80) != 0)) {
/* Put driver into sleep if none is enabled AND our current real enable state is not the same */
drvRealEnabled = drvRealEnabled & 0x7F;
PORTD = PORTD & (~0x10); /* Enter sleep state by pulling SLEEP LOW (enabled) */
}
} else {
if(state[stepperIdx].cmdQueueTail == state[stepperIdx].cmdQueueHead) {
/* Nothing to do ... */
continue;
}
const int qIdx = state[stepperIdx].cmdQueueTail;
if(state[stepperIdx].c_i == -1) {
/* We wake up from idle ... any may have to do some initialization */
if(stepperIdx == 0) {
// PD2
if(state[stepperIdx].cmdQueue[qIdx].forward != 0) {
PORTD = PORTD | 0x04;
} else {
PORTD = PORTD & (~0x04);
}
} else {
// PC0
if(state[stepperIdx].cmdQueue[qIdx].forward != 0) {
PORTC = PORTC | 0x01;
} else {
PORTC = PORTC & (~0x01);
}
}
/* Check if drivers are disabled and we have to execute an command ... */
if((drvRealEnabled & 0x80) == 0) {
/* Steppers are disabled. Re-enable and wait for startup period */
PORTD = PORTD | 0x10; /* Leave sleep state by pulling SLEEP HIGH (disabled) */
drvRealEnabled = 0x80 | STEPPER_WAKEUP_SLEEP_TICKS;
drvEnableState = drvEnableState | (1 << stepperIdx);
break; // Next stepper as usual
}
if(state[stepperIdx].cmdQueue[qIdx].cmdType == stepperCommand_AccelerateStopToStop) {
state[stepperIdx].c_i = state[stepperIdx].cmdQueue[qIdx].data.acceleratedStopToStop.initialDelayTicks;
if(state[stepperIdx].c_i > 4294967295.0) {
state[stepperIdx].counterCurrent = ~0;
} else if(state[stepperIdx].c_i < 1.0) {
state[stepperIdx].counterCurrent = 1;
} else {
state[stepperIdx].counterCurrent = (uint32_t)state[stepperIdx].c_i;
}
} else if(state[stepperIdx].cmdQueue[qIdx].cmdType == stepperCommand_ConstantSpeed) {
state[stepperIdx].c_i = state[stepperIdx].cmdQueue[qIdx].data.constantSpeed.cConst;
state[stepperIdx].counterCurrent = state[stepperIdx].cmdQueue[qIdx].data.constantSpeed.cConst;
} else if(state[stepperIdx].cmdQueue[qIdx].cmdType == stepperCommand_Disable) {
drvEnableState = drvEnableState & (~(0x01 << stepperIdx));
/* Switch to next state at next interrupt */
state[stepperIdx].cmdQueueTail = (state[stepperIdx].cmdQueueTail + 1) % STEPPER_COMMANDQUEUELENGTH;
state[stepperIdx].c_i = -1; /* This will initialize the next state as soon as it is available */
continue;
} else if(state[stepperIdx].cmdQueue[qIdx].cmdType == stepperCommand_Stop) {
/*
Our driver has already been enabled since we do that whenever we
reach an enqueued command. So simply remove this task from the task
queue. As long as nothing has been enqueued the steppers will be
engaged and stopped
*/
state[stepperIdx].cmdQueueTail = (state[stepperIdx].cmdQueueTail + 1) % STEPPER_COMMANDQUEUELENGTH;
state[stepperIdx].c_i = -1; /* This will initialize the next state as soon as it is available */
continue;
} else if((state[stepperIdx].cmdQueue[qIdx].cmdType == stepperCommand_AccelerateToSpeed) || (state[stepperIdx].cmdQueue[qIdx].cmdType == stepperCommand_DecelerateToSpeed)) {
state[stepperIdx].c_i = state[stepperIdx].cmdQueue[qIdx].data.accelerateDecelerateToConstSpeed.cStart;
if(state[stepperIdx].c_i > 4294967295.0) {
state[stepperIdx].counterCurrent = ~0;
} else if(state[stepperIdx].c_i < 1.0) {
state[stepperIdx].counterCurrent = 1;
} else {
state[stepperIdx].counterCurrent = (uint32_t)state[stepperIdx].c_i;
}
} else {
continue;
}
} else if((state[stepperIdx].counterCurrent = state[stepperIdx].counterCurrent - 1) > 0) {
/* Just advance our counter ... */
continue;
} else if(state[stepperIdx].cmdQueue[qIdx].cmdType == stepperCommand_AccelerateStopToStop) {
if(state[stepperIdx].cmdQueue[qIdx].data.acceleratedStopToStop.nA > 0) {
state[stepperIdx].c_i = state[stepperIdx].c_i / (1 + state[stepperIdx].constants.c8 * state[stepperIdx].c_i * state[stepperIdx].c_i);
state[stepperIdx].cmdQueue[qIdx].data.acceleratedStopToStop.nA = state[stepperIdx].cmdQueue[qIdx].data.acceleratedStopToStop.nA - 1;
/* c_i to counter value */
if(state[stepperIdx].c_i > 4294967295.0) {
state[stepperIdx].counterCurrent = ~0;
} else if(state[stepperIdx].c_i < 1.0) {
state[stepperIdx].counterCurrent = 1;
} else {
state[stepperIdx].counterCurrent = (uint32_t)state[stepperIdx].c_i;
}
} else if(state[stepperIdx].cmdQueue[qIdx].data.acceleratedStopToStop.nC > 0) {
state[stepperIdx].cmdQueue[qIdx].data.acceleratedStopToStop.nC = state[stepperIdx].cmdQueue[qIdx].data.acceleratedStopToStop.nC - 1;
/* c_i to counter value */
if(state[stepperIdx].c_i > 4294967295.0) {
state[stepperIdx].counterCurrent = ~0;
} else if(state[stepperIdx].c_i < 1.0) {
state[stepperIdx].counterCurrent = 1;
} else {
state[stepperIdx].counterCurrent = (uint32_t)state[stepperIdx].c_i;
}
} else if(state[stepperIdx].cmdQueue[qIdx].data.acceleratedStopToStop.nD > 0) {
state[stepperIdx].c_i = state[stepperIdx].c_i / (1 + state[stepperIdx].constants.c9 * state[stepperIdx].c_i * state[stepperIdx].c_i);
/* c_i to counter value */
if(state[stepperIdx].c_i > 4294967295.0) {
state[stepperIdx].counterCurrent = ~0;
} else if(state[stepperIdx].c_i < 1.0) {
state[stepperIdx].counterCurrent = 1;
} else {
state[stepperIdx].counterCurrent = (uint32_t)state[stepperIdx].c_i;
}
if((state[stepperIdx].cmdQueue[qIdx].data.acceleratedStopToStop.nD = state[stepperIdx].cmdQueue[qIdx].data.acceleratedStopToStop.nD - 1) == 0) {
/* Switch to next state at next interrupt */
state[stepperIdx].cmdQueueTail = (state[stepperIdx].cmdQueueTail + 1) % STEPPER_COMMANDQUEUELENGTH;
state[stepperIdx].c_i = -1; /* This will initialize the next state as soon as it is available */
}
}
} else if(state[stepperIdx].cmdQueue[qIdx].cmdType == stepperCommand_AccelerateToSpeed) {
state[stepperIdx].c_i = state[stepperIdx].c_i / (1 + state[stepperIdx].constants.c8 * state[stepperIdx].c_i * state[stepperIdx].c_i);
/* c_i to counter value */
if(state[stepperIdx].c_i > 4294967295.0) {
state[stepperIdx].counterCurrent = ~0;
} else if(state[stepperIdx].c_i < 1.0) {
state[stepperIdx].counterCurrent = 1;
} else {
state[stepperIdx].counterCurrent = (uint32_t)state[stepperIdx].c_i;
}
if((state[stepperIdx].cmdQueue[qIdx].data.accelerateDecelerateToConstSpeed.nA = state[stepperIdx].cmdQueue[qIdx].data.accelerateDecelerateToConstSpeed.nA - 1) == 0) {
/* Switch to next state at next interrupt */
state[stepperIdx].cmdQueueTail = (state[stepperIdx].cmdQueueTail + 1) % STEPPER_COMMANDQUEUELENGTH;
state[stepperIdx].c_i = -1; /* This will initialize the next state as soon as it is available */
}
} else if(state[stepperIdx].cmdQueue[qIdx].cmdType == stepperCommand_DecelerateToSpeed) {
state[stepperIdx].c_i = state[stepperIdx].c_i / (1 + state[stepperIdx].constants.c9 * state[stepperIdx].c_i * state[stepperIdx].c_i);
/* c_i to counter value */
if(state[stepperIdx].c_i > 4294967295.0) {
state[stepperIdx].counterCurrent = ~0;
} else if(state[stepperIdx].c_i < 1.0) {
state[stepperIdx].counterCurrent = 1;
} else {
state[stepperIdx].counterCurrent = (uint32_t)state[stepperIdx].c_i;
}
if((state[stepperIdx].cmdQueue[qIdx].data.accelerateDecelerateToConstSpeed.nD = state[stepperIdx].cmdQueue[qIdx].data.accelerateDecelerateToConstSpeed.nD - 1) == 0) {
/* Switch to next state at next interrupt */
state[stepperIdx].cmdQueueTail = (state[stepperIdx].cmdQueueTail + 1) % STEPPER_COMMANDQUEUELENGTH;
state[stepperIdx].c_i = -1; /* This will initialize the next state as soon as it is available */
}
} else if(state[stepperIdx].cmdQueue[qIdx].cmdType == stepperCommand_ConstantSpeed) {
state[stepperIdx].counterCurrent = state[stepperIdx].c_i; // Counter reload ...
}
if(stepperIdx == 0) {
// Pulse PD2 high
PORTD = PORTD | 0x08;
#ifdef ENABLE_ABSOLUTEPOSITION
if((PORTD & 0x04) != 0) {
if(state[0].currentPosition < 2147483647) {
state[0].currentPosition++;
}
} else {
if(state[0].currentPosition > 0) {
state[0].currentPosition--;
}
}
#endif
} else {
// Pulse PC1 high
PORTC = PORTC | 0x02;
#ifdef ENABLE_ABSOLUTEPOSITION
if((PORTC & 0x01) != 0) {
if(state[1].currentPosition < 2147483647) {
state[1].currentPosition++;
}
} else {
if(state[1].currentPosition > 0) {
state[1].currentPosition--;
}
}
#endif
}
}
}
bResetRun = !bResetRun;
}
/*@
assigns bIntTriggered;
ensures bIntTriggered == true;
*/
ISR(TIMER2_COMPA_vect) {
bIntTriggered = true;
}
/*@
requires \valid_read(&PINC);
assigns stateFault;
*/
ISR(PCINT1_vect) {
/*
Update fault pins ...
*/
uint8_t faultPins = PINC;
stateFault = stateFault | (((faultPins >> 2) & 0x01) ^ 0x01) | (((faultPins >> 2) & 0x02) ^ 0x02);
}
/*
c2 Trapezoidal profile n_acceleration = v_d^2 / (2 * a * \alpha)
c4 Trapezoidal profile n_deceleration = v_d^2 / (2 * d * \alpha)
c5 Triangular profile d / (d + a)
c7 Time stepping: Initial time step delay squared (dt_0 * dt_0)
c8 Time stepping constant R_a for acceleration
c9 Time stepping constant R_d for deceleration
c10 Constant speed timestep constant (alpha * f)
*/
/*@
requires \forall integer iStep; 0 <= iStep < STEPPER_COUNT
==> (state[iStep].settings.vmax >= STEPPER_MIN_VMAX) && (state[iStep].settings.vmax <= STEPPER_MAX_VMAX);
requires \forall integer iStep; 0 <= iStep < STEPPER_COUNT
==> (state[iStep].settings.alpha >= STEPPER_MIN_ALPHA) && (state[iStep].settings.alpha <= STEPPER_MAX_ALPHA);
requires \forall integer iStep; 0 <= iStep < STEPPER_COUNT
==> (state[iStep].settings.acceleration >= STEPPER_MIN_ACCELERATION) && (state[iStep].settings.acceleration <= STEPPER_MAX_ACCELERATION);
requires \forall integer iStep; 0 <= iStep < STEPPER_COUNT
==> (state[iStep].settings.deceleration >= STEPPER_MIN_DECELERATION) && (state[iStep].settings.deceleration <= STEPPER_MAX_DECELERATION);
behavior unknownChannel:
assumes (stepperIndex < 0) || (stepperIndex >= STEPPER_COUNT);
assigns \nothing;
behavior knownChannel:
assumes (stepperIndex >= 0) && (stepperIndex < STEPPER_COUNT);
assigns state[stepperIndex].constants.c1,
state[stepperIndex].constants.c2,
state[stepperIndex].constants.c3,
state[stepperIndex].constants.c4,
state[stepperIndex].constants.c5,
state[stepperIndex].constants.c7,
state[stepperIndex].constants.c8,
state[stepperIndex].constants.c9,
state[stepperIndex].constants.c10;
ensures state[stepperIndex].constants.c2 == (state[stepperIndex].settings.vmax * state[stepperIndex].settings.vmax) / (2 * state[stepperIndex].settings.acceleration * state[stepperIndex].settings.alpha);
ensures state[stepperIndex].constants.c4 == -1 * (state[stepperIndex].settings.vmax * state[stepperIndex].settings.vmax) / (2 * state[stepperIndex].settings.deceleration * state[stepperIndex].settings.alpha);
ensures state[stepperIndex].constants.c5 == state[stepperIndex].settings.deceleration / (state[stepperIndex].settings.deceleration - state[stepperIndex].settings.acceleration);
ensures state[stepperIndex].constants.c7 == 2 * state[stepperIndex].settings.alpha / state[stepperIndex].settings.acceleration;
ensures state[stepperIndex].constants.c8 == state[stepperIndex].settings.acceleration / (state[stepperIndex].settings.alpha * (double)STEPPER_TIMERTICK_FRQ * (double)STEPPER_TIMERTICK_FRQ);
ensures state[stepperIndex].constants.c9 == state[stepperIndex].settings.acceleration / (state[stepperIndex].settings.alpha * (double)STEPPER_TIMERTICK_FRQ * (double)STEPPER_TIMERTICK_FRQ);
ensures state[stepperIndex].constants.c10 == state[stepperIndex].settings.alpha * (double)STEPPER_TIMERTICK_FRQ;
disjoint behaviors;
complete behaviors;
*/
static bool updateConstants(int stepperIndex) {
/*
Expensive update of constants for motion planner
*/
if((stepperIndex >= STEPPER_COUNT) || (stepperIndex < 0)) {
return false;
}
state[stepperIndex].constants.c1 = 1.0 / (2.0 * state[stepperIndex].settings.acceleration * state[stepperIndex].settings.alpha);
state[stepperIndex].constants.c2 = state[stepperIndex].settings.vmax * state[stepperIndex].settings.vmax * state[stepperIndex].constants.c1; /*@ assert (state[stepperIndex].constants.c2 < 4294967296) && (state[stepperIndex].constants.c2 >= 0); */
state[stepperIndex].constants.c3 = 1.0 / (2.0 * state[stepperIndex].settings.deceleration * state[stepperIndex].settings.alpha);
state[stepperIndex].constants.c4 = -1.0 * state[stepperIndex].settings.vmax * state[stepperIndex].settings.vmax * state[stepperIndex].constants.c3; /*@ assert (state[stepperIndex].constants.c4 < 4294967296) && (state[stepperIndex].constants.c4 >= 0); */
state[stepperIndex].constants.c5 = state[stepperIndex].settings.deceleration / (state[stepperIndex].settings.deceleration - state[stepperIndex].settings.acceleration);
state[stepperIndex].constants.c7 = 2 * state[stepperIndex].settings.alpha / state[stepperIndex].settings.acceleration;
state[stepperIndex].constants.c8 = state[stepperIndex].settings.acceleration / (state[stepperIndex].settings.alpha * (double)STEPPER_TIMERTICK_FRQ * (double)STEPPER_TIMERTICK_FRQ);
state[stepperIndex].constants.c9 = state[stepperIndex].settings.deceleration / (state[stepperIndex].settings.alpha * (double)STEPPER_TIMERTICK_FRQ * (double)STEPPER_TIMERTICK_FRQ);
state[stepperIndex].constants.c10 = state[stepperIndex].settings.alpha * (double)STEPPER_TIMERTICK_FRQ;
return true;
}
/*@
requires \valid(&PORTB) && \valid(&PORTD);
behavior supportedMicrosteps:
assumes (microsteps == 0) || (microsteps == 2) || (microsteps == 4)
|| (microsteps == 8) || (microsteps == 16) || (microsteps == 32);
assigns PORTB;
assigns PORTD;
assigns stateMicrostepping;
ensures stateMicrostepping == microsteps;
behavior unsupportedMicrosteps:
assumes (microsteps != 0) && (microsteps != 2) && (microsteps != 4)
&& (microsteps != 8) && (microsteps != 16) && (microsteps != 32);
assigns \nothing;
disjoint behaviors;
complete behaviors;
*/
static void stepperSetMicrostepping(uint8_t microsteps) {
/*
pinMode0 8 PB0 OUT
pinMode2 6 PD6 OUT
pinMode1 7 PD7 OUT
M0 M1 M2 Steps
0 0 0 Full steps
1 0 0 1/2
0 1 0 1/4
1 1 0 1/8
0 0 1 1/16
1 0 1 1/32
0 1 1 Undefined (1/64)
1 1 1 Undefined (1/128)
*/
switch(microsteps) {
case 0: stateMicrostepping = 0; PORTB = PORTB & (~(0x01)); PORTD = PORTD & 0x3F; break; /* PB0: 0, PD7: 0, PD6: 0 */
case 2: stateMicrostepping = 2; PORTB = PORTB | 0x01 ; PORTD = PORTD & 0x3F; break; /* PB0: 1, PD7: 0, PD6: 0 */
case 4: stateMicrostepping = 4; PORTB = PORTB & (~(0x01)); PORTD = PORTD & 0x3F; PORTD = PORTD | 0x80; break; /* PB0: 0, PD7: 1, PD6: 0 */
case 8: stateMicrostepping = 8; PORTB = PORTB | 0x01 ; PORTD = PORTD & 0x3F; PORTD = PORTD | 0x80; break; /* PB0: 1, PD7: 1, PD6: 0 */
case 16: stateMicrostepping = 16; PORTB = PORTB & (~(0x01)); PORTD = PORTD & 0x3F; PORTD = PORTD | 0x40; break; /* PB0: 0, PD7: 0, PD6: 1 */
case 32: stateMicrostepping = 32; PORTB = PORTB | 0x01 ; PORTD = PORTD & 0x3F; PORTD = PORTD | 0x40; break; /* PB0: 1, PD7: 0, PD6: 1 */
default: break;
}
}
/*@
requires \valid(&TCCR2B) && \valid(&TCNT2) && \valid(&TCCR2A) && \valid(&OCR2A) && \valid(&TIMSK2);
requires \valid(&PORTB) && \valid(&DDRB);
requires \valid(&PORTC) && \valid(&DDRC) && \valid(&PINC);
requires \valid(&PORTD) && \valid(&DDRD);
requires \valid(&PCICR) && \valid(&PCMSK1);
assigns stateFault;
assigns drvEnableState;
assigns drvRealEnabled;
ensures
\forall integer iStep; 0 <= iStep < STEPPER_COUNT
==> (state[iStep].cmdQueueHead == 0) && (state[iStep].cmdQueueTail == 0) &&
(state[iStep].counterCurrent == 0) && (state[iStep].c_i == -1) &&
(state[iStep].settings.acceleration == STEPPER_INITIAL_ACCELERATION) &&
(state[iStep].settings.deceleration == STEPPER_INITIAL_DECELERATION) &&
(state[iStep].settings.alpha == STEPPER_INITIAL_ALPHA) &&
(state[iStep].settings.vmax == STEPPER_INITIAL_VMAX);
ensures
\forall integer stepperIndex; 0 <= stepperIndex < STEPPER_COUNT
==> (state[stepperIndex].settings.vmax >= STEPPER_MIN_VMAX) && (state[stepperIndex].settings.vmax <= STEPPER_MAX_VMAX)
&& (state[stepperIndex].settings.alpha >= STEPPER_MIN_ALPHA) && (state[stepperIndex].settings.alpha <= STEPPER_MAX_ALPHA)
&& (state[stepperIndex].settings.acceleration >= STEPPER_MIN_ACCELERATION) && (state[stepperIndex].settings.acceleration <= STEPPER_MAX_ACCELERATION)
&& (state[stepperIndex].settings.deceleration >= STEPPER_MIN_DECELERATION) && (state[stepperIndex].settings.deceleration <= STEPPER_MAX_DECELERATION);
*/
static void stepperSetup() {
/*
Stop our timer if it's currently running
*/
#ifndef FRAMAC_SKIP
cli();
#endif
TCCR2B = 0; /* Disable timer if it's currently running */
#ifndef FRAMAC_SKIP
sei();
#endif
/*
Initialize pins with usage as described above
Pull enable HIGH (inputs disabled), RESET and SLEEP low (ENABLED)
Microstepping initally set to full step
*/
DDRB = 0x03;
PORTB = 0x02;
DDRC = 0x03;
PORTC = 0x00;
DDRD = 0xFC;
PORTD = 0x00;
stateMicrostepping = 0;
/* Enable pin change interrupts for FAULT pins */
PCICR = 0x02; /* Set PCIE1 (PCIE0 and PCIE2 are disabled) */
PCMSK1 = 0x0C; /* Mask only for PCINT10, PCINT11 i.e. PC2, PC3) */
/* Perform reset and initialization */
delay(10);
PORTD = PORTD | 0x10; /* Leave sleep state by pulling SLEEP HIGH (disabled) */
delay(2);
PORTB = PORTB & (~0x02); /* Enable inputs by pulling ENABLE LOW (enabled) */
delay(150);
PORTD = PORTD | 0x20; /* Leave reset mode by pulling RESET HIGH (disabled) */
delay(150);
/*
Initialize stepper state machine to zero
*/
int i;
/*@
loop assigns state[0 .. (STEPPER_COUNT-1)];
loop invariant 0 <= i < STEPPER_COUNT;
*/
for(i = 0; i < STEPPER_COUNT; i=i+1) {
state[i].cmdQueueHead = 0;
state[i].cmdQueueTail = 0;
state[i].counterCurrent = 0;
state[i].c_i = -1; /* Will trigger initialization after planning */
#ifdef ENABLE_ABSOLUTEPOSITION
state[i].currentPosition = 0; /* Initialize always at position 0 */
#endif
state[i].settings.acceleration = STEPPER_INITIAL_ACCELERATION;
state[i].settings.deceleration = STEPPER_INITIAL_DECELERATION;
state[i].settings.alpha = STEPPER_INITIAL_ALPHA;
state[i].settings.vmax = STEPPER_INITIAL_VMAX;
updateConstants(i);
}
drvEnableState = 0x03; /* Both steppers enabled after initialization. */
drvRealEnabled = 0x80; /* Both steppers are enabled and we do not need any grace period till we can perform some work ... */
/* Fault status to status register */
{
#ifndef FRAMAC_SKIP
cli();
#endif
uint8_t faultPins = PINC;
stateFault = (((faultPins >> 2) & 0x01) ^ 0x01) | (((faultPins >> 2) & 0x02) ^ 0x02);
#ifndef FRAMAC_SKIP
sei();
#endif
}
/*
Initialize our timer. We use Timer2 for our purposes
*/
TCNT2 = 0; /* Set current timer counter to zero */
TCCR2A = 0x02; /* CTC Mode (count up to OCR2A), disable OCR output pins */
OCR2A = 0x01; /* We count up to one - so trigger every pulse */
TIMSK2 = 0x02; /* Set OCIE2A flag to enable interrupts on output compare */
TCCR2B = STEPPER_TIMERTICK_PRESCALER; /* Select our prescaler, non FOCA, enable timer */
#ifdef STEPPER_DISABLE_STARTUP
drvEnableState = 0x00; /* Steppers will be disabled on startup */
#endif
}
/*@
requires \forall integer iStep; 0 <= iStep < STEPPER_COUNT
==> (state[iStep].settings.vmax >= STEPPER_MIN_VMAX) && (state[iStep].settings.vmax <= STEPPER_MAX_VMAX);
requires \forall integer iStep; 0 <= iStep < STEPPER_COUNT
==> (state[iStep].cmdQueueHead >= 0) && (state[iStep].cmdQueueHead < STEPPER_COMMANDQUEUELENGTH);
requires \forall integer iStep; 0 <= iStep < STEPPER_COUNT
==> (state[iStep].constants.c10 == state[iStep].settings.alpha * STEPPER_TIMERTICK_FRQ);
requires (immediate == true) || (immediate == false);
behavior unknownStepper:
assumes (stepperIndex < 0) || (stepperIndex >= STEPPER_COUNT);
assigns \nothing;
behavior knownStepperImmediate:
assumes (stepperIndex >= 0) && (stepperIndex < STEPPER_COUNT);
assumes immediate == true;
requires (v >= (state[stepperIndex].settings.alpha * STEPPER_TIMERTICK_FRQ)/(4294967295)) && (v <= state[stepperIndex].settings.alpha * STEPPER_TIMERTICK_FRQ);
assigns state[stepperIndex].cmdQueue[\old(state[stepperIndex].cmdQueueHead)].cmdType;
assigns state[stepperIndex].cmdQueue[\old(state[stepperIndex].cmdQueueHead)].data.constantSpeed.cConst;
assigns state[stepperIndex].cmdQueue[\old(state[stepperIndex].cmdQueueHead)].forward;
assigns state[stepperIndex].cmdQueueHead;
assigns state[stepperIndex].cmdQueueTail;
ensures state[stepperIndex].cmdQueueTail == \old(state[stepperIndex].cmdQueueHead);
ensures state[stepperIndex].cmdQueueHead == ((\old(state[stepperIndex].cmdQueueHead) + 1) % STEPPER_COMMANDQUEUELENGTH);
ensures state[stepperIndex].c_i == -1;
behavior knownStepperPlanned:
assumes (stepperIndex >= 0) && (stepperIndex < STEPPER_COUNT);
assumes immediate == false;
requires (v >= (state[stepperIndex].settings.alpha * STEPPER_TIMERTICK_FRQ)/(4294967295)) && (v <= state[stepperIndex].settings.alpha * STEPPER_TIMERTICK_FRQ);
assigns state[stepperIndex].cmdQueue[\old(state[stepperIndex].cmdQueueHead)].cmdType;
assigns state[stepperIndex].cmdQueue[\old(state[stepperIndex].cmdQueueHead)].data.constantSpeed.cConst;
assigns state[stepperIndex].cmdQueue[\old(state[stepperIndex].cmdQueueHead)].forward;
assigns state[stepperIndex].cmdQueueHead;
ensures state[stepperIndex].cmdQueueHead == ((\old(state[stepperIndex].cmdQueueHead) + 1) % STEPPER_COMMANDQUEUELENGTH);
disjoint behaviors;
complete behaviors;
*/
static void stepperPlanMovement_ConstantSpeed(int stepperIndex, double v, int direction, bool immediate) {
if((stepperIndex < 0) || (stepperIndex >= STEPPER_COUNT)) { return; }
/*
Determine at which index we want to plan
*/
const int idx = state[stepperIndex].cmdQueueHead;
state[stepperIndex].cmdQueue[idx].cmdType = stepperCommand_ConstantSpeed;
double vMin = state[stepperIndex].settings.alpha * STEPPER_TIMERTICK_FRQ / (4294967295);
if(v < vMin) { v = vMin; }
else if(v > state[stepperIndex].settings.vmax) { v = state[stepperIndex].settings.vmax; }
double tickCount = state[stepperIndex].constants.c10 / v;
if(tickCount > 4294967295.0) {
/* Clamp to maximum */
state[stepperIndex].cmdQueue[idx].data.constantSpeed.cConst = ~0;
} else if(tickCount < 1.0) {
/* Clamp to minimum */
state[stepperIndex].cmdQueue[idx].data.constantSpeed.cConst = 1;
} else {
state[stepperIndex].cmdQueue[idx].data.constantSpeed.cConst = (uint32_t)(tickCount);
}
state[stepperIndex].cmdQueue[idx].forward = direction;
/*
Make command active
*/
if(immediate) {
state[stepperIndex].cmdQueueTail = state[stepperIndex].cmdQueueHead;
state[stepperIndex].c_i = -1;
state[stepperIndex].cmdQueueHead = (state[stepperIndex].cmdQueueHead + 1) % STEPPER_COMMANDQUEUELENGTH;
} else {
state[stepperIndex].cmdQueueHead = (state[stepperIndex].cmdQueueHead + 1) % STEPPER_COMMANDQUEUELENGTH;
}
return;
}
/*@
requires \forall integer iStep; 0 <= iStep < STEPPER_COUNT
==> (state[iStep].cmdQueueHead >= 0) && (state[iStep].cmdQueueHead < STEPPER_COMMANDQUEUELENGTH);
requires \forall integer iStep; 0 <= iStep < STEPPER_COUNT
==> state[iStep].constants.c2 == (state[iStep].settings.vmax * state[iStep].settings.vmax) / (2 * state[iStep].settings.acceleration * state[iStep].settings.alpha);
requires \forall integer iStep; 0 <= iStep < STEPPER_COUNT
==> state[iStep].constants.c4 == -1 * (state[iStep].settings.vmax * state[iStep].settings.vmax) / (2 * state[iStep].settings.deceleration * state[iStep].settings.alpha);
requires \forall integer iStep; 0 <= iStep < STEPPER_COUNT
==> state[iStep].constants.c5 == state[iStep].settings.deceleration / (state[iStep].settings.deceleration - state[iStep].settings.acceleration);
requires \forall integer iStep; 0 <= iStep < STEPPER_COUNT
==> state[iStep].constants.c7 == 2 * state[iStep].settings.alpha / state[iStep].settings.acceleration;
requires \forall integer iStep; 0 <= iStep < STEPPER_COUNT
==> state[iStep].constants.c8 == state[iStep].settings.acceleration / (state[iStep].settings.alpha * (double)STEPPER_TIMERTICK_FRQ * (double)STEPPER_TIMERTICK_FRQ);
requires \forall integer iStep; 0 <= iStep < STEPPER_COUNT
==> state[iStep].constants.c9 == state[iStep].settings.acceleration / (state[iStep].settings.alpha * (double)STEPPER_TIMERTICK_FRQ * (double)STEPPER_TIMERTICK_FRQ);
requires \forall integer iStep; 0 <= iStep < STEPPER_COUNT
==> state[iStep].constants.c10 == state[iStep].settings.alpha * (double)STEPPER_TIMERTICK_FRQ;
requires \forall integer iStep; 0 <= iStep < STEPPER_COUNT
==> (state[iStep].settings.vmax >= STEPPER_MIN_VMAX) && (state[iStep].settings.vmax <= STEPPER_MAX_VMAX);
requires \forall integer iStep; 0 <= iStep < STEPPER_COUNT
==> (state[iStep].settings.alpha >= STEPPER_MIN_ALPHA) && (state[iStep].settings.alpha <= STEPPER_MAX_ALPHA);
requires \forall integer iStep; 0 <= iStep < STEPPER_COUNT
==> (state[iStep].settings.acceleration >= STEPPER_MIN_ACCELERATION) && (state[iStep].settings.acceleration <= STEPPER_MAX_ACCELERATION);
requires \forall integer iStep; 0 <= iStep < STEPPER_COUNT
==> (state[iStep].settings.deceleration >= STEPPER_MIN_DECELERATION) && (state[iStep].settings.deceleration <= STEPPER_MAX_DECELERATION);
requires (sTotal >= STEPPER_MIN_RELATIVEDISTANCE);
requires (sTotal <= STEPPER_MAX_RELATIVEDISTANCE);
behavior unknownStepper:
assumes (stepperIndex < 0) || (stepperIndex >= STEPPER_COUNT);
assigns \nothing;
behavior knownStepperImmediate:
assumes (stepperIndex >= 0) && (stepperIndex < STEPPER_COUNT);
assumes immediate != 0;
assigns state[stepperIndex].cmdQueue[\old(state[stepperIndex].cmdQueueHead)].cmdType;
assigns state[stepperIndex].cmdQueue[\old(state[stepperIndex].cmdQueueHead)].data.acceleratedStopToStop.nA;
assigns state[stepperIndex].cmdQueue[\old(state[stepperIndex].cmdQueueHead)].data.acceleratedStopToStop.nC;
assigns state[stepperIndex].cmdQueue[\old(state[stepperIndex].cmdQueueHead)].data.acceleratedStopToStop.nD;
assigns state[stepperIndex].cmdQueue[\old(state[stepperIndex].cmdQueueHead)].data.acceleratedStopToStop.c7;
assigns state[stepperIndex].cmdQueue[\old(state[stepperIndex].cmdQueueHead)].data.acceleratedStopToStop.c8;
assigns state[stepperIndex].cmdQueue[\old(state[stepperIndex].cmdQueueHead)].data.acceleratedStopToStop.c9;
assigns state[stepperIndex].cmdQueue[\old(state[stepperIndex].cmdQueueHead)].data.acceleratedStopToStop.initialDelayTicks;
assigns state[stepperIndex].cmdQueue[\old(state[stepperIndex].cmdQueueHead)].forward;
assigns state[stepperIndex].cmdQueueTail, state[stepperIndex].cmdQueueHead;
ensures state[stepperIndex].cmdQueueTail == \old(state[stepperIndex].cmdQueueHead);
ensures state[stepperIndex].cmdQueueHead == ((\old(state[stepperIndex].cmdQueueHead) + 1) % STEPPER_COMMANDQUEUELENGTH);
ensures state[stepperIndex].c_i == -1;
behavior knownStepperPlanned:
assumes (stepperIndex >= 0) && (stepperIndex < STEPPER_COUNT);
assumes immediate == 0;
assigns state[stepperIndex].cmdQueue[\old(state[stepperIndex].cmdQueueHead)].cmdType;
assigns state[stepperIndex].cmdQueue[\old(state[stepperIndex].cmdQueueHead)].data.acceleratedStopToStop.nA;
assigns state[stepperIndex].cmdQueue[\old(state[stepperIndex].cmdQueueHead)].data.acceleratedStopToStop.nC;
assigns state[stepperIndex].cmdQueue[\old(state[stepperIndex].cmdQueueHead)].data.acceleratedStopToStop.nD;
assigns state[stepperIndex].cmdQueue[\old(state[stepperIndex].cmdQueueHead)].data.acceleratedStopToStop.c7;
assigns state[stepperIndex].cmdQueue[\old(state[stepperIndex].cmdQueueHead)].data.acceleratedStopToStop.c8;
assigns state[stepperIndex].cmdQueue[\old(state[stepperIndex].cmdQueueHead)].data.acceleratedStopToStop.c9;
assigns state[stepperIndex].cmdQueue[\old(state[stepperIndex].cmdQueueHead)].data.acceleratedStopToStop.initialDelayTicks;
assigns state[stepperIndex].cmdQueue[\old(state[stepperIndex].cmdQueueHead)].forward;
assigns state[stepperIndex].cmdQueueHead;
ensures state[stepperIndex].cmdQueueHead == ((\old(state[stepperIndex].cmdQueueHead) + 1) % STEPPER_COMMANDQUEUELENGTH);
disjoint behaviors;
*/
static void stepperPlanMovement_AccelerateStopToStop(int stepperIndex, double sTotal, int direction, bool immediate) {
if((stepperIndex < 0) || (stepperIndex > 1)) { return; }
/*@ assert (stepperIndex >= 0) && (stepperIndex < STEPPER_COUNT); */
/*@ assert (state[stepperIndex].settings.alpha >= STEPPER_MIN_ALPHA) && (state[stepperIndex].settings.alpha <= STEPPER_MAX_ALPHA); */
if((sTotal < STEPPER_MIN_RELATIVEDISTANCE) || (sTotal > STEPPER_MAX_RELATIVEDISTANCE)) { return; }
/*@ assert sTotal >= STEPPER_MIN_RELATIVEDISTANCE; */
/*@ assert sTotal <= STEPPER_MAX_RELATIVEDISTANCE; */
const int idx = state[stepperIndex].cmdQueueHead;
state[stepperIndex].cmdQueue[idx].cmdType = stepperCommand_AccelerateStopToStop;
double nTotal = sTotal / state[stepperIndex].settings.alpha;
state[stepperIndex].cmdQueue[idx].data.acceleratedStopToStop.nA = state[stepperIndex].constants.c2;
/*@ assert state[stepperIndex].cmdQueue[idx].data.acceleratedStopToStop.nA < (STEPPER_MAX_VMAX*STEPPER_MAX_VMAX)/(STEPPER_MIN_ACCELERATION*STEPPER_MIN_ALPHA); */
/*@ assert state[stepperIndex].cmdQueue[idx].data.acceleratedStopToStop.nA >= (STEPPER_MIN_VMAX*STEPPER_MIN_VMAX)/(STEPPER_MAX_ACCELERATION*STEPPER_MAX_ALPHA); */
state[stepperIndex].cmdQueue[idx].data.acceleratedStopToStop.nD = state[stepperIndex].constants.c4;
/*@ assert state[stepperIndex].cmdQueue[idx].data.acceleratedStopToStop.nD < -1*(STEPPER_MAX_VMAX*STEPPER_MAX_VMAX)/(STEPPER_MIN_DECELERATION*STEPPER_MIN_ALPHA); */
/*@ assert state[stepperIndex].cmdQueue[idx].data.acceleratedStopToStop.nD >= -1*(STEPPER_MIN_VMAX*STEPPER_MIN_VMAX)/(STEPPER_MAX_DECELERATION*STEPPER_MAX_ALPHA); */
double nTP = state[stepperIndex].cmdQueue[idx].data.acceleratedStopToStop.nA + state[stepperIndex].cmdQueue[idx].data.acceleratedStopToStop.nD;
/*@ assert nTP >= 0; */
/*@ assert nTP <= 4294967295; */
/*
Check if we have a constant velocity part (nTP < nTotal) or not (nTP >= nTotal)
*/
if(nTP < nTotal) {
state[stepperIndex].cmdQueue[idx].data.acceleratedStopToStop.nC = ((uint32_t)nTotal) - nTP;
} else {
state[stepperIndex].cmdQueue[idx].data.acceleratedStopToStop.nA = nTotal * state[stepperIndex].constants.c5;
state[stepperIndex].cmdQueue[idx].data.acceleratedStopToStop.nD = nTotal - state[stepperIndex].cmdQueue[idx].data.acceleratedStopToStop.nA;
state[stepperIndex].cmdQueue[idx].data.acceleratedStopToStop.nC = 0;
}
state[stepperIndex].cmdQueue[idx].data.acceleratedStopToStop.c7 = state[stepperIndex].constants.c7;
state[stepperIndex].cmdQueue[idx].data.acceleratedStopToStop.c8 = state[stepperIndex].constants.c8;
state[stepperIndex].cmdQueue[idx].data.acceleratedStopToStop.c9 = state[stepperIndex].constants.c9;
state[stepperIndex].cmdQueue[idx].data.acceleratedStopToStop.initialDelayTicks = sqrt(state[stepperIndex].constants.c7) * (double)STEPPER_TIMERTICK_FRQ;
state[stepperIndex].cmdQueue[idx].forward = direction;
/*
Make command active
*/
if(immediate) {
state[stepperIndex].cmdQueueTail = state[stepperIndex].cmdQueueHead;
state[stepperIndex].c_i = -1;
state[stepperIndex].cmdQueueHead = (state[stepperIndex].cmdQueueHead + 1) % STEPPER_COMMANDQUEUELENGTH;
} else {
state[stepperIndex].cmdQueueHead = (state[stepperIndex].cmdQueueHead + 1) % STEPPER_COMMANDQUEUELENGTH;
}
return;
}
#ifdef ENABLE_ABSOLUTEPOSITION
/*@
requires \forall integer iStep; 0 <= iStep < STEPPER_COUNT
==> (state[iStep].cmdQueueHead >= 0) && (state[iStep].cmdQueueHead < STEPPER_COMMANDQUEUELENGTH);
requires \forall integer iStep; 0 <= iStep < STEPPER_COUNT
==> state[iStep].constants.c2 == (state[iStep].settings.vmax * state[iStep].settings.vmax) / (2 * state[iStep].settings.acceleration * state[iStep].settings.alpha);
requires \forall integer iStep; 0 <= iStep < STEPPER_COUNT
==> state[iStep].constants.c4 == -1 * (state[iStep].settings.vmax * state[iStep].settings.vmax) / (2 * state[iStep].settings.deceleration * state[iStep].settings.alpha);
requires \forall integer iStep; 0 <= iStep < STEPPER_COUNT
==> state[iStep].constants.c5 == state[iStep].settings.deceleration / (state[iStep].settings.deceleration - state[iStep].settings.acceleration);
requires \forall integer iStep; 0 <= iStep < STEPPER_COUNT
==> state[iStep].constants.c7 == 2 * state[iStep].settings.alpha / state[iStep].settings.acceleration;
requires \forall integer iStep; 0 <= iStep < STEPPER_COUNT
==> state[iStep].constants.c8 == state[iStep].settings.acceleration / (state[iStep].settings.alpha * (double)STEPPER_TIMERTICK_FRQ * (double)STEPPER_TIMERTICK_FRQ);
requires \forall integer iStep; 0 <= iStep < STEPPER_COUNT
==> state[iStep].constants.c9 == state[iStep].settings.acceleration / (state[iStep].settings.alpha * (double)STEPPER_TIMERTICK_FRQ * (double)STEPPER_TIMERTICK_FRQ);
requires \forall integer iStep; 0 <= iStep < STEPPER_COUNT
==> state[iStep].constants.c10 == state[iStep].settings.alpha * (double)STEPPER_TIMERTICK_FRQ;
requires \forall integer iStep; 0 <= iStep < STEPPER_COUNT
==> (state[iStep].settings.vmax >= STEPPER_MIN_VMAX) && (state[iStep].settings.vmax <= STEPPER_MAX_VMAX);
requires \forall integer iStep; 0 <= iStep < STEPPER_COUNT
==> (state[iStep].settings.alpha >= STEPPER_MIN_ALPHA) && (state[iStep].settings.alpha <= STEPPER_MAX_ALPHA);
requires \forall integer iStep; 0 <= iStep < STEPPER_COUNT
==> (state[iStep].settings.acceleration >= STEPPER_MIN_ACCELERATION) && (state[iStep].settings.acceleration <= STEPPER_MAX_ACCELERATION);
requires \forall integer iStep; 0 <= iStep < STEPPER_COUNT
==> (state[iStep].settings.deceleration >= STEPPER_MIN_DECELERATION) && (state[iStep].settings.deceleration <= STEPPER_MAX_DECELERATION);
behavior unknownStepper:
assumes (stepperIndex < 0) || (stepperIndex >= STEPPER_COUNT);
assigns \nothing;
behavior knownStepperImmediate:
assumes (stepperIndex >= 0) && (stepperIndex < STEPPER_COUNT);
assumes immediate != 0;
requires (state[stepperIndex].cmdQueueHead >= 0) && (state[stepperIndex].cmdQueueHead < STEPPER_COMMANDQUEUELENGTH);
requires (state[stepperIndex].settings.vmax >= STEPPER_MIN_VMAX) && (state[stepperIndex].settings.vmax <= STEPPER_MAX_VMAX);
requires (state[stepperIndex].settings.alpha >= STEPPER_MIN_ALPHA) && (state[stepperIndex].settings.alpha <= STEPPER_MAX_ALPHA);