-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdSPIN_L6472.cpp
1382 lines (1196 loc) · 45.1 KB
/
dSPIN_L6472.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
////////////////////////////////////////////////////////////
//ORIGINAL CODE 12/12/2011- Mike Hord, SparkFun Electronics
//LIBRARY Created by Adam Meyer of bildr Aug 18th 2012
//Released as MIT license
////////////////////////////////////////////////////////////
//#include <WProgram.h>//#include <Arduino.h>
#include <dSPIN_L6472.h>
//#include <SPI.h>
L6472::L6472(unsigned char BOARD_ID, int MOSIPin, int MISOPin, int SCKPin, int SSPin, int RSTPin, bool (*Safe_Move)(bool))
{
_MOSI = MOSIPin;
_MISO = MISOPin;
_SCK = SCKPin;
_SS = SSPin;
_RST = RSTPin;
_BOARD_ID = BOARD_ID;
_current = 45;
_current_holding = 15;
_Safe_Move = Safe_Move;
}
void L6472::setupPort()
{
pinMode(_SS, OUTPUT);
digitalWrite(_SS, HIGH);
pinMode(_MOSI, OUTPUT);
pinMode(_MISO, INPUT);
pinMode(_SCK, OUTPUT);
pinMode(_RST, OUTPUT);
digitalWrite(_RST, HIGH);
// initialize SPI for the dSPIN chip's needs:
// most significant bit first,
// SPI clock not to exceed 5MHz,
// SPI_MODE3 (clock idle high, latch data on rising edge of clock)
SPI.begin();
// SPI.setBitOrder(MSBFIRST);
SPI.setClockDivider(SPI_CLOCK_DIV16); // or 2, 8, 16, 32, 64
// SPI.setClockDivider(SPI_CLOCK_DIV64); // or 2, 8, 16, 32, 64
SPI.setDataMode(SPI_MODE3);
}
//function that converts a nonnumber terminated string of numbers to an int
int L6472::parseNumber(char* s)
{
unsigned char i = 0;
int out = 0;
int mult = 1;
if(s[i]=='-')
{
mult = -1;
i++;
}
while(s[i]>='0' && s[i]<='9')
{
out = out * 10 + (s[i++]-'0');
}
return out*mult;
}
//function that finds next null or space from current position
//returns number of places to get there +1
unsigned char L6472::findSpaceOffset(char* s)
{
unsigned char i = 0;
while(s[i] != 0 && s[i] != ' ') // && s[i] != '\r' && s[i] != '\n'
i++;
return i+1;
}
//positive_direction true for positive direction false for negative direction
void L6472::stp300_H(bool positive_direction)
{
if(_Safe_Move != NULL)
{
if(_Safe_Move((positive_direction ? true : false)))
{
goUntil(ACT_ACTIVE_LO, (positive_direction ? 1 : 0), 20000); //todo 3: get to move at same speed as other move commands
_HRunning = true;
_Hmoveingpos = positive_direction;
}
} else {
goUntil(ACT_ACTIVE_LO, (positive_direction ? 1 : 0), 20000); //todo 3: get to move at same speed as other move commands
_HRunning = true;
_Hmoveingpos = positive_direction;
}
}
bool L6472::SendPausedStringIfNeeded(char* _str, char* _lineend)
{ //if(SendPausedStringIfNeeded(&_str[0], &_lineend[0])){return;}
if(_paused)
{
sprintf(_str, "NOK P%s",_lineend);
_IOStream->print(_str);
}
return _paused;
}
void L6472::stp300_MI(int32_t steps)
{
if(_Safe_Move != NULL)
{
int32_t position = stp300_RC();
if(_Safe_Move(steps>position))
{
goTo(steps);
}
} else {
goTo(steps);
}
}
void L6472::stp300_II(int32_t steps)
{
if(_Safe_Move != NULL)
{
if(_Safe_Move(steps>0))
{
move(steps);
}
} else {
move(steps);
}
}
inline void L6472::stp300_SP(void)
{
hardStop();
}
inline void L6472::stp300_SO(void)
{
free();
}
void L6472::stp300_H0(void)
{
softStop();
_HRunning = false;
}
void L6472::stp300_HI(void)
{
hardStop();
_HRunning = false;
}
bool L6472::stp300_PA(uint8_t new_paused)
{
if((new_paused == 1) && !_paused) // 1,0
{
// save destination position, halt axis
_pDestinationPosition = stp300_RD();
_pauseMotionInterrupted = !(GetParam(STATUS) & STATUS_BUSY); //flag is low when moving and is high after completed.
softStop();
_paused = true;
}
else if((new_paused == 0) && _paused) // 0,1
{
// move to saved destination position
if(_HRunning)
{
stp300_H(_Hmoveingpos);
}
else
{
goTo(_pDestinationPosition);
}
_paused = false;
}
else if(((new_paused == 1) && _paused ) || ((new_paused == 0) && !_paused )) // 1,1 or 0,0
{
// do nothing state unchanged
}
else // new_paused any number other than one or zero aborts pause without moving
{
_paused = false;
}
return _paused;
}
pause_state L6472::stp300_RPA(void)
{
pause_state result;
result.paused = _paused;
result.pauseMotionInterrupted = _pauseMotionInterrupted;
return result;
}
void L6472::command(char* input, Stream* IOStream)
{
_IOStream = IOStream;
char _lineend[] = "\r\n";
char _str[80];
if (input[0] == '/' && parseNumber(input+1) == _BOARD_ID)
{
char* rxBuffParsPoint = input + findSpaceOffset(input);
int32_t cmmdVal;
// sprintf(_str, "CMMD:[%c%c] Board[%d] Drive[%d] %d\r\n", cmmd1, cmmd2, boardID, axisID, cmmdVal);
// _IOStream->print(_str);
// L6472 Specific Commands
if(strncmp(rxBuffParsPoint, "CF",2) == 0) // Read Config
{
sprintf(_str, "CONFIG:%X, Status:%X%s", GetParam(CONFIG), GetParam(STATUS),_lineend);
_IOStream->print(_str);
}
else if(strncmp(rxBuffParsPoint, "RI",2) == 0) // Retry Init
{
init(_current, _current_holding, true);
sprintf(_str, "CONFIG:%X%s", GetParam(CONFIG), _lineend);
_IOStream->print(_str);
}
else if(strncmp(rxBuffParsPoint, "HS",2) == 0) // Print out L6472 Config Register
{
sprintf(_str, "CONFIG:%X%s", GetParam(CONFIG),_lineend);
_IOStream->print(_str);
}
// STP AT Style Commands with API implimented
else if(strncmp(rxBuffParsPoint, "V?",2) == 0) // Report a version number
{
sprintf(_str, "STP300 V1.21%s",_lineend);
_IOStream->print(_str);
}
else if(strncmp(rxBuffParsPoint, "MI",2) == 0) // Move Absolute
{
if(SendPausedStringIfNeeded(&_str[0], &_lineend[0])){return;}
rxBuffParsPoint += findSpaceOffset(rxBuffParsPoint);
cmmdVal = parseNumber(rxBuffParsPoint);
stp300_MI(cmmdVal);
sprintf(_str, "%d%s", cmmdVal, _lineend);
_IOStream->print(_str);
}
else if(strncmp(rxBuffParsPoint, "II",2) == 0) // Move Incramental
{
if(SendPausedStringIfNeeded(&_str[0], &_lineend[0])){return;}
rxBuffParsPoint += findSpaceOffset(rxBuffParsPoint);
cmmdVal = parseNumber(rxBuffParsPoint);
stp300_II(cmmdVal);
sprintf(_str, "%d%s", cmmdVal, _lineend);
_IOStream->print(_str);
}
else if(strncmp(rxBuffParsPoint, "HM",2) == 0) // Set Home
{
if(SendPausedStringIfNeeded(&_str[0], &_lineend[0])){return;}
setAsHome();
rxBuffParsPoint += findSpaceOffset(rxBuffParsPoint);
if(rxBuffParsPoint[0]>='0' && rxBuffParsPoint[0]<='9')
{
cmmdVal = parseNumber(rxBuffParsPoint);
stp300_HM(cmmdVal); //SetParam(ABS_POS, cmmdVal);
}
sprintf(_str, "OK%s",_lineend);
_IOStream->print(_str);
}
else if(strncmp(rxBuffParsPoint, "H+",2) == 0) // Home at Speed and direction
{
if(SendPausedStringIfNeeded(&_str[0], &_lineend[0])){return;}
stp300_H(true);
sprintf(_str, "OK%s",_lineend);
_IOStream->print(_str);
}
else if(strncmp(rxBuffParsPoint, "H-",2) == 0) // Home at Speed and direction
{
if(SendPausedStringIfNeeded(&_str[0], &_lineend[0])){return;}
stp300_H(false);
sprintf(_str, "OK%s",_lineend);
_IOStream->print(_str);
}
else if(strncmp(rxBuffParsPoint, "HI",2) == 0) // Halt Immediately
{
if(SendPausedStringIfNeeded(&_str[0], &_lineend[0])){return;}
stp300_HI();
sprintf(_str, "OK%s",_lineend);
_IOStream->print(_str);
}
else if(strncmp(rxBuffParsPoint, "H0",2) == 0) // Halt with deceleration
{
if(SendPausedStringIfNeeded(&_str[0], &_lineend[0])){return;}
stp300_H0();
sprintf(_str, "OK%s",_lineend);
_IOStream->print(_str);
}
else if(strncmp(rxBuffParsPoint, "RC",2) == 0) // Read Current (Position)
{
int32_t position = stp300_RC();
sprintf(_str, "%d%s", (int32_t)position,_lineend);
_IOStream->print(_str);
}
else if(strncmp(rxBuffParsPoint, "RX",2) == 0) // Read Delta Sign SignOf(Destination - Current)
{
switch(stp300_RX())
{
case -1:
_IOStream->print("-");
break;
case 0:
_IOStream->print("0");
break;
case 1:
_IOStream->print("+");
break;
case 'P':
_IOStream->print("P");
break;
}
_IOStream->print(_lineend);
}
else if(strncmp(rxBuffParsPoint, "RT",2) == 0) // Read Delta (Destination - Current)
{
sprintf(_str, "%d%s", stp300_RT(),_lineend);
_IOStream->print(_str);
}
else if(strncmp(rxBuffParsPoint, "RD",2) == 0) // Read Destination (Position)
{
sprintf(_str, "%d%s", stp300_RD(),_lineend);
_IOStream->print(_str);
}
else if(strncmp(rxBuffParsPoint, "SP",2) == 0) // Stepper Powered
{
stp300_SP();
sprintf(_str, "OK%s",_lineend);
_IOStream->print(_str);
}
else if(strncmp(rxBuffParsPoint, "SO",2) == 0) // Stepper Off
{
if(SendPausedStringIfNeeded(&_str[0], &_lineend[0])){return;}
stp300_SO();
sprintf(_str, "OK%s",_lineend);
_IOStream->print(_str);
}
else if(strncmp(rxBuffParsPoint, "SD",2) == 0) // Set Speed Max
{
rxBuffParsPoint += findSpaceOffset(rxBuffParsPoint);
cmmdVal = parseNumber(rxBuffParsPoint);
cmmdVal = (cmmdVal>0x000003FF) ? 0x000003FF : cmmdVal;
SetParam(MAX_SPEED, cmmdVal);
//setMaxSpeed(cmmdVal);
sprintf(_str, "%d%s", cmmdVal,_lineend);
_IOStream->print(_str);
}
else if(strncmp(rxBuffParsPoint, "RSD",3) == 0) // get Speed Max
{
sprintf(_str, "%d%s", GetParam(MAX_SPEED),_lineend);
_IOStream->print(_str);
}
else if(strncmp(rxBuffParsPoint, "SM",2) == 0) // Set Speed Min
{
rxBuffParsPoint += findSpaceOffset(rxBuffParsPoint);
cmmdVal = parseNumber(rxBuffParsPoint);
cmmdVal = (cmmdVal>0x00000FFF) ? 0x00000FFF : cmmdVal;
SetParam(MIN_SPEED, cmmdVal);
//setMinSpeed(cmmdVal);
sprintf(_str, "%d%s", cmmdVal,_lineend);
_IOStream->print(_str);
}
else if(strncmp(rxBuffParsPoint, "RSM",3) == 0) // get Speed Min
{
sprintf(_str, "%d%s", GetParam(MIN_SPEED),_lineend);
_IOStream->print(_str);
}
else if(strncmp(rxBuffParsPoint, "SA",2) == 0) // Set Accel
{
rxBuffParsPoint += findSpaceOffset(rxBuffParsPoint);
cmmdVal = parseNumber(rxBuffParsPoint);
cmmdVal = (cmmdVal>0x00000FFF) ? 0x00000FFF : cmmdVal;
SetParam(ACC, cmmdVal);
SetParam(DECEL, cmmdVal);
//setAcc((float) cmmdVal);
//setDec((float) cmmdVal);
sprintf(_str, "%d%s", cmmdVal,_lineend);
_IOStream->print(_str);
}
else if(strncmp(rxBuffParsPoint, "RSA",3) == 0) // get Accel
{
sprintf(_str, "%d%s", GetParam(ACC),_lineend);
_IOStream->print(_str);
}
else if(strncmp(rxBuffParsPoint, "SCM",3) == 0) // Set current moving
{
rxBuffParsPoint += findSpaceOffset(rxBuffParsPoint);
cmmdVal = parseNumber(rxBuffParsPoint);
cmmdVal = (cmmdVal>0x000007F) ? 0x000007F : cmmdVal;
_current = cmmdVal;
SetParam(TVAL_RUN, _current);
SetParam(TVAL_ACC, _current);
SetParam(TVAL_DEC, _current);
sprintf(_str, "%d%s", cmmdVal,_lineend);
_IOStream->print(_str);
}
else if(strncmp(rxBuffParsPoint, "RSCM",4) == 0) // get current moving
{
sprintf(_str, "%d%s", GetParam(TVAL_RUN),_lineend);
_IOStream->print(_str);
}
else if(strncmp(rxBuffParsPoint, "SCH",3) == 0) // Set current holding
{
rxBuffParsPoint += findSpaceOffset(rxBuffParsPoint);
cmmdVal = parseNumber(rxBuffParsPoint);
cmmdVal = (cmmdVal>0x000007F) ? 0x000007F : cmmdVal;
_current_holding = cmmdVal;
SetParam(TVAL_HOLD, _current_holding);
sprintf(_str, "%d%s", cmmdVal,_lineend);
_IOStream->print(_str);
}
else if(strncmp(rxBuffParsPoint, "RSCH",4) == 0) // get current holding
{
sprintf(_str, "%d%s", GetParam(TVAL_HOLD),_lineend);
_IOStream->print(_str);
}
else if(strncmp(rxBuffParsPoint, "PA ", 3) == 0) // set pause state
{
rxBuffParsPoint += findSpaceOffset(rxBuffParsPoint);
uint8_t new_paused = parseNumber(rxBuffParsPoint);
stp300_PA(new_paused);
sprintf(_str, "%d%s", _paused,_lineend);
_IOStream->print(_str);
}
else if(strncmp(rxBuffParsPoint, "RPA", 2) == 0) // read pause state
{
pause_state result = stp300_RPA();
sprintf(_str, "%d %d%s", result.paused,result.pauseMotionInterrupted,_lineend);
_IOStream->print(_str);
}
}
}
void L6472::BoardId(unsigned char newId)
{
_BOARD_ID = newId;
}
int L6472::init2(float current, float hold_current)
{
int res = 0;
resetDev();
//SetParam(STEP_MODE, !SYNC_EN | STEP_SEL_1_16 | SYNC_SEL_1);
SetParam(CONFIG,
//CONFIG_TSW_20_us
//CONFIG_TSW_60_us
CONFIG_TSW_124_us
| CONFIG_SR_110V_us
//| CONFIG_SR_270V_us
//| CONFIG_TQ_EXTERNAL
| CONFIG_TQ_INTERNAL
//| CONFIG_OC_SD_DISABLE
| CONFIG_OC_SD_ENABLE
| CONFIG_SW_HARD_STOP
//| CONFIG_SW_USER
| CONFIG_INT_16MHZ
| CONFIG_PRED_EN);
SetParam(CONFIG, 0xFF90);
SetParam(CONFIG, CONFIG_TSW_124_us | CONFIG_SR_110V_us | CONFIG_TQ_INTERNAL | CONFIG_OC_SD_ENABLE | CONFIG_SW_USER | CONFIG_INT_16MHZ | CONFIG_PRED_EN);
res = (int)GetParam(CONFIG);
return res;
}
int L6472::init(float current, float hold_current, bool userawcurrent)
{
int res = 0;
// This is the generic initialization function to set up the Arduino to
// communicate with the dSPIN chip.
// set up the input/output pins for the application.
// pinMode(10, OUTPUT); // The SPI peripheral REQUIRES the hardware SS pin-
// pin 10- to be an output. This is in here just
// in case some future user makes something other
// than pin 10 the SS pin.
// pinMode(_SS, OUTPUT);
// digitalWrite(_SS, HIGH);
// pinMode(_MOSI, OUTPUT);
// pinMode(_MISO, INPUT);
// pinMode(_SCK, OUTPUT);
//pinMode(_RST, OUTPUT);
//pinMode(BUSYN, INPUT);
// reset the dSPIN chip. This could also be accomplished by
// calling the "L6472::resetDev()" function after SPI is initialized.
//digitalWrite(_RST, HIGH);
//delay(10);
//digitalWrite(_RST, LOW);
//delay(10);
//digitalWrite(_RST, HIGH);
//delay(50);
// initialize SPI for the dSPIN chip's needs:
// most significant bit first,
// SPI clock not to exceed 5MHz,
// SPI_MODE3 (clock idle high, latch data on rising edge of clock)
// SPI.begin();
// SPI.setBitOrder(MSBFIRST);
// //SPI.setClockDivider(SPI_CLOCK_DIV16); // or 2, 8, 16, 32, 64
// SPI.setDataMode(SPI_MODE3);
resetDev();
delay(50);
free(); //adding this is what made it work
delay(50);
free(); //adding this is what made it work
delay(50);
// First things first: let's check communications. The CONFIG register should
// power up to 0x2E88, so we can use that to check the communications.
// if (GetParam(CONFIG) == 0x2E88){
// //_IOStream->println('good to go');
// res = 0;
// }
// else
// {
// //_IOStream->println('Comm issue');
// res = -1;
// }
// First, let's set the step mode register:
// - SYNC_EN controls whether the BUSY/SYNC pin reflects the step
// frequency or the BUSY status of the chip. We want it to be the BUSY
// status.
// - STEP_SEL_x is thsetThresholdSpeede microstepping rate- we'll go full step.
// - SYNC_SEL_x is the ratio of (micro)steps to toggles on the
// BUSY/SYNC pin (when that pin is used for SYNC). Make it 1:1, despite
// not using that pin.
SetParam(STEP_MODE, !SYNC_EN | STEP_SEL_1_16 | SYNC_SEL_1);
//SetParam(FS_SPD, FSCalc(FULL_STEP_POINT)); // point of change from microstep to full steps
setThresholdSpeed(1000);
if(userawcurrent) {
_current = current;
_current_holding = hold_current;
}
else {
_current = TVALCalc(current);
_current_holding = TVALCalc(hold_current);
}
_current = TVALCalc(current);
_current_holding = TVALCalc(hold_current);
SetParam(TVAL_HOLD, _current_holding);
SetParam(TVAL_RUN, _current);
SetParam(TVAL_ACC, _current);
SetParam(TVAL_DEC, _current);
setMaxSpeed(1000);
setMinSpeed(10);
setAcc(400);
setDec(400);
setOverCurrent(6000);
SetParam(TON_MIN, 0x3F);
SetParam(TOFF_MIN, 0x3F);
SetParam(T_FAST, 0x2A);
delay(10);
// Set up the CONFIG register as follows:
// PWM frequency divisor = 1
// PWM frequency multiplier = 2 (62.5kHz PWM frequency)
// Slew rate is 290V/us
// Do NOT shut down bridges on overcurrent
// Disable motor voltage compensation
// Hard stop on switch low
// 16MHz internal oscillator, nothing on output
// SetParam(CONFIG, CONFIG_PWM_DIV_1 | CONFIG_PWM_MUL_2 | CONFIG_SR_290V_us| CONFIG_OC_SD_DISABLE | CONFIG_VS_COMP_DISABLE | CONFIG_SW_HARD_STOP | CONFIG_INT_16MHZ);
unsigned long val = CONFIG_TSW_124_us | CONFIG_SR_110V_us | CONFIG_TQ_INTERNAL | CONFIG_OC_SD_ENABLE | CONFIG_SW_HARD_STOP | CONFIG_INT_16MHZ | CONFIG_PRED_EN;
SetParam(CONFIG,val);
delay(10);
SetParam(CONFIG,val);
delay(10);
// Configure the RUN KVAL. This defines the duty cycle of the PWM of the bridges
// during running. 0xFF means that they are essentially NOT PWMed during run; this
// MAY result in more power being dissipated than you actually need for the task.
// Setting this value too low may result in failure to turn.
// There are ACC, DEC, and HOLD KVAL registers as well; you may need to play with
// those values to get acceptable performance for a given application.
// SetParam(KVAL_RUN, 0xFF);
delay(10);
//SetParam(ABS_POS, 0); // Reset Position Counter
// Calling GetStatus() clears the UVLO bit in the status register, which is set by
// default on power-up. The driver may not run without that bit cleared by this
// read operation.
getStatus();
res = (int)GetParam(CONFIG);
//SetParam(ABS_POS, 0); // Reset Position Counter
//delay(200);
setAsHome();
hardStop(); //engage motors
_DestinationPosition = 0;
_HRunning = false;
_paused = false;
return res;
}
boolean L6472::isBusy()
{
int status = getStatus();
return !((status >> 1) & 0b1);
}
void L6472::setMicroSteps(int microSteps)
{
byte stepVal;
for(stepVal = 0; stepVal < 8; stepVal++){
if(microSteps == 1) break;
microSteps = microSteps >> 1;
}
SetParam(STEP_MODE, !SYNC_EN | stepVal | SYNC_SEL_1);
}
void L6472::setThresholdSpeed(float thresholdSpeed)
{
// Configure the FS_SPD register- this is the speed at which the driver ceases
// microstepping and goes to full stepping. FSCalc() converts a value in steps/s
// to a value suitable for this register; to disable full-step switching, you
// can pass 0x3FF to this register.
if(thresholdSpeed == 0.0){
SetParam(FS_SPD, 0x3FF);
}else{
SetParam(FS_SPD, FSCalc(thresholdSpeed));
}
}
void L6472::setCurrent(float current)
{
SetParam(TVAL_RUN, TVALCalc(current));
SetParam(TVAL_ACC, TVALCalc(current));
SetParam(TVAL_DEC, TVALCalc(current));
}
void L6472::setCurrentHold(float current)
{
SetParam(TVAL_HOLD, TVALCalc(current));
}
void L6472::setMaxSpeed(int speed)
{
// Configure the MAX_SPEED register- this is the maximum number of (micro)steps per
// second allowed. You'll want to mess around with your desired application to see
// how far you can push it before the motor starts to slip. The ACTUAL parameter
// passed to this function is in steps/tick; MaxSpdCalc() will convert a number of
// steps/s into an appropriate value for this function. Note that for any move or
// goto type function where no speed is specified, this value will be used.
SetParam(MAX_SPEED, MaxSpdCalc(speed));
}
void L6472::setMinSpeed(int speed)
{
// Configure the MAX_SPEED register- this is the maximum number of (micro)steps per
// second allowed. You'll want to mess around with your desired application to see
// how far you can push it before the motor starts to slip. The ACTUAL parameter
// passed to this function is in steps/tick; MaxSpdCalc() will convert a number of
// steps/s into an appropriate value for this function. Note that for any move or
// goto type function where no speed is specified, this value will be used.
SetParam(MIN_SPEED, MinSpdCalc(speed));
}
void L6472::setAcc(float acceleration)
{
// Configure the acceleration rate, in steps/tick/tick. There is also a DEC register;
// both of them have a function (AccCalc() and DecCalc() respectively) that convert
// from steps/s/s into the appropriate value for the register. Writing ACC to 0xfff
// sets the acceleration and deceleration to 'infinite' (or as near as the driver can
// manage). If ACC is set to 0xfff, DEC is ignored. To get infinite deceleration
// without infinite acceleration, only hard stop will work.
unsigned long accelerationBYTES = AccCalc(acceleration);
SetParam(ACC, accelerationBYTES);
}
void L6472::setDec(float deceleration)
{
unsigned long decelerationBYTES = DecCalc(deceleration);
SetParam(DECEL, decelerationBYTES);
}
int32_t L6472::stp300_RC()
{
uint32_t position = GetParam(ABS_POS);
return convert(position);
}
void L6472::stp300_HM(long newposition)
{
SetParam(ABS_POS, newposition);
_DestinationPosition = newposition;
}
int32_t L6472::stp300_RD()
{
return _DestinationPosition;
}
int32_t L6472::stp300_RT()
{
return stp300_RD() - stp300_RC();
}
int32_t L6472::stp300_RX()
{
int32_t stat = GetParam(STATUS);
if(_paused)
return (int32_t)'P';
else
if((stat & STATUS_MOT_STATUS) == 0)
return 0;
else if ((stat & STATUS_DIR) == 0)
return -1;
else
return 1;
}
float L6472::getSpeed(){
// SPEED
// The SPEED register contains the current motor speed, expressed in step/tick (format unsigned fixed point 0.28).
// In order to convert the SPEED value in step/s the following formula can be used:
// Equation 4
// where SPEED is the integer number stored into the register and tick is 250 ns.
// The available range is from 0 to 15625 step/s with a resolution of 0.015 step/s.
// Note: The range effectively available to the user is limited by the MAX_SPEED parameter.
return (float) GetParam(SPEED);
//return (float) speed * pow(8, -22);
//return FSCalc(speed); NEEDS FIX
}
void L6472::setOverCurrent(unsigned int ma_current)
{
// Configure the overcurrent detection threshold.
byte OCValue = floor(ma_current / 375);
if(OCValue > 0x0F)OCValue = 0x0F;
SetParam(OCD_TH, OCValue);
}
void L6472::SetLowSpeedOpt(boolean enable)
{
// Enable or disable the low-speed optimization option. If enabling,
// the other 12 bits of the register will be automatically zero.
// When disabling, the value will have to be explicitly written by
// the user with a SetParam() call. See the datasheet for further
// information about low-speed optimization.
Xfer(SET_PARAM | MIN_SPEED);
if (enable) Param(0x1000, 13);
else Param(0, 13);
}
void L6472::run(byte dir, float spd)
{
// RUN sets the motor spinning in a direction (defined by the constants
// FWD and REV). Maximum speed and minimum speed are defined
// by the MAX_SPEED and MIN_SPEED registers; exceeding the FS_SPD value
// will switch the device into full-step mode.
// The SpdCalc() function is provided to convert steps/s values into
// appropriate integer values for this function.
unsigned long speedVal = SpdCalc(spd);
Xfer(RUN | dir);
if (speedVal > 0xFFFFF) speedVal = 0xFFFFF;
Xfer((byte)(speedVal >> 16));
Xfer((byte)(speedVal >> 8));
Xfer((byte)(speedVal));
}
void L6472::Step_Clock(byte dir){
// STEP_CLOCK puts the device in external step clocking mode. When active,
// pin 25, STCK, becomes the step clock for the device, and steps it in
// the direction (set by the FWD and REV constants) imposed by the call
// of this function. Motion commands (RUN, MOVE, etc) will cause the device
// to exit step clocking mode.
Xfer(STEP_CLOCK | dir);
}
void L6472::move(long n_step){
// MOVE will send the motor n_step steps (size based on step mode) in the
// direction imposed by dir (FWD or REV constants may be used). The motor
// will accelerate according the acceleration and deceleration curves, and
// will run at MAX_SPEED. Stepping mode will adhere to FS_SPD value, as well.
byte dir;
if(n_step >= 0)
dir = FWD;
else
dir = REV;
long n_stepABS = abs(n_step);
Xfer(MOVE | dir); //set direction
if (n_stepABS > 0x3FFFFF)
n_step = 0x3FFFFF;
Xfer((byte)(n_stepABS >> 16));
Xfer((byte)(n_stepABS >> 8));
Xfer((byte)(n_stepABS));
_DestinationPosition += n_step;
}
void L6472::goTo(long pos){
// GOTO operates much like MOVE, except it produces absolute motion instead
// of relative motion. The motor will be moved to the indicated position
// in the shortest possible fashion.
Xfer(GOTO);
if (pos > 0x3FFFFF) pos = 0x3FFFFF;
Xfer((byte)(pos >> 16));
Xfer((byte)(pos >> 8));
Xfer((byte)(pos));
_DestinationPosition = pos;
}
void L6472::goTo_DIR(byte dir, long pos){
// Same as GOTO, but with user constrained rotational direction.
Xfer(GOTO_DIR);
if (pos > 0x3FFFFF) pos = 0x3FFFFF;
Xfer((byte)(pos >> 16));
Xfer((byte)(pos >> 8));
Xfer((byte)(pos));
_DestinationPosition = pos;
}
void L6472::goUntil(byte act, byte dir, unsigned long spd){
// GoUntil will set the motor running with direction dir (REV or
// FWD) until a falling edge is detected on the SW pin. Depending
// on bit SW_MODE in CONFIG, either a hard stop or a soft stop is
// performed at the falling edge, and depending on the value of
// act (either RESET or COPY) the value in the ABS_POS register is
// either RESET to 0 or COPY-ed into the MARK register.
Xfer(GO_UNTIL | act | dir);
if (spd > 0x3FFFFF) spd = 0x3FFFFF;
Xfer((byte)(spd >> 16));
Xfer((byte)(spd >> 8));
Xfer((byte)(spd));
if(dir>0)
_DestinationPosition = 2097151;
else
_DestinationPosition = -2097152;
}
bool L6472::sensorStop(char pin, bool rising, bool positive){
bool forward = (getStatus() & STATUS_DIR) > 0;
if((forward && positive) || (!forward && !positive))
{
if((digitalRead(pin) && rising) || (!digitalRead(pin) && !rising))
{
hardStop();
_HRunning = false;
return true;
}
}
return false;
}
bool L6472::getHRunning(){
return _HRunning;
}
void L6472::releaseSW(byte act, byte dir){
// Similar in nature to GoUntil, ReleaseSW produces motion at the
// higher of two speeds: the value in MIN_SPEED or 5 steps/s.
// The motor continues to run at this speed until a rising edge
// is detected on the switch input, then a hard stop is performed
// and the ABS_POS register is either COPY-ed into MARK or RESET to
// 0, depending on whether RESET or COPY was passed to the function
// for act.
Xfer(RELEASE_SW | act | dir);
}
void L6472::goHome(){
// GoHome is equivalent to GoTo(0), but requires less time to send.
// Note that no direction is provided; motion occurs through shortest
// path. If a direction is required, use GoTo_DIR().
Xfer(GO_HOME);
_DestinationPosition = 0;
}
void L6472::goMark(){
// GoMark is equivalent to GoTo(MARK), but requires less time to send.
// Note that no direction is provided; motion occurs through shortest
// path. If a direction is required, use GoTo_DIR().
Xfer(GO_MARK);
_DestinationPosition = convert(GetParam(MARK));
}
void L6472::setMark(int32_t value){
Xfer(MARK);
if (value > 0x3FFFFF) value = 0x3FFFFF;
if (value < -0x3FFFFF) value = -0x3FFFFF;
Xfer((byte)(value >> 16));
Xfer((byte)(value >> 8));
Xfer((byte)(value));
}
void L6472::setMark(){
int32_t value = stp300_RC();
Xfer(MARK);
if (value > 0x3FFFFF) value = 0x3FFFFF;
if (value < -0x3FFFFF) value = -0x3FFFFF;
Xfer((byte)(value >> 16));
Xfer((byte)(value >> 8));
Xfer((byte)(value));
}
void L6472::setAsHome(){
// Sets the ABS_POS register to 0, effectively declaring the current
// position to be "HOME".
Xfer(RESET_POS);
_DestinationPosition = 0;
}
void L6472::resetDev()
{
// Reset device to power up conditions. Equivalent to toggling the STBY
// pin or cycling power.
Xfer(RESET_DEVICE);
_DestinationPosition = 0;
}
void L6472::softStop(){
// Bring the motor to a halt using the deceleration curve.
Xfer(SOFT_STOP);
_DestinationPosition = stp300_RC();
}