Changeset 31 for trunk/Arduino


Ignore:
Timestamp:
Oct 17, 2019, 8:55:57 AM (7 years ago)
Author:
markus
Message:

Prototype OT and VC40 Adapter

Location:
trunk/Arduino
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Arduino/libraries/OT_Slave/OT_Slave.cpp

    r18 r31  
    2626  cbAction(0),
    2727  chEnable(false),
    28   chActive(false),
    2928  chTemperature(0),
    3029  dhwEnable(false),
    31   dhwActive(false),
    3230  dhwTemperature(0),
     31  tRequest(0),
    3332  tBoiler(28.0),
    3433  tReturn(28.0)
    … …  
    128127}
    129128
    130 void  OT_Slave::ConfigReq (OT_Action& action)
    131 {
    132   action.response.type = OT_MSGTYPE_READ_ACK;
    133   action.response.valueHB = 0; // 0x11;  //  slave configuration:
    134   action.response.valueLB = 0;  //  Slave MemberID code
    135 }
    136 
    137 void  OT_Slave::StatusReq (OT_Action& action)
    138 {
    139   action.response.type = OT_MSGTYPE_READ_ACK;
    140   CH_Enable(action.request.valueHB & 0x01);
    141   DHW_Enable(action.request.valueHB & 0x02);
    142   action.response.valueLB = 0;  //  Slave status
    143   if (chActive) {
    144     action.response.valueLB |= 0x02;
    145   } else {
    146     action.response.valueLB &= ~0x02;
    147   }
    148   if (dhwActive) {
    149     action.response.valueLB |= 0x04;
    150   } else {
    151     action.response.valueLB &= ~0x04;
    152   }
    153 }
    154 
    155 void  OT_Slave::FaultReq (OT_Action& action)
    156 {
    157   action.response.type = OT_MSGTYPE_READ_ACK;
    158   action.response.valueHB = 0;  //  Application-specific fault flags
    159   action.response.valueLB = 0;  //  OEM fault code
    160 }
    161 
    162 bool  OT_Slave::CH_Enable (bool enable)
    163 {
    164   chEnable = enable;
    165   chActive = enable;
    166   return chEnable;
    167 }
    168 
    169 bool  OT_Slave::CH_Disable ()
    170 {
    171   chEnable = false;
    172   chActive = false;
    173   return chEnable;
    174 }
    175 
    176 void  OT_Slave::CH_Temperature (float temp)
    177 {
    178   chTemperature = temp;
    179 }
    180 
    181 bool  OT_Slave::DHW_Enable (bool enable)
    182 {
    183   dhwEnable = enable;
    184   dhwActive = enable;
    185   return dhwEnable;
    186 }
    187 
    188 bool  OT_Slave::DHW_Disable ()
    189 {
    190   dhwEnable = false;
    191   dhwActive = false;
    192   return dhwEnable;
    193 }
    194 
    195 void  OT_Slave::DHW_Temperature (float temp)
    196 {
    197   dhwTemperature = temp;
     129void    OT_Slave::ConfigReq (OT_Action& action)
     130{
     131        action.response.type = OT_MSGTYPE_READ_ACK;
     132        action.response.valueHB = 0;                    // 0x11;  //  slave configuration:
     133        action.response.valueLB = 0;                    //  Slave MemberID code
     134}
     135
     136void    OT_Slave::StatusReq (OT_Action& action)
     137{
     138        action.response.type = OT_MSGTYPE_READ_ACK;
     139        CH_Enable(action.request.valueHB & 0x01);
     140        DHW_Enable(action.request.valueHB & 0x02);
     141        action.response.valueLB = 0;                    //  Slave status
     142        if (chEnable & IsActive()) {
     143                action.response.valueLB |= 0x02;
     144        } else {
     145                action.response.valueLB &= ~0x02;
     146        }
     147        if (dhwEnable & IsActive()) {
     148                action.response.valueLB |= 0x04;
     149        } else {
     150                action.response.valueLB &= ~0x04;
     151        }
     152}
     153
     154void    OT_Slave::FaultReq (OT_Action& action)
     155{
     156        action.response.type = OT_MSGTYPE_READ_ACK;
     157        action.response.valueHB = 0;                    //  Application-specific fault flags
     158        action.response.valueLB = 0;                    //  OEM fault code
     159}
     160
     161bool    OT_Slave::CH_Enable (bool enable)
     162{
     163        chEnable = enable;
     164        return chEnable;
     165}
     166
     167bool    OT_Slave::CH_Disable ()
     168{
     169        return CH_Enable(false);
     170}
     171
     172void    OT_Slave::CH_Temperature (float temp)
     173{
     174        chTemperature = temp;
     175        SetRequestTemperature(IsEnabled() ? max(chTemperature, dhwTemperature) : min(chTemperature, dhwTemperature));
     176}
     177
     178bool    OT_Slave::DHW_Enable (bool enable)
     179{
     180        dhwEnable = enable;
     181        return dhwEnable;
     182}
     183
     184bool    OT_Slave::DHW_Disable ()
     185{
     186        return DHW_Enable(false);
     187}
     188
     189void    OT_Slave::DHW_Temperature (float temp)
     190{
     191        dhwTemperature = temp;
     192        SetRequestTemperature(IsEnabled() ? max(chTemperature, dhwTemperature) : min(chTemperature, dhwTemperature));
     193}
     194
     195bool    OT_Slave::IsEnabled ()
     196{
     197        return (chEnable || dhwEnable);
    198198}
    199199
    200200bool    OT_Slave::IsActive ()
    201201{
    202         return (chActive || dhwActive);
     202        return (chEnable || dhwEnable);
     203}
     204
     205void    OT_Slave::SetRequestTemperature (float t)
     206{
     207        tRequest = t;
    203208}
    204209
    205210float   OT_Slave::GetBoilerTemperature ()
    206211{
    207         if (IsActive()) {
     212        if (IsEnabled()) {
    208213                // increase
    209                 float tTarget = (chTemperature > dhwTemperature) ? chTemperature : dhwTemperature;
    210                 if (tBoiler < tTarget) {
    211                         tBoiler = (tBoiler + 2.0 * tTarget) / 3.0;
     214                if (tBoiler < tRequest) {
     215                        tBoiler = (tBoiler + 2.0 * tRequest) / 3.0;
    212216                }
    213217        } else {
    … …  
    229233}
    230234
    231 void  OT_Slave::ListenAfterResponse ()
    232 {
    233   if (Serial) {
    234     Serial.print("<- ");
    235     OPENTHERM::printToSerial(action.response);
    236     Serial.println();
    237     Serial.println();
    238   }
    239   OPENTHERM::listen(THERMOSTAT_IN, -1);
    240 }
     235void    OT_Slave::ListenAfterResponse ()
     236{
     237        if (Serial) {
     238                Serial.print("<- ");
     239                OPENTHERM::printToSerial(action.response);
     240                Serial.println();
     241                Serial.println();
     242        }
     243        OPENTHERM::listen(THERMOSTAT_IN, -1);
     244}
  • trunk/Arduino/libraries/OT_Slave/OT_Slave.h

    r17 r31  
    2323  static OT_Action     action;
    2424  OT_Action_CB  cbAction;
     25
    2526  bool          chEnable;   // ch : Central Heating
    26   bool          chActive;
    2727  float         chTemperature;
    2828  bool          dhwEnable;  // dhw  : Domain Hot Water
    29   bool          dhwActive;
    3029  float         dhwTemperature;
    3130
    32   float         tBoiler;        // max(ch,dhw,return)
    33   float         tReturn;        // Troom (28.0)
     31  float         tRequest;
     32  float         tBoiler;       
     33  float         tReturn;
    3434
    3535public:
    … …  
    5151  virtual void  DHW_Temperature (float);
    5252
     53  virtual bool  IsEnabled ();
    5354  virtual bool  IsActive ();
    5455
     56  virtual void  SetRequestTemperature (float);
    5557  virtual float GetBoilerTemperature ();
    5658  virtual float GetReturnTemperature ();
  • trunk/Arduino/projects/OT_boiler/OT_boiler.ino

    r29 r31  
    33#include <OT_Slave.h>
    44
    5 //#define ACTIVITY_LED_PIN  LED_BUILTIN
    6 #define ACTIVITY_LED_PIN  9
    7 
    8 #define USE_ETHERNET  0
     5//#define ACTIVITY_LED_PIN      LED_BUILTIN
     6#define ACTIVITY_LED_PIN        9
     7#define PWM_PIN         10
     8
     9#define USE_ETHERNET    0
    910
    1011#define CONSOLE(cmd)  do { if (Serial) Serial.cmd; } while (0)
    1112
    12 void  Sent_OT_Data (const OT_Action& data);
    13 
    14 byte  mac[] = { 0x02, 0xDE, 0xAD, 0xDE, 0xAD, 0x01 };
    15 byte  server[] = { 192, 168, 0, 22 };
     13void    Sent_OT_Data (const OT_Action& data);
     14
     15byte    mac[] = { 0x02, 0xDE, 0xAD, 0xDE, 0xAD, 0x01 };
     16byte    server[] = { 192, 168, 0, 22 };
    1617
    1718volatile long  loops = 0;
    … …  
    2425
    2526protected:
    26         int             a2;
    27   int   a3;
    28   int   a4;
    29   int   a5;
    30 
    31   virtual double AD2Temperature (int);
     27        long            a2;
     28        long            a3;
     29        long            a4;
     30        long            a5;
     31        long            cnt;
     32        long            cntVal;
     33
     34        long            pwm;
     35
     36  long    a2Val;
     37        long            a2Dir;          // Boiler (VC-40) active detection
     38  bool  a2Rising;
     39
     40        void            ReadData ();
     41        void            CalcData ();
     42
     43        virtual double  AD2Temperature (int);
    3244
    3345public:
     46        OT_Boiler ();
     47
     48        virtual void    maintain ();
     49
     50        virtual bool    IsActive ();
     51
     52        virtual void    SetRequestTemperature (float);
    3453        virtual float   GetBoilerTemperature ();
    3554        virtual float   GetReturnTemperature ();
    3655
    37 friend void loop();
     56        friend void     loop ();
    3857};
    3958
    … …  
    4261EthernetClient  client;
    4362
    44 void setup()
    45 {
    46   msTicks = millis();
    47   digitalWrite(ACTIVITY_LED_PIN, LOW);  // Built-in LED off
    48   pinMode(ACTIVITY_LED_PIN, OUTPUT);            // Built-in LED active
    49   // Console
    50   Serial.begin(19200);
    51   CONSOLE(println("\nSystem starting..."));
    52   // Setup OT
    53   CONSOLE(print("OpenTherm.begin..."));
    54   boiler.begin(Sent_OT_Data);
    55   CONSOLE(println("done"));
     63void    setup()
     64{
     65        msTicks = millis();
     66        digitalWrite(ACTIVITY_LED_PIN, LOW);    // Activity LED off
     67        pinMode(ACTIVITY_LED_PIN, OUTPUT);      // Activity LED active
     68        pinMode(PWM_PIN, OUTPUT);               // PWM output
     69        // Console
     70//      Serial.begin(19200);
     71        Serial.begin(115200);
     72        CONSOLE(println("\nSystem starting..."));
     73        // Setup OT
     74        CONSOLE(print("OpenTherm.begin..."));
     75        boiler.begin(Sent_OT_Data);
     76        CONSOLE(println("done"));
    5677#if USE_ETHERNET
    57   // Setup Ethernet
    58   CONSOLE(print("Ethernet.begin..."));
    59   Ethernet.begin(mac);
    60   CONSOLE(println("done"));
     78        // Setup Ethernet
     79        CONSOLE(print("Ethernet.begin..."));
     80        Ethernet.begin(mac);
     81        CONSOLE(println("done"));
    6182#else
    62   CONSOLE(println("Ethernet is disabled!"));
     83        CONSOLE(println("Ethernet is disabled!"));
    6384#endif
    6485}
    6586
    66 void loop()
    67 {
    68   loops++;
     87void    loop()
     88{
     89        loops++;
    6990/*
    70   Serial.print("[");
    71   Serial.print(loops);
    72   Serial.print("|");
    73   Serial.print(state,HEX);
    74   Serial.println("]");
     91        Serial.print("[");
     92        Serial.print(loops);
     93        Serial.print("|");
     94        Serial.print(state,HEX);
     95        Serial.println("]");
    7596*/
    76   msTicks = millis();
     97        msTicks = millis();
    7798#if USE_ETHERNET
    78   maintainEthernet();
     99        maintainEthernet();
    79100#endif
    80   boiler.maintain();
     101        boiler.maintain();
    81102#ifdef _NEVER_
    82   unsigned long base = boiler.IsActive() ? 500 : 3000;
    83   unsigned long periode = msTicks / base;
    84   int led = (periode & 1) ? HIGH : LOW;
    85   if (Serial) {
    86           static unsigned long prev = 0;
    87           if (prev != periode) {
    88                   prev = periode;
    89                   Serial.print("Time: ");
    90                   Serial.print(msTicks);
    91                   Serial.print("\tPeriode: ");
    92                   Serial.print(periode);
    93                   Serial.print("\tLED: ");
    94                   Serial.println(led);
    95           }
     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;
    96113  }
    97   digitalWrite(ACTIVITY_LED_PIN, led);
    98   //digitalWrite(ACTIVITY_LED_PIN, boiler.IsActive() ? HIGH : LOW);
    99 #else
    100   unsigned long periode = msTicks / (boiler.IsActive() ? 500 : 1000);
    101   static unsigned long prev = 0;
    102   if (prev != periode) {
    103           prev = periode;
    104           int led = (periode & 1) ? HIGH : LOW;
    105           if (Serial) {
    106                   Serial.print("Time: ");
    107                   Serial.print(msTicks);
    108                   Serial.print("\tPeriode: ");
    109                   Serial.print(periode);
    110                   Serial.print("\tLED: ");
    111                   Serial.print(led);
    112       float tBoiler = boiler.GetBoilerTemperature();
    113                   Serial.print("\tA2: ");
    114                   Serial.print(boiler.a2);
    115                   Serial.print("\tT(A2): ");
    116                   Serial.print(tBoiler);
    117       float tReturn = boiler.GetReturnTemperature();
    118       Serial.print("\tA3: ");
    119       Serial.print(boiler.a3);
    120       Serial.print("\tT(A3): ");
    121       Serial.print(tReturn);
    122       boiler.a4 = analogRead(A4);   // read the input pin
    123       Serial.print("\tA4: ");
    124       Serial.print(boiler.a4);
    125       boiler.a5 = analogRead(A5);   // read the input pin
    126       Serial.print("\tA5: ");
    127       Serial.println(boiler.a5);
    128           }
    129           digitalWrite(ACTIVITY_LED_PIN, led);
    130   }
     114  static unsigned long periode = 0;
     115  if (msTicks >= limit) {
     116    limit = 0;
     117    periode += 1;   
    131118#endif
    132 }
    133 
    134 void maintainEthernet ()
    135 {
    136   // Check for Ethernet hardware present
    137   if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    138     return;
    139   }
    140   byte rc = Ethernet.maintain();
    141   switch (rc) {
    142     case 1: // renew failed
    143       CONSOLE(println("Ethernet: renew failed!"));
    144       break;
    145     case 2: // renew success
    146       CONSOLE(println("Ethernet: renew success!"));
    147       break;
    148     case 3: // rebind failed
    149       CONSOLE(println("Ethernet: rebind failed!"));
    150       break;
    151     case 4: // rebind success 
    152       CONSOLE(println("Ethernet: rebind success!"));
    153       break;
    154   }
    155   if (client.connected()) {
    156     if (!_connected) {
    157       _connected = true;
    158       CONSOLE(println("Ethernet: connected!")); 
    159     }
    160   } else {
    161     if (_connected) {
    162       _connected = false;
    163       CONSOLE(println("Ethernet: connection lost!"));
    164     }
    165 //    CONSOLE(println("Ethernet: restarting..."));
    166     client.stop();
    167 //    CONSOLE(println("Ethernet: connecting..."));
    168     client.connect(server, 23456); 
    169   }
     119                int led = (periode & 1) ? HIGH : LOW;
     120                if (Serial) {
     121                        boiler.CalcData();
     122                        Serial.print("Time: ");
     123                        Serial.print(msTicks);
     124                        Serial.print("\tPeriode: ");
     125                        Serial.print(periode);
     126/*
     127                        Serial.print("\tLED: ");
     128                        Serial.print(led);
     129*/
     130                        Serial.print("\tCnt: ");
     131                        Serial.print(boiler.cntVal);
     132                        float tBoiler = boiler.GetBoilerTemperature();
     133                        Serial.print("\tA2: ");
     134                        Serial.print(boiler.a2);
     135                        Serial.print("\tT(A2): ");
     136                        Serial.print(tBoiler);
     137                        Serial.print("\tDir(A2): ");
     138                        Serial.print(boiler.a2Dir);
     139      Serial.print("\tRising(A2): ");
     140      Serial.print(boiler.a2Rising);
     141                        float tReturn = boiler.GetReturnTemperature();
     142                        Serial.print("\tA3: ");
     143                        Serial.print(boiler.a3);
     144                        Serial.print("\tT(A3): ");
     145                        Serial.print(tReturn);
     146                        Serial.print("\tA4: ");
     147                        Serial.print(boiler.a4);
     148                        Serial.print("\tA5: ");
     149                        Serial.print(boiler.a5);
     150                        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);
     155                        Serial.print("\tReq: ");
     156                        Serial.print(boiler.tRequest);
     157                        Serial.print("\tQ(A): ");
     158                        Serial.println(double(boiler.a5)/double(boiler.a4));
     159                }
     160                digitalWrite(ACTIVITY_LED_PIN, led);
     161        }
     162}
     163
     164void    maintainEthernet ()
     165{
     166        // Check for Ethernet hardware present
     167        if (Ethernet.hardwareStatus() == EthernetNoHardware) {
     168                return;
     169        }
     170        byte rc = Ethernet.maintain();
     171        switch (rc) {
     172                case 1: // renew failed
     173                        CONSOLE(println("Ethernet: renew failed!"));
     174                        break;
     175                case 2: // renew success
     176                        CONSOLE(println("Ethernet: renew success!"));
     177                        break;
     178                case 3: // rebind failed
     179                        CONSOLE(println("Ethernet: rebind failed!"));
     180                        break;
     181                case 4: // rebind success 
     182                        CONSOLE(println("Ethernet: rebind success!"));
     183                        break;
     184        }
     185        if (client.connected()) {
     186                if (!_connected) {
     187                        _connected = true;
     188                        CONSOLE(println("Ethernet: connected!")); 
     189                }
     190        } else {
     191                if (_connected) {
     192                        _connected = false;
     193                        CONSOLE(println("Ethernet: connection lost!"));
     194                }
     195//              CONSOLE(println("Ethernet: restarting..."));
     196                client.stop();
     197//              CONSOLE(println("Ethernet: connecting..."));
     198                client.connect(server, 23456); 
     199        }
    170200}
    171201
    172202void    Sent_OT_Data (const OT_Action& data)
    173203{
    174   state = OPENTHERM::getMode();
    175   if (client.connected()) {
    176     client.print("[");
    177     client.print(loops);
    178     client.print("|");
    179     client.print(state,HEX);
    180     client.print("]\t");
     204        state = OPENTHERM::getMode();
     205        if (client.connected()) {
     206                client.print("[");
     207                client.print(loops);
     208                client.print("|");
     209                client.print(state,HEX);
     210                client.print("]\t");
    181211/*
    182     client.print(data.type,HEX);
    183     client.print(":");
    184     client.print(data.id,HEX);
    185     client.print(":");
    186     client.print(data.valueHB,HEX);
    187     client.print(":");
    188     client.print(data.valueLB,HEX);
     212                client.print(data.type,HEX);
     213                client.print(":");
     214                client.print(data.id,HEX);
     215                client.print(":");
     216                client.print(data.valueHB,HEX);
     217                client.print(":");
     218                client.print(data.valueLB,HEX);
    189219*/
    190     client.print(data.GetRequestValue(),HEX);
    191     client.print("\t");
    192     client.print(data.GetResponseValue(),HEX);
    193     client.println("");
    194   }
    195 }
    196 
    197 double OT_Boiler::AD2Temperature (int x)
    198 {
    199   const double m = 0.2059;
    200   const double b = -27.951;
    201   return m*double(x)+b;
    202 }
    203 
    204 float OT_Boiler::GetBoilerTemperature ()
    205 {
    206         a2 = analogRead(A2);   // read the input pin
     220                client.print(data.GetRequestValue(),HEX);
     221                client.print("\t");
     222                client.print(data.GetResponseValue(),HEX);
     223                client.println("");
     224        }
     225}
     226
     227OT_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
     241void    OT_Boiler::maintain ()
     242{
     243        ReadData();
     244        OT_Slave::maintain();
     245}
     246
     247void    OT_Boiler::ReadData ()
     248{
     249        long a2Prev = a2Val;
     250  a2Val = analogRead(A2);
     251        a2 += a2Val;
     252        a3 += analogRead(A3);
     253        a4 += analogRead(A4);
     254        a5 += analogRead(A5);
     255        cnt++;
     256        // Calc A2 direction...
     257        if (a2Val != a2Prev) {
     258                if (a2Val < a2Prev) {
     259                        if (a2Dir > 0) {
     260                                a2Dir = -1;
     261                        } else {
     262                                a2Dir -= 1;
     263        if (a2Dir < -5) {
     264          a2Rising = false;
     265        }
     266                        }
     267                } else {
     268                        if (a2Dir < 0) {
     269                                a2Dir = 1;
     270                        } else {
     271                                a2Dir += 1;
     272        if (a2Dir > 5) {
     273          a2Rising = true;
     274        }
     275                        }
     276                }
     277        }
     278}
     279
     280void    OT_Boiler::CalcData ()
     281{
     282        if (cnt > 1) {
     283                cntVal = cnt;
     284                a2 /= cnt;
     285                a3 /= cnt;
     286                a4 /= cnt;
     287                a5 /= cnt;
     288                cnt = 1;
     289        }
     290}
     291
     292double  OT_Boiler::AD2Temperature (int x)
     293{
     294        const double m = 0.1816;
     295        const double b = -19.234;
     296        return m*double(x)+b;
     297}
     298
     299bool    OT_Boiler::IsActive ()
     300{
     301        return a2Rising;
     302}
     303
     304void    OT_Boiler::SetRequestTemperature (float t)
     305{
     306        bool update(t != tRequest);
     307        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);
     311        }
     312}
     313
     314float   OT_Boiler::GetBoilerTemperature ()
     315{
     316        CalcData();
    207317        tBoiler = AD2Temperature(a2);
    208318        return tBoiler;
    209319}
    210320
    211 float OT_Boiler::GetReturnTemperature ()
    212 {
    213   a3 = analogRead(A3);   // read the input pin
    214   tReturn = AD2Temperature(a3);
    215   return tReturn;
    216 }
     321float   OT_Boiler::GetReturnTemperature ()
     322{
     323        CalcData();
     324        tReturn = AD2Temperature(a3);
     325        return tReturn;
     326}
Note: See TracChangeset for help on using the changeset viewer.