6
6
#endif
7
7
8
8
// As in StepperDemo for Motor 1 on AVR
9
- #define dirPinStepperX 5
9
+ #define dirPinStepperX 5
10
10
#define enablePinStepperX 6
11
- #define stepPinStepperX stepPinStepper1A
11
+ #define stepPinStepperX stepPinStepper1A
12
12
13
13
// As in StepperDemo for Motor 2 on AVR
14
- #define dirPinStepperY 7
14
+ #define dirPinStepperY 7
15
15
#define enablePinStepperY 8
16
- #define stepPinStepperY stepPinStepper1B
16
+ #define stepPinStepperY stepPinStepper1B
17
17
18
18
FastAccelStepperEngine engine = FastAccelStepperEngine();
19
19
FastAccelStepper *stepperX = NULL ;
@@ -24,22 +24,25 @@ struct control_s {
24
24
uint16_t phi;
25
25
uint16_t drift; // time delta in ticks
26
26
};
27
- struct control_s controlX = {
28
- .time = 0 , .phi = 0 , .drift = 0
29
- };
30
- struct control_s controlY = {
31
- .time = 0 , .phi = 90 , .drift = 0
32
- };
27
+ struct control_s controlX = {.time = 0 , .phi = 0 , .drift = 0 };
28
+ struct control_s controlY = {.time = 0 , .phi = 90 , .drift = 0 };
33
29
34
30
// This means, the circle is executed in 360*4ms = 1440ms
35
31
#ifndef TIMESTEP_TICKS
36
- #define TIMESTEP_TICKS (TICKS_PER_S/ 250 )
32
+ #define TIMESTEP_TICKS (TICKS_PER_S / 250 )
37
33
#endif
38
34
39
35
// Table generated by python:
40
36
// [int(math.sin(x*math.pi/180)*1600) for x in range(0,91)]
41
- int16_t steps[91 ] = { 0 , 27 , 55 , 83 , 111 , 139 , 167 , 194 , 222 , 250 , 277 , 305 , 332 , 359 , 387 , 414 , 441 , 467 , 494 , 520 , 547 , 573 , 599 , 625 , 650 , 676 , 701 , 726 , 751 , 775 , 799 , 824 , 847 , 871 , 894 , 917 , 940 , 962 , 985 , 1006 , 1028 , 1049 , 1070 , 1091 , 1111 , 1131 , 1150 , 1170 , 1189 , 1207 , 1225 , 1243 , 1260 , 1277 , 1294 , 1310 , 1326 , 1341 , 1356 , 1371 , 1385 , 1399 , 1412 , 1425 , 1438 , 1450 , 1461 , 1472 , 1483 , 1493 , 1503 , 1512 , 1521 , 1530 , 1538 , 1545 , 1552 , 1558 , 1565 , 1570 , 1575 , 1580 , 1584 , 1588 , 1591 , 1593 , 1596 , 1597 , 1599 , 1599 , 1600
42
- };
37
+ int16_t steps[91 ] = {
38
+ 0 , 27 , 55 , 83 , 111 , 139 , 167 , 194 , 222 , 250 , 277 , 305 ,
39
+ 332 , 359 , 387 , 414 , 441 , 467 , 494 , 520 , 547 , 573 , 599 , 625 ,
40
+ 650 , 676 , 701 , 726 , 751 , 775 , 799 , 824 , 847 , 871 , 894 , 917 ,
41
+ 940 , 962 , 985 , 1006 , 1028 , 1049 , 1070 , 1091 , 1111 , 1131 , 1150 , 1170 ,
42
+ 1189 , 1207 , 1225 , 1243 , 1260 , 1277 , 1294 , 1310 , 1326 , 1341 , 1356 , 1371 ,
43
+ 1385 , 1399 , 1412 , 1425 , 1438 , 1450 , 1461 , 1472 , 1483 , 1493 , 1503 , 1512 ,
44
+ 1521 , 1530 , 1538 , 1545 , 1552 , 1558 , 1565 , 1570 , 1575 , 1580 , 1584 , 1588 ,
45
+ 1591 , 1593 , 1596 , 1597 , 1599 , 1599 , 1600 };
43
46
44
47
void setup () {
45
48
Serial.begin (115200 );
@@ -48,8 +51,8 @@ void setup() {
48
51
stepperX = engine.stepperConnectToPin (stepPinStepperX);
49
52
stepperY = engine.stepperConnectToPin (stepPinStepperY);
50
53
if (!stepperX || !stepperY) {
51
- while ( 0 == 0 ) {
52
- Serial.println (" Cannot initialize steppers" );
54
+ while ( 0 == 0 ) {
55
+ Serial.println (" Cannot initialize steppers" );
53
56
}
54
57
}
55
58
stepperX->setDirectionPin (dirPinStepperX);
@@ -66,61 +69,61 @@ void setup() {
66
69
stepperY->enableOutputs ();
67
70
68
71
// Fill the queue with 1ms delay and start the queues.
69
- stepperX->moveTimed (0 ,TICKS_PER_S/ 1000 ,NULL ,false );
70
- stepperY->moveTimed (0 ,TICKS_PER_S/ 1000 ,NULL ,false );
72
+ stepperX->moveTimed (0 , TICKS_PER_S / 1000 , NULL , false );
73
+ stepperY->moveTimed (0 , TICKS_PER_S / 1000 , NULL , false );
71
74
72
75
// Start the queues. Currently best but not perfect synchronicity
73
- stepperX->moveTimed (0 ,0 , NULL ,true );
74
- stepperY->moveTimed (0 ,0 , NULL ,true );
76
+ stepperX->moveTimed (0 , 0 , NULL , true );
77
+ stepperY->moveTimed (0 , 0 , NULL , true );
75
78
}
76
79
77
80
void moveStepper (FastAccelStepper *stepper, struct control_s *control) {
78
- uint16_t phi = control->phi % 360 ;
79
-
80
- // The table contains only one quadrant
81
- uint16_t index = phi;
82
- bool negate = false ;
83
- if (index > 180 ) {
84
- negate = true ;
85
- index = 360 - index ;
86
- }
87
- if (index > 90 ) {
88
- index = 180 - index ;
89
- }
90
- int16_t position = steps[index ];
91
- if (negate) {
92
- position = -position;
93
- }
94
- int32_t current_position = stepper->getPositionAfterCommandsCompleted ();
95
- int32_t steps = position - current_position;
96
- uint32_t actual;
97
- int8_t rc;
98
- uint32_t duration = TIMESTEP_TICKS + control->drift ;
99
- rc = stepper->moveTimed (steps,duration,&actual,true );
100
- switch (rc) {
101
- case MOVE_TIMED_EMPTY:
102
- Serial.print (" Empty:" );
103
- Serial.println (stepper->getStepPin () == dirPinStepperX ? ' X' : ' Y' );
104
- /* fallthrough */
105
- case MOVE_TIMED_OK:
106
- control->drift = duration- actual;
107
- control->time += actual;
108
- control->phi += 1 ;
109
- break ;
110
- case MOVE_TIMED_BUSY:
111
- // Serial.println("Busy");
112
- break ;
113
- case MOVE_TIMED_TOO_LARGE_ERROR:
114
- Serial.println (" Too large" );
115
- break ;
116
- case AQE_ERROR_TICKS_TOO_LOW:
117
- Serial.print (" Ticks too low:" );
118
- Serial.print (duration/ steps);
119
- Serial.print (" steps:" );
120
- Serial.println (steps);
121
- default :
122
- Serial.println (rc);
123
- }
81
+ uint16_t phi = control->phi % 360 ;
82
+
83
+ // The table contains only one quadrant
84
+ uint16_t index = phi;
85
+ bool negate = false ;
86
+ if (index > 180 ) {
87
+ negate = true ;
88
+ index = 360 - index ;
89
+ }
90
+ if (index > 90 ) {
91
+ index = 180 - index ;
92
+ }
93
+ int16_t position = steps[index ];
94
+ if (negate) {
95
+ position = -position;
96
+ }
97
+ int32_t current_position = stepper->getPositionAfterCommandsCompleted ();
98
+ int32_t steps = position - current_position;
99
+ uint32_t actual;
100
+ int8_t rc;
101
+ uint32_t duration = TIMESTEP_TICKS + control->drift ;
102
+ rc = stepper->moveTimed (steps, duration, &actual, true );
103
+ switch (rc) {
104
+ case MOVE_TIMED_EMPTY:
105
+ Serial.print (" Empty:" );
106
+ Serial.println (stepper->getStepPin () == dirPinStepperX ? ' X' : ' Y' );
107
+ /* fallthrough */
108
+ case MOVE_TIMED_OK:
109
+ control->drift = duration - actual;
110
+ control->time += actual;
111
+ control->phi += 1 ;
112
+ break ;
113
+ case MOVE_TIMED_BUSY:
114
+ // Serial.println("Busy");
115
+ break ;
116
+ case MOVE_TIMED_TOO_LARGE_ERROR:
117
+ Serial.println (" Too large" );
118
+ break ;
119
+ case AQE_ERROR_TICKS_TOO_LOW:
120
+ Serial.print (" Ticks too low:" );
121
+ Serial.print (duration / steps);
122
+ Serial.print (" steps:" );
123
+ Serial.println (steps);
124
+ default :
125
+ Serial.println (rc);
126
+ }
124
127
}
125
128
126
129
uint32_t last_millis = 0 ;
@@ -141,12 +144,11 @@ void loop() {
141
144
142
145
if (controlX.time < controlY.time ) {
143
146
moveStepper (stepperX, &controlX);
144
- }
145
- else {
147
+ } else {
146
148
moveStepper (stepperY, &controlY);
147
149
}
148
150
if ((controlX.phi != 361 ) && (controlY.phi != 451 )) {
149
- return ;
151
+ return ;
150
152
}
151
153
// Full round should be completed
152
154
#ifdef SIMULATOR
0 commit comments