Erick / Mbed 2 deprecated ICE_BLE_TEST

Dependencies:   NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed

Fork of ICE by Erick

Committer:
jmarkel44
Date:
Mon Oct 10 12:51:32 2016 +0000
Revision:
207:55aabde2d4bf
Parent:
205:3c84af5f711f
Child:
208:784c46652863
modify-sp command added;

Who changed what in which revision?

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