summaryrefslogtreecommitdiff
path: root/trunk/mendel/firmware/FiveD_GCode/FiveD_GCode_Interpreter/extruder.h
blob: 7d74f020fdd9eac4f14e59bca729f670475840ee (plain)
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519

// Class for controlling each extruder
//
// Adrian Bowyer 14 May 2009

#ifndef EXTRUDER_H
#define EXTRUDER_H



void manageAllExtruders();

void newExtruder(byte e);

/**********************************************************************************************

* Sanguino/RepRap Motherboard v 1.0

*/

#if MOTHERBOARD == 1

#define EXTRUDER_FORWARD true
#define EXTRUDER_REVERSE false

#define TEMPERATURE_SAMPLES 3

class extruder
{

     
public:

   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, float spm);
   void waitForTemperature();
   void valveSet(bool open, int dTime);
   void setDirection(bool direction);
//   void set_speed(float es);
   void setCooler(byte e_speed);
   void setTemperature(int temp);
   int getTemperature();
   int getTarget();
   void manage();
// Interrupt setup and handling functions for stepper-driven extruders
   
//   void interrupt();
   void sStep();

   void enableStep();
   void disableStep();
   
   void shutdown();
   float stepsPerMM();
      
private:

//these our the default values for the extruder.
    byte e_speed;
   // int targetTemperature;
    int max_celsius;
    byte heater_low;
    byte heater_high;
    byte heater_current;
    int extrude_step_count;
    float sPerMM;
    
    int targetTemperature;

// These are used for temperature control    
    byte count ;
    int oldT, newT;
    
//this is for doing encoder based extruder control
//    int rpm;
//    long e_delay;
//    int error;
//    int last_extruder_error;
//    int error_delta;
    bool e_direction;
    bool valve_open;

// The pins we control
    byte motor_dir_pin, motor_speed_pin, heater_pin, fan_pin, temp_pin, valve_dir_pin, valve_en_pin, step_en_pin;
    
     byte wait_till_hot();
     //byte wait_till_cool();
     void temperatureError(); 
     int sampleTemperature();
   
};

inline int extruder::getTarget()
{
  return targetTemperature;
}

inline void extruder::enableStep()
{
  if(step_en_pin < 0)
    return;
  digitalWrite(step_en_pin, ENABLE_ON); 
}

inline void extruder::disableStep()
{
  if(step_en_pin < 0)
    return;
#if DISABLE_E
  digitalWrite(step_en_pin, !ENABLE_ON);
#endif
}

inline void extruder::sStep()
{
   digitalWrite(motor_speed_pin, HIGH);
   delayMicrosecondsInterruptible(5); 
   digitalWrite(motor_speed_pin, LOW);
}


inline void extruder::setDirection(bool dir)
{
	e_direction = dir;
	digitalWrite(motor_dir_pin, e_direction);
}

inline void extruder::setCooler(byte sp)
{
  if(step_en_pin >= 0) // Step enable conflicts with the fan
    return;
  analogWrite(fan_pin, sp);
}

#endif

/**********************************************************************************************
*
* RepRap Motherboard with extruder is on RS485
*
*/

#if MOTHERBOARD == 2

#define WAIT_T 'W'        // wait_for_temperature();
#define VALVE 'V'         // valve_set(bool open, int dTime);
#define DIRECTION 'D'     // set_direction(bool direction);
#define COOL 'C'          // set_cooler(byte e_speed);
#define SET_T 'T'         // set_temperature(int temp);
#define GET_T 't'         // get_temperature();
#define SET_BED_T 'B'     // set bed temperature(int temp);
#define GET_BED_T 'b'     // get bed temperature();
#define STEP 'S'          // step();
#define ENABLE 'E'        // enableStep();
#define DISABLE 'e'       // disableStep();
#define PREAD 'R'         // read the pot voltage
#define SPWM 'M'          // Set the motor PWM
#define UPFM 'U'          // Use the pot to control the motor
#define SHUT 'X'          // Shutdown
#define PING 'P'          // Just acknowledge

