-
Notifications
You must be signed in to change notification settings - Fork 0
/
StepperController.cpp
308 lines (263 loc) · 6.65 KB
/
StepperController.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
#include <inttypes.h>
#include "StepperController.h"
#include "config.h"
#include "Timer.h"
#include "utils.h"
#define STEPPER_HZ 100000
#define MAX(x,y) x > y ? x : y
#define MIN(x,y) x < y ? x : y
#define ABS(x) (x < 0 ? -x : x)
StepperController stepperController;
namespace {
struct STEPPER {
const volatile uint8_t * stepPort;
const volatile uint8_t stepPin;
const volatile uint8_t * dirPort;
const volatile uint8_t dirPin;
const volatile uint8_t * enPort;
const volatile uint8_t enPin;
const volatile uint8_t * minPort;
const volatile uint8_t minPin;
const volatile uint8_t * maxPort;
const volatile uint8_t maxPin;
bool bInverted;
volatile bool bDirectionPositive;
volatile int32_t currentStep;
volatile int32_t targetStep;
volatile int32_t deltaSteps;
volatile uint32_t stepDelay;
volatile uint32_t currentStepTime;
};
volatile STEPPER steppers[3] = {
{
&STEPPERX_STEP_PORT,_BV(STEPPERX_STEP_PIN),
&STEPPERX_DIR_PORT,_BV(STEPPERX_DIR_PIN),
&STEPPERX_EN_PORT,_BV(STEPPERX_EN_PIN),
&STEPPERX_MIN_PORT,_BV(STEPPERX_MIN_PIN),
&STEPPERX_MAX_PORT,_BV(STEPPERX_MAX_PIN),
false,
false,
0,
0,
0,
0,
0,
},
{
&STEPPERY_STEP_PORT,_BV(STEPPERY_STEP_PIN),
&STEPPERY_DIR_PORT,_BV(STEPPERY_DIR_PIN),
&STEPPERY_EN_PORT,_BV(STEPPERY_EN_PIN),
&STEPPERY_MIN_PORT,_BV(STEPPERY_MIN_PIN),
&STEPPERY_MAX_PORT,_BV(STEPPERY_MAX_PIN),
false,
false,
0,
0,
0,
0,
0,
},
{
&STEPPERZ_DIR_PORT,_BV(STEPPERZ_DIR_PIN),
&STEPPERZ_EN_PORT,_BV(STEPPERZ_EN_PIN),
&STEPPERZ_MIN_PORT,_BV(STEPPERZ_MIN_PIN),
&STEPPERZ_MAX_PORT,_BV(STEPPERZ_MAX_PIN),
false,
false,
0,
0,
0,
0,
0,
},
};
/*
volatile Stepper stepperX(&STEPPERX_STEP_PORT,_BV(STEPPERX_STEP_PIN),
&STEPPERX_DIR_PORT,_BV(STEPPERX_DIR_PIN),
&STEPPERX_EN_PORT,_BV(STEPPERX_EN_PIN),
&STEPPERX_MIN_PORT,_BV(STEPPERX_MIN_PIN),
&STEPPERX_MAX_PORT,_BV(STEPPERX_MAX_PIN));
volatile Stepper stepperY(&STEPPERY_STEP_PORT,_BV(STEPPERY_STEP_PIN),
&STEPPERY_DIR_PORT,_BV(STEPPERY_DIR_PIN),
&STEPPERY_EN_PORT,_BV(STEPPERY_EN_PIN),
&STEPPERY_MIN_PORT,_BV(STEPPERY_MIN_PIN),
&STEPPERY_MAX_PORT,_BV(STEPPERY_MAX_PIN));
volatile Stepper stepperZ(&STEPPERZ_STEP_PORT,_BV(STEPPERZ_STEP_PIN),
&STEPPERZ_DIR_PORT,_BV(STEPPERZ_DIR_PIN),
&STEPPERZ_EN_PORT,_BV(STEPPERZ_EN_PIN),
&STEPPERZ_MIN_PORT,_BV(STEPPERZ_MIN_PIN),
&STEPPERZ_MAX_PORT,_BV(STEPPERZ_MAX_PIN));
Stepper* steppers[3] = { (Stepper*)&stepperX, (Stepper*)&stepperY, (Stepper*)&stepperZ };
*/
#ifdef STEPPER_TIMER_8BIT
Timer8 timer(STEPPER_TIMER,STEPPER_HZ);
#else
Timer timer(STEPPER_TIMER,STEPPER_HZ);
#endif
}
StepperController::StepperController()
{
timer.setCallback(timerCallback);
}
void StepperController::getPoint(Point& rPoint)
{
rPoint.x = steppers[0].currentStep;
rPoint.y = steppers[1].currentStep;
rPoint.z = steppers[2].currentStep;
}
bool StepperController::isMoving()
{
bool ret = false;
for (int i=0;i<3;i++)
ret |= steppers[i].currentStep != steppers[i].targetStep;
return ret;
}
void StepperController::disableSteppers(bool x,bool y,bool z)
{
if (x)
PORTP(steppers[0].enPort) &= ~steppers[0].enPin;
if (y)
PORTP(steppers[1].enPort) &= ~steppers[1].enPin;
if (z)
PORTP(steppers[2].enPort) &= ~steppers[2].enPin;
}
void StepperController::enableSteppers(bool x, bool y, bool z)
{
if (x)
PORTP(steppers[0].enPort) |= steppers[0].enPin;
if (y)
PORTP(steppers[1].enPort) |= steppers[1].enPin;
if (z)
PORTP(steppers[2].enPort) |= steppers[2].enPin;
}
void StepperController::setPoint(int32_t x,int32_t y,int32_t z)
{
steppers[0].currentStep=x;
steppers[1].currentStep=y;
steppers[2].currentStep=z;
}
void StepperController::moveTo(int32_t x,int32_t y,int32_t z,int32_t feedRate)
{
uint32_t moveTime;
uint8_t i;
for (i=0;i<3;i++)
{
steppers[i].targetStep = x;
int32_t deltaSteps = x - steppers[i].currentStep;
steppers[i].bDirectionPositive = deltaSteps >= 0;
steppers[i].deltaSteps = ABS(deltaSteps);
if (steppers[i].deltaSteps > 0)
{
PORTP(steppers[i].enPort) |= steppers[i].enPin;
if (steppers[i].bDirectionPositive)
PORTP(steppers[i].dirPort) |= steppers[i].dirPin;
else
PORTP(steppers[i].dirPort) &= ~steppers[i].dirPin;
}
moveTime = MAX(moveTime,steppers[i].deltaSteps / feedRate);
}
for (i=0;i<3;i++)
{
if (steppers[i].deltaSteps > 0)
{
steppers[i].stepDelay = STEPPER_HZ / (steppers[i].deltaSteps / moveTime);
steppers[i].currentStepTime = steppers[i].stepDelay;
}
}
mbMoving = isMoving();
if (mbMoving)
timer.enable();
}
void StepperController::update()
{
if (mbMoving)
{
#ifdef STEPPERX_DISABLE_INACTIVE
if (!steppers[0].deltaSteps)
PORTP(steppers[0].enPort) &= ~steppers[0].enPin;
#endif
#ifdef STEPPERY_DISABLE_INACTIVE
if (!steppers[1].deltaSteps)
PORTP(steppers[1].enPort) &= ~steppers[1].enPin;
#endif
#ifdef STEPPERZ_DISABLE_INACTIVE
if (!steppers[2].deltaSteps)
PORTP(steppers[2].enPort) &= ~steppers[2].enPin;
#endif
mbMoving = isMoving();
if (!mbMoving)
timer.disable();
}
}
void StepperController::doISR()
{
//HACK: move z axis first if it needs to be moved to the head does not try to go through the object
//essentially the host software should split the move into two commands, move z first and move xy after
if (steppers[3].deltaSteps > 0)
{
steppers[3].currentStepTime--;
if (!steppers[3].currentStepTime)
{
PORTP(steppers[3].stepPort) |= steppers[3].stepPin;
PORTP(steppers[3].stepPort) &= ~steppers[3].stepPin;
steppers[3].currentStepTime = steppers[3].stepDelay;
steppers[3].deltaSteps--;
steppers[3].currentStep += steppers[3].bDirectionPositive ? 1 : -1;
}
}
else
{
for (int i=0;i<2;i++)
{
steppers[i].currentStepTime--;
if (!steppers[i].currentStepTime)
{
PORTP(steppers[i].stepPort) |= steppers[i].stepPin;
PORTP(steppers[i].stepPort) &= ~steppers[i].stepPin;
steppers[i].currentStepTime = steppers[i].stepDelay;
steppers[i].deltaSteps--;
steppers[i].currentStep += steppers[i].bDirectionPositive ? 1 : -1;
}
}
}
/*
if (steppers[3]->needStepping())
{
currentStepTime[3]--;
if (currentStepTime[3] == 0)
{
steppers[3]->doStep();
currentStepTime[3] = stepDelay[3];
}
}
else
{
for (int i=0;i<2;i++)
{
if (steppers[i]->needStepping())
{
currentStepTime[i]--;
if (currentStepTime[i] == 0)
{
steppers[i]->doStep();
currentStepTime[i] = stepDelay[i];
}
}
}
}
*/
#if 0
for (uint8_t i=0;i<3;i++)
{
if (steppers[i].needStepping())
{
currentStepTime[i]--;
if (currentStepTime[i] == 0)
{
steppers[i].doStep();
currentStepTime[i] = stepDelay[i];
}
}
}
#endif
}