This repository has been archived by the owner on Jun 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Drivers.h
268 lines (257 loc) · 8 KB
/
Drivers.h
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
#ifndef DRIVERS_H_INCLUDED
#define DRIVERS_H_INCLUDED
/**
For some special printers you need to control extra motors. Possible reasons are
- Extruder switches
- Clearing surface
- Leveling
Repetier-Firmware supports up to 4 extra motors that can be controlled by
G201 P<motorId> X<pos> - Go to position X with motor motorId
G202 P<motorId> X<setpos> - Mark current position as X
G203 P<motorId> - Report current motor position
G204 P<motorId> S<0/1> - Enable/disable motor
G205 P<motorId> S<0/1> E<0/1> - Home motor, S1 = go back to stored position, E1 = home only if endstop was never met, meaning it was never homed with motor.
These motors are already special and there might be different types, so we can not assume
one class fits all needs. So to keep it simple, the firmware defines this general
interface which a motor must implement. That way we can handle any type without changing
the main code.
*/
class MotorDriverInterface
{
public:
virtual void initialize() = 0;
virtual float getPosition() = 0;
virtual void setCurrentAs(float newPos) = 0;
virtual void gotoPosition(float newPos) = 0;
virtual void enable() = 0;
virtual void disable() = 0;
virtual void home(bool goToCurrent, bool onlyIfNotHomed) = 0;
};
/**
Simple class to drive a stepper motor with fixed speed.
*/
template<int stepPin, int dirPin, int enablePin,bool invertDir, bool invertEnable>
class StepperDriver : public MotorDriverInterface
{
int32_t position;
int32_t delayUS;
float stepsPerMM;
public:
StepperDriver(float _stepsPerMM,float speed)
{
stepsPerMM = _stepsPerMM;
position = 0;
delayUS = 500000 / (speed * stepsPerMM);
}
void initialize() {
HAL::pinMode(enablePin, OUTPUT);
HAL::pinMode(stepPin, OUTPUT);
HAL::pinMode(dirPin, OUTPUT);
HAL::digitalWrite(enablePin, !invertEnable);
}
float getPosition()
{
return position / stepsPerMM;
}
void setCurrentAs(float newPos)
{
position = floor(newPos * stepsPerMM + 0.5f);
}
void gotoPosition(float newPos)
{
enable();
int32_t target = floor(newPos * stepsPerMM + 0.5f) - position;
position += target;
if(target > 0) {
HAL::digitalWrite(dirPin, !invertDir);
} else {
target = -target;
HAL::digitalWrite(dirPin, invertDir);
}
while(target) {
HAL::digitalWrite(stepPin, HIGH);
HAL::delayMicroseconds(delayUS);
HAL::digitalWrite(stepPin, LOW);
HAL::delayMicroseconds(delayUS);
target--;
HAL::pingWatchdog();
if((target & 127) == 0) {
Commands::checkForPeriodicalActions(false);
GCode::keepAlive(Processing);
}
}
}
void enable()
{
HAL::digitalWrite(enablePin, invertEnable);
}
void disable()
{
HAL::digitalWrite(enablePin, !invertEnable);
}
void home(bool goToCurrent, bool onlyIfNotHomed) {}
};
/**
Simple class to drive a stepper motor with fixed speed with additional endstop.
Min position is 0 and max. position maxDistance.
*/
template<int stepPin, int dirPin, int enablePin,bool invertDir, bool invertEnable, int endstopPin, bool invertEndstop, bool minEndstop, bool endstopPullup>
class StepperDriverWithEndstop : public MotorDriverInterface
{
int32_t position;
int32_t delayUS;
float stepsPerMM;
float maxDistance;
bool isHomed;
public:
StepperDriverWithEndstop(float _stepsPerMM,float speed,float maxDist)
{
stepsPerMM = _stepsPerMM;
maxDistance = maxDist;
isHomed = false;
position = 0;
delayUS = 500000 / (speed * stepsPerMM);
}
void initialize() {
HAL::pinMode(enablePin, OUTPUT);
HAL::pinMode(stepPin, OUTPUT);
HAL::pinMode(dirPin, OUTPUT);
HAL::pinMode(endstopPin, endstopPullup ? INPUT_PULLUP : INPUT);
HAL::digitalWrite(enablePin, !invertEnable);
}
bool endstopHit() {
return invertEndstop ? !HAL::digitalRead(endstopPin) : HAL::digitalRead(endstopPin);
}
float getPosition()
{
return position / stepsPerMM;
}
void setCurrentAs(float newPos)
{
position = floor(newPos * stepsPerMM + 0.5f);
}
void gotoPosition(float newPos)
{
bool up = true;
if(newPos < 0)
newPos = 0;
if(newPos > maxDistance)
newPos = maxDistance;
enable();
int32_t target = floor(newPos * stepsPerMM + 0.5f) - position;
position += target;
if(target > 0) {
HAL::digitalWrite(dirPin, !invertDir);
} else {
target = -target;
up = false;
HAL::digitalWrite(dirPin, invertDir);
}
while(target) {
HAL::digitalWrite(stepPin, HIGH);
HAL::delayMicroseconds(delayUS);
HAL::digitalWrite(stepPin, LOW);
HAL::delayMicroseconds(delayUS);
target--;
HAL::pingWatchdog();
if((target & 127) == 0) {
Commands::checkForPeriodicalActions(false);
GCode::keepAlive(Processing);
}
if(up != minEndstop) {
if(endstopHit()) {
isHomed = true;
if(minEndstop)
position = 0;
else
setCurrentAs(maxDistance);
break;
}
}
}
}
void home(bool goToCurrent, bool onlyIfNotHomed) {
if(onlyIfNotHomed && isHomed)
return;
float origPosition = getPosition();
if(minEndstop) {
setCurrentAs(maxDistance);
gotoPosition(0);
} else {
position = 0;
gotoPosition(maxDistance);
}
if(goToCurrent)
gotoPosition(origPosition);
}
void enable()
{
HAL::digitalWrite(enablePin, invertEnable);
}
void disable()
{
HAL::digitalWrite(enablePin, !invertEnable);
}
};
#if defined(NUM_MOTOR_DRIVERS) && NUM_MOTOR_DRIVERS > 0
class GCode;
extern void commandG201(GCode &code);
extern void commandG202(GCode &code);
extern void commandG203(GCode &code);
extern void commandG204(GCode &code);
extern void commandG205(GCode &code);
extern void disableAllMotorDrivers();
extern MotorDriverInterface *getMotorDriver(int idx);
extern void initializeAllMotorDrivers();
#endif
#if defined(SUPPORT_LASER) && SUPPORT_LASER
/**
With laser support you can exchange a extruder by a laser. A laser gets controlled by a digital pin.
By default all intensities > 200 are always on, and lower values are always off. You can overwrite
this with a programmed event EVENT_SET_LASER(intensity) that return false to signal the default
implementation that it has set it's value already.
EVENT_INITIALIZE_LASER should return false to prevent default initialization.
*/
class LaserDriver {
public:
static secondspeed_t intensity; // Intensity to use for next move queued. This is NOT the current value!
static secondspeed_t intens;
static bool laserOn; // Enabled by M3?
static bool firstMove ;
static void initialize();
static void changeIntensity(secondspeed_t newIntensity);
};
#endif
#if defined(SUPPORT_CNC) && SUPPORT_CNC
/**
The CNC driver differs a bit from laser driver. Here only M3,M4,M5 have an influence on the spindle.
The motor also keeps running for G0 moves. M3 and M4 wait for old moves to be finished and then enables
the motor. It then waits CNC_WAIT_ON_ENABLE milliseconds for the spindle to reach target speed.
*/
class CNCDriver {
public:
static int8_t direction;
static secondspeed_t spindleSpeed;
static uint16_t spindleRpm;
/** Initialize cnc pins. EVENT_INITIALIZE_CNC should return false to prevent default initialization.*/
static void initialize();
/** Turns off spindle. For event override implement
EVENT_SPINDLE_OFF
returning false.
*/
static void spindleOff();
/** Turns spindle on. Default implementation uses a enable pin CNC_ENABLE_PIN. If
CNC_DIRECTION_PIN is not -1 it sets direction to CNC_DIRECTION_CW. rpm is ignored.
To override with event system, return false for the event
EVENT_SPINDLE_CW(rpm)
*/
static void spindleOnCW(int32_t rpm);
/** Turns spindle on. Default implementation uses a enable pin CNC_ENABLE_PIN. If
CNC_DIRECTION_PIN is not -1 it sets direction to !CNC_DIRECTION_CW. rpm is ignored.
To override with event system, return false for the event
EVENT_SPINDLE_CCW(rpm)
*/
static void spindleOnCCW(int32_t rpm);
};
#endif
#endif // DRIVERS_H_INCLUDED