-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmotor.c
1312 lines (1135 loc) · 35.5 KB
/
motor.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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "LPC11xx.h" /* LPC11xx definitions */
#include "constan.h"
#include "types.h"
#include "motor.h"
#include "main.h"
volatile long timer1count;
volatile int timer1done;
volatile status_t stepstat ;
volatile systemcfg_t systemconfig ;
//#define USEV1
void setup_motorIO(void)
{
LPC_SYSCON->SYSAHBCLKCTRL |= (1UL << 10); /* enable power to Timer32_1 */
LPC_IOCON->R_PIO1_1=0xc3; // STEP CT32B1_M0 mode for IO pin P1_1
LPC_IOCON->PIO3_3=0xc0; // DIR output
LPC_IOCON->PIO3_2=0xf0; // Home 1 input (hys + pu)
LPC_IOCON->PIO1_10=0xf0; // Home 2 input (hys + pu)
LPC_IOCON->PIO3_1=0xc0; // SLEEP
LPC_IOCON->PIO2_6=0xc0; // STPDIS
LPC_IOCON->PIO0_7=0xc0; // led1
LPC_IOCON->PIO3_0=0xc0; // led2
LPC_IOCON->PIO1_8=0xc0; // led3
LPC_IOCON->PIO2_7=0xf0; // F1
LPC_IOCON->PIO2_8=0xf0; // F2
LPC_IOCON->PIO2_10=0xf0; // F3
LPC_GPIO0->DIR=0x80;
LPC_GPIO1->DIR=0x102;
LPC_GPIO2->DIR=0x40;
LPC_GPIO3->DIR=0xb;
//setup timer 0
// timer 0 clock source=PCLK edge
LPC_TMR32B1->CTCR=0;
// timer 0 prescaler register ( om te delen door 15 : 14 invullen)
LPC_TMR32B1->PR=14; //resolutie 1us
LPC_TMR32B1->EMR=0 ;
// reset on match en interrupt
LPC_TMR32B1->MCR=MCR_MR0R | MCR_MR0I ;
//t0 Match on value clock=15000000
LPC_TMR32B1->MR0=999;
LPC_TMR32B1->TCR=TCR_RESET ; //stop & reset
//enable TIMER_32_1_IRQ in NVIC
NVIC_EnableIRQ(TIMER_32_1_IRQn);
}
void init_motorvars(void)
{
stepstat.homingdone=FALSE;
stepstat.targetpos=0;
stepstat.accel=DEFAULT_ACC;
stepstat.decel=DEFAULT_DEC;
stepstat.v_max=DEFAULT_SPEED;
stepstat.backlash=0;
stepstat.newposrequest=FALSE;
stepstat.currentpos=0;
stepstat.running=FALSE;
stepstat.stopreq=FALSE;
stepstat.recalc_after_stop=FALSE;
stepstat.toholdpowertimer=0;
}
void process_serialdata(char * serial_buffer)
{
char *token;
char seps[] = " ";
token = strtok((char *) serial_buffer, seps );
if( token != NULL )
{
if (0==strncmp(token,"hm",2))
{
printf ("to home\r\n");
stepstat.targetpos=0;
DoMotorRun();
}
else if (0==strncmp(token,"nh",2))
{
printf ("setting current pos new homepos\r\n");
stepstat.currentpos=0;
//stepstat.newtargetpos=stepstat.currentpos;
}
else if (0==strncmp(token,"init",4))
{
printf ("initialize defaults\r\n");
FindHomePosition();
}
else if (0==strncmp(token,"cw",2))
{
printf ("run forward (CW)\r\n");
DoContineousRun(CW);
}
else if (0==strncmp(token,"ccw",3))
{
printf ("run backward (CCW)\r\n");
DoContineousRun(CCW);
}
else if (0==strncmp(token,"stop",4))
{
printf ("stopping\r\n");
StopMotor();
}
else if (0==strncmp(token,"ma",2)) // move absolute
{
long pos;
token = strtok( NULL, seps );
if (token==NULL) // geen locatie nummer opgegeven
goto error;
pos=atol(token);
if (stepstat.targetpos!=pos || stepstat.targetpos!=stepstat.currentpos ) //we want a new target position
{
while(stepstat.newposrequest!=FALSE); //wacht tot request is verwerkt
//while(stepstat.recalc_after_stop);
// if(stepstat.newposrequest==FALSE && stepstat.run_state!=DECEL)
// while(stepstat.run_state==DECEL);// && ( (stepstat.currentpos-stepstat.newtargetpos) < -20 || (stepstat.currentpos-stepstat.newtargetpos) > 20) );
//if(stepstat.newposrequest==FALSE)
//{
//if(stepstat.run_state==DECEL)
//{
//stepstat.newtargetpos=pos;
//}
//if ((stepstat.currentpos-pos)<-2 || (stepstat.currentpos-pos)>2)
//if(stepstat.run_state!=ACCEL)
//if(stepstat.run_state!=DECEL)
//{
stepstat.recalc_after_stop=FALSE;
if (stepstat.running)
{
//stepstat.recalc_after_stop=FALSE;
stepstat.newtargetpos=pos;
//deze laatst zetten (irq)
stepstat.newposrequest=TRUE;
}
else
{
stepstat.newtargetpos=pos;
stepstat.targetpos=pos;
DoMotorRun();
}
//}
}
}
else if (0==strncmp(token,"sy",2))//continue to last target position
{
//printf ("syncing real with last job position\r\n");
DoMotorRun();
}
else if (0==strncmp(token,"j+",2)) //jog 1 step CW
{
//printf ("1 step forward\r\n");
stepstat.targetpos+=1;
DoMotorRun();
}
else if (0==strncmp(token,"j-",2)) //jog 1 step CCW
{
//printf ("1 step backward\r\n");
stepstat.targetpos-=1;
DoMotorRun();
}
else if (0==strncmp(token,"J+",2)) //rel 10 steps CW
{
//printf ("1 step forward\r\n");
stepstat.targetpos+=10;
DoMotorRun();
}
else if (0==strncmp(token,"J-",2)) //rel 10 steps CCW
{
//printf ("1 step backward\r\n");
stepstat.targetpos-=10;
DoMotorRun();
}
else if (0==strncmp(token,"mr",2)) //relative move
{
int rel;
token = strtok( NULL, seps );
if (token==NULL)
goto error;
rel=atoi(token);
stepstat.targetpos+=rel;
printf ("relative move %i\r\n",rel);
DoMotorRun();
}
else if (0==strncmp(token,"ac",2)) //set acceleration
{
int acc;
token = strtok( NULL, seps );
if (token==NULL)
goto error;
acc=atoi(token);
if ((acc>500000)||(acc<=0))
goto error;
stepstat.accel=acc;
//printf ("set acceleration %i\r\n",acc);
}
else if (0==strncmp(token,"de",2)) //set decelleration
{
int dec;
token = strtok( NULL, seps );
if (token==NULL)
goto error;
dec=atoi(token);
if ((dec>500000)||(dec<=0))
goto error;
stepstat.decel=dec;
printf ("set deceleration %i\r\n",dec);
}
else if (0==strncmp(token,"ad",2)) // set accel and deceleration
{
int dec;
token = strtok( NULL, seps );
if (token==NULL)
goto error;
dec=atoi(token);
if ((dec>500000)||(dec<=0))
goto error;
stepstat.accel=stepstat.decel=dec;
printf ("set deceleration %i\r\n",dec);
}
// else if (0==strncmp(token,"bl",2))
// {
// int v;
// token = strtok( NULL, seps );
// if (token==NULL)
// goto error;
// v=atoi(token);
// if ((v>1000)||(v<0))
// goto error;
// stepstat.backlash=v;
// printf ("set backlash %i\r\n",v);
// }
else if (0==strncmp(token,"vm",2)) //set vMax
{
int v;
token = strtok( NULL, seps );
if (token==NULL) goto error;
v=atoi(token);
if ((v>5000000)||(v<0))
goto error;
stepstat.v_max=v;
printf ("set vmax %i\r\n",v);
}
else if (0==strncmp(token,"ver",3)) //show version
{
printf("version %s %s\r\n",SWVERSION,SWDATE );
}
else if (0==strncmp(token,"sh",2))
{
printf ("position %li\r\n",stepstat.targetpos );
printf ("real position %li\r\n",stepstat.currentpos );
printf ("backlash %i\r\n",stepstat.backlash);
printf ("vmax %i\r\n",stepstat.v_max);
printf ("acceleration %li\r\n",stepstat.accel);
printf ("deceleration %li\r\n",stepstat.decel);
printf ("state %i\r\n",stepstat.run_state);
printf ("runstate %i\r\n",stepstat.running);
printf ("decel_val %li\r\n",stepstat.decel_val);
printf ("decel_start %li\r\n",stepstat.decel_start);
printf ("step_count %li\r\n",stepstat.step_count);
printf ("accel_count %li\r\n",stepstat.accel_count);
printf ("=====================\r\n");
}
else if (0==strncmp(token,"p?",2)) //show current position
{
printf ("real position %li\n",stepstat.currentpos );
}
else
goto error;
goto noerrors;
error:
printf("command error\n");
noerrors:
;
}
else
printf("ready\r\n");
}
/***************************************************
* Reads the limit switch
****************************************************/
static byte GetHomeSwitch(void)
{
//FIO0MASK1=~HOME_0_MASK;
//return FIO0PIN1 & HOME_0_MASK;
return LPC_GPIO3->MASKED_ACCESS [HOME_0_MASK]==HOME_0_MASK;
}
void FindHomePosition(void)
{
stepstat.homingdone=FALSE;
//achteruit tot switch schakelt
stepstat.currentpos=0;
stepstat.accel=DEFAULT_ACC;
stepstat.decel=DEFAULT_DEC;
stepstat.v_max=systemconfig.homing_speed;
stepstat.currentpos=0;
//homing limit and direction
stepstat.targetpos=systemconfig.home_detection_steplimit;
//draaien
DoMotorRun();
//tot de homeswitch schakelt
while(GetHomeSwitch());
StopMotor();
stepstat.currentpos=0;
stepstat.targetpos=0;
printf("Detected SWITCH ON\n\r");
//dan traag terugkeren tot de switch ontschakelt
do
{
stepstat.targetpos+=1;
DoMotorRun();
while(stepstat.running);
}
while(GetHomeSwitch()==0);
//als het schakelmoment niet op de 0 staat, dan hier de juiste waarde invullen
stepstat.currentpos=systemconfig.homing_offset;//positie van homing switch tov nullpunt
stepstat.homingdone=TRUE;
printf("Homing done\n\r");
//vooruit to switch ontschakeld
}
void Set_MotorPower(enum POWERSTATE powermode)
{
static enum POWERSTATE prevpowermode;
if (prevpowermode==powermode)
return;
prevpowermode=powermode;
switch(powermode)
{
case POWEROFF:
//set io
break;
case HOLDMODE:
//set io
break;
case FULLPOWER:
//set io
//wait
timer1done=0;
timer1count=FULLPOWERDELAY;
while(!timer1done);
break;
default:
break;
}
}
__inline void Set_Motor_Direction(int dir)
{
if (dir==CCW)
{
//ccw OUTPUT HIGH (p0.22)
//FIO0MASK2=0xbf;
//FIO0PIN2=0x40;
LPC_GPIO3->MASKED_ACCESS[0x08]= 0x08;
}
else
{
//ccw OUTPUT low (p0.22)
//FIO0MASK2=0xbf;
//FIO0PIN2=0x00;
LPC_GPIO3->MASKED_ACCESS[0x08]= 0x00;
}
}
__inline void Step_Motor(void)
{
//step pulse word door MAT02 pin gegenereerd
//indien geen MAT hw pin gebruikt word dan de code hieronder gebruiken
//Step output high. (p0.23)
//FIO0MASK2=0x7f;
//FIO0PIN2=0x80;
//Step output low. (p0.23)
//FIO0MASK2=0x7f;
//FIO0PIN2=0x00;
// huidige positie bijhouden
if(stepstat.dir==CW)
stepstat.currentpos++;
else
stepstat.currentpos--;
stepstat.step_count++;
}
void StopMotor(void)
{
stepstat.stopreq=TRUE;
while (stepstat.running);
//save power
Set_MotorPower(HOLDMODE);
}
void DoContineousRun(enum DIRECTION dir)
{
if (dir==CCW)
stepstat.targetpos=0x7fffffffUL;
else
stepstat.targetpos=0x80000000UL;
DoMotorRun();
}
/*! \brief Move the stepper motor a given number of steps.
*
* Makes the stepper motor move the given number of steps.
* It accelrate with given accelration up to maximum speed and decelerate
* with given deceleration so it stops at the given step.
* If accel/decel is to small and steps to move is to few, speed might not
* reach the max speed limit before deceleration starts.
*
* step Number of steps to move (pos - CW, neg - CCW).
* accel Accelration to use, in 0.01*rad/sec^2.
* decel Decelration to use, in 0.01*rad/sec^2.
* speed Max speed, in 0.01*rad/sec.
*/
void DoMotorRun(void)
{
long relativesteps;
//! Number of steps before we must start deceleration (if accel does not hit max speed).
unsigned long accel_lim;
if (stepstat.running)
{
return;
}
stepstat.newtargetpos=stepstat.targetpos;
relativesteps=stepstat.targetpos-stepstat.currentpos;
// Set direction from sign on step value.
if(relativesteps < 0)
{
stepstat.dir = CCW;
Set_Motor_Direction(CCW);
relativesteps = -relativesteps; // positief getal van maken.
// if (stepstat.backlash)
// {
// relativesteps+=stepstat.backlash; // bij achteruit draaien ,iets doordraaien en dan terugkeren
// }
}
else if(relativesteps > 0)
{
stepstat.dir = CW;
Set_Motor_Direction(CW);
}
else //niets te doen bij relativesteps is 0
return;
Set_MotorPower(FULLPOWER);
//berekenen en onthouden
stepstat.start_step_delay = (T1_FREQ_148 * sqrt(A_SQ / stepstat.accel) )/100;
stepstat.step_delay=stepstat.start_step_delay;
// Reset counter.
stepstat.accel_count = 0;
stepstat.running = TRUE;
LPC_TMR32B1->TCR=TCR_RESET; //reset
LPC_TMR32B1->EMR=EMR_EMC0_CLR ;
// If moving only 1 step.
if(relativesteps == 1)
{
// Move one step...
stepstat.accel_count = -1;
// ...in DECEL state.
stepstat.run_state = DECEL;
// Just a short delay so main() can act on 'running'.
LPC_TMR32B1->MR0=stepstat.start_step_delay;
}
else// if(step != 0) Only move if number of steps to move is not zero.
{
LPC_TMR32B1->MR0=10;// moet minimum 2 zijn !! anders komt de match pin niet hoog ! niet tegenstaande er toch een irq is. geen idee van de reden.
// Refer to documentation for detailed information about these calculations.
// Set max speed limit, by calc min_delay to use in timer.
// min_delay = (alpha / tt)/ w
//stepstat.min_delay = T1_FREQ / vmax; //maw de max snelheid in usteps/s
//orig
stepstat.min_delay = A_T_x100 / stepstat.v_max; //maw de max snelheid in usteps/s
// printf("stepstat.min_delay=%i\n\r",stepstat.min_delay);
// Set accelration by calc the first (c0) step delay .
// printf("stepstat.initial_delay=%i\n\r",stepstat.step_delay);
// Find out after how many STEPS does the speed hit the max speed limit.
// max_s_lim = speed^2 / (2*alpha*accel)
stepstat.max_s_lim = (long)stepstat.v_max*stepstat.v_max/(long)(((long)A_x20000*stepstat.accel)/100);
// If we hit max speed limit before 0,5 step it will round to 0.
// But in practice we need to move atleast 1 step to get any speed at all.
if(stepstat.max_s_lim == 0)
{
stepstat.max_s_lim = 1;
}
//printf("max_s_lim=%i\n\r",max_s_lim);
// Find out after how many steps we must start deceleration.
// n1 = (n1+n2)decel / (accel + decel)
//kans op overflow !!!!
//accel_lim = ((long)step*decel) / (accel+decel); // step * 180000 / 185000
//voorkomt overflow door gebruik 64 bit long long
accel_lim = ((long long)relativesteps*stepstat.decel) / (stepstat.accel+stepstat.decel); // (4000 * 180000) / (5000 + 180000 )
// We must accelrate at least 1 step before we can start deceleration.
if(accel_lim == 0)
{
accel_lim = 1;
}
// printf("accel_lim=%i\n\r",accel_lim);
// Use the limit we hit first to calc decel.
if(accel_lim <= stepstat.max_s_lim)
{
stepstat.decel_val = accel_lim - relativesteps; //driehoek profiel
//printf("stepstat.decel_val=%i\n\r",stepstat.decel_val);
}
else
{
stepstat.decel_val = - ((stepstat.max_s_lim*stepstat.accel)/stepstat.decel); //trapezium profiel
//printf("STEPstat.decel_val=%i\n\r",stepstat.decel_val);
}
// We must decelrate at least 1 step to stop.
if(stepstat.decel_val == 0)
{
stepstat.decel_val = -1;
}
// Find step to start decleration. decel_cal is altijd negatief
stepstat.decel_start = relativesteps + stepstat.decel_val;
//printf("stepstat.decel_start=%i\n\r",stepstat.decel_start);
// If the maximum speed is so low that we dont need to go via accelration state.
if(stepstat.step_delay <= stepstat.min_delay)
{
stepstat.step_delay = stepstat.min_delay;
stepstat.run_state = RUN;
}
else
{
stepstat.run_state = ACCEL;
}
}
LPC_TMR32B1->TCR=TCR_RUN; //run
}
void DoMotorRun2(void)
{
long relativesteps=stepstat.targetpos-stepstat.currentpos;
//unsigned int vmax=stepstat.v_max;
//! Number of steps before we hit max speed.
//unsigned long max_s_lim;
//! Number of steps before we must start deceleration (if accel does not hit max speed).
unsigned long accel_lim;
LPC_TMR32B1->TCR=TCR_RESET; //reset
// Set direction from sign on step value.
if(relativesteps < 0)
{
stepstat.dir = CCW;
Set_Motor_Direction(CCW);
relativesteps = -relativesteps; // positief getal van maken.
if (stepstat.backlash)
{
relativesteps+=stepstat.backlash; // bij achteruit draaien ,iets doordraaien en dan terugkeren
}
}
else if(relativesteps > 0)
{
stepstat.dir = CW;
Set_Motor_Direction(CW);
}
else
{
//zorgtvoor 1 extra int.
LPC_TMR32B1->MR0=stepstat.step_delay;
// go for it :Start Timer
LPC_TMR32B1->TCR=TCR_RUN; //run
//niets te doen bij relativesteps is 0
return;
}
// Reset counter.
stepstat.accel_count = 0;
//startsnelheid berekenen
stepstat.step_delay=stepstat.start_step_delay;
// If moving only 1 step.
if(relativesteps == 1)
{
// Move one step...
stepstat.accel_count = -1;
// ...in DECEL state.
stepstat.run_state = DECEL;
}
// Only move if number of steps to move is not zero.
else// if(step != 0)
{
accel_lim = ((long long)relativesteps*stepstat.decel) / (stepstat.accel+stepstat.decel); // (4000 * 180000) / (5000 + 180000 )
// We must accelrate at least 1 step before we can start deceleration.
if(accel_lim == 0)
accel_lim = 1;
// Use the limit we hit first to calc decel.
if(accel_lim <= stepstat.max_s_lim)
{
stepstat.decel_val = accel_lim - relativesteps;
}
else
{
stepstat.decel_val = - ((stepstat.max_s_lim*stepstat.accel)/stepstat.decel);
}
// We must decelrate at least 1 step to stop.
if(stepstat.decel_val == 0)
stepstat.decel_val = -1;
// Find step to start decleration. decel_cal is altijd negatief
stepstat.decel_start = relativesteps + stepstat.decel_val;
// If the maximum speed is so low that we dont need to go via accelration state.
if(stepstat.step_delay <= stepstat.min_delay)
{
stepstat.step_delay = stepstat.min_delay;
stepstat.run_state = RUN;
}
else
{
stepstat.run_state = ACCEL;
}
}
//bijna direct irq als we de timer starten
LPC_TMR32B1->MR0=10;
// go for it :Start Timer
LPC_TMR32B1->TCR=TCR_RUN; //run
}
/*! \brief Timer/Counter1 Output Compare A Match Interrupt.
*
* Timer/Counter1 Output Compare A Match Interrupt.
* Increments/decrements the position of the stepper motor
* exept after last position, when it stops.
* The \ref step_delay defines the period of this interrupt
* and controls the speed of the stepper motor.
* A new step delay is calculated to follow wanted speed profile
* on basis of accel/decel parameters.
*/
// Timer Match Interrupt.
void TIMER32_1_IRQHandler( void )
{
volatile int i;//voor dummy delay loop
// Holds next delay period.
long new_step_delay=0;
// Remember the last step delay used when accelrating.
static unsigned long last_accel_delay;
// Keep track of remainder from new_step-delay calculation to increase accurancy
static long rest = 0;
long relativesteps2;
long relativesteps;
unsigned long accel_lim;
//reset the int flag
if ((LPC_TMR32B1->IR & 1)==1 )
LPC_TMR32B1->IR=1;
if(stepstat.step_delay>stepstat.start_step_delay)
stepstat.step_delay=stepstat.start_step_delay;
//the next delay is setup in advance in the match register
LPC_TMR32B1->MR0= stepstat.step_delay;
eval:
switch(stepstat.run_state)
{
// case DOBACKLASH:
// if(step_count==stepstat.backlash)
// {
// stepstat.run_state = STOP;
// }
// else
// {
// Step_Motor();
// }
// new_step_delay=stepstat.step_delay ;
// break;
case RUN:
Step_Motor();
new_step_delay = stepstat.min_delay;
//herbereken decel_start
if(stepstat.newposrequest)
{
stepstat.newposrequest=FALSE;
//herberekenen van decel_val bij dezlfde richting
if(stepstat.dir==CW )
{
//hebben we nog genoeg ruimte over om af te remmen ?
if ((stepstat.currentpos-stepstat.newtargetpos) < stepstat.decel_val)
{
//decel_start punt verder leggen
//stepstat.decel_start= stepstat.decel_start+(stepstat.newtargetpos-stepstat.targetpos);
relativesteps=stepstat.newtargetpos-stepstat.currentpos;
accel_lim = ((long long)relativesteps*stepstat.decel) / (stepstat.accel+stepstat.decel); // (4000 * 180000) / (5000 + 180000 )
// Use the limit we hit first to calc decel.
if(accel_lim <= stepstat.max_s_lim)
{
stepstat.decel_val = accel_lim - relativesteps;
}
else
{
stepstat.decel_val = - ((stepstat.max_s_lim*stepstat.accel)/stepstat.decel);
}
// We must decelrate at least 1 step to stop.
if(stepstat.decel_val == 0)
stepstat.decel_val = -1;
// Find step to start decleration. decel_cal is altijd negatief
stepstat.decel_start = relativesteps + stepstat.decel_val;
stepstat.step_count = 0;
}
else
{
//nieuwe target ligt in huidig decel gebied dus (of in andere richting) direct beginnen vertragen met de
stepstat.accel_count = stepstat.decel_val;
// Start decelration with same delay as accel ended with.
new_step_delay = last_accel_delay;
stepstat.run_state = DECEL;
stepstat.recalc_after_stop=TRUE;
}
}
else // CCW
{
if ((stepstat.newtargetpos-stepstat.currentpos) < stepstat.decel_val)
{
//decel_start verder leggen
//stepstat.decel_start=stepstat.decel_start-(stepstat.newtargetpos-stepstat.targetpos);
relativesteps=stepstat.currentpos-stepstat.newtargetpos;
accel_lim = ((long long)relativesteps*stepstat.decel) / (stepstat.accel+stepstat.decel); // (4000 * 180000) / (5000 + 180000 )
// Use the limit we hit first to calc decel.
if(accel_lim <= stepstat.max_s_lim)
{
stepstat.decel_val = accel_lim - relativesteps;
}
else
{
stepstat.decel_val = - ((stepstat.max_s_lim*stepstat.accel)/stepstat.decel);
}
// We must decelrate at least 1 step to stop.
if(stepstat.decel_val == 0)
stepstat.decel_val = -1;
// Find step to start decleration. decel_cal is altijd negatief
stepstat.decel_start = relativesteps + stepstat.decel_val;
stepstat.step_count = 0;
}
else
{
//we moeten omkeren want we zijn te ver om nog af te remmen
stepstat.accel_count = stepstat.decel_val;
// Start decelration with same delay as accel ended with.
new_step_delay = last_accel_delay;
stepstat.run_state = DECEL;
stepstat.recalc_after_stop=TRUE;
}
}
stepstat.targetpos=stepstat.newtargetpos;
break;
} //if (newposrequest)
// Check if we should start decelration.
if(stepstat.step_count >= stepstat.decel_start || stepstat.stopreq)
{
stepstat.stopreq=FALSE;
stepstat.accel_count = stepstat.decel_val;
// Start decelration with same delay as accel ended with.
new_step_delay = last_accel_delay;
stepstat.run_state = DECEL;
}
break;
case STOP:
// if (stepstat.dir == CCW && stepstat.backlash >0)
// {
// long step=stepstat.backlash;
//
// // identiek stuk zoals de initiele berekening.
//
// stepstat.step_count=0;
// rest = 0;
// T0TCR=TCR_STOP;
//
// stepstat.dir = CW;
// Set_Motor_Direction(stepstat.dir);
//
// stepstat.step_delay=stepstat.start_step_delay;
//
// accel_lim = ((long)step*stepstat.decel) / (stepstat.accel+stepstat.decel);
// if(accel_lim == 0)
// accel_lim = 1;
//
// if(accel_lim <= stepstat.max_s_lim)
// {
// stepstat.decel_val = accel_lim - step;
// }
// else
// {
// stepstat.decel_val = - ((stepstat.max_s_lim*stepstat.accel)/stepstat.decel);
// }
//
// if(stepstat.decel_val == 0)
// stepstat.decel_val = -1;
//
// stepstat.decel_start = step + stepstat.decel_val;
//
// if(stepstat.step_delay <= stepstat.min_delay)
// {
// stepstat.step_delay = stepstat.min_delay;
// stepstat.run_state = RUN;
// }
// else
// {
// stepstat.run_state = ACCEL;
// }
//
// // Reset counter.
// stepstat.accel_count = 0;
//
// //stepstat.running = TRUE;
// T0MR0=0; //korte tijd om timer te starten
// // Start Timer
// T0TCR=TCR_RESET; //reset
// T0TCR=TCR_RUN; //run
// VICVectAddr = 0; // Acknowledge Interrupt
// //stepstat.cmdbusy=FALSE;
// return ;
// //goto ok;
// }
// else //gewoon zonder backlash
{
if(stepstat.recalc_after_stop)
{
stepstat.recalc_after_stop=FALSE;
stepstat.step_count = 0;
rest = 0;
DoMotorRun2();
new_step_delay=stepstat.step_delay;
break;
}
else
{
if(stepstat.newposrequest)
{
stepstat.targetpos=stepstat.newtargetpos;
stepstat.newposrequest=0;
stepstat.step_count = 0;
rest = 0;
DoMotorRun2();
new_step_delay=stepstat.step_delay;
break;
}
else
{
// Stop Timer
LPC_TMR32B1->TCR=TCR_RESET; //impliceert TCR_STOP
stepstat.step_count = 0;
rest = 0;
stepstat.running = FALSE;
//clear pin en Disable Match Pin
LPC_TMR32B1->EMR=0;
}
}
}
break;
case ACCEL:
Step_Motor();
stepstat.accel_count++;
new_step_delay = stepstat.step_delay - ((long)((2 * stepstat.step_delay) + rest)/(4 * stepstat.accel_count + 1));
rest = (long)((2 * stepstat.step_delay)+rest)%(4 * stepstat.accel_count + 1);
//stepstat.step_delay=new_step_delay;
// index=stepstat.accel_count;
// if (index<LUTSIZE)
// {
// new_step_delay=lut[index];
// if (index>0)
// stepstat.step_delay=lut[index-1];
// }
if(stepstat.stopreq)
{
stepstat.stopreq=FALSE;
stepstat.accel_count =-stepstat.step_count;
stepstat.run_state = DECEL;
//last_accel_delay = stepstat.step_delay;
new_step_delay = last_accel_delay;
break;
}
if(stepstat.newposrequest)
{
stepstat.newposrequest=FALSE;
//herberekenen van decel_val bij dezlfde richting
if(stepstat.dir==CW )
{
//ligt de nieuwe positie verder ?
if (stepstat.currentpos<stepstat.newtargetpos)
{
//opnieuw berekenen decel punt en reking houden met reeds afgelegde weg
//! Number of steps before we must start deceleration (if accel does not hit max speed).
relativesteps=stepstat.newtargetpos-stepstat.currentpos;
accel_lim = ((long long)relativesteps*stepstat.decel) / (stepstat.accel+stepstat.decel); // (4000 * 180000) / (5000 + 180000 )
// We must accelrate at least 1 step before we can start deceleration.