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 13:55:23 2016 +0000
Revision:
208:784c46652863
Parent:
207:55aabde2d4bf
Child:
239:cfb1a917e7f7
modify manual controls (ACTION_MODIFY)

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