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

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

SMA based trend analysis

File size: 6.7 KB
RevLine 
[8]1#include <SPI.h>
2#include <Ethernet.h>
[11]3#include <OT_Slave.h>
[33]4#include <OT_Boiler.h>
[8]5
[31]6//#define ACTIVITY_LED_PIN      LED_BUILTIN
7#define ACTIVITY_LED_PIN        9
8#define PWM_PIN         10
[19]9
[31]10#define USE_ETHERNET    0
[19]11
[10]12#define CONSOLE(cmd)  do { if (Serial) Serial.cmd; } while (0)
13
[31]14void    Sent_OT_Data (const OT_Action& data);
[8]15
[31]16byte    mac[] = { 0x02, 0xDE, 0xAD, 0xDE, 0xAD, 0x01 };
17byte    server[] = { 192, 168, 0, 22 };
[19]18
[8]19volatile long  loops = 0;
[19]20volatile unsigned long msTicks = 0;
21
[8]22volatile long  state = 0;
23volatile bool  _connected = false;
24
[21]25class OT_Boiler : public OT_Slave {
[8]26
[21]27protected:
[33]28        SMA<int,float>  a2;
29        SMA<int,float>  a3;
30        SMA<int,float>  a4;
31        SMA<int,float>  a5;
[21]32
[33]33        SMA<float,float>        a2Trend;
34        bool                    a2Rising;       // Boiler (VC-506E) active detection
35
[32]36        float           qRequest;
37        float           qCurrent;       // debug only ???
38        int             pwmRequest;
39        int             pwmCurrent;
[25]40
[31]41        void            ReadData ();
42
[33]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);
[32]45        static double   Func_Q789_of_T (double t);
46        static double   Func_PWM_of_Q789 (double q);
[31]47
[21]48public:
[31]49        OT_Boiler ();
50
51        virtual void    maintain ();
[33]52        virtual void    periodic ();
[31]53
54        virtual bool    IsActive ();
55
56        virtual void    SetRequestTemperature (float);
[21]57        virtual float   GetBoilerTemperature ();
58        virtual float   GetReturnTemperature ();
59
[31]60        friend void     loop ();
[21]61};
62
63OT_Boiler       boiler;
64
[19]65EthernetClient  client;
[8]66
[31]67void    setup()
[8]68{
[31]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"));
[19]81#if USE_ETHERNET
[31]82        // Setup Ethernet
83        CONSOLE(print("Ethernet.begin..."));
84        Ethernet.begin(mac);
85        CONSOLE(println("done"));
[19]86#else
[31]87        CONSOLE(println("Ethernet is disabled!"));
[19]88#endif
[8]89}
90
[31]91void    loop()
[8]92{
[31]93        loops++;
[8]94/*
[31]95        Serial.print("[");
96        Serial.print(loops);
97        Serial.print("|");
98        Serial.print(state,HEX);
99        Serial.println("]");
[8]100*/
[31]101        msTicks = millis();
[19]102#if USE_ETHERNET
[31]103        maintainEthernet();
[19]104#endif
[31]105        boiler.maintain();
[32]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;   
[31]115                int led = (periode & 1) ? HIGH : LOW;
[33]116                boiler.periodic();
[31]117                if (Serial) {
118                        Serial.print("Time: ");
119                        Serial.print(msTicks);
120                        Serial.print("\tPeriode: ");
121                        Serial.print(periode);
122                        Serial.print("\tA2: ");
[33]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);
[31]128                        Serial.print("\tT(A2): ");
[33]129                        Serial.print(boiler.GetBoilerTemperature());
130                        Serial.print("\tA3: ");
131                        Serial.print(boiler.a3.mean());
[31]132                        Serial.print("\tT(A3): ");
[33]133                        Serial.print(boiler.GetReturnTemperature());
[31]134                        Serial.print("\tA4: ");
[33]135                        Serial.print(boiler.a4.mean());
[31]136                        Serial.print("\tA5: ");
[33]137                        Serial.print(boiler.a5.mean());
[31]138                        Serial.print("\tPWM: ");
[32]139                        Serial.print(boiler.pwmCurrent);
[31]140                        Serial.print("\tReq: ");
141                        Serial.print(boiler.tRequest);
142                        Serial.print("\tQ(A): ");
[32]143                        Serial.println(boiler.qCurrent);
[31]144                }
145                digitalWrite(ACTIVITY_LED_PIN, led);
146        }
[8]147}
148
[31]149void    maintainEthernet ()
[8]150{
[31]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        }
[8]185}
186
[11]187void    Sent_OT_Data (const OT_Action& data)
[8]188{
[31]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");
[11]196/*
[31]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);
[11]204*/
[31]205                client.print(data.GetRequestValue(),HEX);
206                client.print("\t");
207                client.print(data.GetResponseValue(),HEX);
208                client.println("");
209        }
[8]210}
[21]211
[31]212void    OT_Boiler::ReadData ()
213{
[33]214        a2.push(analogRead(A2));
215        a3.push(analogRead(A3));
216        a4.push(analogRead(A4));
217        a5.push(analogRead(A5));
[31]218}
219
[33]220double OT_Boiler::Func_T_of_A (float a, int pin)
[31]221{
[32]222        double x(a);
[33]223//      'Messung.xlsx'::Messung-3.1.3M
[32]224        const double a0(-19.234);
225        const double a1(0.1816);
226        return a0 + x * a1;
[31]227}
228
[32]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:
[33]249        a2(13),
250        a3(13),
251        a4(13),
252        a5(13),
253        a2Trend(7),
254        a2Rising(false),
[32]255        qRequest(0),
256        qCurrent(0),
257        pwmRequest(0),
[33]258        pwmCurrent(0)
[32]259{
[33]260        analogRead(A2);
261        analogRead(A3);
262        analogRead(A4);
263        analogRead(A5);
[32]264}
265
266void    OT_Boiler::maintain ()
267{
268        ReadData();
269        OT_Slave::maintain();
270}
271
[33]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
[31]284bool    OT_Boiler::IsActive ()
285{
286        return a2Rising;
287}
288
289void    OT_Boiler::SetRequestTemperature (float t)
290{
[32]291        bool update = (t != tRequest);
[31]292        OT_Slave::SetRequestTemperature(t);
[32]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);
[31]304        }
305}
306
307float   OT_Boiler::GetBoilerTemperature ()
308{
[33]309        tBoiler = Func_T_of_A(a2.mean(), A2);
[21]310        return tBoiler;
311}
312
[31]313float   OT_Boiler::GetReturnTemperature ()
[21]314{
[33]315        tReturn = Func_T_of_A(a3.mean(), A3);
[31]316        return tReturn;
[21]317}
Note: See TracBrowser for help on using the repository browser.