-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStepperDriver.cpp
More file actions
231 lines (188 loc) · 5.57 KB
/
StepperDriver.cpp
File metadata and controls
231 lines (188 loc) · 5.57 KB
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
#include "StepperDriver.h"
#include <avr/interrupt.h>
#include <util/atomic.h>
static volatile struct stepper_motor _motors[NUM_AXIS];
static volatile uint8_t _num_motors = 0;
void _StepperDriver::init()
{
/* Configure timer */
/* Timer2, prescaler /1, enable overflow interrupt */
TCCR2A = 0; /* huh, we need to disable all PWMs */
TCCR2B = (1<<CS20); /* prescaler */
TIMSK2 = (1<<TOIE2); /* overflow interrupt */
}
axis_t _StepperDriver::newAxis(uint8_t step, uint8_t dir, uint8_t enable, uint16_t steps)
{
/* Library is limited in number of axis.
* If you need to control more axis, change NUM_AXIS
* in header file, but be careful! You are still limited
* in memory! */
if (_num_motors == NUM_AXIS)
return 255;
_motors[_num_motors].step = step;
_motors[_num_motors].dir = dir;
_motors[_num_motors].enable = enable;
_motors[_num_motors].steps = steps;
pinMode(step, OUTPUT);
pinMode(dir, OUTPUT);
if (enable != 255)
pinMode(enable, OUTPUT);
return _num_motors++;
}
axis_t _StepperDriver::newAxis(uint8_t step, uint8_t dir, uint16_t steps)
{
return newAxis(step, dir, 255, steps); /* auto-ignoring ENABLE pin feature */
}
void _StepperDriver::enable(axis_t axis)
{
if (axis >= _num_motors)
return;
if (_motors[axis].enable != 255)
#ifdef INVERSE_ENABLE_LEVELS
digitalWrite(_motors[axis].enable, HIGH);
#else
digitalWrite(_motors[axis].enable, LOW);
#endif
}
void _StepperDriver::disable(axis_t axis)
{
if (axis >= _num_motors)
return;
if (_motors[axis].enable != 255)
#ifdef INVERSE_ENABLE_LEVELS
digitalWrite(_motors[axis].enable, LOW);
#else
digitalWrite(_motors[axis].enable, HIGH);
#endif
}
void _StepperDriver::setDir(axis_t axis, uint8_t dir)
{
if (axis >= _num_motors)
return;
digitalWrite(_motors[axis].dir, dir);
_motors[axis]._dir = dir == FORWARD ? 1 : -1;
}
void _StepperDriver::setDelay(axis_t axis, uint16_t delay)
{
if (axis >= _num_motors)
return;
if (delay != 0 && delay < 16)
delay = 16;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
_motors[axis]._base_delay = delay / 16; /* this division is for conversion to microseconds */
_motors[axis]._rq_path = 0;
}
}
void _StepperDriver::setSpeed(axis_t axis, uint16_t value)
{
if (axis >= _num_motors)
return;
if (value != 0)
value = 60000000 / _motors[axis].steps / value;
setDelay(axis, value);
}
void _StepperDriver::write(axis_t axis, int32_t value)
{
if (axis >= _num_motors)
return;
if (value < 0) {
value = -value;
setDir(axis, BACKWARD);
} else {
setDir(axis, FORWARD);
}
setSpeed(axis, (uint16_t) value);
}
void _StepperDriver::write(axis_t axis, int32_t speed, uint32_t path)
{
if (axis >= _num_motors)
return;
write(axis, speed);
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
_motors[axis]._rq_path = path;
}
}
void _StepperDriver::stop(axis_t axis)
{
write(axis, 0);
}
uint8_t _StepperDriver::busy(axis_t axis)
{
if (axis >= _num_motors)
return 0;
uint8_t ret;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
ret = _motors[axis]._base_delay > 0;
}
return ret;
}
void _StepperDriver::wait(axis_t axis)
{
if (axis >= _num_motors || getPath(axis) == 0)
return;
while (busy(axis))
asm volatile ("nop");
}
void _StepperDriver::move(axis_t axis, int32_t speed, uint32_t path)
{
write(axis, speed, path);
wait(axis);
}
int32_t _StepperDriver::getPath(axis_t axis)
{
if (axis >= _num_motors)
return 0;
int32_t val;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
val = _motors[axis].path;
}
return val;
}
void _StepperDriver::resetPath(axis_t axis)
{
if (axis >= _num_motors)
return;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
_motors[axis].path = 0;
}
}
_StepperDriver StepperDriver;
/*
* Special interrupt service functions
*/
static inline void toggle(volatile struct stepper_motor *s)
{
if (s->_base_delay) {
if (s->_state)
digitalWrite(s->step, HIGH);
else
digitalWrite(s->step, LOW);
s->path += s->_dir;
s->_state = !(s->_state);
if (s->_rq_path > 0) { /* path limit detector */
s->_rq_path--;
if (s->_rq_path == 0)
s->_base_delay = 0;
}
}
}
static inline void timer_interrupt()
{
for (uint8_t i=0; i<_num_motors; i++) {
if (_motors[i]._delay != 0) {
_motors[i]._delay--;
} else {
toggle(&_motors[i]);
_motors[i]._delay = _motors[i]._base_delay;
}
}
}
ISR(TIMER2_OVF_vect)
{
timer_interrupt();
}