-
Notifications
You must be signed in to change notification settings - Fork 3
/
bbqfanonly.cpp
186 lines (152 loc) · 6.4 KB
/
bbqfanonly.cpp
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
#include "bbqfanonly.h"
#include <memory>
#include <algorithm>
#ifdef UNIT_TEST
#include <iostream>
#include <iomanip>
#endif
#include <FuzzyRule.h>
#include <FuzzyComposition.h>
#include <Fuzzy.h>
#include <FuzzyRuleConsequent.h>
#include <FuzzyOutput.h>
#include <FuzzyInput.h>
#include <FuzzyIO.h>
#include <FuzzySet.h>
#include <FuzzyRuleAntecedent.h>
#include <cmath>
#define TEMP_ERROR_INPUT 1
#define TEMP_CHANGE_INPUT 2
#define FAN_OUTPUT 1
#define LID_ALERT_OUTPUT 2
#define CHARCOAL_ALERT_OUTPUT 3
#define CHARCOAL_ALERT_RULE 10
#define TEMP_CHANGE_DELAY 5000
BBQFanOnly::BBQFanOnly(std::shared_ptr<TemperatureSensor> pTempSensor,
std::shared_ptr<Ventilator> pFan) :
BBQ(),
m_tempSensor(pTempSensor),
m_fan(pFan),
m_fuzzy(new Fuzzy()),
m_setPoint(20.0f),
m_periodStartMillis(0) {
m_tempStore.fill(m_tempSensor->get());
}
BBQFanOnly::~BBQFanOnly() {
m_fan->speed(0.f);
}
void BBQFanOnly::config(const BBQFanOnlyConfig& p_config) {
m_config = p_config;
}
BBQFanOnlyConfig BBQFanOnly::config() const {
return m_config;
}
void BBQFanOnly::init() {
delete m_fuzzy;
m_fuzzy = new Fuzzy();
// Create input for Temperature errors
FuzzyInput* tempErrorInput = new FuzzyInput(TEMP_ERROR_INPUT);
m_fuzzy->addFuzzyInput(tempErrorInput);
FuzzySet* tempErrorNegativeHigh = fuzzyFromVector(m_config.temp_error_hight, true);
tempErrorInput->addFuzzySet(tempErrorNegativeHigh);
FuzzySet* tempErrorNegativeMedium = fuzzyFromVector(m_config.temp_error_medium, true);
tempErrorInput->addFuzzySet(tempErrorNegativeMedium);
FuzzySet* tempErrorLow = fuzzyFromVector(m_config.temp_error_low, false);
tempErrorInput->addFuzzySet(tempErrorLow);
FuzzySet* tempErrorPositiveMedium = fuzzyFromVector(m_config.temp_error_medium, false);
tempErrorInput->addFuzzySet(tempErrorPositiveMedium);
FuzzySet* tempErrorPositiveHigh = fuzzyFromVector(m_config.temp_error_hight, false);
tempErrorInput->addFuzzySet(tempErrorPositiveHigh);
// Input for temperature changes
FuzzyInput* tempDrop = new FuzzyInput(TEMP_CHANGE_INPUT);
m_fuzzy->addFuzzyInput(tempDrop);
FuzzySet* tempDecreasesFast = fuzzyFromVector(m_config.temp_change_fast, true);
tempDrop->addFuzzySet(tempDecreasesFast);
FuzzySet* tempDecreasesMedium = fuzzyFromVector(m_config.temp_change_medium, true);
tempDrop->addFuzzySet(tempDecreasesMedium);
FuzzySet* tempChangesSlow = fuzzyFromVector(m_config.temp_change_slow, false);
tempDrop->addFuzzySet(tempChangesSlow);
FuzzySet* tempIncreasedMedium = fuzzyFromVector(m_config.temp_change_medium, false);
tempDrop->addFuzzySet(tempIncreasedMedium);
FuzzySet* tempIncreasesFast = fuzzyFromVector(m_config.temp_change_fast, false);
tempDrop->addFuzzySet(tempIncreasesFast);
// Create Output for Fan
FuzzyOutput* fan = new FuzzyOutput(FAN_OUTPUT);
m_fuzzy->addFuzzyOutput(fan);
//FuzzySet* fanOff = new FuzzySet(0, 0, 0, 0);
//fan->addFuzzySet(fanOff);
FuzzySet* fanLower = fuzzyFromVector(m_config.fan_lower, false);
fan->addFuzzySet(fanLower);
FuzzySet* fanSteady = fuzzyFromVector(m_config.fan_steady, false);
fan->addFuzzySet(fanSteady);
FuzzySet* fanHigher = fuzzyFromVector(m_config.fan_higher, false);
fan->addFuzzySet(fanHigher);
uint8_t rule = 30;
joinSingle(rule++, tempErrorNegativeHigh, fanHigher);
joinSingle(rule++, tempErrorPositiveHigh, fanLower);
// 32
joinSingleAND(rule++, tempErrorNegativeMedium, tempIncreasesFast, fanHigher);
joinSingleAND(rule++, tempErrorNegativeMedium, tempIncreasedMedium, fanHigher);
joinSingleAND(rule++, tempErrorNegativeMedium, tempChangesSlow, fanHigher);
joinSingleAND(rule++, tempErrorNegativeMedium, tempDecreasesMedium, fanHigher);
joinSingleAND(rule++, tempErrorNegativeMedium, tempDecreasesFast, fanHigher);
// 37
joinSingleAND(rule++, tempErrorLow, tempIncreasesFast, fanLower);
joinSingleAND(rule++, tempErrorLow, tempIncreasedMedium, fanLower);
joinSingleAND(rule++, tempErrorLow, tempChangesSlow, fanSteady);
joinSingleAND(rule++, tempErrorLow, tempDecreasesMedium, fanHigher);
joinSingleAND(rule++, tempErrorLow, tempDecreasesFast, fanHigher);
// 42
joinSingleAND(rule++, tempErrorPositiveMedium, tempIncreasesFast, fanLower);
joinSingleAND(rule++, tempErrorPositiveMedium, tempIncreasedMedium, fanLower);
joinSingleAND(rule++, tempErrorPositiveMedium, tempChangesSlow, fanLower);
joinSingleAND(rule++, tempErrorPositiveMedium, tempDecreasesMedium, fanLower);
joinSingleAND(rule++, tempErrorPositiveMedium, tempDecreasesFast, fanLower);
}
void BBQFanOnly::setPoint(float setTemp) {
m_setPoint = setTemp;
}
float BBQFanOnly::setPoint() const {
return m_setPoint;
}
void BBQFanOnly::handle(const uint32_t millis) {
if (millis - m_periodStartMillis < (1000 / UPDATES_PER_SECOND)) {
return;
}
m_periodStartMillis = millis;
// rotate right and store on the first position the latest temperature
std::rotate(m_tempStore.begin(), m_tempStore.begin() + m_tempStore.size() - 1, m_tempStore.end());
m_tempStore[0] = m_tempSensor->get();
// Feed temp error
m_fuzzy->setInput(TEMP_ERROR_INPUT, lastErrorInput());
// Temperature change input
m_fuzzy->setInput(TEMP_CHANGE_INPUT, tempChangeInput());
/////////////////////////////////////////////
// Run fuzzy rules
m_fuzzy->fuzzify();
m_fan->increase(m_fuzzy->defuzzify(FAN_OUTPUT));
}
bool BBQFanOnly::ruleFired(uint8_t i) {
return m_fuzzy->isFiredRule(i);
}
bool BBQFanOnly::lowCharcoal() {
return m_fuzzy->isFiredRule(CHARCOAL_ALERT_RULE);
}
FuzzyRule* BBQFanOnly::joinSingle(int rule, FuzzySet* fi, FuzzySet* fo) {
FuzzyRuleAntecedent* ifCondition = new FuzzyRuleAntecedent();
ifCondition->joinSingle(fi);
FuzzyRuleConsequent* thenConsequence = new FuzzyRuleConsequent();
thenConsequence->addOutput(fo);
FuzzyRule* fr = new FuzzyRule(rule, ifCondition, thenConsequence);
m_fuzzy->addFuzzyRule(fr);
return fr;
}
FuzzyRule* BBQFanOnly::joinSingleAND(int rule, FuzzySet* fi1, FuzzySet* fi2, FuzzySet* fo) {
FuzzyRuleAntecedent* ifCondition = new FuzzyRuleAntecedent();
ifCondition->joinWithAND(fi1, fi2);
FuzzyRuleConsequent* thenConsequence = new FuzzyRuleConsequent();
thenConsequence->addOutput(fo);
FuzzyRule* fr = new FuzzyRule(rule, ifCondition, thenConsequence);
m_fuzzy->addFuzzyRule(fr);
return fr;
}