2 #include <stepper-interrupt.h>
3 #include <stepper-move.h>
7 #define PRINTF(...) printf(__VA_ARGS__)
9 #define PRINTF(...) do {} while (0)
13 isqrt(
unsigned long x)
16 unsigned int b2 = 0x40000000;
17 unsigned int b = 0x8000;
36 #define ACC_FIRST_UP 0
40 #define ACC_FIRST_DOWN 4
42 #define ACC_LAST_DOWN 6
45 typedef struct _AccDiff AccDiff;
54 base_acc(
unsigned long t,
unsigned long n,
unsigned long l,
unsigned long a_max)
69 static AccDiff acc[ACC_END+1];
71 stepper_move(
unsigned int stepper_index,
unsigned long *periodp,
72 unsigned long a_max,
unsigned long v_max,
long s_end)
74 unsigned long start_period = *periodp;
80 unsigned long n = (v_max+a_max-1)/a_max;
81 unsigned long a_speed_adj = v_max - (n-1)*a_max;
89 d = s_res - (long)a_max*(n*n-1) - (long)a_speed_adj;
91 acc[ACC_END].diff = 0;
92 acc[ACC_END].pos = UINT_MAX;
95 n = isqrt(s_res/a_max);
96 if (n*(
unsigned long long)n*a_max < s_res) n++;
98 acc[ACC_LAST_UP].diff=0;
99 acc[ACC_FIRST_DOWN].diff=0;
101 l = (d+v_max-1)/v_max;
102 acc[ACC_LAST_UP].diff= a_speed_adj - a_max;
103 acc[ACC_FIRST_DOWN].diff= a_max - a_speed_adj;
105 acc[ACC_LAST_UP].pos = n-1;
106 acc[ACC_FIRST_DOWN].pos = n+l;
108 s = a_max*(n*n-1) + a_speed_adj + l * (a_max*(n-1) + a_speed_adj);
112 acc[ACC_FIRST_UP].diff = -da0;
113 acc[ACC_LAST_DOWN].diff = da0;
114 acc[ACC_FIRST_UP].pos = 0;
115 acc[ACC_LAST_DOWN].pos = 2*n+l-1;
118 acc[ACC_K1_UP].diff = 0;
119 acc[ACC_K1_DOWN].diff = 0;
120 acc[ACC_K1_UP].pos = 0;
121 acc[ACC_K1_DOWN].pos = 2*n+l-1;
123 acc[ACC_TOP].diff = 0;
124 acc[ACC_TOP].pos = n;
130 acc[ACC_K1_UP].diff = -1;
131 acc[ACC_K1_DOWN].diff = 1;
132 acc[ACC_K1_UP].pos = k1;
133 acc[ACC_K1_DOWN].pos = 2*n+l-1 - k1;
134 ds -= (2*(n-k1)+l-1);
137 acc[ACC_LAST_UP].diff--;
138 acc[ACC_TOP].diff = 1;
139 acc[ACC_TOP].pos = n+ds-1;
145 PRINTF(
"n=%ld l=%ld a_max=%ld v_max=%ld s_res=%ld\n",
146 n,l ,a_max, v_max, s_res);
147 for (k = 0; k < 7; k++) {
148 PRINTF(
" %ld@%ld", acc[k].diff, acc[k].pos);
158 long a_prev = ULONG_MAX;
159 for (k = 0; k < ACC_END; k++) {
162 if (acc[k].pos != acc[k+1].pos) {
163 if (t != acc[k].pos) {
164 a = base_acc(t,n,l,a_max);
165 if (s_end < 0) a = -a;
167 res = stepper_add_acc(stepper_index, t+start_period, a);
168 if (res != STEPPER_OK)
return res;
169 PRINTF(
"%d: %ld@%ld\n", stepper_index, a, t+start_period);
174 a = da + base_acc(t,n,l,a_max);
175 if (s_end < 0) a = -a;
177 res = stepper_add_acc(stepper_index, t+start_period, a);
178 if (res != STEPPER_OK)
return res;
179 PRINTF(
"%d: %ld@%ld\n", stepper_index, a, t+start_period);
186 res = stepper_add_acc(stepper_index, t+start_period, 0);
187 PRINTF(
"%d: %d@%ld\n", stepper_index, 0, t+start_period);
188 if (res != STEPPER_OK)
return res;