class extruder
{
  
public:
   extruder(char name, float spm);
   void waitForTemperature();
   void valveSet(bool open, int dTime);
   void setDirection(bool direction);
   void setCooler(byte e_speed);
   void setTemperature(int temp);
   int getBedTemperature();
   void setBedTemperature(int temp);
   int getTemperature();
   int getTarget();
   void manage();
   void sStep();
   void enableStep();
   void disableStep();
   int potVoltage();
   void setPWM(int p);
   void usePotForMotor();
   void shutdown();
   bool ping();
   float stepsPerMM();
 
private:

   char my_name;
   int targetTemperature;
   int count;
   int oldT, newT;
   char commandBuffer[RS485_BUF_LEN];
   char* reply;
   bool stp;
   float sPerMM;

   void buildCommand(char c);   
   void buildCommand(char c, char v);
   void buildNumberCommand(char c, int v);
   void temperatureError();  
};

inline extruder::extruder(char name, float spm)
{
  my_name = name;
  sPerMM = spm;
  pinMode(E_STEP_PIN, OUTPUT);
  pinMode(E_DIR_PIN, OUTPUT);
  digitalWrite(E_STEP_PIN, 0);
  digitalWrite(E_DIR_PIN, 0);
  setTemperature(0);
  stp = false;
}


inline void extruder::buildCommand(char c)
{
  commandBuffer[0] = c;
  commandBuffer[1] = 0;  
}

inline void extruder::buildCommand(char c, char v)
{
  commandBuffer[0] = c;
  commandBuffer[1] = v;
  commandBuffer[2] = 0;  
}

inline void extruder::buildNumberCommand(char c, int v)
{
  commandBuffer[0] = c;
  itoa(v, &commandBuffer[1], 10);
}




inline  void extruder::valveSet(bool open, int dTime)
{
   if(open)
     buildCommand(VALVE, '1');
   else
     buildCommand(VALVE, '0');
   talker.sendPacketAndCheckAcknowledgement(my_name, commandBuffer);
   
   delay(dTime);
}

inline  void extruder::setCooler(byte e_speed)
{   
   buildNumberCommand(COOL, e_speed);
   talker.sendPacketAndCheckAcknowledgement(my_name, commandBuffer);
}

inline  void extruder::setTemperature(int temp)
{
   targetTemperature = temp;
   buildNumberCommand(SET_T, temp);
   talker.sendPacketAndCheckAcknowledgement(my_name, commandBuffer); 
}

inline int extruder::getTarget()
{
   return targetTemperature;
}

inline  int extruder::getTemperature()
{
   buildCommand(GET_T);
   char* reply = talker.sendPacketAndGetReply(my_name, commandBuffer);
   return(atoi(reply));
}

inline  void extruder::setBedTemperature(int temp)
{
   //target_celsius = temp;
   buildNumberCommand(SET_BED_T, temp);
   talker.sendPacketAndCheckAcknowledgement(my_name, commandBuffer); 
}

inline  int extruder::getBedTemperature()
{
   buildCommand(GET_BED_T);
   char* reply = talker.sendPacketAndGetReply(my_name, commandBuffer);
   return(atoi(reply));
}

inline  void extruder::manage()
{

}

inline void extruder::setDirection(bool direction)
{
//   if(direction)
//     buildCommand(DIRECTION, '1');
//   else
//     buildCommand(DIRECTION, '0');
//   talker.sendPacketAndCheckAcknowledgement(my_name, commandBuffer);
   if(direction)
     digitalWrite(E_DIR_PIN, 1);
   else
     digitalWrite(E_DIR_PIN, 0);
}


inline  void extruder::sStep()
{
   //buildCommand(STEP);
   //talker.sendPacketAndCheckAcknowledgement(my_name, commandBuffer); 
   stp = !stp;
   if(stp)
     digitalWrite(E_STEP_PIN, 1);
   else
     digitalWrite(E_STEP_PIN, 0);
}

inline  void extruder::enableStep()
{
  // Not needed - stepping the motor enables it automatically

}

