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
308
309
310
311
312
313
|
#include "configuration.h"
#include "pins.h"
#include "ThermistorTable.h"
#include "intercom.h"
#include "extruder.h"
// Keep all extruders up to temperature etc.
void manageAllExtruders()
{
for(byte i = 0; i < EXTRUDER_COUNT; i++)
ex[i]->manage();
}
// Select a new extruder
void newExtruder(byte e)
{
if(e < 0)
e = 0;
if(e >= EXTRUDER_COUNT)
e = EXTRUDER_COUNT - 1;
if(e != extruder_in_use)
{
extruder_in_use = e;
//setExtruder();
}
}
/***************************************************************************************************************************
If we have a new motherboard (V 1.x, x >= 1), the extruder is entirely controlled via the RS485, and all the functions to do
it are simple inlines in extruder.h
Otherwise, we have to do the work ourselves...
*/
#if MOTHERBOARD < 2
extruder::extruder(byte md_pin, byte ms_pin, byte h_pin, byte f_pin, byte t_pin, byte vd_pin, byte ve_pin, byte se_pin)
{
motor_dir_pin = md_pin;
motor_speed_pin = ms_pin;
heater_pin = h_pin;
fan_pin = f_pin;
temp_pin = t_pin;
valve_dir_pin = vd_pin;
valve_en_pin = ve_pin;
step_en_pin = se_pin;
//setup our pins
pinMode(motor_dir_pin, OUTPUT);
pinMode(motor_speed_pin, OUTPUT);
pinMode(heater_pin, OUTPUT);
pinMode(temp_pin, INPUT);
pinMode(valve_dir_pin, OUTPUT);
pinMode(valve_en_pin, OUTPUT);
//initialize values
digitalWrite(motor_dir_pin, EXTRUDER_FORWARD);
analogWrite(heater_pin, 0);
analogWrite(motor_speed_pin, 0);
digitalWrite(valve_dir_pin, false);
digitalWrite(valve_en_pin, 0);
// The step enable pin and the fan pin are the same...
// We can have one, or the other, but not both
if(step_en_pin >= 0)
{
pinMode(step_en_pin, OUTPUT);
disableStep();
} else
{
pinMode(fan_pin, OUTPUT);
analogWrite(fan_pin, 0);
}
//these our the default values for the extruder.
e_speed = 0;
target_celsius = 0;
max_celsius = 0;
heater_low = 64;
heater_high = 255;
heater_current = 0;
valve_open = false;
//this is for doing encoder based extruder control
// rpm = 0;
// e_delay = 0;
// error = 0;
// last_extruder_error = 0;
// error_delta = 0;
e_direction = EXTRUDER_FORWARD;
//default to cool
setTemperature(target_celsius);
}
void extruder::waitForTemperature()
{
count = 0;
oldT = getTemperature();
while (getTemperature() < target_celsius - HALF_DEAD_ZONE)
{
manageAllExtruders();
count++;
if(count > 20)
{
newT = getTemperature();
if(newT > oldT)
oldT = newT;
else
{
temperatureError();
return;
}
count = 0;
}
delay(1000);
}
}
/*
byte extruder::wait_till_cool()
{
count = 0;
oldT = get_temperature();
while (get_temperature() > target_celsius + HALF_DEAD_ZONE)
{
manage_all_extruders();
count++;
if(count > 20)
{
newT = get_temperature();
if(newT < oldT)
oldT = newT;
else
return 1;
count = 0;
}
delay(1000);
}
return 0;
}
*/
void extruder::valveSet(bool open, int dTime)
{
waitForTemperature();
valve_open = open;
digitalWrite(valve_dir_pin, open);
digitalWrite(valve_en_pin, 1);
delay(dTime);
digitalWrite(valve_en_pin, 0);
}
void extruder::setTemperature(int temp)
{
target_celsius = temp;
max_celsius = (temp*11)/10;
// If we've turned the heat off, we might as well disable the extrude stepper
// if(target_celsius < 1)
// disableStep();
}
/**
* Samples the temperature and converts it to degrees celsius.
* Returns degrees celsius.
*/
int extruder::getTemperature()
{
#ifdef USE_THERMISTOR
int raw = sample_temperature(temp_pin);
int celsius = 0;
byte i;
for (i=1; i<NUMTEMPS; i++)
{
if (temptable[i][0] > raw)
{
celsius = temptable[i-1][1] +
(raw - temptable[i-1][0]) *
(temptable[i][1] - temptable[i-1][1]) /
(temptable[i][0] - temptable[i-1][0]);
break;
}
}
// Overflow: Set to last value in the table
if (i == NUMTEMPS) celsius = temptable[i-1][1];
// Clamp to byte
if (celsius > 255) celsius = 255;
else if (celsius < 0) celsius = 0;
return celsius;
#else
return ( 5.0 * sampleTemperature() * 100.0) / 1024.0;
#endif
}
/*
* This function gives us an averaged sample of the analog temperature pin.
*/
int extruder::sampleTemperature()
{
int raw = 0;
//read in a certain number of samples
for (byte i=0; i<TEMPERATURE_SAMPLES; i++)
raw += analogRead(temp_pin);
//average the samples
raw = raw/TEMPERATURE_SAMPLES;
//send it back.
return raw;
}
/*!
Manages extruder functions to keep temps, speeds etc
at the set levels. Should be called only by manage_all_extruders(),
which should be called in all non-trivial loops.
o If temp is too low, don't start the motor
o Adjust the heater power to keep the temperature at the target
*/
void extruder::manage()
{
//make sure we know what our temp is.
int current_celsius = getTemperature();
byte newheat = 0;
//put the heater into high mode if we're not at our target.
if (current_celsius < target_celsius)
newheat = heater_high;
//put the heater on low if we're at our target.
else if (current_celsius < max_celsius)
newheat = heater_low;
// Only update heat if it changed
if (heater_current != newheat) {
heater_current = newheat;
analogWrite(heater_pin, heater_current);
}
}
#if 0
void extruder::setSpeed(float sp)
{
// DC motor?
if(step_en_pin < 0)
{
e_speed = (byte)sp;
if(e_speed > 0)
waitForTemperature();
analogWrite(motor_speed_pin, e_speed);
return;
}
// No - stepper
disableTimerInterrupt();
if(sp <= 1.0e-4)
{
disableStep();
e_speed = 0; // Just use this as a flag
return;
} else
{
waitForTemperature();
enableStep();
e_speed = 1;
}
extrude_step_count = 0;
float milliseconds_per_step = 60000.0/(E_STEPS_PER_MM*sp);
long thousand_ticks_per_step = 4*(long)(milliseconds_per_step);
setupTimerInterrupt();
setTimer(thousand_ticks_per_step);
enableTimerInterrupt();
}
void extruder::interrupt()
{
if(!e_speed)
return;
extrude_step_count++;
if(extrude_step_count > 1000)
{
step();
extrude_step_count = 0;
}
}
#endif
#endif
|