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 20 15:30:21 2016 +0000
Revision:
242:3b0086a6d625
Parent:
239:cfb1a917e7f7
Child:
253:ae850c19cf81
use of new JSON parser

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