Changeset 32 for trunk/Arduino/projects


Ignore:
Timestamp:
Oct 21, 2019, 9:57:34 AM (7 years ago)
Author:
markus
Message:

Adaptive PWM handling

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Arduino/projects/OT_boiler/OT_boiler.ino

    r31 r32  
    22#include <Ethernet.h>
    33#include <OT_Slave.h>
     4
     5#include "BoilerDefinition.h"
    46
    57//#define ACTIVITY_LED_PIN      LED_BUILTIN
    … …  
    3234        long            cntVal;
    3335
    34         long            pwm;
    35 
    36   long    a2Val;
    37         long            a2Dir;          // Boiler (VC-40) active detection
    38   bool  a2Rising;
     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;
    3944
    4045        void            ReadData ();
    4146        void            CalcData ();
    4247
    43         virtual double  AD2Temperature (int);
     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);
    4451
    4552public:
    … …  
    100107#endif
    101108        boiler.maintain();
    102 #ifdef _NEVER_
    103 //  unsigned long periode = msTicks / (boiler.IsActive() ? 500 : 2000);
    104         unsigned long periode = msTicks / (boiler.IsActive() ? 2000 : 2000);
    105         static unsigned long prev = 0;
    106         if (prev != periode) {
    107                 prev = periode;
    108 #else
    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 #endif
     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;   
    119118                int led = (periode & 1) ? HIGH : LOW;
    120119                if (Serial) {
    … …  
    137136                        Serial.print("\tDir(A2): ");
    138137                        Serial.print(boiler.a2Dir);
    139       Serial.print("\tRising(A2): ");
    140       Serial.print(boiler.a2Rising);
     138                        Serial.print("\tRising(A2): ");
     139                        Serial.print(boiler.a2Rising);
    141140                        float tReturn = boiler.GetReturnTemperature();
    142141                        Serial.print("\tA3: ");
    … …  
    149148                        Serial.print(boiler.a5);
    150149                        Serial.print("\tPWM: ");
    151 //                      long _pwm = (msTicks / 20000) % 256;
    152 //                      long _pwm = long(0.9097+164.44*(0.4283+boiler.tRequest*(0.0009+boiler.tRequest*(0.00006-boiler.tRequest*0.0000003))));
    153 //                      analogWrite(PWM_PIN, _pwm);
    154                         Serial.print(boiler.pwm);
     150                        Serial.print(boiler.pwmCurrent);
    155151                        Serial.print("\tReq: ");
    156152                        Serial.print(boiler.tRequest);
    157153                        Serial.print("\tQ(A): ");
    158                         Serial.println(double(boiler.a5)/double(boiler.a4));
     154                        Serial.println(boiler.qCurrent);
    159155                }
    160156                digitalWrite(ACTIVITY_LED_PIN, led);
    … …  
    225221}
    226222
    227 OT_Boiler::OT_Boiler ()
    228 :
    229         a2(0),
    230         a3(0),
    231         a4(0),
    232         a5(0),
    233         cnt(0),
    234         cntVal(0),
    235   a2Val(0),
    236         a2Dir(0),
    237   a2Rising(false)
    238 {
    239 }
    240 
    241 void    OT_Boiler::maintain ()
    242 {
    243         ReadData();
    244         OT_Slave::maintain();
    245 }
    246 
    247223void    OT_Boiler::ReadData ()
    248224{
    249225        long a2Prev = a2Val;
    250   a2Val = analogRead(A2);
     226        a2Val = analogRead(A2);
    251227        a2 += a2Val;
    252228        a3 += analogRead(A3);
    … …  
    261237                        } else {
    262238                                a2Dir -= 1;
    263         if (a2Dir < -5) {
    264           a2Rising = false;
    265         }
     239                                if (a2Dir < -5) {
     240                                        a2Rising = false;
     241                                }
    266242                        }
    267243                } else {
    … …  
    270246                        } else {
    271247                                a2Dir += 1;
    272         if (a2Dir > 5) {
    273           a2Rising = true;
    274         }
     248                                if (a2Dir > 5) {
     249                                        a2Rising = true;
     250                                }
    275251                        }
    276252                }
    … …  
    290266}
    291267
    292 double  OT_Boiler::AD2Temperature (int x)
    293 {
    294         const double m = 0.1816;
    295         const double b = -19.234;
    296         return m*double(x)+b;
     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();
    297317}
    298318
    … …  
    304324void    OT_Boiler::SetRequestTemperature (float t)
    305325{
    306         bool update(t != tRequest);
     326        bool update = (t != tRequest);
    307327        OT_Slave::SetRequestTemperature(t);
    308         if (update) {
    309                 pwm = long(0.9097+164.44*(0.4283+boiler.tRequest*(0.0009+boiler.tRequest*(0.00006-boiler.tRequest*0.0000003))));
    310                 analogWrite(PWM_PIN, pwm);
     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);
    311340        }
    312341}
    … …  
    315344{
    316345        CalcData();
    317         tBoiler = AD2Temperature(a2);
     346        tBoiler = Func_T_of_A(a2,A2);
    318347        return tBoiler;
    319348}
    … …  
    322351{
    323352        CalcData();
    324         tReturn = AD2Temperature(a3);
     353        tBoiler = Func_T_of_A(a3,A3);
    325354        return tReturn;
    326355}
Note: See TracChangeset for help on using the changeset viewer.