Erick / Mbed 2 deprecated ICE_BLE_TEST

Dependencies:   NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed

Fork of ICE by Erick

Committer:
davidjhoward
Date:
Thu Oct 06 15:24:10 2016 +0000
Revision:
190:af7ab603c9fe
Parent:
183:44f7fea6b208
Child:
193:3acbcc2f6fcb
Child:
197:594afd088f32
add new modbus driver and error flags for readings

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