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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
// Yep, this is actually -*- c++ -*-
// Sanguino G-code Interpreter
// Arduino v1.0 by Mike Ellery - initial software (mellery@gmail.com)
// v1.1 by Zach Hoeken - cleaned up and did lots of tweaks (hoeken@gmail.com)
// v1.2 by Chris Meighan - cleanup / G2&G3 support (cmeighan@gmail.com)
// v1.3 by Zach Hoeken - added thermocouple support and multi-sample temp readings. (hoeken@gmail.com)
// Sanguino v1.4 by Adrian Bowyer - added the Sanguino; extensive mods... (a.bowyer@bath.ac.uk)
// Sanguino v1.5 by Adrian Bowyer - implemented 4D Bressenham XYZ+ stepper control... (a.bowyer@bath.ac.uk)
#include <ctype.h>
#include <HardwareSerial.h>
#include "WProgram.h"
#include "parameters.h"
#include "pins.h"
#include "extruder.h"
#include "vectors.h"
#include "cartesian_dda.h"
// Inline interrupt control functions
inline void enableTimerInterrupt()
{
TIMSK1 |= (1<<OCIE1A);
}
inline void disableTimerInterrupt()
{
TIMSK1 &= ~(1<<OCIE1A);
}
inline void setTimerCeiling(unsigned int c)
{
OCR1A = c;
}
inline void resetTimer()
{
TCNT2 = 0;
}
char debugstring[COMMAND_SIZE];
// Maintain a list of extruders...
byte extruder_in_use = 0;
extruder* ex[EXTRUDER_COUNT];
// ...creating static instances of them here
extruder ex0(EXTRUDER_0_MOTOR_DIR_PIN, EXTRUDER_0_MOTOR_SPEED_PIN , EXTRUDER_0_HEATER_PIN,
EXTRUDER_0_FAN_PIN, EXTRUDER_0_TEMPERATURE_PIN, EXTRUDER_0_VALVE_DIR_PIN,
EXTRUDER_0_VALVE_ENABLE_PIN, EXTRUDER_0_STEP_ENABLE_PIN);
// Each entry in the buffer is an instance of cartesian_dda.
cartesian_dda* cdda[BUFFER_SIZE];
cartesian_dda cdda0;
cartesian_dda cdda1;
cartesian_dda cdda2;
cartesian_dda cdda3;
volatile byte head;
volatile byte tail;
// Where the machine is from the point of view of the command stream
FloatPoint where_i_am;
// Our interrupt function
SIGNAL(SIG_OUTPUT_COMPARE1A)
{
disableTimerInterrupt();
if(cdda[tail]->active())
cdda[tail]->dda_step();
else
dQMove();
enableTimerInterrupt();
}
//ERIK
#ifdef MODIFIER_PIN
double modifier = 500.0;
double modifier_old = 1.0;
#endif
double modifier_speed = 512.0; //ERIK
void setup()
{
disableTimerInterrupt();
// This screws with PWM :(
setupTimerInterrupt();
debugstring[0] = 0;
extruder_in_use = 0;
ex[extruder_in_use] = &ex0;
head = 0;
tail = 0;
cdda[0] = &cdda0;
cdda[1] = &cdda1;
cdda[2] = &cdda2;
cdda[3] = &cdda3;
setExtruder();
init_process_string();
where_i_am.x = 0.0;
where_i_am.y = 0.0;
where_i_am.z = 0.0;
where_i_am.e = 0.0;
where_i_am.f = SLOW_XY_FEEDRATE;
Serial.begin(57600);
Serial.println("start");
setTimer(DEFAULT_TICK);
enableTimerInterrupt();
// ERIK:
#ifdef MODIFIER_PIN
pinMode(MODIFIER_PIN,INPUT);
#endif
}
byte softPWMduty = 0;
byte softPWMcount = 0;
int PWMstate = HIGH;
static void softPWM()
{
if(softPWMduty == softPWMcount)
{
PWMstate = LOW;
}
digitalWrite(12,PWMstate);
if(softPWMcount<254)
softPWMcount++;
else
{
PWMstate = HIGH;
softPWMcount=0;
}
}
void loop()
{
manage_all_extruders();
get_and_do_command();
}
//******************************************************************************************
// The move buffer
inline bool qFull()
{
if(tail == 0)
return head == (BUFFER_SIZE - 1);
else
return head == (tail - 1);
}
inline bool qEmpty()
{
return tail == head && !cdda[tail]->active();
}
inline void qMove(FloatPoint p)
{
while(qFull()) delay(WAITING_DELAY);
byte h = head;
h++;
if(h >= BUFFER_SIZE)
h = 0;
cdda[h]->set_target(p);
head = h;
}
inline void dQMove()
{
if(qEmpty())
return;
byte t = tail;
t++;
if(t >= BUFFER_SIZE)
t = 0;
cdda[t]->dda_start();
tail = t;
}
inline void setUnits(bool u)
{
for(byte i = 0; i < BUFFER_SIZE; i++)
cdda[i]->set_units(u);
}
inline void setExtruder()
{
for(byte i = 0; i < BUFFER_SIZE; i++)
cdda[i]->set_extruder(ex[extruder_in_use]);
}
inline void setPosition(FloatPoint p)
{
where_i_am = p;
}
//******************************************************************************************
// Interrupt functions
void setupTimerInterrupt()
{
//clear the registers
TCCR1A = 0;
TCCR1B = 0;
TCCR1C = 0;
TIMSK1 = 0;
//waveform generation = 0100 = CTC
TCCR1B &= ~(1<<WGM13);
TCCR1B |= (1<<WGM12);
TCCR1A &= ~(1<<WGM11);
TCCR1A &= ~(1<<WGM10);
//output mode = 00 (disconnected)
TCCR1A &= ~(1<<COM1A1);
TCCR1A &= ~(1<<COM1A0);
TCCR1A &= ~(1<<COM1B1);
TCCR1A &= ~(1<<COM1B0);
//start off with a slow frequency.
setTimerResolution(5);
setTimerCeiling(65535);
}
void setTimerResolution(byte r)
{
//here's how you figure out the tick size:
// 1000000 / ((16000000 / prescaler))
// 1000000 = microseconds in 1 second
// 16000000 = cycles in 1 second
// prescaler = your prescaler
// no prescaler == 0.0625 usec tick
if (r == 0)
{
// 001 = clk/1
TCCR1B &= ~(1<<CS12);
TCCR1B &= ~(1<<CS11);
TCCR1B |= (1<<CS10);
}
// prescale of /8 == 0.5 usec tick
else if (r == 1)
{
// 010 = clk/8
TCCR1B &= ~(1<<CS12);
TCCR1B |= (1<<CS11);
TCCR1B &= ~(1<<CS10);
}
// prescale of /64 == 4 usec tick
else if (r == 2)
{
// 011 = clk/64
TCCR1B &= ~(1<<CS12);
TCCR1B |= (1<<CS11);
TCCR1B |= (1<<CS10);
}
// prescale of /256 == 16 usec tick
else if (r == 3)
{
// 100 = clk/256
TCCR1B |= (1<<CS12);
TCCR1B &= ~(1<<CS11);
TCCR1B &= ~(1<<CS10);
}
// prescale of /1024 == 64 usec tick
else
{
// 101 = clk/1024
TCCR1B |= (1<<CS12);
TCCR1B &= ~(1<<CS11);
TCCR1B |= (1<<CS10);
}
}
unsigned int getTimerCeiling(long delay)
{
// our slowest speed at our highest resolution ( (2^16-1) * 0.0625 usecs = 4095 usecs)
if (delay <= 65535L)
return (delay & 0xffff);
// our slowest speed at our next highest resolution ( (2^16-1) * 0.5 usecs = 32767 usecs)
else if (delay <= 524280L)
return ((delay / 8) & 0xffff);
// our slowest speed at our medium resolution ( (2^16-1) * 4 usecs = 262140 usecs)
else if (delay <= 4194240L)
return ((delay / 64) & 0xffff);
// our slowest speed at our medium-low resolution ( (2^16-1) * 16 usecs = 1048560 usecs)
else if (delay <= 16776960L)
return ((delay / 256) & 0xffff);
// our slowest speed at our lowest resolution ((2^16-1) * 64 usecs = 4194240 usecs)
else if (delay <= 67107840L)
return ((delay / 1024) & 0xffff);
//its really slow... hopefully we can just get by with super slow.
else
return 65535;
}
byte getTimerResolution(long delay)
{
// these also represent frequency: 1000000 / delay / 2 = frequency in hz.
// our slowest speed at our highest resolution ( (2^16-1) * 0.0625 usecs = 4095 usecs (4 millisecond max))
// range: 8Mhz max - 122hz min
if (delay <= 65535L)
return 0;
// our slowest speed at our next highest resolution ( (2^16-1) * 0.5 usecs = 32767 usecs (32 millisecond max))
// range:1Mhz max - 15.26hz min
else if (delay <= 524280L)
return 1;
// our slowest speed at our medium resolution ( (2^16-1) * 4 usecs = 262140 usecs (0.26 seconds max))
// range: 125Khz max - 1.9hz min
else if (delay <= 4194240L)
return 2;
// our slowest speed at our medium-low resolution ( (2^16-1) * 16 usecs = 1048560 usecs (1.04 seconds max))
// range: 31.25Khz max - 0.475hz min
else if (delay <= 16776960L)
return 3;
// our slowest speed at our lowest resolution ((2^16-1) * 64 usecs = 4194240 usecs (4.19 seconds max))
// range: 7.812Khz max - 0.119hz min
else if (delay <= 67107840L)
return 4;
//its really slow... hopefully we can just get by with super slow.
else
return 4;
}
// Depending on how much work the interrupt function has to do, this is
// pretty accurate between 10 us and 0.1 s. At fast speeds, the time
// taken in the interrupt function becomes significant, of course.
// Note - it is up to the user to call enableTimerInterrupt() after a call
// to this function.
inline void setTimer(long delay)
{
// delay is the delay between steps in microsecond ticks.
//
// we break it into 5 different resolutions based on the delay.
// then we set the resolution based on the size of the delay.
// we also then calculate the timer ceiling required. (ie what the counter counts to)
// the result is the timer counts up to the appropriate time and then fires an interrupt.
// Actual ticks are 0.0625 us, so multiply delay by 16
delay <<= 4;
setTimerCeiling(getTimerCeiling(delay));
setTimerResolution(getTimerResolution(delay));
}
|