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

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

Simulation of T_Boiler and T_Ret

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