source: trunk/Arduino/projects/OT_boiler/OT_boiler.ino

Last change on this file was 33, checked in by markus, 7 years ago

SMA based trend analysis

File size: 6.7 KB
Line 
1#include <SPI.h>
2#include <Ethernet.h>
3#include <OT_Slave.h>
4#include <OT_Boiler.h>
5
6//#define ACTIVITY_LED_PIN      LED_BUILTIN
7#define ACTIVITY_LED_PIN        9
8#define PWM_PIN         10
9
10#define USE_ETHERNET    0
11
12#define CONSOLE(cmd)  do { if (Serial) Serial.cmd; } while (0)
13
14void    Sent_OT_Data (const OT_Action& data);
15
16byte    mac[] = { 0x02, 0xDE, 0xAD, 0xDE, 0xAD, 0x01 };
17byte    server[] = { 192, 168, 0, 22 };
18
19volatile long  loops = 0;
20volatile unsigned long msTicks = 0;
21
22volatile long  state = 0;
23volatile bool  _connected = false;
24
25class OT_Boiler : public OT_Slave {
26
27protected:
28        SMA<int,float>  a2;
29        SMA<int,float>  a3;
30        SMA<int,float>  a4;
31        SMA<int,float>  a5;
32
33        SMA<float,float>        a2Trend;
34        bool                    a2Rising;       // Boiler (VC-506E) active detection
35
36        float           qRequest;
37        float           qCurrent;       // debug only ???
38        int             pwmRequest;
39        int             pwmCurrent;
40
41        void            ReadData ();
42
43        static double   Func_T_of_A (int a, int pin) { return Func_T_of_A(float(a), pin); }
44        static double   Func_T_of_A (float a, int pin);
45        static double   Func_Q789_of_T (double t);
46        static double   Func_PWM_of_Q789 (double q);
47
48public:
49        OT_Boiler ();
50
51        virtual void    maintain ();
52        virtual void    periodic ();
53
54        virtual bool    IsActive ();
55
56        virtual void    SetRequestTemperature (float);
57        virtual float   GetBoilerTemperature ();
58        virtual float   GetReturnTemperature ();
59
60        friend void     loop ();
61};
62
63OT_Boiler       boiler;
64
65EthernetClient  client;
66
67void    setup()
68{
69        msTicks = millis();
70        digitalWrite(ACTIVITY_LED_PIN, LOW);    // Activity LED off
71        pinMode(ACTIVITY_LED_PIN, OUTPUT);      // Activity LED active
72        pinMode(PWM_PIN, OUTPUT);               // PWM output
73        // Console
74//      Serial.begin(19200);
75        Serial.begin(115200);
76        CONSOLE(println("\nSystem starting..."));
77        // Setup OT
78        CONSOLE(print("OpenTherm.begin..."));
79        boiler.begin(Sent_OT_Data);
80        CONSOLE(println("done"));
81#if USE_ETHERNET
82        // Setup Ethernet
83        CONSOLE(print("Ethernet.begin..."));
84        Ethernet.begin(mac);
85        CONSOLE(println("done"));
86#else
87        CONSOLE(println("Ethernet is disabled!"));
88#endif
89}
90
91void    loop()
92{
93        loops++;
94/*
95        Serial.print("[");
96        Serial.print(loops);
97        Serial.print("|");
98        Serial.print(state,HEX);
99        Serial.println("]");
100*/
101        msTicks = millis();
102#if USE_ETHERNET
103        maintainEthernet();
104#endif
105        boiler.maintain();
106        static unsigned long limit = 0;
107        if (!limit) {
108                unsigned long interval = boiler.IsActive() ? 500 : 2000;
109                limit = ((msTicks / interval) + 1 ) * interval;
110        }
111        static unsigned long periode = 0;
112        if (msTicks >= limit) {
113                limit = 0;
114                periode += 1;   
115                int led = (periode & 1) ? HIGH : LOW;
116                boiler.periodic();
117                if (Serial) {
118                        Serial.print("Time: ");
119                        Serial.print(msTicks);
120                        Serial.print("\tPeriode: ");
121                        Serial.print(periode);
122                        Serial.print("\tA2: ");
123                        Serial.print(boiler.a2.mean());
124                        Serial.print("\tA2.Trend: ");
125                        Serial.print(boiler.a2Trend.slope());
126                        Serial.print("\tA2.Rising: ");
127                        Serial.print(boiler.a2Rising);
128                        Serial.print("\tT(A2): ");
129                        Serial.print(boiler.GetBoilerTemperature());
130                        Serial.print("\tA3: ");
131                        Serial.print(boiler.a3.mean());
132                        Serial.print("\tT(A3): ");
133                        Serial.print(boiler.GetReturnTemperature());
134                        Serial.print("\tA4: ");
135                        Serial.print(boiler.a4.mean());
136                        Serial.print("\tA5: ");
137                        Serial.print(boiler.a5.mean());
138                        Serial.print("\tPWM: ");
139                        Serial.print(boiler.pwmCurrent);
140                        Serial.print("\tReq: ");
141                        Serial.print(boiler.tRequest);
142                        Serial.print("\tQ(A): ");
143                        Serial.println(boiler.qCurrent);
144                }
145                digitalWrite(ACTIVITY_LED_PIN, led);
146        }
147}
148
149void    maintainEthernet ()
150{
151        // Check for Ethernet hardware present
152        if (Ethernet.hardwareStatus() == EthernetNoHardware) {
153                return;
154        }
155        byte rc = Ethernet.maintain();
156        switch (rc) {
157                case 1: // renew failed
158                        CONSOLE(println("Ethernet: renew failed!"));
159                        break;
160                case 2: // renew success
161                        CONSOLE(println("Ethernet: renew success!"));
162                        break;
163                case 3: // rebind failed
164                        CONSOLE(println("Ethernet: rebind failed!"));
165                        break;
166                case 4: // rebind success 
167                        CONSOLE(println("Ethernet: rebind success!"));
168                        break;
169        }
170        if (client.connected()) {
171                if (!_connected) {
172                        _connected = true;
173                        CONSOLE(println("Ethernet: connected!")); 
174                }
175        } else {
176                if (_connected) {
177                        _connected = false;
178                        CONSOLE(println("Ethernet: connection lost!"));
179                }
180//              CONSOLE(println("Ethernet: restarting..."));
181                client.stop();
182//              CONSOLE(println("Ethernet: connecting..."));
183                client.connect(server, 23456); 
184        }
185}
186
187void    Sent_OT_Data (const OT_Action& data)
188{
189        state = OPENTHERM::getMode();
190        if (client.connected()) {
191                client.print("[");
192                client.print(loops);
193                client.print("|");
194                client.print(state,HEX);
195                client.print("]\t");
196/*
197                client.print(data.type,HEX);
198                client.print(":");
199                client.print(data.id,HEX);
200                client.print(":");
201                client.print(data.valueHB,HEX);
202                client.print(":");
203                client.print(data.valueLB,HEX);
204*/
205                client.print(data.GetRequestValue(),HEX);
206                client.print("\t");
207                client.print(data.GetResponseValue(),HEX);
208                client.println("");
209        }
210}
211
212void    OT_Boiler::ReadData ()
213{
214        a2.push(analogRead(A2));
215        a3.push(analogRead(A3));
216        a4.push(analogRead(A4));
217        a5.push(analogRead(A5));
218}
219
220double OT_Boiler::Func_T_of_A (float a, int pin)
221{
222        double x(a);
223//      'Messung.xlsx'::Messung-3.1.3M
224        const double a0(-19.234);
225        const double a1(0.1816);
226        return a0 + x * a1;
227}
228
229double  OT_Boiler::Func_Q789_of_T (double t)
230{
231//      'Heizung.xlsx'::'789-Calc'
232        const double a0(0.4283);
233        const double a1(0.0009);
234        const double a2(0.00006);
235        const double a3(-0.0000003);
236        return a0 + t * (a1 + t * (a2 + t * a3));
237}
238
239double  OT_Boiler::Func_PWM_of_Q789 (double q)
240{
241//      'Heizung.xlsx'::'789-Mess'
242        const double a0(-0.9097);
243        const double a1(164.44);
244        return a0 + q * a1;
245}
246
247OT_Boiler::OT_Boiler ()
248:
249        a2(13),
250        a3(13),
251        a4(13),
252        a5(13),
253        a2Trend(7),
254        a2Rising(false),
255        qRequest(0),
256        qCurrent(0),
257        pwmRequest(0),
258        pwmCurrent(0)
259{
260        analogRead(A2);
261        analogRead(A3);
262        analogRead(A4);
263        analogRead(A5);
264}
265
266void    OT_Boiler::maintain ()
267{
268        ReadData();
269        OT_Slave::maintain();
270}
271
272void    OT_Boiler::periodic ()
273{
274        a2Trend.push(a2.mean());
275        if (a2Rising) {
276//              a2Rising = (a2Trend.slope() > (0.00025 * a2Trend.mean()));
277                a2Rising = (a2Trend.slope() >= 0.0);
278        } else {
279//              a2Rising = a2Trend.loaded() && (a2Trend.slope() > (0.0025 * a2Trend.mean()));
280                a2Rising = a2Trend.loaded() && (a2Trend.slope() > 0.1);
281        }
282}
283
284bool    OT_Boiler::IsActive ()
285{
286        return a2Rising;
287}
288
289void    OT_Boiler::SetRequestTemperature (float t)
290{
291        bool update = (t != tRequest);
292        OT_Slave::SetRequestTemperature(t);
293        qRequest = Func_Q789_of_T(tRequest);
294        pwmRequest = Func_PWM_of_Q789(qRequest);
295        qCurrent = a5/a4;
296        int diff = round(100.0 * (qCurrent - qRequest));
297        if (diff) {
298                if (update) {
299                        pwmCurrent = pwmRequest;
300                } else {
301                        pwmCurrent += ((diff > 0) ? -1 : 1);
302                }
303                analogWrite(PWM_PIN, pwmCurrent);
304        }
305}
306
307float   OT_Boiler::GetBoilerTemperature ()
308{
309        tBoiler = Func_T_of_A(a2.mean(), A2);
310        return tBoiler;
311}
312
313float   OT_Boiler::GetReturnTemperature ()
314{
315        tReturn = Func_T_of_A(a3.mean(), A3);
316        return tReturn;
317}
Note: See TracBrowser for help on using the repository browser.