blob: 4ea75778f917678c6a32270a64b9ad1bb7820ca1 (
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
|
/****************************************************************************************************
RepRapFirmware - Heat
This is all the code to deal with heat and temperature.
-----------------------------------------------------------------------------------------------------
Version 0.1
18 November 2012
Adrian Bowyer
RepRap Professional Ltd
http://reprappro.com
Licence: GPL
****************************************************************************************************/
#include "RepRapFirmware.h"
Heat::Heat(Platform* p, GCodes* g)
{
platform = p;
gCodes = g;
for(int8_t heater=0; heater < HEATERS; heater++)
pids[heater] = new PID(platform, heater);
active = false;
}
void Heat::Init()
{
for(int8_t heater=0; heater < HEATERS; heater++)
pids[heater]->Init();
lastTime = platform->Time();
active = true;
}
void Heat::Exit()
{
active = false;
}
void Heat::Spin()
{
if(!active)
return;
unsigned long t = platform->Time();
if(t - lastTime < platform->HeatSampleTime())
return;
lastTime = t;
for(int8_t heater=0; heater < HEATERS; heater++)
pids[heater]->Spin();
}
void Heat::Diagnostics()
{
platform->Message(HOST_MESSAGE, "Heat Diagnostics:\n");
}
//******************************************************************************************************
PID::PID(Platform* p, int8_t h)
{
platform = p;
heater = h;
}
void PID::Init()
{
platform->SetHeater(heater, 0.0);
temperature = platform->GetTemperature(heater);
activeTemperature = ABS_ZERO;
standbyTemperature = ABS_ZERO;
lastTemperature = temperature;
temp_iState = 0.0;
temp_dState = 0.0;
active = false;
}
void PID::Spin()
{
temperature = platform->GetTemperature(heater);
float error;
if(active)
error = activeTemperature - temperature;
else
error = standbyTemperature - temperature;
if(!platform->UsePID(heater))
{
if(error > 0.0)
platform->SetHeater(heater, 1.0);
else
platform->SetHeater(heater, 0.0);
return;
}
if(error < -platform->FullPidBand(heater))
{
temp_iState = 0.0;
platform->SetHeater(heater, 0.0);
return;
}
if(error > platform->FullPidBand(heater))
{
temp_iState = 0.0;
platform->SetHeater(heater, 1.0);
return;
}
temp_iState += error;
if (temp_iState < platform->PidMin(heater)) temp_iState = platform->PidMin(heater);
if (temp_iState > platform->PidMax(heater)) temp_iState = platform->PidMax(heater);
temp_dState = platform->PidKd(heater)*(temperature - lastTemperature)*(1.0 - platform->DMix(heater)) + platform->DMix(heater)*temp_dState;
float result = platform->PidKp(heater)*error + platform->PidKi(heater)*temp_iState - temp_dState;
lastTemperature = temperature;
if (result < 0.0) result = 0.0;
if (result > 255.0) result = 255.0;
result = result/255.0;
platform->SetHeater(heater, result);
}
|