-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArduinoOven.ino
1723 lines (1590 loc) · 68.1 KB
/
ArduinoOven.ino
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Serial Options
//#define DEBUG
//#define DEBUG_JSON
#define DEBUG_PID_JSON
#if defined DEBUG_JSON
#define DEBUG_CONVEYOR_PID_JSON
#define DEBUG_SENSOR_TEMP_JSON
#endif
#if defined DEBUG
#define DEBUG_TOUCHSCREEN
#define DEBUG_SENSOR_SHOW_ERROR
#define DEBUG_PID_GRAPH
#endif
//#define SERIAL_COMMANDER
// Display
#define DISPLAY_WIDTH 320
#define DISPLAY_HEIGHT 240
#define ORIENTATION 1
#include <Adafruit_GFX.h>
#include <UTFTGLUE.h> // Modified file MCUFRIEND_kbv.cpp: Enabled #define SUPPORT_8347D
UTFTGLUE myGLCD(0x9341, A2, A1, A3, A4, A0);
extern uint8_t SmallFont[]; // Declare which fonts we will be using
// Colors - RGB565 color picker -> https://ee-programming-notepad.blogspot.com.co/2016/10/16-bit-color-generator-picker.html
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define GRAY 0x7BEF
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define PALEGREEN 0x9FD3
// Text sizes
#define SET_CONTROL_TEXT_SIZE 6
#define PROFILE_ID_TEXT_SIZE 4
#define PROFILE_PARAM_TEXT_SIZE 1
#define SENSOR_TEXT_SIZE 3
// Thermocouples
#include <max6675.h> // https://github.com/SirUli/MAX6675
#include <SPI.h>
#define PIN_CS_TOP_TEMP_SENSOR_1 23
#define PIN_CS_TOP_TEMP_SENSOR_2 25
#define PIN_CS_BOTTOM_TEMP_SENSOR_1 27
#define PIN_CS_BOTTOM_TEMP_SENSOR_2 29
#define NUM_OF_MEASUREMENTS_TO_READ 10
#define MAX_TEMP_SENSOR_ERROR 30
// Servos
#include <Servo.h> // Modified file ServoTimers.h: disabled timer5 to use analogWrite() on pins 44,45,46. Now timer1 is used.
Servo topServo;
Servo bottomServo;
// pins
#define TOP_SERVO_PIN 45
#define BOTTOM_SERVO_PIN 46
// Motor Speed Control with L298N
#define CONVEYOR_L298N_PWM 44 // ENB
#define CONVEYOR_L298N_DIR1_PIN 42 // IN3
#define CONVEYOR_L298N_DIR2_PIN 40 // IN4
// Encoder
#include <Encoder.h>
#define ENCODER_PIN_1 20
#define ENCODER_PIN_2 21
Encoder myEncoder(ENCODER_PIN_1, ENCODER_PIN_2);
#define ENCODER_REBOUND_MS 20
// Arduino and Oven Specific Parameters
#define STEPS_PER_GEAR_REVOLUTION 400.0
#define GEAR_REVOLUTIONS_TO_CROSS_OVEN 7.333333333
#define CONVEYOR_MAX_STEPS_PER_MS STEPS_PER_GEAR_REVOLUTION / 8000.0 // 0.05
#define STEPS_TO_CROSS_OVEN STEPS_PER_GEAR_REVOLUTION * GEAR_REVOLUTIONS_TO_CROSS_OVEN
#define ARDUINO_TIME_CORRECTION 1.111
#define STEPS_TO_CROSS_OVEN__TIME_CORRECTED STEPS_TO_CROSS_OVEN * ARDUINO_TIME_CORRECTION
// PID
#include <PID_v1.h>
#define PID_MANUAL_THRESHOLD 50
// Precision
#define PID_PRECISION_KP 1
#define PID_PRECISION_KI 0.001
#define PID_PRECISION_KD 10
// Top
#define TOP_PID_KP PID_PRECISION_KP*100
#define TOP_PID_KI PID_PRECISION_KI*100
#define TOP_PID_KD PID_PRECISION_KD*100
// Conveyor
#define CONVEYOR_PID_KP 3
#define CONVEYOR_PID_KI 0.2
#define CONVEYOR_PID_KD 0
// Bottom
#define BOTTOM_PID_KP PID_PRECISION_KP*100
#define BOTTOM_PID_KI PID_PRECISION_KI*100
#define BOTTOM_PID_KD PID_PRECISION_KD*100
// min/max PWM width in uS
#define TOP_PID_MIN_WIDTH 780
#define TOP_PID_MAX_WIDTH 1200
#define BOTTOM_PID_MIN_WIDTH 750
#define BOTTOM_PID_MAX_WIDTH 1500
#define CONVEYOR_PID_MIN_WIDTH 0
#define CONVEYOR_PID_MAX_WIDTH 255
// Graphs
#define TOP_TEMP_GRAPH_RANGE 100
#define BOTTOM_TEMP_GRAPH_RANGE 100
#define CONVEYOR_GRAPH_RANGE 100
#define GRAPH_GRID_HEIGHT DISPLAY_HEIGHT / 10
// TouchScreen
#include <TouchScreen.h> // Adafruit_TouchScreen
#define YP A2 //A3 for ILI9320
#define YM 6 //9
#define XM A1
#define XP 7 //8
TouchScreen myTouch(XP, YP, XM, YM, 300);
#define NUM_OF_SAMPLES 10
#define QUORUM 3
#define DEAD_ZONE 2 // outest pixels of blocks where Touch-Events are disabled
// Touch events
#define CLICK_EVENT 0
#define LONG_CLICK_EVENT 1+CLICK_EVENT
#define HOLD_EVENT 1+LONG_CLICK_EVENT
#define LONG_HOLD_EVENT 1+HOLD_EVENT
// Time
#define CLICK_TIME 30 // minimum ms since touch started to trigger CLICK_EVENT
#define HOLD_TIME 70 // minimum ms since touch started to trigger HOLD_EVENT
#define LONG_CLICK_TIME 600 // minimum ms since touch started to trigger LONG_CLICK_EVENT
#define LONG_HOLD_TIME LONG_CLICK_TIME // minimum ms since touch started to trigger LONG_HOLD_EVENT
// States # Should've used enum
#define CONTROLLING_SETPOINTS 0
#define CONTROLLING_TOP_PID 1+CONTROLLING_SETPOINTS
#define CONTROLLING_BOTTOM_PID 1+CONTROLLING_TOP_PID
#define CONTROLLING_CONVEYOR_PID 1+CONTROLLING_BOTTOM_PID
#define SHOWING_GRAPH 1+CONTROLLING_CONVEYOR_PID
#define CONTROLLING_TOP_OUTPUT_LIMITS 1+SHOWING_GRAPH
#define CONTROLLING_CONVEYOR_OUTPUT_LIMITS 1+CONTROLLING_TOP_OUTPUT_LIMITS
#define CONTROLLING_BOTTOM_OUTPUT_LIMITS 1+CONTROLLING_CONVEYOR_OUTPUT_LIMITS
// Threads
#include <Thread.h>
Thread updateSensorsThread = Thread();
Thread drawSensorsThread = Thread();
Thread topPIDThread = Thread();
Thread bottomPIDThread = Thread();
Thread conveyorPIDThread = Thread();
Thread drawGraphPointThread = Thread();
#define UPDATE_SENSORS_INTERVAL 1000
#define TOP_PID_INTERVAL 1000
#define BOTTOM_PID_INTERVAL 1000
#define CONVEYOR_PID_INTERVAL 1000
#define DRAW_SENSORS_INTERVAL 1000
#define DRAW_GRAPH_POINT_INTERVAL 1000
// Sensors Draw
#define ERROR_DURATION_MS 10000
//Turns
#define TOP_TURN 0
#define CONVEYOR_TURN 1+TOP_TURN
#define BOTTOM_TURN 1+CONVEYOR_TURN
// EEPROM
#include <EEPROM.h>
struct PidEEPROM { int kp; int ki; int kd; int minOutput; int startOutput; int maxOutput; };
struct ServoEEPROM { int minWidth; int maxWidth; };
struct ProfileEEPROM { int topTemp; int cookTime; int bottomTemp; };
// Addresses
// PIDs
#define TOP_PID_ADDRESS 0
#define CONVEYOR_PID_ADDRESS sizeof(PidEEPROM)+TOP_PID_ADDRESS
#define BOTTOM_PID_ADDRESS sizeof(PidEEPROM)+CONVEYOR_PID_ADDRESS
// Profiles
#define PROFILE_ADDRESS sizeof(PidEEPROM)+BOTTOM_PID_ADDRESS
// Relays
#define SPARK_PIN 35
#define VALVE_PIN 37
#define SPARK_IGNITION_TIME 3000 // ms
//#define VALVE_TIME 200
// ###############################################################
// ##################### GLOBAL VARIABLES ####################
// ###############################################################
// Display
int dispX, dispY;
byte gridWidth, gridHeight, gridInternalWidth, gridInternalHeight;
bool isOutline = false;
// Touch
TSPoint avgTouchPoint, last_avgTouchPoint;
//, touchPoint, touchPointsArr[NUM_OF_SAMPLES];
// byte touchPointsArrIndex;
bool TouchStatus; // the current value read from isPressed()
bool lastTouchStatus = false;
long timeTouchStarted, timeSinceTouchStarted, lastTimeSinceTouchStarted;
// Encoder
long last_computeConveyorTime;
// Graph
double *inputGraph, *setpointGraph, *outputGraph;
int minInputSetpointGraph, maxInputSetpointGraph, minOutputGraph, maxOutputGraph;
// Misc
byte activeProfile;
byte state;
// ###############################################################
// ################ GLOBAL FUNCTIONS FOR CLASSES #################
// ###############################################################
String stringifyDouble (double number) {
char buffer[10];
if (number == int(number)) return String(int(number));
else if (number > 0) {
if (number < 0.1) return String(dtostrf(number,5,4, buffer)).substring(1);
else if (number < 1) return String(dtostrf(number,4,3, buffer)).substring(1);
else return dtostrf(number,1,1, buffer);
}
else if (number > -1 and number < 10) return dtostrf(number,4,2, buffer);
else return dtostrf(number,1,1, buffer);
}
byte getFontSize(String number_str, byte normalFontSize) {
byte number_length = number_str.length();
return (number_length <= 3) ? normalFontSize : ( normalFontSize - (number_length-3) );
}
//DEBUG
void debug(String str) { Serial.println(str); }
void serialStartJsonObject(String name) { Serial.print("{\"name\":\""+name+"\",\"data\":{"); }
void serialEndJsonObject() { Serial.println("}},"); }
void serialAddJsonObject(String key, double value) { Serial.print("\""+key+"\":"+String(value)+","); }
void serialAddJsonObject(String key, String value) { Serial.print("\""+key+"\":\""+value+"\","); }
void serialStartJsonArray() { Serial.print("["); }
void serialEndJsonArray() { Serial.print("],"); }
void serialAddJsonArray(double value) { Serial.print(String(value)+","); }
void serialAddJsonArray(String value) { Serial.print("\""+value+"\","); }
// ###############################################################
// ######################### CLASSES #########################
// ###############################################################
class Coordinates {
public:
int startX, startY, endX, endY;
virtual void setCoordinates(int x, int y) {
startX = x;
startY = y;
endX = startX+gridInternalWidth;
endY = startY+gridInternalHeight;
};
};
class Block : public Coordinates {
public:
uint16_t errorbackgroundColor = MAGENTA;
uint16_t errorforegroundColor = WHITE;
uint16_t highlightbackgroundColor = GREEN;
uint16_t highlightforegroundColor = BLACK;
uint16_t lowlightbackgroundColor = BLACK;
uint16_t lowlightforegroundColor = WHITE;
uint16_t* backgroundColor = & lowlightbackgroundColor;
uint16_t* foregroundColor = & lowlightforegroundColor;
uint16_t old_backgroundColor;
long lastTimeSinceError;
bool isErrorActive = false;
void highlight(void) { backgroundColor = & highlightbackgroundColor; foregroundColor = & highlightforegroundColor; };
void lowlight(void) { backgroundColor = & lowlightbackgroundColor; foregroundColor = & lowlightforegroundColor; };
void drawBackground (void) {
myGLCD.setColor(*backgroundColor);
myGLCD.fillRect(startX, startY, endX, endY);
}
void drawBackgroundIfHasChanged (void) {
if (old_backgroundColor != *backgroundColor) {
drawBackground();
old_backgroundColor = *backgroundColor;
}
}
void showError (void) {
lastTimeSinceError = millis();
backgroundColor = & errorbackgroundColor;
foregroundColor = & errorforegroundColor;
isErrorActive = true;
};
void showError (String str) {
showError();
#if defined DEBUG_SENSOR_SHOW_ERROR
Serial.print("ERROR: ");Serial.print(str);Serial.println();
#endif
}
void removeError (void) {
isErrorActive = false;
lowlight();
};
};
class MinusButton : public Block {
public:
void draw(void) {
drawBackground();
myGLCD.setColor(*foregroundColor);
myGLCD.fillRect(startX+15, startY+27, startX+46, startY+34);
};
MinusButton() {
lowlightbackgroundColor = BLUE;
lowlightforegroundColor = BLACK;
}
};
class SetControl : public Block {
uint16_t getRGB565(byte red, byte green, byte blue) { return ((red&0b11111000) << 8) | ((green&0b11111100) << 3) | (blue>>3);}
uint16_t map_7Bits_to_RGB565FromBlueToRed(uint8_t num7Bits) {
/*
maps 0-128 to blue-red (passing through green)
There are 4 cases to map
- 0-31: blue=maximum green=increasing red=0
- 32-63: blue=decreasing green=maximum red=0
- 64-95: blue=0 green=maximum red=increasing
- 96-127: blue=0 green=decreasing red=maximum
*/
byte red, green, blue;
if (num7Bits < 32 ) { blue = 255; green = (num7Bits<<3); red = 0; }
else if (num7Bits < 64 ) { blue = 255-(num7Bits<<3); green = 255; red = 0; }
else if (num7Bits < 96 ) { blue = 0; green = 255; red = (num7Bits<<3); }
else if (num7Bits < 128) { blue = 0; green = 255-(num7Bits<<3); red = 255; }
uint16_t color = getRGB565(red, green, blue);
return color;
}
public:
double* value;
uint8_t servo_status;
void setCoordinates(int x, int y) {
startX = x;
startY = y;
endX = startX+gridInternalWidth+gridWidth; // +gridWidth for 2 columns width
endY = startY+gridInternalHeight;
};
void draw(double number) {
drawBackground();
lowlightforegroundColor = map_7Bits_to_RGB565FromBlueToRed(servo_status);
myGLCD.setTextColor(*foregroundColor, *backgroundColor);
String number_str = stringifyDouble(number);
myGLCD.setTextSize( getFontSize(number_str, SET_CONTROL_TEXT_SIZE) );
myGLCD.print(number_str, startX+6, startY+11);
};
void draw(void) {draw(*value);};
};
class PlusButton : public Block {
public:
void draw(void) {
drawBackground();
myGLCD.setColor(*foregroundColor);
myGLCD.fillRect(startX+27, startY+15, startX+34, startY+46);
myGLCD.fillRect(startX+15, startY+27, startX+46, startY+34);
};
PlusButton() {
lowlightbackgroundColor = RED;
lowlightforegroundColor = BLACK;
}
};
class Sensors : public Block {
public:
virtual void draw(void){};
virtual void update(void){};
virtual double read(void){};
};
class TempSensors : public Sensors {
public:
double value1Avg;
double value2Avg;
double values1[NUM_OF_MEASUREMENTS_TO_READ];
double values2[NUM_OF_MEASUREMENTS_TO_READ];
byte counter;
MAX6675 Sensor1;
MAX6675 Sensor2;
TempSensors(byte pinSensor1CS,byte pinSensor2CS):
Sensor1(pinSensor1CS),
Sensor2(pinSensor2CS)
{};
void draw(void) {
if ( isErrorActive and ERROR_DURATION_MS < millis() - lastTimeSinceError ) removeError();
drawBackgroundIfHasChanged();
myGLCD.setTextColor(*foregroundColor, *backgroundColor);
myGLCD.setTextSize(SENSOR_TEXT_SIZE);
myGLCD.print(String(int(value1Avg)), startX+7, startY+7);
myGLCD.print(String(int(value2Avg)), startX+7, startY+35);
/* // Draw symbol: ±
myGLCD.setColor(foregroundColor);
myGLCD.fillRect(startX+7, startY+35+18 , startX+7+13, startY+35+19);
myGLCD.fillRect(startX+7, startY+35+7 , startX+7+13, startY+35+8);
myGLCD.fillRect(startX+7+6, startY+35 , startX+7+7, startY+35+15);
*/
};
double getAvgTemp (double value[NUM_OF_MEASUREMENTS_TO_READ]) {
double valueSum=0;
byte validReads=0;
for (byte i=0; i<NUM_OF_MEASUREMENTS_TO_READ; i++) {
if (!isnan(value[i])) {
valueSum += value[i];
validReads++;
};
};
if (validReads > 0) return valueSum / validReads; else return NAN;
};
double updateSensor(double measurements[NUM_OF_MEASUREMENTS_TO_READ], double* avg_measurements, double new_measurement) {
if (!isnan(new_measurement)) {
double valueDiff = new_measurement - *avg_measurements;
if ( valueDiff > MAX_TEMP_SENSOR_ERROR ) { measurements[counter] = *avg_measurements + MAX_TEMP_SENSOR_ERROR; showError(String(new_measurement)); }
else if ( valueDiff < -MAX_TEMP_SENSOR_ERROR ) { measurements[counter] = *avg_measurements - MAX_TEMP_SENSOR_ERROR; showError(String(new_measurement)); }
else measurements[counter] = new_measurement;
}
else measurements[counter] = *avg_measurements;
*avg_measurements = getAvgTemp(measurements);
};
void update(void) {
double new_measurement1 = Sensor1.readCelsius();
double new_measurement2 = Sensor2.readCelsius();
#if defined DEBUG_SENSOR_TEMP_JSON
serialStartJsonObject("temperatures");
serialStartJsonArray();
serialAddJsonArray(new_measurement1);
serialAddJsonArray(new_measurement2);
serialEndJsonArray();
serialEndJsonObject();
#endif
updateSensor(values1, &value1Avg, new_measurement1);
updateSensor(values2, &value2Avg, new_measurement2);
if (counter == NUM_OF_MEASUREMENTS_TO_READ-1) counter=0; else counter++;
};
double read(void) {
byte validReads=0; // If it's not set to 0, then "if (validReads > 0)" will always be True
double valueSum;
double reading=0;
if (value1Avg > 0) { valueSum += value1Avg; validReads++; }
if (value2Avg > 0) { valueSum += value2Avg; validReads++; }
if (validReads > 0) reading = valueSum / validReads;
else showError("No valid reads top sensors");
return reading;
};
};
class EncoderSensor : public Sensors {
public:
double value;
void draw(void) {
drawBackground();
myGLCD.setTextColor(*foregroundColor, *backgroundColor);
String value_str = stringifyDouble(value);
if (value_str.length() <= 3) {
myGLCD.setTextSize(SENSOR_TEXT_SIZE);
myGLCD.print(value_str, startX+7, startY+20);
}
else {
myGLCD.setTextSize(SENSOR_TEXT_SIZE-1);
myGLCD.print(value_str, startX+3, startY+23);
}
};
};
class Control : public Coordinates {
public:
double setpoint;
// Amount to increase/decrease setcontrol
double short_event_amount = 0.1;
double long_event_amount = 1;
MinusButton minusButton;
SetControl setControl;
PlusButton plusButton;
EncoderSensor sensors;
virtual void setCoordinates(int x, int y) {
startX = x;
startY = y;
endX = dispX-1 - isOutline;
endY = startY + gridInternalHeight;
minusButton.setCoordinates(startX, startY);
setControl.setCoordinates(startX+gridWidth, startY);
plusButton.setCoordinates(startX+gridWidth*3, startY);
sensors.setCoordinates(startX+gridWidth*4, startY);
};
virtual void draw(double setControlNumber) {
minusButton.draw();
setControl.draw(setControlNumber);
plusButton.draw();
sensors.draw();
};
virtual void draw(void) {draw(*setControl.value);};
virtual double amountToChange(byte event){ return (event==LONG_HOLD_EVENT) ? long_event_amount : short_event_amount; }
virtual void decreaseSetControl(byte event) {setpoint-=amountToChange(event); setControl.draw();}
virtual void increaseSetControl(byte event) {setpoint+=amountToChange(event); setControl.draw();}
} conveyorControl;
class TempControl : public Control {
public:
TempSensors sensors;
TempControl(byte pinSensor1CS,byte pinSensor2CS):
sensors(pinSensor1CS, pinSensor2CS)
{
// Amount to increase/decrease setcontrol
short_event_amount = 1;
long_event_amount = 10;
};
void setCoordinates(int x, int y) {
startX = x;
startY = y;
endX = dispX-1 - isOutline;
endY = startY + gridInternalHeight;
minusButton.setCoordinates(startX, startY);
setControl.setCoordinates(startX+gridWidth, startY);
plusButton.setCoordinates(startX+gridWidth*3, startY);
sensors.setCoordinates(startX+gridWidth*4, startY);
};
void draw(double setControlNumber) {
minusButton.draw();
setControl.draw(setControlNumber);
plusButton.draw();
sensors.draw();
};
void draw(void) {draw(*setControl.value);};
// TempControl(SensorCS1, SensorCS2)
} topTempControl(PIN_CS_TOP_TEMP_SENSOR_1,
PIN_CS_TOP_TEMP_SENSOR_2
),
bottomTempControl(PIN_CS_BOTTOM_TEMP_SENSOR_1,
PIN_CS_BOTTOM_TEMP_SENSOR_2
);
class Pid : public PID {
public:
const String name;
double kp, ki, kd;
double input, output, setpoint;
double minOutput, startOutput, maxOutput;
byte EEPROMaddress, outputLimitsEEPROMaddress;
Pid(String _name, double _kp, double _ki, double _kd, byte pOn, byte DIR, byte address):
name(_name),
kp(_kp),
ki(_ki),
kd(_kd),
EEPROMaddress(address),
PID(&input, &output, &setpoint, _kp, _ki, _kd, pOn, DIR)
{};
int minOutputGraph(void) { return (GetDirection() == DIRECT) ? minOutput : maxOutput; };
int maxOutputGraph(void) { return (GetDirection() == DIRECT) ? maxOutput : minOutput; };
void updateTuning(void) {SetTunings(kp,ki,kd);};
void updateOutputLimits(void) {SetOutputLimits(minOutput, maxOutput);};
void increaseKp (byte event) {byte scale=(event==LONG_HOLD_EVENT)?10:1; kp+=scale*PID_PRECISION_KP; updateTuning(); topTempControl.setControl.draw(kp);};
void decreaseKp (byte event) {byte scale=(event==LONG_HOLD_EVENT)?10:1; kp-=scale*PID_PRECISION_KP; updateTuning(); topTempControl.setControl.draw(kp);};
void increaseKi (byte event) {byte scale=(event==LONG_HOLD_EVENT)?10:1; ki+=scale*PID_PRECISION_KI; updateTuning(); conveyorControl.setControl.draw(ki);};
void decreaseKi (byte event) {byte scale=(event==LONG_HOLD_EVENT)?10:1; ki-=scale*PID_PRECISION_KI; updateTuning(); conveyorControl.setControl.draw(ki);};
void increaseKd (byte event) {byte scale=(event==LONG_HOLD_EVENT)?10:1; kd+=scale*PID_PRECISION_KD; updateTuning(); bottomTempControl.setControl.draw(kd);};
void decreaseKd (byte event) {byte scale=(event==LONG_HOLD_EVENT)?10:1; kd-=scale*PID_PRECISION_KD; updateTuning(); bottomTempControl.setControl.draw(kd);};
void increaseMinOutput (byte event) {byte scale=(event==LONG_HOLD_EVENT)?10:1; minOutput+= scale*1; updateOutputLimits(); topTempControl.setControl.draw (minOutput);};
void decreaseMinOutput (byte event) {byte scale=(event==LONG_HOLD_EVENT)?10:1; minOutput-= scale*1; updateOutputLimits(); topTempControl.setControl.draw (minOutput);};
void increaseStartOutput (byte event) {byte scale=(event==LONG_HOLD_EVENT)?10:1; startOutput+=scale*1; updateOutputLimits(); conveyorControl.setControl.draw (startOutput);};
void decreaseStartOutput (byte event) {byte scale=(event==LONG_HOLD_EVENT)?10:1; startOutput-=scale*1; updateOutputLimits(); conveyorControl.setControl.draw (startOutput);};
void increaseMaxOutput (byte event) {byte scale=(event==LONG_HOLD_EVENT)?10:1; maxOutput+= scale*1; updateOutputLimits(); bottomTempControl.setControl.draw (maxOutput);};
void decreaseMaxOutput (byte event) {byte scale=(event==LONG_HOLD_EVENT)?10:1; maxOutput-= scale*1; updateOutputLimits(); bottomTempControl.setControl.draw (maxOutput);};
void saveParameters (void) {
PidEEPROM pid;
pid.kp = (int)(kp/PID_PRECISION_KP);
pid.ki = (int)(ki/PID_PRECISION_KI);
pid.kd = (int)(kd/PID_PRECISION_KD);
pid.minOutput = (int)minOutput;
pid.startOutput = (int)startOutput;
pid.maxOutput = (int)maxOutput;
EEPROM.put(EEPROMaddress, pid);
}
void loadParameters (void) {
PidEEPROM pid;
EEPROM.get(EEPROMaddress, pid);
if (pid.kp >= 0 and pid.kp < 10000) kp = pid.kp*PID_PRECISION_KP;
if (pid.ki >= 0 and pid.ki < 10000) ki = pid.ki*PID_PRECISION_KI;
if (pid.kd >= 0 and pid.kd < 10000) kd = pid.kd*PID_PRECISION_KD;
if (pid.minOutput >= 0 and pid.minOutput < 2000) minOutput = pid.minOutput;
if (pid.startOutput >= 0 and pid.startOutput < 2000) startOutput = pid.startOutput;
if (pid.maxOutput >= 0 and pid.maxOutput < 2000) maxOutput = pid.maxOutput;
updateTuning(); updateOutputLimits();
}
};
Pid topPID ("top", TOP_PID_KP , TOP_PID_KI , TOP_PID_KD , P_ON_E, REVERSE, TOP_PID_ADDRESS);
Pid bottomPID ("bottom", BOTTOM_PID_KP , BOTTOM_PID_KI , BOTTOM_PID_KD , P_ON_E, REVERSE, BOTTOM_PID_ADDRESS);
Pid conveyorPID("conveyor", CONVEYOR_PID_KP, CONVEYOR_PID_KI, CONVEYOR_PID_KD, P_ON_E, DIRECT , CONVEYOR_PID_ADDRESS);
class Profile : public Block {
public:
int bottomTemp;
int topTemp;
double cookTime;
bool isActive = false;
byte id;
Profile(int _topTemp, int _cookTime, int _bottomTemp):
topTemp(_topTemp),
cookTime(_cookTime),
bottomTemp(_bottomTemp)
{};
void draw (void)
{
if (isActive) highlight();
else lowlight();
drawBackground();
myGLCD.setTextColor(*foregroundColor, *backgroundColor);
myGLCD.setTextSize(PROFILE_ID_TEXT_SIZE);
myGLCD.print(String(id+1), startX+9 , startY+9);
myGLCD.setTextSize(PROFILE_PARAM_TEXT_SIZE);
myGLCD.print(String(topTemp), startX+38, startY+6);
myGLCD.print(stringifyDouble(cookTime), startX+38, startY+6+14);
myGLCD.print(String(bottomTemp), startX+38, startY+6+28);
};
void load(void)
{
topTempControl.setpoint = topTemp;
conveyorControl.setpoint = cookTime;
bottomTempControl.setpoint = bottomTemp;
isActive = true;
draw();
topTempControl.setControl.draw();
conveyorControl.setControl.draw();
bottomTempControl.setControl.draw();
};
void unload(void)
{
isActive = false;
draw();
};
void save(void)
{
topTemp = topTempControl.setpoint;
cookTime = conveyorControl.setpoint;
bottomTemp = bottomTempControl.setpoint;
isActive = true;
saveToEEPROM();
draw();
};
void saveToEEPROM(void)
{
ProfileEEPROM profile;
profile.topTemp = topTemp;
profile.cookTime = int( cookTime * 10 );
profile.bottomTemp = bottomTemp;
byte address = PROFILE_ADDRESS + sizeof(ProfileEEPROM) * id;
EEPROM.put(address, profile);
isActive = true;
};
} profiles[] = {
// Profile(topTemp, cookTime, bottomTemp)
Profile(0,10,0),
Profile(320,-10,270),
Profile(320,10,270),
Profile(5,5,5),
Profile(-5,-5,-5),
};
byte const profilesSize = sizeof(profiles) / sizeof(Profile);
/*
class Profiles : public Block {
public:
Profile profile1;
Profile profile2;
Profile profile3;
Profile profile4;
Profile profile5;
Profiles(Profile _profile1, Profile _profile2, Profile _profile3, Profile _profile4, Profile _profile5):
profile1(_profile1),
profile2(_profile2),
profile3(_profile3),
profile4(_profile4),
profile5(_profile5)
{};
void load(byte id)
{
profiles[activeProfile].unload();
profiles[id].load();
activeProfile = id;
}
} profiles2(
// Profile(topTemp, cookTime, bottomTemp)
Profile(110,120,130),
Profile(210,220,230),
Profile(310,320,330),
Profile(410,420,430),
Profile(510,520,530)
);
*/
// ###############################################################
// ##################### GLOBAL FUNCTIONS ####################
// ###############################################################
void calculateProfilesProperties (void)
{
for (byte i=0; i<profilesSize; i++)
{
ProfileEEPROM profile;
byte address = PROFILE_ADDRESS + sizeof(ProfileEEPROM) * i;
EEPROM.get(address, profile);
if (profile.topTemp >= 0 and profile.topTemp < 1000) profiles[i].topTemp = profile.topTemp;
if (profile.cookTime >= -500 and profile.cookTime < 500 ) profiles[i].cookTime = profile.cookTime / 10.0;
if (profile.bottomTemp >= 0 and profile.bottomTemp < 1000) profiles[i].bottomTemp = profile.bottomTemp;
profiles[i].id = i;
profiles[i].startX = gridWidth*i + isOutline;
profiles[i].startY = isOutline;
profiles[i].endX = profiles[i].startX + gridInternalWidth;
profiles[i].endY = topTempControl.startY - 2;
}
}
TSPoint readResistiveTouch(void)
{
TSPoint tp = myTouch.getPoint();
pinMode(YP, OUTPUT); //restore shared pins
pinMode(XM, OUTPUT);
return tp;
}
TSPoint getAvgTouchPoint(void)
{
TSPoint tp;
tp.x=0;
tp.y=0;
tp.z=0;
byte count = 0;
for (byte i=0; i < NUM_OF_SAMPLES; i++)
{
TSPoint tptmp = readResistiveTouch();
if (tptmp.z > 50 )
{
tp.x += tptmp.x;
tp.y += tptmp.y;
tp.z += tptmp.z;
count++;
}
}
if (count < QUORUM) tp.z=0;
else { tp.x /= count; tp.y /= count; tp.z /= count; } // get average
int temp = map(tp.y, 930, 140, 0, dispX-1);
tp.y = map(tp.x, 150, 900, 0, dispY-1);
tp.x = temp;
return tp;
}
bool isPressed (TSPoint tp)
{
return tp.z > 0;
}
bool hasTouchStatusChanged () {
return TouchStatus != lastTouchStatus;
}
void serialGraphPIDs(Pid pid); // Compiler complains otherwise ¬¬. Apparently one cannot use functions with parameters that are instances of classes declared in the same file --_(¬.¬)_--
void serialGraphPIDs(Pid pid)
{
int input = pid.input;
int setpoint = pid.setpoint;
int output = map(pid.output, pid.minOutputGraph(), pid.maxOutputGraph(), 101, 399);
Serial.print(output); Serial.print(" ");
Serial.print(input); Serial.print(" ");
Serial.print(setpoint); Serial.print(" ");
Serial.print(100); Serial.print(" ");
Serial.print(400); Serial.print(" ");
Serial.println();
}
void drawProfiles(void)
{
for (byte i=0; i<profilesSize; i++)
{
profiles[i].draw();
}
}
void drawDivisions(void)
{
myGLCD.setColor(WHITE);
if (isOutline) {myGLCD.drawRect(0,0,dispX-1,dispY-1);}
myGLCD.drawLine(gridWidth-1+isOutline , dispY-1 , gridWidth-1+isOutline , 0);
myGLCD.drawLine(gridWidth*2-1+isOutline, 0 , gridWidth*2-1+isOutline, dispY-gridHeight*3);
myGLCD.drawLine(gridWidth*3-1+isOutline, dispY-1 , gridWidth*3-1+isOutline, 0);
myGLCD.drawLine(gridWidth*4-1+isOutline, dispY-1 , gridWidth*4-1+isOutline, 0);
myGLCD.drawLine(0 , topTempControl.startY-1 , dispX-1 , topTempControl.startY-1);
myGLCD.drawLine(0 , conveyorControl.startY-1 , dispX-1 , conveyorControl.startY-1);
myGLCD.drawLine(0 , bottomTempControl.startY-1, dispX-1 , bottomTempControl.startY-1);
}
void drawEverything(void)
{
switch (state) {
case CONTROLLING_SETPOINTS:
drawDivisions();
drawProfiles();
topTempControl.draw();
conveyorControl.draw();
bottomTempControl.draw();
break;
case CONTROLLING_TOP_PID:
drawDivisions();
drawProfiles();
topTempControl.draw(topPID.kp);
conveyorControl.draw(topPID.ki);
bottomTempControl.draw(topPID.kd);
break;
case CONTROLLING_BOTTOM_PID:
drawDivisions();
drawProfiles();
topTempControl.draw(bottomPID.kp);
conveyorControl.draw(bottomPID.ki);
bottomTempControl.draw(bottomPID.kd);
break;
case CONTROLLING_CONVEYOR_PID:
drawDivisions();
drawProfiles();
topTempControl.draw(conveyorPID.kp);
conveyorControl.draw(conveyorPID.ki);
bottomTempControl.draw(conveyorPID.kd);
break;
case SHOWING_GRAPH:
// Clean screen
myGLCD.setColor(BLACK); myGLCD.fillRect(0,0, dispX-1,dispY-1);
// Draw Grid
myGLCD.setColor(GRAY);
for (int row=0; row < dispY; row+=GRAPH_GRID_HEIGHT) myGLCD.drawLine(0, row, dispX-1, row);
break;
case CONTROLLING_TOP_OUTPUT_LIMITS:
drawDivisions();
drawProfiles();
topTempControl.draw(topPID.minOutput);
conveyorControl.draw(topPID.startOutput);
bottomTempControl.draw(topPID.maxOutput);
break;
case CONTROLLING_BOTTOM_OUTPUT_LIMITS:
drawDivisions();
drawProfiles();
topTempControl.draw(bottomPID.minOutput);
conveyorControl.draw(bottomPID.startOutput);
bottomTempControl.draw(bottomPID.maxOutput);
break;
case CONTROLLING_CONVEYOR_OUTPUT_LIMITS:
drawDivisions();
drawProfiles();
topTempControl.draw(conveyorPID.minOutput);
conveyorControl.draw(conveyorPID.startOutput);
bottomTempControl.draw(conveyorPID.maxOutput);
break;
}
}
void controlSetpoints (void) {
state = CONTROLLING_SETPOINTS;
topTempControl.setControl.value = & topTempControl.setpoint;
conveyorControl.setControl.value = & conveyorControl.setpoint;
bottomTempControl.setControl.value = & bottomTempControl.setpoint;
topTempControl.setControl.lowlight();
conveyorControl.setControl.lowlight();
bottomTempControl.setControl.lowlight();
topTempControl.sensors.lowlight();
conveyorControl.sensors.lowlight();
bottomTempControl.sensors.lowlight();
drawEverything();
}
void controlBottomPID (void) {
state = CONTROLLING_BOTTOM_PID;
topTempControl.setControl.value = & bottomPID.kp;
conveyorControl.setControl.value = & bottomPID.ki;
bottomTempControl.setControl.value = & bottomPID.kd;
topTempControl.setControl.lowlight();
conveyorControl.setControl.lowlight();
bottomTempControl.setControl.lowlight();
topTempControl.sensors.lowlight();
conveyorControl.sensors.lowlight();
bottomTempControl.sensors.highlight();
drawEverything();
}
void controlTopPID (void) {
state = CONTROLLING_TOP_PID;
topTempControl.setControl.value = & topPID.kp;
conveyorControl.setControl.value = & topPID.ki;
bottomTempControl.setControl.value = & topPID.kd;
topTempControl.setControl.lowlight();
conveyorControl.setControl.lowlight();
bottomTempControl.setControl.lowlight();
topTempControl.sensors.highlight();
conveyorControl.sensors.lowlight();
bottomTempControl.sensors.lowlight();
drawEverything();
}
void controlConveyorPID (void) {
state = CONTROLLING_CONVEYOR_PID;
topTempControl.setControl.value = & conveyorPID.kp;
conveyorControl.setControl.value = & conveyorPID.ki;
bottomTempControl.setControl.value = & conveyorPID.kd;
topTempControl.setControl.lowlight();
conveyorControl.setControl.lowlight();
bottomTempControl.setControl.lowlight();
topTempControl.sensors.lowlight();
conveyorControl.sensors.highlight();
bottomTempControl.sensors.lowlight();
drawEverything();
}
void showTopPIDGraph (void) {
inputGraph = &topPID.input;
setpointGraph = &topPID.setpoint;
outputGraph = &topPID.output;
minInputSetpointGraph = topPID.setpoint - TOP_TEMP_GRAPH_RANGE;
maxInputSetpointGraph = topPID.setpoint + TOP_TEMP_GRAPH_RANGE;
minOutputGraph = topPID.minOutputGraph();
maxOutputGraph = topPID.maxOutputGraph();
state = SHOWING_GRAPH;
drawEverything();
}
void showBottomPIDGraph (void) {
inputGraph = &bottomPID.input;
setpointGraph = &bottomPID.setpoint;
outputGraph = &bottomPID.output;
minInputSetpointGraph = bottomPID.setpoint - BOTTOM_TEMP_GRAPH_RANGE;
maxInputSetpointGraph = bottomPID.setpoint + BOTTOM_TEMP_GRAPH_RANGE;
minOutputGraph = bottomPID.minOutputGraph();
maxOutputGraph = bottomPID.maxOutputGraph();
state = SHOWING_GRAPH;
drawEverything();
}
void showConveyorPIDGraph (void) {
inputGraph = &conveyorPID.input;
setpointGraph = &conveyorPID.setpoint;
outputGraph = &conveyorPID.output;
minInputSetpointGraph = conveyorPID.setpoint - CONVEYOR_GRAPH_RANGE;
maxInputSetpointGraph = conveyorPID.setpoint + CONVEYOR_GRAPH_RANGE;
minOutputGraph = conveyorPID.minOutputGraph();
maxOutputGraph = conveyorPID.maxOutputGraph();
state = SHOWING_GRAPH;
drawEverything();
}
void controlTopOutputLimits (void) {
state = CONTROLLING_TOP_OUTPUT_LIMITS;
topTempControl.setControl.value = & topPID.minOutput;
conveyorControl.setControl.value = & topPID.startOutput;
bottomTempControl.setControl.value = & topPID.maxOutput;
topTempControl.setControl.highlight();
conveyorControl.setControl.lowlight();
bottomTempControl.setControl.lowlight();
topTempControl.sensors.lowlight();
conveyorControl.sensors.lowlight();
bottomTempControl.sensors.lowlight();
drawEverything();
}
void controlConveyorOutputLimits (void) {
state = CONTROLLING_CONVEYOR_OUTPUT_LIMITS;
topTempControl.setControl.value = & conveyorPID.minOutput;
conveyorControl.setControl.value = & conveyorPID.startOutput;