-
Notifications
You must be signed in to change notification settings - Fork 5
/
StepAndDirection.h
1028 lines (946 loc) · 32.6 KB
/
StepAndDirection.h
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
#ifndef STEPANDDIRECTION_H
#define STEPANDDIRECTION_H
#include <WProgram.h>
#include "Core.h"
#include "TokenParser.h"
#include "CircleBuffer.h"
#include "Vector.h"
#define fast_io
//#define debug_sigmoid
#define array_base_type 0
typedef bool us1; // for some reason typedef for bool or boolean do not work
class StepConfig {
friend class StepAndDirection;
public:
StepConfig() {
currentPosition = 0;
conversion_mx = 1;
conversion_b = 0;
conversion_p = 0;
limit_enabled = false;
}
s32 getCurrentPosition() {
s32 currentPosition_temp;
noInterrupts();
currentPosition_temp = currentPosition;
interrupts();
return currentPosition_temp;
}
void setCurrentPosition(s32 position) {
noInterrupts();
currentPosition = position;
interrupts();
}
/// Set the destination position (in absolute steps)
void setDestinationPosition(s32 position) {
destinationPosition = position;
}
/// Update the destination position (in steps relative to current destinationPosition)
void updateDestinationPosition(s32 position) {
destinationPosition = destinationPosition + position;
}
/// Get the destination position (in absolute steps)
s32 getDestinationPosition() {
return destinationPosition;
}
void setConversion(Variant mx, Variant b = 0, us8 precision = 0) {
if(mx != 0) {
conversion_mx = mx;
conversion_b = b;
conversion_p = pow(10, precision);
}
#ifdef debug_stp
Serial.print("setConversion m=");
Serial.print(conversion_mx.toString());
Serial.print(", b=");
Serial.print(conversion_b.toString());
Serial.print(", p=");
Serial.println(conversion_p.toString());
#endif
}
void setOffset(Variant b) {
conversion_b = b;
}
void setLimits(Variant min, Variant max) {
if(min != 0 && max != 0) {
limit_min = min;
limit_max = max;
limit_enabled = true;
}
else {
limit_max = Variant((s32)0x7fffffff, 0);
limit_min = Variant((s32)0x80000000, 0);
limit_enabled = false;
}
}
Variant unitConversion(Variant units, bool *ok = 0) {
if(limit_enabled) {
// if(units < limit_min || units > limit_max) {
double temp = units.toDouble();
if(temp < limit_min.toDouble() || temp > limit_max.toDouble()) {
if(ok != 0) {
*ok = false;
}
return 0;
}
}
if(ok != 0) {
*ok = true;
}
// Serial.println("In:" + units.toString());
// units *= conversion_p;
units *= conversion_mx;
units += conversion_b;
return units;
}
private:
volatile s32 currentPosition; ///< current step position (updated in the interrupt)
s32 destinationPosition; ///< current step position (updated in with a move or moveto command)
// conversion variables
Variant conversion_mx;
Variant conversion_b;
Variant conversion_p;
Variant speed_max;
bool limit_enabled;
Variant limit_min;
Variant limit_max;
};
// todo: control direction polarity when creating vectors
class StepAndDirection {
////////////////////////////////////////////////
// BigEasy Kard Info
////////////////////////////////////////////////
// GPIO | REV C | REV C1 | REV D |
// -------------------------------------
// 0 | Step | Step | Step |
// 1 | Dir | Dir | Dir |
// 2 | !Enable | !Enable | !Enable |
// 3 | Sleep | Sleep | MS1 |
// 4 | MS3 | MS2 | MS2 |
public:
StepAndDirection(us8 motor, us8 pin_step, us8 pin_direction, us8 pin_enable, us8 pin_sleep_ms1, us8 pin_ms3_ms2, Variant timebase, char *command_prefix = "stp0", char *kard_rev = "C" ) {
StepAndDirection::motor = motor;
StepAndDirection::pin_step = pin_step;
StepAndDirection::pin_direction = pin_direction;
StepAndDirection::pin_enable = pin_enable;
StepAndDirection::pin_sleep_ms1 = pin_sleep_ms1;
StepAndDirection::pin_ms3_ms2 = pin_ms3_ms2;
StepAndDirection::kard_rev = kard_rev;
StepAndDirection::command_prefix = command_prefix;
StepAndDirection::timebase = timebase;
pinMode(pin_step, OUTPUT);
pinMode(pin_direction, OUTPUT);
pinMode(pin_enable, OUTPUT);
pinMode(pin_sleep_ms1, OUTPUT);
pinMode(pin_ms3_ms2, OUTPUT);
setEnabled(false); // true = enabled, false = disabled
setMicrostepsPerStep(4); // Initial step mode
#ifdef fast_io
stepPort = (p32_ioport *)portRegisters(digitalPinToPort(pin_step));
stepBit = digitalPinToBitMask(pin_step);
directionPort = (p32_ioport *)portRegisters(digitalPinToPort(pin_direction));
directionBit = digitalPinToBitMask(pin_direction);
homeSensorPort = 0;
homeSensorPolarity = false;
homeSensorPersistent = false;
#endif
defaultConfig = new StepConfig();
config = defaultConfig;
interruptPeriod = setTimeBase(Variant(250, -6));
currentSkip = 0;
halt();
// sigmoid defaults
setSigmoid(Variant(1, 3), Variant(1, 4), Variant(25, 1), 3.0);
overhead = Variant(20, -6);
}
StepAndDirection(us8 motor, us8 card, Variant timebase, char *command_prefix = "stp?", char *kard_rev = "C") {
*this = StepAndDirection(motor, KardIO[card][0], KardIO[card][1], KardIO[card][2], KardIO[card][3], KardIO[card][4], timebase, command_prefix, kard_rev);
}
/**
* void setMicrostepsPerStep(us8 microsteps)
*
*************************************************
* REV C/C1 Step Truth Table
* REV C: MS1, MS2 pulled high (MS3 Controlled)
* REV C1: MS1, MS3 pulled low (MS2 Controlled)
*************************************************
* MS1 MS2 MS3 Step Mode Excitation
* L L L Full 2 Phase
* H L L Half 1-2 Phase
* L H L Quarter W1-2 Phase
* H H L Eighth 2W1-2 Phase
* H H H Sixteenth 4W1-2 Phase
*************************************************
*
*************************************************
* REV D Step Truth Table
*************************************************
* MS1 MS2 Step Mode Excitation
* L L Full 2 Phase
* H L Half 1-2 Phase
* L H Quarter W1-2 Phase
* H H Sixteenth 4W1-2 Phase
*************************************************/
bool setMicrostepsPerStep(us8 microsteps) {
bool sucessful = true;
if( strncmp(kard_rev, "C", 1) == 0) {
switch(microsteps) {
case 1: // REV C1
case 8: // REV C
digitalWrite(pin_ms3_ms2, LOW);
break;
case 4: // REV C1
case 16: // REV C
digitalWrite(pin_ms3_ms2, HIGH);
break;
default:
sucessful = false;
break;
}
}
else if( strcmp(kard_rev, "D") == 0) {
switch(microsteps) {
case 1:
digitalWrite(pin_sleep_ms1, LOW);
digitalWrite(pin_ms3_ms2, LOW);
break;
case 2:
digitalWrite(pin_sleep_ms1, HIGH);
digitalWrite(pin_ms3_ms2, LOW);
break;
case 4:
digitalWrite(pin_sleep_ms1, LOW);
digitalWrite(pin_ms3_ms2, HIGH);
break;
case 16:
digitalWrite(pin_sleep_ms1, HIGH);
digitalWrite(pin_ms3_ms2, HIGH);
break;
default:
sucessful = false;
break;
}
}
if( sucessful ) this->microsteps = microsteps;
return sucessful;
}
// todo: verify skip starts at 1
inline void sharedInterrupt() {
#ifdef fast_io
if ( stepPort->port.reg & stepBit ) {
stepPort->lat.clr = stepBit;
//return;
}
#else
if( digitalRead(pin_step, HIGH) {
digitalWrite(pin_step, LOW);
//return;
}
#endif
if(vector.steps == 0 && currentSkip <= 0 && running) {
if(!buffer.isEmpty()) {
vector = buffer.pop();
if(vector.steps > 0) {
#ifdef fast_io
directionPort->lat.set = directionBit;
#else
digitalWrite(pin_direction, HIGH);
#endif
}
else {
#ifdef fast_io
directionPort->lat.clr = directionBit;
#else
digitalWrite(pin_direction, LOW);
#endif
}
}
else {
running = false;
}
}
if(vector.steps != 0 || currentSkip > 0) {
if(currentSkip > 0) {
currentSkip--;
}
else {
currentSkip = vector.time; // (vector.time / timebase).toInt();
step();
}
}
}
void unsharedInterrupt() {
if(vector.steps == 0 && running) {
if(!buffer.isEmpty()) {
vector = buffer.pop();
bool ok;
uint32_t temp = vector.time; //setTimeBase(vector.time, &ok);
if(ok) {
interruptPeriod = temp;
if(vector.steps > 0) {
#ifdef fast_io
directionPort->lat.set = directionBit;
#else
digitalWrite(pin_direction, HIGH);
#endif
}
else {
#ifdef fast_io
directionPort->lat.clr = directionBit;
#else
digitalWrite(pin_direction, LOW);
#endif
}
}
else {
Serial.println("Timebase Error");
Serial.println(vector.steps);
Serial.println(vector.time,DEC);
vector.steps = 0;
vector.time = 0; // Variant();
running = false;
}
return;
}
else {
running = false;
}
}
if(vector.steps != 0 && running) {
step();
}
}
uint32_t setTimeBase(Variant milliseconds, bool *ok = 0) {
if(milliseconds >= Variant(5, -6) && milliseconds <= Variant(1, 0)) {
Variant var(1, 6);
var *= milliseconds;
if(ok) {
*ok = true;
}
return (uint32_t)(CORE_TICK_RATE / 1000 * var.toInt());
}
if(ok) {
*ok = false;
}
return 0;
}
// stp0 test 5e3 30e3 300 3 3200
// stp0 test 20e3 40e3 1000 4 32000 // 10 rps w/16x
// stp0 test 18e3 28e3 1000 3.5 32000 // 17.5 rps w/8x
void modifiedSigmoid(Variant beginFreq, Variant endFreq, Variant accelSteps, float coefficient, s32 steps) {
int points = 10;
Variant beginPeriod(1, 0);
beginPeriod /= beginFreq;
Variant endPeriod(1, 0);
endPeriod /= endFreq;
if(steps < 0) {
accelSteps *= Variant(-1, 0);
}
#ifdef debug_sigmoid
Serial.println("beginPeriod: " + beginPeriod.toString() + " sec");
Serial.println("endPeriod : " + endPeriod.toString() + " sec");
Serial.println("accelSteps : " + accelSteps.toString() + "");
Serial.println("coefficient: " + Variant(coefficient).toString() + "");
Serial.println("steps : " + String(steps, DEC) + " steps");
#endif
// =(1 / (1 + (coefficient ^ (-point + 5))))
Vector vectors[points + 1];
Variant prev;
for(int i = 0; i <= points; i++) {
int exp = -i + 5;
Variant base = Variant((float)(1 + pow(coefficient, exp)));
Variant value(1, 0);
value /= base;
#ifdef debug_sigmoid
Serial.print(i);
Serial.print(": ");
Serial.print(base.toString());
Serial.print("/");
Serial.print(value.toString());
Serial.print(" - ");
#endif
if(i == 0) {
vectors[i].steps = 0;
vectors[i].time = 0;
}
else {
vectors[i].steps = ((value - prev) * accelSteps).toInt();
//vectors[i].time = (((endPeriod * value) + (beginPeriod * (Variant(1, 0) - value))) / timebase).toInt();
vectors[i].time = ((Variant(1,0) / ((endFreq-beginFreq)*value + beginFreq))/ timebase).toInt();
}
#ifdef debug_sigmoid
Serial.print(vectors[i].steps);
Serial.print(", ");
Serial.print(vectors[i].time,DEC);
Serial.println(" ");
#endif
prev = value;
}
int totalSteps = 0;
for(int i = 1; i <= points; i++) {
totalSteps += vectors[i].steps;
buffer.push(vectors[i]);
}
Vector flatVector(steps - (totalSteps * 2), (endPeriod/timebase).toInt()); // vectors[10].time); //endPeriod);
#ifdef debug_sigmoid
Serial.print(flatVector.steps);
Serial.print(", ");
Serial.println(flatVector.time, DEC);
#endif
buffer.push(flatVector);
for(int i = points; i > 0; i--) {
buffer.push(vectors[i]);
}
#ifdef debug_sigmoid
Serial.print("TotalSteps: ");
Serial.println(totalSteps);
Serial.println(vectors[points].time,DEC);
#endif
}
void start() {
vector.steps = 0;
vector.time = 0; //Variant();
running = true;
}
void pause() {
running = false;
}
/// Halt, wait for current vector to finish (could be a long long time).
inline void halt() {
running = false;
buffer.clear();
}
/// Halt immediately, do not wait for current vector to finish.
inline void halt_immediatly() {
noInterrupts();
halt();
vector.steps = 0;
config->setDestinationPosition(config->getCurrentPosition());
interrupts();
}
bool getHomeSensorStatus() {
return (homeSensorPort == 0) ? true : false;
}
void setHomeSensorPersistent(int pin, bool desiredState = false, bool desiredPersistentDirection = false) {
setHomeSensor(pin, desiredState);
homeSensorPersistent = true;
homeSensorPersistentDirection = desiredPersistentDirection;
}
void setHomeSensor(int pin, bool desiredState = false) {
homeSensorPersistent = false;
if(pin == 0) {
homeSensorPort = (p32_ioport *)0;
homeSensorBit = 0;
}
else {
homeSensorPort = (p32_ioport *)portRegisters(digitalPinToPort(pin));
homeSensorBit = digitalPinToBitMask(pin);
previousHomeState = (bool)(homeSensorPort->port.reg & homeSensorBit);
}
homeSensorPolarity = desiredState;
// Serial.println((us32)homeSensorPort, DEC);
// Serial.println((us32)homeSensorBit , DEC);
}
void chooseBestMove(s32 steps) {
#ifdef debug_stp
Serial.print("chooseBestMove ");
Serial.print(String(steps,DEC));
#endif
if(steps == 0) {
#ifdef debug_stp
Serial.println(" steps");
#endif
return;
}
config->updateDestinationPosition(steps);
#ifdef debug_stp
Serial.print(" -> ");
Serial.print(fabs((double)steps), DEC);
Serial.print(" >= ");
Serial.print((sigSteps.toInt() * 2.5), DEC);
Serial.print(" is ");
#endif
if(fabs((double)steps) >= (sigSteps.toInt() * 2.5)) {
#ifdef debug_stp
Serial.println("true");
#endif
modifiedSigmoid(sigLow, sigHigh, sigSteps, sigCoefficient.toFloat(), steps);
}
else {
#ifdef debug_stp
Serial.println("false");
#endif
Variant period(1, 0);
period /= sigLow;
buffer.push(Vector(steps, (period/timebase).toInt()));
}
start();
}
/// relative move (wrapper)
bool move(Variant units) {
return moveAbsRel(units, false);
}
/// absolute move (wrapper)
bool moveTo(Variant units) {
return moveAbsRel(units, true);
}
/// combined absolute and relative move function
bool moveAbsRel(Variant units, bool absolute) {
bool ok;
#ifdef debug_stp
Serial.print("moveAbsRel ");
Serial.print(units.toString());
#endif
units = config->unitConversion(units, &ok);
#ifdef debug_stp
Serial.print("->");
Serial.print(units.toString());
#endif
if(!running && ok) {
if(absolute) {
units -= config->getCurrentPosition();
#ifdef debug_stp
Serial.print("->");
Serial.print(units.toString());
#endif
}
else {
#ifdef debug_stp
Serial.print("->rel");
#endif
}
#ifdef debug_stp
Serial.println(" steps");
#endif
// Don't move if we have persistent home sensors and we are sitting on the sensor
if(homeSensorPersistent) {
if( readHomeState() == homeSensorPolarity ) {
#ifdef debug_stp
Serial.println("Sitting on home sensor.");
#endif
if( homeSensorPersistentDirection == (units > 0) ) return false;
}
}
chooseBestMove(units.toInt());
return true;
}
else {
#ifdef debug_stp
Serial.println("no move");
#endif
return false;
}
}
void moveFreq(Variant units, Variant frequency) {
bool ok;
#ifdef debug_stp
Serial.print("moveFreq units=");
Serial.print(units.toString());
#endif
units = config->unitConversion(units, &ok);
if(units.toInt() == 0) {
return;
}
config->updateDestinationPosition(units.toInt());
Variant period(1, 0);
period /= frequency;
#ifdef debug_stp
Serial.print(", steps=");
Serial.print(units.toString());
Serial.print(", frequency=");
Serial.print(frequency.toString());
Serial.print(", period=");
Serial.print(period.toString());
Serial.print(", time=");
Serial.print(timebase.toString());
Serial.print(", skip=");
Serial.println((period / timebase).toString());
#endif
buffer.push(Vector(units.toInt(), (period / timebase).toInt()));
start();
}
StepConfig* getDefaultConfig() {
return defaultConfig;
}
void setConfig(StepConfig *temp) {
if(temp == 0) {
config = defaultConfig;
}
else {
config = temp;
}
}
s32 getCurrentPosition() {
if( config == 0 ) return 0;
return config->getCurrentPosition();
}
s32 getDestinationPosition() {
if( config == 0 ) return 0;
return config->getDestinationPosition();
}
s32 getDeltaPosition() {
if( config == 0 ) return 0;
s32 temp = config->getDestinationPosition() - config->getCurrentPosition();
if( temp == 0 )
while(running); // Wait for the interrupt to fire one last time before returning zero
return temp;
}
void setCurrentPosition(s32 position) {
if( config == 0 ) return;
config->setCurrentPosition(position);
}
void setDestinationPosition(s32 position) {
if( config == 0 ) return;
config->setDestinationPosition(position);
}
void setConversion(Variant mx, Variant b = 0, us8 precision = 0) {
if( config == 0 ) return;
config->setConversion(mx, b, precision);
}
void setLimits(Variant min, Variant max) {
if( config == 0 ) return;
config->setLimits(min, max);
}
Variant unitConversion(Variant units, bool *ok = 0) {
if( config == 0 ) return units;
return config->unitConversion(units, ok);
}
bool isBusy() {
return running;
}
bool isReady() {
return !running;
}
/// Assert or de-assert the enable line for the stepper driver
bool getEnabled() {
return !digitalRead(pin_enable);
}
void setEnabled(bool enabled) {
if( strncmp(kard_rev, "C", 1) == 0) { // REV C/C1
digitalWrite(pin_enable, !enabled); // 0 = enabled
digitalWrite(pin_sleep_ms1, enabled); // 1 = enabled
}
else if( strcmp(kard_rev, "D") == 0) {
digitalWrite(pin_enable, !enabled); // 0 = enabled
}
}
void setSigmoid(Variant begin, Variant end, Variant accelSteps, Variant coefficient) {
sigLow = begin;
sigHigh = end;
sigSteps = accelSteps;
sigCoefficient = coefficient;
}
/**
* Parse commands received from the TokenParser
*/
void command(TokenParser &parser) {
#ifdef debug_stp
parser.print("stepper parser command:\"");
parser.print(parser.toString());
parser.print("\" \"");
parser.print(command_prefix);
parser.println("\"");
#endif
if(parser.startsWith(command_prefix)) {
parser.save();
parser.advanceTail(strlen(command_prefix) - 1);
if(motor != parser.toVariant().toInt()) {
parser.restore();
return;
}
parser.nextToken();
#ifdef debug_stp
parser.print("motor command:\"");
parser.print(parser.toString());
parser.println("\"");
#endif
/// List of support commands
if(parser.compare("pairs")) { /// pairs
for(int i = 0; i < buffer.size; i++) {
if(!parser.nextToken()) {
break;
}
String token = parser.toString();
parser.println(String(i, DEC) + ": " + token);
int index = token.indexOf(",");
Vector temp;
temp.steps = token.substring(0, index).toInt();
/// temp.time needs testing here
temp.time = (Variant::fromString(token.substring(++index)) / timebase).toInt();
buffer.push(temp);
}
parser.println("OK Pairs");
start();
}
else if(parser.compare("enable")) { /// enable true|false (enable the stepper driver)
parser.nextToken();
setEnabled(parser.toVariant().toBool());
}
else if(parser.compare("base")) { /// base n
parser.nextToken();
setTimeBase(parser.toVariant());
}
else if(parser.compare("test")) { /// test n n n n n (generate a modifiedSigmoid)
parser.nextToken();
Variant begin = parser.toVariant();
parser.nextToken();
Variant end = parser.toVariant();
parser.nextToken();
Variant accelSteps = parser.toVariant();
parser.nextToken();
Variant c = parser.toVariant();
parser.nextToken();
Variant steps = parser.toVariant();
modifiedSigmoid(begin, end, accelSteps, c.toFloat(), steps.toInt());
start();
}
else if(parser.compare("scp")) { /// scp (set current position)
parser.nextToken();
setCurrentPosition(parser.toVariant().toInt());
}
else if(parser.compare("rcp")) { /// rcp (read current position)
parser.println(String(getCurrentPosition(), DEC) + " steps");
}
else if(parser.compare("move")) { /// move n (relative n steps)
parser.nextToken();
move(parser.toVariant());
}
else if(parser.compare("moveto")) { /// move n (absolute position n)
parser.nextToken();
moveTo(parser.toVariant());
}
else if(parser.compare("setsig")) { /// setsig sigLow sigHigh sigSteps sigCoefficient (set sigmoid properties)
parser.nextToken();
sigLow = parser.toVariant();
parser.nextToken();
sigHigh = parser.toVariant();
parser.nextToken();
sigSteps = parser.toVariant();
parser.nextToken();
sigCoefficient = parser.toVariant();
parser.println("OK");
}
else if(parser.compare("getsig")) { /// getsig (get sigmoid properties)
parser.print(sigLow.toString());
parser.print(" ");
parser.print(sigHigh.toString());
parser.print(" ");
parser.print(sigSteps.toString());
parser.print(" ");
parser.println(sigCoefficient.toString());
}
else if(parser.compare("units")) {
parser.nextToken();
parser.println(config->unitConversion(parser.toVariant()).toString());
}
else if(parser.compare("conv")) {
parser.nextToken();
Variant mx = parser.toVariant();
parser.nextToken();
Variant b = parser.toVariant();
parser.nextToken();
config->setConversion(mx, b, parser.toVariant().toInt());
}
/**
* Here begins a list of PONTECH STP10x compatible commands
*/
else if(parser.compare("rsm")) { /// RSM (read step minimum delay)
parser.println(sigLow.toString());
}
else if(parser.compare("rsd")) { /// RSD (read step delay)
parser.println(sigHigh.toString());
}
else if(parser.compare("rss")) { /// RSS (read step delay)
parser.println(sigSteps.toString());
}
else if(parser.compare("rsa")) { /// RSA (read step acceleration)
parser.println(sigCoefficient.toString());
}
else if(parser.compare("sm")) { /// SM Hz (set minimum step delay in 1/Hz)
parser.nextToken();
sigLow = parser.toVariant();
parser.println("OK");
}
else if(parser.compare("sd")) { /// SD Hz (set step delay in 1/Hz)
parser.nextToken();
sigHigh = parser.toVariant();
parser.println("OK");
}
else if(parser.compare("ss")) { /// SS s (set number of steps (s) to accelerate over)
parser.nextToken();
sigSteps = parser.toVariant();
parser.println("OK");
}
else if(parser.compare("sa")) { /// SC n (set acceleration sigmoid shape n)
parser.nextToken();
sigCoefficient = parser.toVariant();
parser.println("OK");
}
else if(parser.compare("so")) { /// SO Stepper Off (disable driver)
setEnabled(false);
parser.println("OK");
}
else if(parser.compare("sp")) { /// SP Stepper Powered (enable driver)
setEnabled(true);
parser.println("OK");
}
else if(parser.compare("rc")) { /// RC (read current position)
parser.println(String(getCurrentPosition(), DEC));
}
else if(parser.compare("rd")) { /// RD (read destination position)
parser.println(String(getDestinationPosition(), DEC));
}
else if(parser.compare("rt")) { /// RT (read delta position)
parser.println(String(getDeltaPosition(), DEC));
}
else if(parser.compare("rx")) { /// RX (read sign of delta)
s32 temp = getDeltaPosition();
if( temp > 0 )
parser.println("+");
else if( temp < 0 )
parser.println("-");
else
parser.println("0");
}
else if(parser.compare("hm")) { /// HM n (set current position)
parser.nextToken();
setCurrentPosition(parser.toVariant().toInt());
setDestinationPosition(parser.toVariant().toInt());
parser.println("OK");
}
else if(parser.compare("h0")) { /// H0 (stop the motor from running)
parser.nextToken();
halt_immediatly();
parser.println("OK");
}
else if(parser.compare("h+")) { /// H+ (absolute move the limit max)
if(!moveTo(config->limit_max)) parser.print("N");
parser.println("OK");
}
else if(parser.compare("h-")) { /// H- (absolute move the limit min)
if(!moveTo(config->limit_min)) parser.print("N");
parser.println("OK");
}
else if(parser.compare("ii")) { /// II n (Move relative to position n)
parser.nextToken();
if(!move(parser.toVariant())) parser.print("N");
parser.println("OK");
}
else if(parser.compare("mi")) { /// MI n (Move absolute to position n)
parser.nextToken();
if(!moveTo(parser.toVariant())) parser.print("N");
parser.println("OK");
}
else if(parser.compare("mf")) { /// MF m n (Move absolute to position m at frequency n, use only for testing)
parser.nextToken();
Variant units = parser.toVariant();
parser.nextToken();
Variant frequency = parser.toVariant();
moveFreq(units, frequency);
parser.println("OK");
}
else if(parser.compare("smsps")) { /// SMSPS n (Set microsteps per step, n = microsteps (1,2,4,8,16) per step)
parser.nextToken();
Variant new_microsteps = parser.toVariant();
if(!setMicrostepsPerStep(new_microsteps.toInt())) parser.print("N");
parser.println("OK");
}
else if(parser.compare("v?")) { /// V? (Return command set version)
parser.println("STP100 V2.3");
}
else if(parser.compare("s?")) { /// V? (Return command set version)
parser.println("STP100 V2.3");
parser.print("REV: ");
parser.println(kard_rev);
parser.print("Enabled: ");
parser.println(String((int)getEnabled(), DEC));
parser.print("Current Position: ");
parser.println(String(getCurrentPosition(), DEC));
parser.print("Destination Position: ");
parser.println(String(getDestinationPosition(), DEC));
parser.print("Delta Position: ");
parser.println(String(getDeltaPosition(), DEC));
parser.print("Steps per step: ");
parser.println(String((int)microsteps, DEC));
}
}
}
uint32_t interruptPeriod;
inline bool readHomeState() {
return (bool)(homeSensorPort->port.reg & homeSensorBit);
}
char * getKardRev() {
return kard_rev;
}
private:
inline bool readHomeSensor() {
if(homeSensorPort != 0) {
bool currentHomeState = readHomeState();
if(currentHomeState != previousHomeState) {
previousHomeState = currentHomeState;
return (currentHomeState == homeSensorPolarity);
}
}
return false;
}
inline void step() {
if(readHomeSensor()) {
halt_immediatly();
if( !homeSensorPersistent ) {
homeSensorPort = 0;
}
return;
}
#ifdef fast_io
stepPort->lat.set = stepBit;
#else
digitalWrite(pin_step, HIGH);
#endif
if(vector.steps > 0 ) {
config->currentPosition++;
vector.steps--;
}
else {
config->currentPosition--;
vector.steps++;
}
}
us8 motor;
us8 pin_step;
us8 pin_direction;
us8 pin_enable;
us8 pin_sleep_ms1;
us8 pin_ms3_ms2;
char *command_prefix;
char *kard_rev;
StepConfig *config;
StepConfig *defaultConfig;
#ifdef fast_io
// step pin
p32_ioport *stepPort;
unsigned int stepBit;
// direction pin
p32_ioport *directionPort;