Erick / Mbed 2 deprecated ICE_BLE_TEST

Dependencies:   NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed

Fork of ICE by Erick

Committer:
jmarkel44
Date:
Thu Oct 06 15:46:13 2016 +0000
Revision:
194:5aa463e15d27
Parent:
193:3acbcc2f6fcb
Child:
195:21df85341cb3
use .hasMember() before extracting from json files

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