source: trunk/Arduino/libraries/OT_Slave/OT_Slave.cpp @ 33

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

Prototype OT and VC40 Adapter

File size: 6.4 KB
RevLine 
[11]1#include "OT_Slave.h"
2
[16]3#ifndef _SIM_T_BOILER_
4#define _SIM_T_BOILER_  1
5#endif
6#ifndef _SIM_T_RET_
7#define _SIM_T_RET_     1
8#endif
[11]9
[16]10#define THERMOSTAT_IN   2
11#define THERMOSTAT_OUT  4
[11]12
[12]13uint32_t        OT_Action::OTD2uint32 (const OpenthermData& data)
14{
15        uint32_t val(data.type);
16        val = ((val << 8) | data.id);
17        val = ((val << 8) | data.valueHB);
18        val = ((val << 8) | data.valueLB);
19        return val;
20}
21
[11]22OT_Action OT_Slave::action;
23
24OT_Slave::OT_Slave ()
25:
26  cbAction(0),
27  chEnable(false),
28  chTemperature(0),
29  dhwEnable(false),
[15]30  dhwTemperature(0),
[31]31  tRequest(0),
[15]32  tBoiler(28.0),
[17]33  tReturn(28.0)
[11]34{
35}
36
37void  OT_Slave::begin (OT_Action_CB cb)
38{
39  pinMode(THERMOSTAT_IN, INPUT);
40  digitalWrite(THERMOSTAT_OUT, HIGH);
[17]41  pinMode(THERMOSTAT_OUT, OUTPUT);      // low output = high current, high output = low current
[11]42  cbAction = cb;
43}
44
45/**
46 * Loop will act as boiler (slave) connected to Opentherm thermostat.
47 * It will wait for requests from thermostat and response with unknown data id as a fake response.
48 */
49void  OT_Slave::maintain ()
50{
51  if (OPENTHERM::getMessage(action.request)) {
52    HandleRequest(action);
53  } else if (OPENTHERM::isSent() || OPENTHERM::isIdle() || OPENTHERM::isError()) { // wait for request from thermostat
54    OPENTHERM::listen(THERMOSTAT_IN, -1);
55  }
56}
57
58void  OT_Slave::HandleRequest (OT_Action& action) 
59{
60  if (Serial) {
61    Serial.print("-> ");
62    OPENTHERM::printToSerial(action.request);
63    Serial.println();
64  }
65  delay(100); // Opentherm defines delay between request and response
66  action.response = action.request;
67  switch (action.request.id) {
68    case OT_MSGID_STATUS: // 0  (Master and slave status flags) [M:ReadAck]
69      StatusReq(action);
70      break; 
71    case OT_MSGID_CH_SETPOINT:  // 1  (Control setpoint ie CH water temp. setpoint) [M:WriteAck]
72      action.response.type = OT_MSGTYPE_WRITE_ACK;
73      CH_Temperature(action.request.f88());
74      break;
75    case OT_MSGID_SLAVE_CONFIG: // 3  (Slave configuration flags) [M:ReadAck]
76      ConfigReq(action);
77      break; 
78    case OT_MSGID_FAULT_FLAGS:  // 5  (Application-specific fault flags / OEM fault code)
79      FaultReq(action);
80      break; 
81    case 14:  // 14 (Maximum relative modulation level setting) [M:WriteAck] 
82      action.response.type = OT_MSGTYPE_WRITE_ACK;
83      break;
84    case OT_MSGID_MODULATION_LEVEL: // 17 (Relative modulation level)  [M:ReadAck|DataInvalid]
85      action.response.type = OT_MSGTYPE_DATA_INVALID;
86      break;
[15]87    case OT_MSGID_FEED_TEMP:  // 25 (Boiler water temperature)  [M:ReadAck|DataInvalid]
[16]88#if _SIM_T_BOILER_
[15]89      action.response.type = OT_MSGTYPE_READ_ACK;
90      action.response.f88(GetBoilerTemperature());
[16]91#else
92      action.response.type = OT_MSGTYPE_DATA_INVALID;
93#endif
[11]94      break;
[16]95#if _SIM_T_RET_
[15]96    case OT_MSGID_RETURN_WATER_TEMP: // 28 (Return water temperature)
97      action.response.type = OT_MSGTYPE_READ_ACK;
98      action.response.f88(GetReturnTemperature());
99      break;
[16]100#endif
[11]101    case OT_MSGID_DHW_SETPOINT: // 56 (DHW setpoint)
102      if (action.request.type == OT_MSGTYPE_READ_DATA) {
103        action.response.type = OT_MSGTYPE_READ_ACK;
104        action.response.f88(dhwTemperature);
105        break;
106      } else if (action.request.type == OT_MSGTYPE_WRITE_DATA) {
107        action.response.type = OT_MSGTYPE_WRITE_ACK;
108        DHW_Temperature(action.request.f88());
109      } else {
110        action.response.type = OT_MSGTYPE_UNKNOWN_DATAID; // construct fake response
111      }
112      break; 
113    case OT_MSGID_VERSION_SLAVE:  // 127  (Slave product version number and type)
114      action.response.type = OT_MSGTYPE_READ_ACK;
115      action.response.valueHB = 0;  //  product type
116      action.response.valueLB = 0;  //  product version
117      break; 
118    default:
119      action.response.type = OT_MSGTYPE_UNKNOWN_DATAID; // construct fake response
120      action.response.valueHB = 0;
121      action.response.valueLB = 0;
122  }
123  if (cbAction) {
124    (*cbAction)(action);
125  }
126  OPENTHERM::send(THERMOSTAT_OUT, action.response, OT_Slave::ListenAfterResponse); // send some response and wait for antoher request from thermostat set by callback
127}
128
[31]129void    OT_Slave::ConfigReq (OT_Action& action)
[11]130{
[31]131        action.response.type = OT_MSGTYPE_READ_ACK;
132        action.response.valueHB = 0;                    // 0x11;  //  slave configuration:
133        action.response.valueLB = 0;                    //  Slave MemberID code
[11]134}
135
[31]136void    OT_Slave::StatusReq (OT_Action& action)
[11]137{
[31]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        }
[11]152}
153
[31]154void    OT_Slave::FaultReq (OT_Action& action)
[11]155{
[31]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
[11]159}
160
[31]161bool    OT_Slave::CH_Enable (bool enable)
[11]162{
[31]163        chEnable = enable;
164        return chEnable;
[11]165}
166
[31]167bool    OT_Slave::CH_Disable ()
[11]168{
[31]169        return CH_Enable(false);
[11]170}
171
[31]172void    OT_Slave::CH_Temperature (float temp)
[11]173{
[31]174        chTemperature = temp;
175        SetRequestTemperature(IsEnabled() ? max(chTemperature, dhwTemperature) : min(chTemperature, dhwTemperature));
[11]176}
177
[31]178bool    OT_Slave::DHW_Enable (bool enable)
[11]179{
[31]180        dhwEnable = enable;
181        return dhwEnable;
[11]182}
183
[31]184bool    OT_Slave::DHW_Disable ()
[11]185{
[31]186        return DHW_Enable(false);
[11]187}
188
[31]189void    OT_Slave::DHW_Temperature (float temp)
[11]190{
[31]191        dhwTemperature = temp;
192        SetRequestTemperature(IsEnabled() ? max(chTemperature, dhwTemperature) : min(chTemperature, dhwTemperature));
[11]193}
194
[31]195bool    OT_Slave::IsEnabled ()
196{
197        return (chEnable || dhwEnable);
198}
199
[17]200bool    OT_Slave::IsActive ()
201{
[31]202        return (chEnable || dhwEnable);
[17]203}
204
[31]205void    OT_Slave::SetRequestTemperature (float t)
206{
207        tRequest = t;
208}
209
[15]210float   OT_Slave::GetBoilerTemperature ()
211{
[31]212        if (IsEnabled()) {
[15]213                // increase
[31]214                if (tBoiler < tRequest) {
215                        tBoiler = (tBoiler + 2.0 * tRequest) / 3.0;
[15]216                }
217        } else {
218                // decrease
[17]219                if (tBoiler > tReturn) {
220                        tBoiler = (tBoiler + 2.0 * tReturn) / 3.0;
[15]221                }
222        }
223        return tBoiler; // FAKE NEWS !!!
224}
225
226extern volatile long  loops;
227
228float   OT_Slave::GetReturnTemperature ()
229{
[17]230        float temp = 100.0 * (tReturn - 1);
[15]231        temp += float((loops >> 4) % 100);
232        return temp / 100.0;
233}
234
[31]235void    OT_Slave::ListenAfterResponse () 
[11]236{
[31]237        if (Serial) {
238                Serial.print("<- ");
239                OPENTHERM::printToSerial(action.response);
240                Serial.println();
241                Serial.println();
242        }
243        OPENTHERM::listen(THERMOSTAT_IN, -1);
[11]244}
Note: See TracBrowser for help on using the repository browser.