Erick / Mbed 2 deprecated ICE_BLE_TEST

Dependencies:   NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed

Fork of ICE by Erick

Committer:
davidjhoward
Date:
Thu Oct 06 21:02:11 2016 +0000
Revision:
198:2fe695f5cb42
Parent:
195:21df85341cb3
Parent:
197:594afd088f32
Child:
202:d6011a00bfb8
merge for modbus write changes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jmarkel44 14:cc916fa8dd11 1 /******************************************************************************
jmarkel44 14:cc916fa8dd11 2 *
jmarkel44 19:9bc8fabeddfa 3 * File: SetpointControl.cpp
jmarkel44 183:44f7fea6b208 4 * Desciption: ICE Setpoint Control class implementation
jmarkel44 14:cc916fa8dd11 5 *
jmarkel44 14:cc916fa8dd11 6 *****************************************************************************/
jmarkel44 13:c80c283f9db2 7 #include "SetpointControl.h"
jmarkel44 14:cc916fa8dd11 8 #include "mDot.h"
jmarkel44 20:653923c2f37a 9 #include "MbedJSONValue.h"
davidjhoward 91:0e8d76030598 10 #include "ModbusMasterApi.h"
jmarkel44 51:66b820f203a5 11 #include "global.h"
jmarkel44 28:c410a61238bb 12 #include <string>
jmarkel44 13:c80c283f9db2 13
jmarkel44 14:cc916fa8dd11 14 extern mDot *GLOBAL_mdot;
jmarkel44 14:cc916fa8dd11 15
jmarkel44 195:21df85341cb3 16 //
jmarkel44 195:21df85341cb3 17 // method: load
jmarkel44 195:21df85341cb3 18 // description: open the configuration file and assign data to the
jmarkel44 56:225786c56315 19 // setpoint control object
jmarkel44 56:225786c56315 20 //
jmarkel44 56:225786c56315 21 // @param controlFile -> name of the control file
jmarkel44 56:225786c56315 22 // @return true if data is assigned; false on error
jmarkel44 195:21df85341cb3 23 //
jmarkel44 28:c410a61238bb 24 bool SetpointControl::load(string _controlFile)
jmarkel44 20:653923c2f37a 25 {
jmarkel44 51:66b820f203a5 26 MbedJSONValue json_value; // JSON parsing element
jmarkel44 51:66b820f203a5 27 controlFile = _controlFile;
jmarkel44 51:66b820f203a5 28
jmarkel44 56:225786c56315 29 // open and read from the control file
jmarkel44 14:cc916fa8dd11 30 mDot::mdot_file file = GLOBAL_mdot->openUserFile(controlFile.c_str(), mDot::FM_RDONLY);
jmarkel44 51:66b820f203a5 31 if ( file.fd < 0 )
jmarkel44 28:c410a61238bb 32 return false;
jmarkel44 20:653923c2f37a 33
jmarkel44 20:653923c2f37a 34 // read the data into a buffer
jmarkel44 177:9ec90c8e3ce1 35 char dataBuf[MAX_FILE_SIZE];
jmarkel44 28:c410a61238bb 36
jmarkel44 28:c410a61238bb 37 int bytes_read = GLOBAL_mdot->readUserFile(file, (void *)dataBuf, sizeof(dataBuf));
jmarkel44 28:c410a61238bb 38 if ( bytes_read != sizeof(dataBuf) ) {
jmarkel44 28:c410a61238bb 39 logError("%s: failed to read %d bytes from %s", __func__, sizeof(dataBuf), controlFile.c_str());
jmarkel44 93:1553fb156915 40 // caller should destroy the object
jmarkel44 28:c410a61238bb 41 return false;
jmarkel44 28:c410a61238bb 42 }
jmarkel44 51:66b820f203a5 43
jmarkel44 51:66b820f203a5 44 // close the file
jmarkel44 28:c410a61238bb 45 GLOBAL_mdot->closeUserFile(file);
jmarkel44 28:c410a61238bb 46
jmarkel44 51:66b820f203a5 47 // parse the json data
jmarkel44 28:c410a61238bb 48 parse(json_value, dataBuf);
jmarkel44 51:66b820f203a5 49
jmarkel44 192:052a419837fa 50 if ( !json_value.hasMember("id") ||
jmarkel44 192:052a419837fa 51 !json_value.hasMember("priority") ||
jmarkel44 192:052a419837fa 52 !json_value.hasMember("input") ||
jmarkel44 192:052a419837fa 53 !json_value.hasMember("output") ||
jmarkel44 192:052a419837fa 54 !json_value.hasMember("setpoint") ||
jmarkel44 192:052a419837fa 55 !json_value.hasMember("prodfact") ||
jmarkel44 192:052a419837fa 56 !json_value.hasMember("actingDir") ||
jmarkel44 192:052a419837fa 57 !json_value.hasMember("halert") ||
jmarkel44 192:052a419837fa 58 !json_value.hasMember("lalert") ||
jmarkel44 192:052a419837fa 59 !json_value.hasMember("hfs") ||
jmarkel44 192:052a419837fa 60 !json_value.hasMember("lfs") ||
jmarkel44 192:052a419837fa 61 !json_value.hasMember("tol") ) {
jmarkel44 192:052a419837fa 62 logError("Setpoint Control file is missing expexted tags");
jmarkel44 192:052a419837fa 63 return false;
jmarkel44 192:052a419837fa 64 }
jmarkel44 192:052a419837fa 65
jmarkel44 96:807f04bd5256 66 id = json_value ["id"].get<string>();
jmarkel44 28:c410a61238bb 67 priority = atoi(json_value["priority"].get<string>().c_str());
jmarkel44 96:807f04bd5256 68 input = json_value ["input"].get<string>();
jmarkel44 96:807f04bd5256 69 output = json_value ["output"].get<string>();
jmarkel44 89:55ac65d7f206 70 setpoint = atof(json_value["setpoint"].get<string>().c_str());
jmarkel44 28:c410a61238bb 71 productFactor = atof(json_value["prodfact"].get<string>().c_str());
jmarkel44 90:7d9731dec0da 72 actingDir = atoi(json_value["actingDir"].get<string>().c_str());
jmarkel44 28:c410a61238bb 73 highAlert = atof(json_value["halert"].get<string>().c_str());
jmarkel44 28:c410a61238bb 74 lowAlert = atof(json_value["lalert"].get<string>().c_str());
jmarkel44 28:c410a61238bb 75 highFailsafe = atof(json_value["hfs"].get<string>().c_str());
jmarkel44 28:c410a61238bb 76 lowFailsafe = atof(json_value["lfs"].get<string>().c_str());
jmarkel44 96:807f04bd5256 77 tolerance = atof(json_value["tol"].get<string>().c_str());
jmarkel44 20:653923c2f37a 78
jmarkel44 56:225786c56315 79 return true; // object created successfully
jmarkel44 51:66b820f203a5 80 }
jmarkel44 51:66b820f203a5 81
jmarkel44 195:21df85341cb3 82 //
jmarkel44 195:21df85341cb3 83 // method: start
jmarkel44 195:21df85341cb3 84 // description: start the setpoint control
jmarkel44 56:225786c56315 85 //
jmarkel44 56:225786c56315 86 // @param none
jmarkel44 56:225786c56315 87 // @return none
jmarkel44 195:21df85341cb3 88 //
jmarkel44 51:66b820f203a5 89 void SetpointControl::start(void)
jmarkel44 51:66b820f203a5 90 {
jmarkel44 56:225786c56315 91 // this is the initial state; what else needs to be done??
jmarkel44 51:66b820f203a5 92 this->currentState = STATE_STARTUP;
jmarkel44 19:9bc8fabeddfa 93 }
jmarkel44 19:9bc8fabeddfa 94
jmarkel44 195:21df85341cb3 95 //
jmarkel44 195:21df85341cb3 96 // method: update
jmarkel44 195:21df85341cb3 97 // description: based on the state of the control, check for
jmarkel44 56:225786c56315 98 // under limit and over limit values, adjust the
jmarkel44 56:225786c56315 99 // state accordingly
jmarkel44 56:225786c56315 100 //
jmarkel44 56:225786c56315 101 // @param none
jmarkel44 56:225786c56315 102 // @return none
jmarkel44 195:21df85341cb3 103 //
jmarkel44 51:66b820f203a5 104 void SetpointControl::update(void)
jmarkel44 51:66b820f203a5 105 {
jmarkel44 51:66b820f203a5 106 switch (this->currentState) {
jmarkel44 131:a290a3934132 107 case STATE_INIT:
jmarkel44 131:a290a3934132 108 // do nothing
jmarkel44 131:a290a3934132 109 break;
jmarkel44 51:66b820f203a5 110 case STATE_STARTUP:
jmarkel44 51:66b820f203a5 111 if ( this->underLimit() ) {
jmarkel44 51:66b820f203a5 112 // start the feed right away
jmarkel44 51:66b820f203a5 113 this->startFeed();
jmarkel44 51:66b820f203a5 114 this->currentState = STATE_CONTROL_ON;
jmarkel44 51:66b820f203a5 115 } else {
jmarkel44 51:66b820f203a5 116 this->currentState = STATE_CONTROL_OFF;
jmarkel44 143:23a572f084dd 117 this->stopFeed();
jmarkel44 51:66b820f203a5 118 }
jmarkel44 51:66b820f203a5 119 break;
jmarkel44 51:66b820f203a5 120 case STATE_CONTROL_ON:
jmarkel44 51:66b820f203a5 121 if ( this->overLimit() ) {
jmarkel44 51:66b820f203a5 122 // stop the feed
jmarkel44 51:66b820f203a5 123 this->stopFeed();
jmarkel44 51:66b820f203a5 124 this->currentState = STATE_CONTROL_OFF;
jmarkel44 51:66b820f203a5 125 } else {
jmarkel44 51:66b820f203a5 126 // do nothing
jmarkel44 51:66b820f203a5 127 }
jmarkel44 51:66b820f203a5 128 break;
jmarkel44 51:66b820f203a5 129 case STATE_CONTROL_OFF:
jmarkel44 51:66b820f203a5 130 if ( this->underLimit() ) {
jmarkel44 51:66b820f203a5 131 // start the feed
jmarkel44 51:66b820f203a5 132 this->startFeed();
jmarkel44 51:66b820f203a5 133 this->currentState = STATE_CONTROL_ON;
jmarkel44 51:66b820f203a5 134 } else {
jmarkel44 51:66b820f203a5 135 // do nothing
jmarkel44 51:66b820f203a5 136 }
jmarkel44 51:66b820f203a5 137 break;
jmarkel44 56:225786c56315 138 //case STATE_CONTROL_DISABLED:
jmarkel44 56:225786c56315 139 //case STATE_CONTROL_PAUSED:
jmarkel44 51:66b820f203a5 140 default:
jmarkel44 51:66b820f203a5 141 break;
jmarkel44 51:66b820f203a5 142 }
jmarkel44 51:66b820f203a5 143 }
jmarkel44 51:66b820f203a5 144
jmarkel44 195:21df85341cb3 145 //
jmarkel44 195:21df85341cb3 146 // method: overLimit
jmarkel44 195:21df85341cb3 147 // description: (see @return)
jmarkel44 56:225786c56315 148 //
jmarkel44 56:225786c56315 149 // @param none
jmarkel44 56:225786c56315 150 // @return true if product is over the upper limit for normal mode
jmarkel44 56:225786c56315 151 // or under the limit for reverse mode; false otherwise
jmarkel44 195:21df85341cb3 152 //
jmarkel44 51:66b820f203a5 153 bool SetpointControl::overLimit(void)
jmarkel44 51:66b820f203a5 154 {
davidjhoward 91:0e8d76030598 155 ModbusValue value;
jmarkel44 92:9a6a1adca19c 156 ModbusMasterReadRegister( input, &value );
jmarkel44 92:9a6a1adca19c 157 float flimit;
davidjhoward 91:0e8d76030598 158
jmarkel44 92:9a6a1adca19c 159 if ( !actingDir ) {
jmarkel44 96:807f04bd5256 160 flimit = setpoint + tolerance;
jmarkel44 92:9a6a1adca19c 161 return (value.value > flimit);
jmarkel44 92:9a6a1adca19c 162 } else {
jmarkel44 115:1558e01d04c6 163 flimit = setpoint - tolerance;
jmarkel44 92:9a6a1adca19c 164 return (value.value < flimit);
davidjhoward 91:0e8d76030598 165 }
jmarkel44 51:66b820f203a5 166 }
jmarkel44 51:66b820f203a5 167
jmarkel44 195:21df85341cb3 168 //
jmarkel44 195:21df85341cb3 169 // method: underLimit
jmarkel44 195:21df85341cb3 170 // description: (see @return)
jmarkel44 56:225786c56315 171 //
jmarkel44 56:225786c56315 172 // @param none
jmarkel44 56:225786c56315 173 // @return true if product is under lower limit for normal mode or
jmarkel44 56:225786c56315 174 // over the upper limit for reverse mode; false otherwise
jmarkel44 195:21df85341cb3 175 //
jmarkel44 51:66b820f203a5 176 bool SetpointControl::underLimit(void)
jmarkel44 51:66b820f203a5 177 {
davidjhoward 91:0e8d76030598 178 ModbusValue value;
jmarkel44 92:9a6a1adca19c 179 ModbusMasterReadRegister( input, &value );
jmarkel44 92:9a6a1adca19c 180 float flimit;
davidjhoward 91:0e8d76030598 181
jmarkel44 92:9a6a1adca19c 182 if ( !actingDir ) {
jmarkel44 115:1558e01d04c6 183 flimit = setpoint - tolerance;
jmarkel44 92:9a6a1adca19c 184 return (value.value < flimit);
jmarkel44 92:9a6a1adca19c 185 } else {
jmarkel44 96:807f04bd5256 186 flimit = setpoint + tolerance;
jmarkel44 92:9a6a1adca19c 187 return (value.value > flimit);
davidjhoward 91:0e8d76030598 188 }
jmarkel44 51:66b820f203a5 189 }
jmarkel44 51:66b820f203a5 190
jmarkel44 195:21df85341cb3 191 //
jmarkel44 195:21df85341cb3 192 // method: startFeed()
jmarkel44 195:21df85341cb3 193 // description: send ON indication to Output Master for this control's
jmarkel44 56:225786c56315 194 // relay
jmarkel44 56:225786c56315 195 //
jmarkel44 56:225786c56315 196 // @param none
jmarkel44 56:225786c56315 197 // @return none
jmarkel44 195:21df85341cb3 198 //
jmarkel44 51:66b820f203a5 199 void SetpointControl::startFeed(void)
jmarkel44 51:66b820f203a5 200 {
jmarkel44 56:225786c56315 201 logInfo("%s: %s attempting to start feed on relay %s\n",
jmarkel44 56:225786c56315 202 __func__, controlFile.c_str(), output.c_str());
jmarkel44 56:225786c56315 203
jmarkel44 71:34856d21f2bf 204 OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc();
jmarkel44 71:34856d21f2bf 205 memset(output_mail, 0, sizeof(OutputControlMsg_t));
jmarkel44 115:1558e01d04c6 206
jmarkel44 115:1558e01d04c6 207 output_mail->action = ACTION_CONTROL_ON;
jmarkel44 115:1558e01d04c6 208 output_mail->controlType = CONTROL_SETPOINT;
jmarkel44 115:1558e01d04c6 209 strncpy(output_mail->input_tag, this->input.c_str(), sizeof(output_mail->input_tag)-1);
jmarkel44 115:1558e01d04c6 210 strncpy(output_mail->output_tag, this->output.c_str(), sizeof(output_mail->output_tag)-1);
jmarkel44 71:34856d21f2bf 211 output_mail->priority = this->priority;
jmarkel44 71:34856d21f2bf 212 strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1);
jmarkel44 71:34856d21f2bf 213 OutputMasterMailBox.put(output_mail);
jmarkel44 51:66b820f203a5 214 }
jmarkel44 51:66b820f203a5 215
jmarkel44 195:21df85341cb3 216 //
jmarkel44 195:21df85341cb3 217 // method: stopFeed
jmarkel44 195:21df85341cb3 218 // description: send OFF indication to Output Master for this control's
jmarkel44 56:225786c56315 219 // relay
jmarkel44 56:225786c56315 220 //
jmarkel44 56:225786c56315 221 // @param none
jmarkel44 56:225786c56315 222 // @return none
jmarkel44 195:21df85341cb3 223 //
jmarkel44 51:66b820f203a5 224 void SetpointControl::stopFeed(void)
jmarkel44 51:66b820f203a5 225 {
jmarkel44 71:34856d21f2bf 226 logInfo("%s: %s attempting to stop feed on relay %s\n",
jmarkel44 56:225786c56315 227 __func__, controlFile.c_str(), output.c_str());
jmarkel44 56:225786c56315 228
jmarkel44 71:34856d21f2bf 229 OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc();
jmarkel44 71:34856d21f2bf 230 memset(output_mail, 0, sizeof(OutputControlMsg_t));
jmarkel44 115:1558e01d04c6 231
jmarkel44 115:1558e01d04c6 232 output_mail->action = ACTION_CONTROL_OFF;
jmarkel44 115:1558e01d04c6 233 output_mail->controlType = CONTROL_SETPOINT;
jmarkel44 115:1558e01d04c6 234 strncpy(output_mail->input_tag, this->input.c_str(), sizeof(output_mail->input_tag)-1);
jmarkel44 115:1558e01d04c6 235 strncpy(output_mail->output_tag, this->output.c_str(), sizeof(output_mail->output_tag)-1);
jmarkel44 71:34856d21f2bf 236 output_mail->priority = this->priority;
jmarkel44 71:34856d21f2bf 237 strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1);
jmarkel44 71:34856d21f2bf 238 OutputMasterMailBox.put(output_mail);
jmarkel44 72:3754b352f156 239 }
jmarkel44 72:3754b352f156 240
jmarkel44 195:21df85341cb3 241 //
jmarkel44 195:21df85341cb3 242 // dethod: unregisterControl
jmarkel44 195:21df85341cb3 243 // description: send OFF indication to Output Master for this control's
jmarkel44 72:3754b352f156 244 // relay
jmarkel44 72:3754b352f156 245 //
jmarkel44 72:3754b352f156 246 // @param none
jmarkel44 72:3754b352f156 247 // @return none
jmarkel44 195:21df85341cb3 248 //
jmarkel44 72:3754b352f156 249 void SetpointControl::unregisterControl(void)
jmarkel44 72:3754b352f156 250 {
jmarkel44 115:1558e01d04c6 251 logInfo("%s: %s attempting to unregister %s\n",
jmarkel44 115:1558e01d04c6 252 __func__, controlFile.c_str());
jmarkel44 72:3754b352f156 253
jmarkel44 72:3754b352f156 254 OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc();
jmarkel44 72:3754b352f156 255 memset(output_mail, 0, sizeof(OutputControlMsg_t));
jmarkel44 192:052a419837fa 256
jmarkel44 115:1558e01d04c6 257 output_mail->action = ACTION_CONTROL_UNREGISTER;
jmarkel44 115:1558e01d04c6 258 output_mail->controlType = CONTROL_MANUAL;
jmarkel44 115:1558e01d04c6 259 output_mail->priority = this->priority;
jmarkel44 115:1558e01d04c6 260 strncpy(output_mail->output_tag, this->output.c_str(), sizeof(output_mail->output_tag)-1);
jmarkel44 72:3754b352f156 261 strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1);
jmarkel44 192:052a419837fa 262
jmarkel44 72:3754b352f156 263 OutputMasterMailBox.put(output_mail);
jmarkel44 93:1553fb156915 264 }
jmarkel44 93:1553fb156915 265
jmarkel44 195:21df85341cb3 266 //
jmarkel44 195:21df85341cb3 267 // method: display
jmarkel44 195:21df85341cb3 268 // description: display the control's information
jmarkel44 183:44f7fea6b208 269 //
jmarkel44 183:44f7fea6b208 270 // @param none
jmarkel44 183:44f7fea6b208 271 // @return none
jmarkel44 195:21df85341cb3 272 //
jmarkel44 93:1553fb156915 273 void SetpointControl::display(void)
jmarkel44 93:1553fb156915 274 {
jmarkel44 183:44f7fea6b208 275 // NOTE: this mapping must be 1:1 with "State" enumeration in SetpointControl.h
jmarkel44 131:a290a3934132 276 string mapper[] = { "INIT",
jmarkel44 131:a290a3934132 277 "STARTUP",
jmarkel44 93:1553fb156915 278 "CONTROL_OFF",
jmarkel44 93:1553fb156915 279 "CONTROL_ON",
jmarkel44 93:1553fb156915 280 "CONTROL_DISABLE",
jmarkel44 93:1553fb156915 281 "CONTROL_PAUSE",
jmarkel44 115:1558e01d04c6 282 "CONTROL_MAX"
jmarkel44 115:1558e01d04c6 283 };
jmarkel44 115:1558e01d04c6 284
jmarkel44 115:1558e01d04c6 285
jmarkel44 183:44f7fea6b208 286 ModbusValue inputValue;
jmarkel44 183:44f7fea6b208 287 ModbusMasterReadRegister(input, &inputValue);
jmarkel44 192:052a419837fa 288
jmarkel44 93:1553fb156915 289 printf("\r controlFile : %s \n", controlFile.c_str());
jmarkel44 93:1553fb156915 290 printf("\r id : %s \n", id.c_str());
jmarkel44 93:1553fb156915 291 printf("\r priority : %d \n", priority);
jmarkel44 93:1553fb156915 292 printf("\r input : %s \n", input.c_str());
jmarkel44 93:1553fb156915 293 printf("\r output : %s \n", output.c_str());
jmarkel44 93:1553fb156915 294 printf("\r setpoint : %0.2f\n", setpoint);
jmarkel44 93:1553fb156915 295 printf("\r prodfact : %0.2f\n", productFactor);
jmarkel44 94:18ae2fd82638 296 printf("\r actingDir : %s \n", (actingDir) ? "DIRECT" : "INDIRECT");
jmarkel44 93:1553fb156915 297 printf("\r halert : %0.2f\n", highAlert);
jmarkel44 93:1553fb156915 298 printf("\r lalert : %0.2f\n", lowAlert);
jmarkel44 93:1553fb156915 299 printf("\r hfs : %0.2f\n", highFailsafe);
jmarkel44 93:1553fb156915 300 printf("\r lfs : %0.2f\n", lowFailsafe);
jmarkel44 115:1558e01d04c6 301 printf("\r tol : %0.2f\n", tolerance);
jmarkel44 93:1553fb156915 302 printf("\r\n");
jmarkel44 96:807f04bd5256 303 printf("\r currentState : %s\n", mapper[currentState].c_str());
jmarkel44 96:807f04bd5256 304 printf("\r high band : %0.2f\n", setpoint + tolerance);
jmarkel44 194:5aa463e15d27 305 printf("\r current reading : %0.2f [0x%04x]\n", inputValue.value, inputValue.errflag );
jmarkel44 96:807f04bd5256 306 printf("\r low band : %0.2f\n", setpoint - tolerance);
jmarkel44 93:1553fb156915 307 printf("\r\n");
jmarkel44 51:66b820f203a5 308 }