summaryrefslogtreecommitdiff
path: root/trunk/users/adrian/FiveD_GCode/Extruder/extruder.h
blob: 758b8992883bed7d93f66a90ee6e44dfbf381b01 (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
#ifndef EXTRUDER_H
#define EXTRUDER_H

#define REPLY_LENGTH 20

#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 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 PING 'P'          // Just acknowledge

// PID definitions

#define TEMP_PID_INTEGRAL_DRIVE_MAX 110
#define TEMP_PID_PGAIN 5.0
#define TEMP_PID_IGAIN 0.1
#define TEMP_PID_DGAIN 100.0

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

// Divide by this to correct for the fact that we have
// messed up the timer clock

#define MILLI_CORRECTION 64

// The temperature routines get called each time the main loop
// has gone round this many times

#define SLOW_CLOCK 5000

// Default PWM for the extruder stepper

#define STEP_PWM 150

// Pin defintion section.  This is for the RepRap Extruder Controler V2.2

//our RS485 pins

#define RX_ENABLE_PIN 4 
#define TX_ENABLE_PIN 16  

// Pins to direct-drive the extruder stepper

#define E_STEP_PIN 10
#define E_DIR_PIN 9 

#ifdef MAX6675_THERMOCOUPLE
// I2C pins for the MAX 6675 temperature chip
 #define SO 18    // MISO
 #define SCK 19   // Serial Clock
 #define TC_0 17  // CS Pin of MAX6607
#else
 #define TEMP_PIN 3
#endif

// Control pins for the A3949 chips

#define H1D 7
#define H1E 5
#define H2D 8
#define H2E 6

// Analogue read of this pin gets the potentiometer setting

#define POT 0

// MOSFET drivers

#define OUTPUT_A 15
#define OUTPUT_B 11
#define OUTPUT_C 12

#define DEBUG_PIN 13

// The LED blink function

extern void blink(bool on);

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

class extruder
{

public:
   extruder();

   char* processCommand(char command[]);
   
   void manage();
  
private:

   byte coilPosition;// Stepper position between 0 and 7 inclusive
   byte pwmValue;    // PWM to the motor
   byte stp;         // Tracks the step signal
   int  targetTemperature;        // Target temperature in C
   int  currentTemperature;           // Current temperature in C
   int  manageCount; // Timing in the manage function
   bool forward;     // Extrude direction
   char reply[REPLY_LENGTH];  // For sending messages back
   
#ifdef PID_CONTROL

  volatile int iState; // Integrator state
  volatile int dState; // Last position input
  unsigned long previousTime; // ms
  float pGain;
  float iGain;
  float dGain;
  int temp_dState;
  long temp_iState;
  float temp_iState_max;
  float temp_iState_min;

  byte pidCalculation(int dt);

#endif

   void waitForTemperature();
   void slowManage();
   int internalTemperature();
   void valveSet(bool open);
   void setDirection(bool direction);
   void setCooler(byte e_speed);
   void setTemperature(int t);
   int getTemperature();
   void controlTemperature();
   void sStep();
   void enableStep();
   void disableStep();
   int potVoltage();
   void setPWM(int p); 
};

#endif