summaryrefslogtreecommitdiff
path: root/trunk/users/buzz/mendel_firmware_branch/FiveD_GCode/Extruder/extruder.h
blob: 34e1eec7c7b911919affeab6993c68243c4ccf7e (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
#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 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'          // Hard stop
#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


#define VALVE_STARTING 0
#define VALVE_RUNNING 1
#define VALVE_STOPPING 2

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

class PIDcontrol
{
  
private:

  volatile int iState; // Integrator state
  volatile int dState; // Last position input
  unsigned long previousTime; // ms
  unsigned long time;
  int dt;
  float pGain;
  float iGain;
  float dGain;
  int temp_dState;
  long temp_iState;
  float temp_iState_max;
  float temp_iState_min;
  int output;
  int error;
  float pTerm, iTerm, dTerm;
  byte heat_pin, temp_pin;
  bool bedTable;
  int currentTemperature;
  
public:

  PIDcontrol(byte hp, byte tp, bool b);
  void internalTemperature(short table[][2]);
  void pidCalculation(int target);
  int temperature();
  
};

inline int PIDcontrol::temperature() 
{ 
  return currentTemperature; 
}


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 potVal;      // The averaged pot voltage
   byte potCount;    // Averaging counter
   int  potSum;      // For computing the pot average
   bool usePot;      // True to control the motor by the pot
   byte stp;         // Tracks the step signal
   int  targetTemperature;        // Target temperature in C
   int  targetBedTemperature;        // Target bed temperature in C
   int  manageCount; // Timing in the manage function
   bool forward;     // Extrude direction
   char reply[REPLY_LENGTH];  // For sending messages back
   PIDcontrol* extruderPID;    // Temperature control - extruder...
   PIDcontrol* bedPID;         // ... and bed (if any).

#ifdef PASTE_EXTRUDER

  bool valveAlreadyRunning;
  long valveEndTime;
  bool valveAtEnd;
  bool seenHighLow;
  bool valveState;
  bool requiredValveState;

  bool valveTimeCheck(int millisecs);
  void valveTurn(bool close);
  void valveMonitor();
  void kickStartValve();

#endif

   void waitForTemperature();
   void slowManage();
   int internalTemperature(byte pin, short table[NUMTEMPS][2]);
   void valveSet(bool open);
   void setDirection(bool direction);
   void setCooler(byte e_speed);
   void setTemperature(int t);
   void setBedTemperature(int t);
   int getTemperature();
   int getBedTemperature();
   void controlTemperature();
   void sStep(byte dir);
   void enableStep();
   void disableStep();
   int potVoltage();
   void setPWM(int p);
   void usePotForMotor();
   void shutdown(); 
};


#endif