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

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

Adaptive PWM handling

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