Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed
Fork of ICE by
src/ConfigurationHandler/Controls/SetpointControl.cpp@91:0e8d76030598, 2016-09-20 (annotated)
- Committer:
- davidjhoward
- Date:
- Tue Sep 20 18:26:08 2016 +0000
- Revision:
- 91:0e8d76030598
- Parent:
- 90:7d9731dec0da
- Child:
- 92:9a6a1adca19c
setpoint control work
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jmarkel44 | 14:cc916fa8dd11 | 1 | /****************************************************************************** |
jmarkel44 | 14:cc916fa8dd11 | 2 | * |
jmarkel44 | 19:9bc8fabeddfa | 3 | * File: SetpointControl.cpp |
jmarkel44 | 20:653923c2f37a | 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 | 28:c410a61238bb | 34 | char dataBuf[1024]; |
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 | 46:4cb96ab2d1c8 | 39 | // we can't throw exceptions in mbed, so just return false. the calling function will |
jmarkel44 | 51:66b820f203a5 | 40 | // destroy the object |
jmarkel44 | 28:c410a61238bb | 41 | return false; |
jmarkel44 | 28:c410a61238bb | 42 | } |
jmarkel44 | 51:66b820f203a5 | 43 | |
jmarkel44 | 51:66b820f203a5 | 44 | // close the file |
jmarkel44 | 28:c410a61238bb | 45 | GLOBAL_mdot->closeUserFile(file); |
jmarkel44 | 28:c410a61238bb | 46 | |
jmarkel44 | 51:66b820f203a5 | 47 | // parse the json data |
jmarkel44 | 28:c410a61238bb | 48 | parse(json_value, dataBuf); |
jmarkel44 | 51:66b820f203a5 | 49 | |
jmarkel44 | 28:c410a61238bb | 50 | id = json_value["id"].get<string>(); |
jmarkel44 | 28:c410a61238bb | 51 | priority = atoi(json_value["priority"].get<string>().c_str()); |
jmarkel44 | 28:c410a61238bb | 52 | input = json_value["input"].get<string>(); |
jmarkel44 | 28:c410a61238bb | 53 | output = json_value["output"].get<string>(); |
jmarkel44 | 89:55ac65d7f206 | 54 | setpoint = atof(json_value["setpoint"].get<string>().c_str()); |
jmarkel44 | 28:c410a61238bb | 55 | productFactor = atof(json_value["prodfact"].get<string>().c_str()); |
jmarkel44 | 90:7d9731dec0da | 56 | actingDir = atoi(json_value["actingDir"].get<string>().c_str()); |
jmarkel44 | 28:c410a61238bb | 57 | highAlert = atof(json_value["halert"].get<string>().c_str()); |
jmarkel44 | 28:c410a61238bb | 58 | lowAlert = atof(json_value["lalert"].get<string>().c_str()); |
jmarkel44 | 28:c410a61238bb | 59 | highFailsafe = atof(json_value["hfs"].get<string>().c_str()); |
jmarkel44 | 28:c410a61238bb | 60 | lowFailsafe = atof(json_value["lfs"].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 | 51:66b820f203a5 | 88 | case STATE_STARTUP: |
jmarkel44 | 51:66b820f203a5 | 89 | if ( this->underLimit() ) { |
jmarkel44 | 51:66b820f203a5 | 90 | // start the feed right away |
jmarkel44 | 51:66b820f203a5 | 91 | this->startFeed(); |
jmarkel44 | 51:66b820f203a5 | 92 | this->currentState = STATE_CONTROL_ON; |
jmarkel44 | 51:66b820f203a5 | 93 | } else { |
jmarkel44 | 51:66b820f203a5 | 94 | this->currentState = STATE_CONTROL_OFF; |
jmarkel44 | 51:66b820f203a5 | 95 | } |
jmarkel44 | 51:66b820f203a5 | 96 | break; |
jmarkel44 | 51:66b820f203a5 | 97 | case STATE_CONTROL_ON: |
jmarkel44 | 51:66b820f203a5 | 98 | if ( this->overLimit() ) { |
jmarkel44 | 51:66b820f203a5 | 99 | // stop the feed |
jmarkel44 | 51:66b820f203a5 | 100 | this->stopFeed(); |
jmarkel44 | 51:66b820f203a5 | 101 | this->currentState = STATE_CONTROL_OFF; |
jmarkel44 | 51:66b820f203a5 | 102 | } else { |
jmarkel44 | 51:66b820f203a5 | 103 | // do nothing |
jmarkel44 | 51:66b820f203a5 | 104 | } |
jmarkel44 | 51:66b820f203a5 | 105 | break; |
jmarkel44 | 51:66b820f203a5 | 106 | case STATE_CONTROL_OFF: |
jmarkel44 | 51:66b820f203a5 | 107 | if ( this->underLimit() ) { |
jmarkel44 | 51:66b820f203a5 | 108 | // start the feed |
jmarkel44 | 51:66b820f203a5 | 109 | this->startFeed(); |
jmarkel44 | 51:66b820f203a5 | 110 | this->currentState = STATE_CONTROL_ON; |
jmarkel44 | 51:66b820f203a5 | 111 | } else { |
jmarkel44 | 51:66b820f203a5 | 112 | // do nothing |
jmarkel44 | 51:66b820f203a5 | 113 | } |
jmarkel44 | 51:66b820f203a5 | 114 | break; |
jmarkel44 | 56:225786c56315 | 115 | //case STATE_CONTROL_DISABLED: |
jmarkel44 | 56:225786c56315 | 116 | //case STATE_CONTROL_PAUSED: |
jmarkel44 | 51:66b820f203a5 | 117 | default: |
jmarkel44 | 51:66b820f203a5 | 118 | break; |
jmarkel44 | 51:66b820f203a5 | 119 | } |
jmarkel44 | 51:66b820f203a5 | 120 | } |
jmarkel44 | 51:66b820f203a5 | 121 | |
jmarkel44 | 56:225786c56315 | 122 | // Method: overLimit |
jmarkel44 | 56:225786c56315 | 123 | // Description: (see @return) |
jmarkel44 | 56:225786c56315 | 124 | // |
jmarkel44 | 56:225786c56315 | 125 | // @param none |
jmarkel44 | 56:225786c56315 | 126 | // @return true if product is over the upper limit for normal mode |
jmarkel44 | 56:225786c56315 | 127 | // or under the limit for reverse mode; false otherwise |
jmarkel44 | 56:225786c56315 | 128 | |
jmarkel44 | 51:66b820f203a5 | 129 | bool SetpointControl::overLimit(void) |
jmarkel44 | 51:66b820f203a5 | 130 | { |
davidjhoward | 91:0e8d76030598 | 131 | ModbusValue value; |
davidjhoward | 91:0e8d76030598 | 132 | |
davidjhoward | 91:0e8d76030598 | 133 | ModbusMasterReadRegister( input, &value ); |
davidjhoward | 91:0e8d76030598 | 134 | printf("overLimit: input=%s value=%2.2f, direction=%d\r\n", input, value.value, actingDir); |
davidjhoward | 91:0e8d76030598 | 135 | float high_band = setpoint + (setpoint * .04); |
davidjhoward | 91:0e8d76030598 | 136 | if( (value.value >= high_band && actingDir == true) || (value.value <= high_band && actingDir == false) ) { |
davidjhoward | 91:0e8d76030598 | 137 | printf("overLimit returning TRUE\r\n"); |
davidjhoward | 91:0e8d76030598 | 138 | // over limit |
davidjhoward | 91:0e8d76030598 | 139 | return true; |
davidjhoward | 91:0e8d76030598 | 140 | } |
davidjhoward | 91:0e8d76030598 | 141 | printf("overLimit returning FALSE\r\n"); |
jmarkel44 | 51:66b820f203a5 | 142 | return false; |
jmarkel44 | 51:66b820f203a5 | 143 | } |
jmarkel44 | 51:66b820f203a5 | 144 | |
jmarkel44 | 56:225786c56315 | 145 | // Method: underLimit |
jmarkel44 | 56:225786c56315 | 146 | // Description: (see @return) |
jmarkel44 | 56:225786c56315 | 147 | // |
jmarkel44 | 56:225786c56315 | 148 | // @param none |
jmarkel44 | 56:225786c56315 | 149 | // @return true if product is under lower limit for normal mode or |
jmarkel44 | 56:225786c56315 | 150 | // over the upper limit for reverse mode; false otherwise |
jmarkel44 | 56:225786c56315 | 151 | |
jmarkel44 | 51:66b820f203a5 | 152 | bool SetpointControl::underLimit(void) |
jmarkel44 | 51:66b820f203a5 | 153 | { |
davidjhoward | 91:0e8d76030598 | 154 | ModbusValue value; |
davidjhoward | 91:0e8d76030598 | 155 | |
davidjhoward | 91:0e8d76030598 | 156 | float low_band = setpoint - (setpoint *.04); |
davidjhoward | 91:0e8d76030598 | 157 | printf("underLimit: input=%s value=%2.2f, direction=%d\r\n", input, value.value, actingDir); |
davidjhoward | 91:0e8d76030598 | 158 | if( (value.value <= low_band && actingDir == true) || (value.value >= low_band && actingDir == false) ) { |
davidjhoward | 91:0e8d76030598 | 159 | // over limit |
davidjhoward | 91:0e8d76030598 | 160 | printf("underLimit returning TRUE\r\n"); |
davidjhoward | 91:0e8d76030598 | 161 | return true; |
davidjhoward | 91:0e8d76030598 | 162 | } |
davidjhoward | 91:0e8d76030598 | 163 | printf("underLimit returning FALSE\r\n"); |
jmarkel44 | 87:c466bde76fa0 | 164 | return false; |
jmarkel44 | 51:66b820f203a5 | 165 | } |
jmarkel44 | 51:66b820f203a5 | 166 | |
jmarkel44 | 56:225786c56315 | 167 | // Method: startFeed() |
jmarkel44 | 56:225786c56315 | 168 | // Description: send ON indication to Output Master for this control's |
jmarkel44 | 56:225786c56315 | 169 | // relay |
jmarkel44 | 56:225786c56315 | 170 | // |
jmarkel44 | 56:225786c56315 | 171 | // @param none |
jmarkel44 | 56:225786c56315 | 172 | // @return none |
jmarkel44 | 56:225786c56315 | 173 | |
jmarkel44 | 51:66b820f203a5 | 174 | void SetpointControl::startFeed(void) |
jmarkel44 | 51:66b820f203a5 | 175 | { |
jmarkel44 | 56:225786c56315 | 176 | logInfo("%s: %s attempting to start feed on relay %s\n", |
jmarkel44 | 56:225786c56315 | 177 | __func__, controlFile.c_str(), output.c_str()); |
jmarkel44 | 56:225786c56315 | 178 | |
jmarkel44 | 71:34856d21f2bf | 179 | OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc(); |
jmarkel44 | 71:34856d21f2bf | 180 | memset(output_mail, 0, sizeof(OutputControlMsg_t)); |
jmarkel44 | 71:34856d21f2bf | 181 | output_mail->action = ACTION_CONTROL_ON; |
jmarkel44 | 71:34856d21f2bf | 182 | output_mail->priority = this->priority; |
jmarkel44 | 71:34856d21f2bf | 183 | strncpy(output_mail->output, this->output.c_str(), sizeof(output_mail->output)-1); |
jmarkel44 | 71:34856d21f2bf | 184 | strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1); |
jmarkel44 | 71:34856d21f2bf | 185 | OutputMasterMailBox.put(output_mail); |
jmarkel44 | 51:66b820f203a5 | 186 | } |
jmarkel44 | 51:66b820f203a5 | 187 | |
jmarkel44 | 56:225786c56315 | 188 | // Method: stopFeed |
jmarkel44 | 56:225786c56315 | 189 | // Description: send OFF indication to Output Master for this control's |
jmarkel44 | 56:225786c56315 | 190 | // relay |
jmarkel44 | 56:225786c56315 | 191 | // |
jmarkel44 | 56:225786c56315 | 192 | // @param none |
jmarkel44 | 56:225786c56315 | 193 | // @return none |
jmarkel44 | 56:225786c56315 | 194 | |
jmarkel44 | 51:66b820f203a5 | 195 | void SetpointControl::stopFeed(void) |
jmarkel44 | 51:66b820f203a5 | 196 | { |
jmarkel44 | 71:34856d21f2bf | 197 | logInfo("%s: %s attempting to stop feed on relay %s\n", |
jmarkel44 | 56:225786c56315 | 198 | __func__, controlFile.c_str(), output.c_str()); |
jmarkel44 | 56:225786c56315 | 199 | |
jmarkel44 | 71:34856d21f2bf | 200 | OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc(); |
jmarkel44 | 71:34856d21f2bf | 201 | memset(output_mail, 0, sizeof(OutputControlMsg_t)); |
jmarkel44 | 71:34856d21f2bf | 202 | output_mail->action = ACTION_CONTROL_OFF; |
jmarkel44 | 71:34856d21f2bf | 203 | output_mail->priority = this->priority; |
jmarkel44 | 71:34856d21f2bf | 204 | strncpy(output_mail->output, this->output.c_str(), sizeof(output_mail->output)-1); |
jmarkel44 | 71:34856d21f2bf | 205 | strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1); |
jmarkel44 | 71:34856d21f2bf | 206 | OutputMasterMailBox.put(output_mail); |
jmarkel44 | 72:3754b352f156 | 207 | } |
jmarkel44 | 72:3754b352f156 | 208 | |
jmarkel44 | 72:3754b352f156 | 209 | // Method: unregisterControl |
jmarkel44 | 72:3754b352f156 | 210 | // Description: send OFF indication to Output Master for this control's |
jmarkel44 | 72:3754b352f156 | 211 | // relay |
jmarkel44 | 72:3754b352f156 | 212 | // |
jmarkel44 | 72:3754b352f156 | 213 | // @param none |
jmarkel44 | 72:3754b352f156 | 214 | // @return none |
jmarkel44 | 72:3754b352f156 | 215 | void SetpointControl::unregisterControl(void) |
jmarkel44 | 72:3754b352f156 | 216 | { |
jmarkel44 | 72:3754b352f156 | 217 | logInfo("%s: %s attempting to stop feed on relay %s\n", |
jmarkel44 | 72:3754b352f156 | 218 | __func__, controlFile.c_str(), output.c_str()); |
jmarkel44 | 72:3754b352f156 | 219 | |
jmarkel44 | 72:3754b352f156 | 220 | OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc(); |
jmarkel44 | 72:3754b352f156 | 221 | memset(output_mail, 0, sizeof(OutputControlMsg_t)); |
jmarkel44 | 72:3754b352f156 | 222 | output_mail->action = ACTION_CONTROL_UNREGISTER; |
jmarkel44 | 72:3754b352f156 | 223 | output_mail->priority = this->priority; |
jmarkel44 | 72:3754b352f156 | 224 | strncpy(output_mail->output, this->output.c_str(), sizeof(output_mail->output)-1); |
jmarkel44 | 72:3754b352f156 | 225 | strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1); |
jmarkel44 | 72:3754b352f156 | 226 | OutputMasterMailBox.put(output_mail); |
jmarkel44 | 51:66b820f203a5 | 227 | } |