-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathHeaterService.h
116 lines (99 loc) · 3.29 KB
/
HeaterService.h
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
#ifndef HeaterService_h
#define HeaterService_h
#include <TemperatureService.h>
#include <ActiveStatus.h>
#include <PID_v1.h>
#include <PID_AutoTune_v0.h>
#include <enum.h>
#include <BrewSettingsService.h>
struct HeaterServiceStatus
{
double PWM;
double PWMPercentage;
boolean PIDActing;
};
class HeaterService
{
public:
HeaterService(TemperatureService *temperatureService,
ActiveStatus *activeStatus,
BrewSettingsService *brewSettingsService) : _temperatureService(temperatureService),
_activeStatus(activeStatus),
_brewSettingsService(brewSettingsService)
{
}
HeaterServiceStatus Compute(double input, double target, double heaterPercentage)
{
HeaterServiceStatus status;
uint8_t _heaterBus = GetBus();
SetUP();
if (StopCompute())
{
status.PIDActing = false;
status.PWM = 0;
status.PWMPercentage = 0;
TurnOff();
return status;
}
SetPidParameters(input, target);
if (_activeStatus->PIDSettingsUpdated)
{
_activeStatus->PIDSettingsUpdated = false;
StartPID(_brewSettingsService->KP, _brewSettingsService->KI, _brewSettingsService->KD);
Serial.println("BrewSettings Updated: " + String(_brewSettingsService->KP) + "/" + String(_brewSettingsService->KI) + "/" + String(_brewSettingsService->KD));
return status;
}
if (_activeStatus->ActiveStep == boil)
{
status.PIDActing = false;
status.PWM = ((1023 * _brewSettingsService->BoilPowerPercentage) / 100);
status.PWMPercentage = (status.PWM * 100) / 1023;
analogWrite(_heaterBus, InvertedPWM() ? abs(status.PWM - 1023) : status.PWM);
return status;
}
if (_activeStatus->FullPower)
heaterPercentage = 100;
if (GetPidSetPoint() - GetPidInput() > _brewSettingsService->PIDStart)
{
status.PIDActing = false;
status.PWM = ((1023 * heaterPercentage) / 100);
status.PWMPercentage = (status.PWM * 100) / 1023;
analogWrite(_heaterBus, status.PWM);
return status;
}
// to prevent pid overshoot
if (GetPidInput() > GetPidSetPoint() + 0.1)
{
status.PWM = 0;
status.PWMPercentage = 0;
status.PIDActing = false;
analogWrite(_heaterBus, _activeStatus->PWM);
StartPID(_brewSettingsService->KP, _brewSettingsService->KI, _brewSettingsService->KD);
return status;
}
PidCompute();
int maxPWM = ((1023 * heaterPercentage) / 100);
status.PWM = GetPidOutput() > maxPWM ? maxPWM : GetPidOutput();
status.PWMPercentage = (status.PWM * 100) / 1023;
analogWrite(_heaterBus, status.PWM);
status.PIDActing = status.PWM > 0;
return status;
}
protected:
virtual void SetUP();
virtual boolean StopCompute();
virtual void StartPID(double kp, double ki, double kd);
virtual void PidCompute();
virtual double GetPidOutput();
virtual double GetPidInput();
virtual double GetPidSetPoint();
virtual uint8_t GetBus();
virtual void TurnOff();
virtual bool InvertedPWM();
virtual void SetPidParameters(double input, double setpoint);
TemperatureService *_temperatureService;
ActiveStatus *_activeStatus;
BrewSettingsService *_brewSettingsService;
PID *_kettlePID;
};
#endif