Erick / Mbed 2 deprecated ICE_BLE_TEST

Dependencies:   NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed

Fork of ICE by Erick

Committer:
jmarkel44
Date:
Fri Oct 07 14:47:39 2016 +0000
Revision:
202:d6011a00bfb8
Parent:
198:2fe695f5cb42
Child:
205:3c84af5f711f
changed setpoint control display

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