inline  void extruder::disableStep()
{
  // N.B. Disabling the extrude stepper causes the backpressure to
  // turn the motor the wrong way.  Usually leave it on.
#if DISABLE_E  
  buildCommand(DISABLE);
  talker.sendPacketAndCheckAcknowledgement(my_name, commandBuffer);
#endif
}

inline  void extruder::shutdown()
{ 
  buildCommand(SHUT);
  talker.sendPacketAndCheckAcknowledgement(my_name, commandBuffer);
}

inline int extruder::potVoltage()
{
   buildCommand(PREAD);
   char* reply = talker.sendPacketAndGetReply(my_name, commandBuffer);
   return(atoi(reply));  
}

inline void extruder::setPWM(int p)
{
   buildNumberCommand(SPWM, p);
   talker.sendPacketAndCheckAcknowledgement(my_name, commandBuffer);   
}

inline  void extruder::usePotForMotor()
{ 
  buildCommand(UPFM);
  talker.sendPacketAndCheckAcknowledgement(my_name, commandBuffer);
}

inline bool extruder::ping()
{
  buildCommand(PING);
  return talker.sendPacketAndCheckAcknowledgement(my_name, commandBuffer);  
}

#endif

/**********************************************************************************************
*
* RepRap Arduino Mega Motherboard
*
*/

#if MOTHERBOARD == 3

//******************************************************************************************************



class extruder
{
  
public:
   extruder(byte step, byte dir, byte en, byte heat, byte temp, float spm);
   void waitForTemperature();
   
   void setDirection(bool direction);
   void setCooler(byte e_speed);
   void setTemperature(int temp);
   int getTemperature();
   int getTarget();
   void slowManage();
   void manage();
   void sStep();
   void enableStep();
   void disableStep();
   void shutdown();
   float stepsPerMM();
   void controlTemperature();   
   void valveSet(bool open, int dTime); 
 
private:

//   int targetTemperature;
   int count;
   int oldT, newT;
   float sPerMM;
   long manageCount;
   
   PIDcontrol* extruderPID;    // Temperature control - extruder...
   
   int sampleTemperature();

   void temperatureError(); 

// The pins we control
   byte motor_step_pin, motor_dir_pin, heater_pin,  temp_pin,  motor_en_pin;

   //byte fan_pin;
    
#ifdef PASTE_EXTRUDER
   byte valve_dir_pin, valve_en_pin;
   bool valve_open;
#endif
    
 
};

inline void extruder::sStep()
{
	digitalWrite(motor_step_pin, HIGH);
	digitalWrite(motor_step_pin, LOW);  
}

inline void extruder::controlTemperature()
{   
  extruderPID->pidCalculation();
}

inline void extruder::slowManage()
{
  manageCount = 0;  

  controlTemperature();
}

inline void extruder::manage()
{
  
#ifdef PASTE_EXTRUDER
  valveMonitor();
#endif

  manageCount++;
  if(manageCount > SLOW_CLOCK)
    slowManage();   
}

inline void extruder::setDirection(bool direction)
{
  digitalWrite(motor_dir_pin, direction);  
}

inline void extruder::setCooler(byte e_speed)
{
  //analogWrite(fan_pin, e_speed);   
}

inline void extruder::setTemperature(int tp)
{
  extruderPID->setTarget(tp);
}

inline int extruder::getTemperature()
{
  return extruderPID->temperature();  
}

inline int extruder::getTarget()
{
  return extruderPID->getTarget();  
}



inline void extruder::enableStep()
{
    digitalWrite(motor_en_pin, ENABLE_ON);
}

inline void extruder::disableStep()
{
#if DISABLE_E
    digitalWrite(motor_en_pin, !ENABLE_ON);
#endif
}


inline void extruder::valveSet(bool closed, int dTime)
{
#ifdef PASTE_EXTRUDER
  requiredValveState = closed;
  kickStartValve();
#endif
}


#endif

//*********************************************************************************************************

extern extruder* ex[ ];
extern byte extruder_in_use;

inline float extruder::stepsPerMM()
{
  return sPerMM;
}

#endif