-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBBQ
252 lines (189 loc) · 6.98 KB
/
BBQ
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
#include <PID_v1.h>
#include "Modulo.h"
#include "Wire.h"
// Create an object that represents the display
DisplayModulo display;
TemperatureProbeModulo thermocouple;
KnobModulo knob;
enum Selection {
SelectionTargetTemp,
SelectionKp,
SelectionKi,
SelectionKd,
NumSelections
};
Selection selection = SelectionTargetTemp;
//
// Variables for the PID Control Algorithm
//
const double defaultTargetTemp = 225; // The default temperature at power on
double targetTemp = defaultTargetTemp; // The temeprature we're trying to achieve
double currentTemp = 225; // The latest temperature from the sensor
double fanSpeed = 0; // The speed at which to run the fan
// Create a PID controller object and connect it to our variables.
PID myPID(¤tTemp, &fanSpeed, &targetTemp, 3, .025, 0, DIRECT);
//
// Variables for the graph
//
double graphMinTemp = 50; // The minimum temperature to display
double graphMaxTemp = 400; // The maximum temperature to display
double graphMinY = 1; // The display Y position of the min temperatre
double graphMaxY = 10; // The display Y position of the max temperature
double graphDuration = 60*30; // Duration of the graph in seconds
const int graphWidth = 96; // The width of the graph in pixels
uint8_t currentTempGraph[graphWidth] = { // The data for the graph
55, 55, 55, 53, 52, 51, 48, 47, 45, 44, 44, 44, 44, 43, 43, 45,
45, 45, 43, 43, 40, 38, 36, 33, 31, 28, 26, 25, 25, 25, 25, 25};
uint8_t targetTempGraph[graphWidth] = { // The data for the graph
45, 45, 43, 43, 40, 38, 36, 33, 31, 28, 26, 25, 25, 25, 25, 25,
35, 45, 48, 50, 50, 51, 48, 47, 45, 44, 44, 44, 44, 43, 43, 45}; // The data for the graph
uint8_t fanSpeedGraph[graphWidth] = { // The data for the graph
33, 31, 28, 26, 25, 25, 25, 25, 25, 24, 24, 23, 22, 22, 21, 21,
23, 24, 25, 26, 27, 26, 24, 23, 25, 26, 28, 30, 32, 32, 30, 21};
uint64_t nextDataLogTime = 0;void logData(float temp, float targetTemp, float fanSpeed) {
// Save the currentTemp and targetTemp at the current position in the graph
if (millis() > nextDataLogTime) {
nextDataLogTime = millis()+graphDuration*1000/graphWidth;
for (int i=0; i+1 < graphWidth; i++) {
currentTempGraph[i] = currentTempGraph[i+1];
targetTempGraph[i] = targetTempGraph[i+1];
fanSpeedGraph[i] = fanSpeedGraph[i+1];
}
currentTempGraph[graphWidth-1] = tempToGraphY(temp);
targetTempGraph[graphWidth-1] = tempToGraphY(targetTemp);
fanSpeedGraph[graphWidth-1] = speedToGraphY(fanSpeed);
}
}
int tempToGraphY(float temp) {
// Map temperature (in degrees) and to a value between 0 and 1
double t = (temp-graphMinTemp)/(graphMaxTemp-graphMinTemp);
// Map that 0 to 1 value into a Y position on the screen
return t*graphMaxY + (1-t)*graphMinY;
}
int speedToGraphY(float speed) {
// Map fan speed (0 to 255) to a value between 0 and 1
double s = speed/255.0;
// Map that 0 to 1 value into a Y position on the screen
return s*graphMaxY + (1-s)*graphMinY;
}
void drawGraph(uint8_t *graphData) {
// The Y position and starting X position of the
// current line. Changes every time a new value is
// encountered.
int lineStartX = 0;
int lineStartY = 0;
// Walk over the graph from left to right
for (int i=0; i < graphWidth; i++) {
// Figure out the index into the data
int x2 = 96/graphWidth;
int x = i*x2;
int y = graphData[i];
// If the value has not changed since the
// previous position, we don't need to do anything
if (y == lineStartY and i != graphWidth-1) {
continue;
}
// Draw the line, but only if the value was not 0
if (lineStartY != 0) {
display.drawLine(lineStartX, lineStartY, x, y);
}
// Save the start position of the next line segment
lineStartX = x;
lineStartY = y;
}
}
// The setup function is run once, when the program starts
void setup() {
myPID.SetMode(AUTOMATIC);
// Initialize graphData. Values at or below 0 won't be drawn.
for (int i=0; i < graphWidth; i++) {
currentTempGraph[i] = 0;
targetTempGraph[i] = 0;
fanSpeedGraph[i] = 0;
}
}
void drawMainScreen() {
// Clear and draw the screen.
display.fillScreen(0,0,0);
display.setLineColor(255,0,0);
display.setCursor(0,0);
display.setTextColor(255,0,0);
display.print(int(currentTemp));
display.print("\xF7");
display.setCursor(96-24, 0);
display.setTextColor(255,255,0);
display.print(int(targetTemp));
display.print("\xF7");
display.setTextColor(0,0,255);
display.setCursor(0, 5);
display.print(" Fan: ");
display.print(int(fanSpeed*100/255));
display.print("%");
display.refresh();
}
void drawSettingsScreen() {
display.setCursor(0,0);
display.fillScreen(0,0,0);
display.setTextColor(255,255,255);
display.print(selection == SelectionKp ? "> " : " ");
display.print("Kp: ");
display.println(myPID.GetKp());
display.print(selection == SelectionKi ? "> " : " ");
display.print("Ki: ");
display.println(myPID.GetKi());
display.print(selection == SelectionKd ? "> " : " ");
display.print("Kd: ");
display.println(myPID.GetKd());
display.refresh();
}
int previousKnobPosition = 0;
bool knobWasPressed = false;
// The loop function is run constantly
void loop() {
// Always call Modulo.loop() at the top of the loop function. It
// communicates with the modulos and executes callbacks if any events
// have occured.
Modulo.loop();
// Read the thermocouple temperature, update the controller, and output the fan speed
currentTemp = 25;
myPID.Compute();
;
// When the knob is pressed, change the selection
bool knobIsPressed = knob.getButton();
if (!knobWasPressed and knobIsPressed) {
selection = (Selection)((selection+1) % NumSelections);
}
knobWasPressed = knobIsPressed;
// Find out how far the knob moved
int newKnobPos = knob.getPosition();
int knobDelta = newKnobPos-previousKnobPosition;
previousKnobPosition = newKnobPos;
if (selection == SelectionTargetTemp) {
targetTemp += knobDelta;
drawMainScreen();
} else {
if (knobDelta) {
double Kp = myPID.GetKp();
double Ki = myPID.GetKi();
double Kd = myPID.GetKd();
if (selection == SelectionKp) {
Kp += .1*knobDelta;
}
if (selection == SelectionKd) {
Kd += .01*knobDelta;
}
if (selection == SelectionKi) {
Ki += .01*knobDelta;
}
myPID.SetTunings(Kp, Ki, Kd);
}
drawSettingsScreen();
}
if (fabs(currentTemp-targetTemp) < 5) {
knob.setColor(0,1,0);
} else if (currentTemp > targetTemp) {
knob.setColor(1,0,0);
} else {
knob.setColor(0,0,1);
}
}