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.
ICE-Application/src/ConfigurationHandler/Controls/SetpointControl.cpp@0:61364762ee0e, 2017-01-24 (annotated)
- Committer:
- jmarkel44
- Date:
- Tue Jan 24 19:05:33 2017 +0000
- Revision:
- 0:61364762ee0e
Port from IAR to Nucleo-F412 board
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jmarkel44 | 0:61364762ee0e | 1 | /****************************************************************************** |
jmarkel44 | 0:61364762ee0e | 2 | * |
jmarkel44 | 0:61364762ee0e | 3 | * File: SetpointControl.cpp |
jmarkel44 | 0:61364762ee0e | 4 | * Desciption: ICE Setpoint Control class implementation |
jmarkel44 | 0:61364762ee0e | 5 | * |
jmarkel44 | 0:61364762ee0e | 6 | *****************************************************************************/ |
jmarkel44 | 0:61364762ee0e | 7 | #include "SetpointControl.h" |
jmarkel44 | 0:61364762ee0e | 8 | #include "ICELog.h" |
jmarkel44 | 0:61364762ee0e | 9 | #include "cJSON.h" |
jmarkel44 | 0:61364762ee0e | 10 | #include "ModbusMasterApi.h" |
jmarkel44 | 0:61364762ee0e | 11 | #include "global.h" |
jmarkel44 | 0:61364762ee0e | 12 | #include "utilities.h" |
jmarkel44 | 0:61364762ee0e | 13 | #include <string> |
jmarkel44 | 0:61364762ee0e | 14 | #include <iostream> |
jmarkel44 | 0:61364762ee0e | 15 | #include <iomanip> |
jmarkel44 | 0:61364762ee0e | 16 | #include <stdarg.h> |
jmarkel44 | 0:61364762ee0e | 17 | #include <stdlib.h> |
jmarkel44 | 0:61364762ee0e | 18 | |
jmarkel44 | 0:61364762ee0e | 19 | // for debugging - this can be set via the console command: |
jmarkel44 | 0:61364762ee0e | 20 | // "debug-sp 1 or debug-sp 0 |
jmarkel44 | 0:61364762ee0e | 21 | bool debugSetpointControl = false; |
jmarkel44 | 0:61364762ee0e | 22 | |
jmarkel44 | 0:61364762ee0e | 23 | static void debug(const char *fmt, ...) |
jmarkel44 | 0:61364762ee0e | 24 | { |
jmarkel44 | 0:61364762ee0e | 25 | if ( debugSetpointControl ) { |
jmarkel44 | 0:61364762ee0e | 26 | va_list vargs; |
jmarkel44 | 0:61364762ee0e | 27 | va_start(vargs, fmt); |
jmarkel44 | 0:61364762ee0e | 28 | vfprintf(stdout, fmt, vargs); |
jmarkel44 | 0:61364762ee0e | 29 | va_end(vargs); |
jmarkel44 | 0:61364762ee0e | 30 | } |
jmarkel44 | 0:61364762ee0e | 31 | } |
jmarkel44 | 0:61364762ee0e | 32 | |
jmarkel44 | 0:61364762ee0e | 33 | #ifdef MDOT_ICE |
jmarkel44 | 0:61364762ee0e | 34 | extern mDot *GLOBAL_mdot; |
jmarkel44 | 0:61364762ee0e | 35 | #endif |
jmarkel44 | 0:61364762ee0e | 36 | |
jmarkel44 | 0:61364762ee0e | 37 | // |
jmarkel44 | 0:61364762ee0e | 38 | // method: load |
jmarkel44 | 0:61364762ee0e | 39 | // description: open the configuration file and assign data to the |
jmarkel44 | 0:61364762ee0e | 40 | // setpoint control object |
jmarkel44 | 0:61364762ee0e | 41 | // |
jmarkel44 | 0:61364762ee0e | 42 | // @param controlFile -> name of the control file |
jmarkel44 | 0:61364762ee0e | 43 | // @return true if data is assigned; false on error |
jmarkel44 | 0:61364762ee0e | 44 | // |
jmarkel44 | 0:61364762ee0e | 45 | bool SetpointControl::load(std::string _controlFile) |
jmarkel44 | 0:61364762ee0e | 46 | { |
jmarkel44 | 0:61364762ee0e | 47 | controlFile = _controlFile; |
jmarkel44 | 0:61364762ee0e | 48 | |
jmarkel44 | 0:61364762ee0e | 49 | char dataBuf[MAX_FILE_SIZE]; |
jmarkel44 | 0:61364762ee0e | 50 | |
jmarkel44 | 0:61364762ee0e | 51 | // read the control data |
jmarkel44 | 0:61364762ee0e | 52 | bool rc = GLOBAL_mdot->readUserFile(controlFile.c_str(), (void *)dataBuf, sizeof(dataBuf)); |
jmarkel44 | 0:61364762ee0e | 53 | if ( rc != true ) { |
jmarkel44 | 0:61364762ee0e | 54 | logError("%s: failed to read %s", __func__, controlFile.c_str()); |
jmarkel44 | 0:61364762ee0e | 55 | // caller should destroy the object |
jmarkel44 | 0:61364762ee0e | 56 | return false; |
jmarkel44 | 0:61364762ee0e | 57 | } |
jmarkel44 | 0:61364762ee0e | 58 | |
jmarkel44 | 0:61364762ee0e | 59 | // validate control data |
jmarkel44 | 0:61364762ee0e | 60 | if ( !validateControlData(dataBuf) ) { |
jmarkel44 | 0:61364762ee0e | 61 | logError("%s: failed to validate control data", __func__); |
jmarkel44 | 0:61364762ee0e | 62 | return false; |
jmarkel44 | 0:61364762ee0e | 63 | } |
jmarkel44 | 0:61364762ee0e | 64 | |
jmarkel44 | 0:61364762ee0e | 65 | // copy control data |
jmarkel44 | 0:61364762ee0e | 66 | copyControlData(dataBuf); |
jmarkel44 | 0:61364762ee0e | 67 | |
jmarkel44 | 0:61364762ee0e | 68 | ModbusValue val; |
jmarkel44 | 0:61364762ee0e | 69 | // validate the input & output |
jmarkel44 | 0:61364762ee0e | 70 | if ( ModbusMasterReadRegister(input, &val) == false ) { |
jmarkel44 | 0:61364762ee0e | 71 | logError("%s failed to find the input %s", id.c_str(), input.c_str()); |
jmarkel44 | 0:61364762ee0e | 72 | return false; |
jmarkel44 | 0:61364762ee0e | 73 | } |
jmarkel44 | 0:61364762ee0e | 74 | if ( ModbusMasterReadRegister(output, &val) == false ) { |
jmarkel44 | 0:61364762ee0e | 75 | logError("%s failed to find output %s", id.c_str(), output.c_str()); |
jmarkel44 | 0:61364762ee0e | 76 | return false; |
jmarkel44 | 0:61364762ee0e | 77 | } |
jmarkel44 | 0:61364762ee0e | 78 | |
jmarkel44 | 0:61364762ee0e | 79 | isVirtualOutput = Util_isVirtualOutput(output) ? true : false; |
jmarkel44 | 0:61364762ee0e | 80 | return true; |
jmarkel44 | 0:61364762ee0e | 81 | } |
jmarkel44 | 0:61364762ee0e | 82 | |
jmarkel44 | 0:61364762ee0e | 83 | // |
jmarkel44 | 0:61364762ee0e | 84 | // method: validateControlData |
jmarkel44 | 0:61364762ee0e | 85 | // description: validate JSON formatted string |
jmarkel44 | 0:61364762ee0e | 86 | // |
jmarkel44 | 0:61364762ee0e | 87 | bool SetpointControl::validateControlData(const char *buf) |
jmarkel44 | 0:61364762ee0e | 88 | { |
jmarkel44 | 0:61364762ee0e | 89 | bool rc = true; |
jmarkel44 | 0:61364762ee0e | 90 | cJSON * root = cJSON_Parse(buf); |
jmarkel44 | 0:61364762ee0e | 91 | |
jmarkel44 | 0:61364762ee0e | 92 | if ( !cJSON_HasObjectItem(root, "id") || |
jmarkel44 | 0:61364762ee0e | 93 | !cJSON_HasObjectItem(root, "priority") || |
jmarkel44 | 0:61364762ee0e | 94 | !cJSON_HasObjectItem(root, "input") || |
jmarkel44 | 0:61364762ee0e | 95 | !cJSON_HasObjectItem(root, "output") || |
jmarkel44 | 0:61364762ee0e | 96 | !cJSON_HasObjectItem(root, "setpoint") || |
jmarkel44 | 0:61364762ee0e | 97 | !cJSON_HasObjectItem(root, "prodfact") || |
jmarkel44 | 0:61364762ee0e | 98 | !cJSON_HasObjectItem(root, "actingDir") || |
jmarkel44 | 0:61364762ee0e | 99 | !cJSON_HasObjectItem(root, "tol") ) { |
jmarkel44 | 0:61364762ee0e | 100 | logError("Setpoint control is missing expected tags\n"); |
jmarkel44 | 0:61364762ee0e | 101 | rc = false; |
jmarkel44 | 0:61364762ee0e | 102 | } |
jmarkel44 | 0:61364762ee0e | 103 | |
jmarkel44 | 0:61364762ee0e | 104 | cJSON_Delete(root); |
jmarkel44 | 0:61364762ee0e | 105 | return rc; |
jmarkel44 | 0:61364762ee0e | 106 | } |
jmarkel44 | 0:61364762ee0e | 107 | |
jmarkel44 | 0:61364762ee0e | 108 | // |
jmarkel44 | 0:61364762ee0e | 109 | // method: copyControlData |
jmarkel44 | 0:61364762ee0e | 110 | // description: copy JSON formatted data |
jmarkel44 | 0:61364762ee0e | 111 | // |
jmarkel44 | 0:61364762ee0e | 112 | void SetpointControl::copyControlData(const char *buf) |
jmarkel44 | 0:61364762ee0e | 113 | { |
jmarkel44 | 0:61364762ee0e | 114 | cJSON * root = cJSON_Parse(buf); |
jmarkel44 | 0:61364762ee0e | 115 | |
jmarkel44 | 0:61364762ee0e | 116 | id = cJSON_GetObjectItem(root,"id")->valuestring; |
jmarkel44 | 0:61364762ee0e | 117 | priority = atoi(cJSON_GetObjectItem(root,"priority")->valuestring); |
jmarkel44 | 0:61364762ee0e | 118 | input = cJSON_GetObjectItem(root,"input")->valuestring; |
jmarkel44 | 0:61364762ee0e | 119 | output = cJSON_GetObjectItem(root,"output")->valuestring; |
jmarkel44 | 0:61364762ee0e | 120 | setpoint = atof(cJSON_GetObjectItem(root,"setpoint")->valuestring); |
jmarkel44 | 0:61364762ee0e | 121 | productFactor = atof(cJSON_GetObjectItem(root, "prodfact")->valuestring); |
jmarkel44 | 0:61364762ee0e | 122 | actingDir = atoi(cJSON_GetObjectItem(root, "actingDir")->valuestring); |
jmarkel44 | 0:61364762ee0e | 123 | tolerance = atof(cJSON_GetObjectItem(root, "tol")->valuestring); |
jmarkel44 | 0:61364762ee0e | 124 | |
jmarkel44 | 0:61364762ee0e | 125 | cJSON_Delete(root); |
jmarkel44 | 0:61364762ee0e | 126 | } |
jmarkel44 | 0:61364762ee0e | 127 | |
jmarkel44 | 0:61364762ee0e | 128 | // |
jmarkel44 | 0:61364762ee0e | 129 | // method: start |
jmarkel44 | 0:61364762ee0e | 130 | // description: start the setpoint control |
jmarkel44 | 0:61364762ee0e | 131 | // |
jmarkel44 | 0:61364762ee0e | 132 | // @param none |
jmarkel44 | 0:61364762ee0e | 133 | // @return none |
jmarkel44 | 0:61364762ee0e | 134 | // |
jmarkel44 | 0:61364762ee0e | 135 | void SetpointControl::start(void) |
jmarkel44 | 0:61364762ee0e | 136 | { |
jmarkel44 | 0:61364762ee0e | 137 | // this is the initial state; what else needs to be done?? |
jmarkel44 | 0:61364762ee0e | 138 | this->currentState = STATE_STARTUP; |
jmarkel44 | 0:61364762ee0e | 139 | } |
jmarkel44 | 0:61364762ee0e | 140 | |
jmarkel44 | 0:61364762ee0e | 141 | // |
jmarkel44 | 0:61364762ee0e | 142 | // method: |
jmarkel44 | 0:61364762ee0e | 143 | // description: based on the state of the control, check for |
jmarkel44 | 0:61364762ee0e | 144 | // under limit and over limit values, adjust the |
jmarkel44 | 0:61364762ee0e | 145 | // state accordingly |
jmarkel44 | 0:61364762ee0e | 146 | // |
jmarkel44 | 0:61364762ee0e | 147 | // @param none |
jmarkel44 | 0:61364762ee0e | 148 | // @return none |
jmarkel44 | 0:61364762ee0e | 149 | // |
jmarkel44 | 0:61364762ee0e | 150 | SetpointControlError_t SetpointControl::update(void) |
jmarkel44 | 0:61364762ee0e | 151 | { |
jmarkel44 | 0:61364762ee0e | 152 | SetpointControlError_t rc = SETPOINT_CONTROL_OK; |
jmarkel44 | 0:61364762ee0e | 153 | |
jmarkel44 | 0:61364762ee0e | 154 | switch (this->currentState) { |
jmarkel44 | 0:61364762ee0e | 155 | case STATE_INIT: |
jmarkel44 | 0:61364762ee0e | 156 | // do nothing |
jmarkel44 | 0:61364762ee0e | 157 | break; |
jmarkel44 | 0:61364762ee0e | 158 | case STATE_STARTUP: |
jmarkel44 | 0:61364762ee0e | 159 | if ( this->underLimit() ) { |
jmarkel44 | 0:61364762ee0e | 160 | // start the feed right away |
jmarkel44 | 0:61364762ee0e | 161 | this->startFeed(); |
jmarkel44 | 0:61364762ee0e | 162 | this->currentState = STATE_CONTROL_ON; |
jmarkel44 | 0:61364762ee0e | 163 | debug("\r%s: [START]->under limit->[ON]\n", id.c_str()); |
jmarkel44 | 0:61364762ee0e | 164 | } else { |
jmarkel44 | 0:61364762ee0e | 165 | this->currentState = STATE_CONTROL_OFF; |
jmarkel44 | 0:61364762ee0e | 166 | this->stopFeed(); |
jmarkel44 | 0:61364762ee0e | 167 | debug("\r%s: [START]->!under limit->[OFF]\n", id.c_str()); |
jmarkel44 | 0:61364762ee0e | 168 | } |
jmarkel44 | 0:61364762ee0e | 169 | break; |
jmarkel44 | 0:61364762ee0e | 170 | case STATE_CONTROL_ON: |
jmarkel44 | 0:61364762ee0e | 171 | if ( this->overLimit() ) { |
jmarkel44 | 0:61364762ee0e | 172 | // stop the feed |
jmarkel44 | 0:61364762ee0e | 173 | this->stopFeed(); |
jmarkel44 | 0:61364762ee0e | 174 | this->currentState = STATE_CONTROL_OFF; |
jmarkel44 | 0:61364762ee0e | 175 | debug("\r%s: [ON]->over limit->[OFF]\n", id.c_str()); |
jmarkel44 | 0:61364762ee0e | 176 | } else { |
jmarkel44 | 0:61364762ee0e | 177 | // do nothing |
jmarkel44 | 0:61364762ee0e | 178 | } |
jmarkel44 | 0:61364762ee0e | 179 | break; |
jmarkel44 | 0:61364762ee0e | 180 | case STATE_CONTROL_OFF: |
jmarkel44 | 0:61364762ee0e | 181 | if ( this->underLimit() ) { |
jmarkel44 | 0:61364762ee0e | 182 | // start the feed |
jmarkel44 | 0:61364762ee0e | 183 | this->startFeed(); |
jmarkel44 | 0:61364762ee0e | 184 | this->currentState = STATE_CONTROL_ON; |
jmarkel44 | 0:61364762ee0e | 185 | debug("\r%s: [OFF]->under limit->[ON]\n", id.c_str()); |
jmarkel44 | 0:61364762ee0e | 186 | } else { |
jmarkel44 | 0:61364762ee0e | 187 | // do nothing |
jmarkel44 | 0:61364762ee0e | 188 | } |
jmarkel44 | 0:61364762ee0e | 189 | break; |
jmarkel44 | 0:61364762ee0e | 190 | default: |
jmarkel44 | 0:61364762ee0e | 191 | rc = SETPOINT_CONTROL_UNK_STATE; |
jmarkel44 | 0:61364762ee0e | 192 | break; |
jmarkel44 | 0:61364762ee0e | 193 | } |
jmarkel44 | 0:61364762ee0e | 194 | return rc; |
jmarkel44 | 0:61364762ee0e | 195 | } |
jmarkel44 | 0:61364762ee0e | 196 | |
jmarkel44 | 0:61364762ee0e | 197 | // |
jmarkel44 | 0:61364762ee0e | 198 | // method: overLimit |
jmarkel44 | 0:61364762ee0e | 199 | // description: (see @return) |
jmarkel44 | 0:61364762ee0e | 200 | // |
jmarkel44 | 0:61364762ee0e | 201 | // @param none |
jmarkel44 | 0:61364762ee0e | 202 | // @return true if product is over the upper limit for normal mode |
jmarkel44 | 0:61364762ee0e | 203 | // or under the limit for reverse mode; false otherwise |
jmarkel44 | 0:61364762ee0e | 204 | // |
jmarkel44 | 0:61364762ee0e | 205 | bool SetpointControl::overLimit(void) |
jmarkel44 | 0:61364762ee0e | 206 | { |
jmarkel44 | 0:61364762ee0e | 207 | ModbusValue value; |
jmarkel44 | 0:61364762ee0e | 208 | ModbusMasterReadRegister( input, &value ); |
jmarkel44 | 0:61364762ee0e | 209 | float flimit; |
jmarkel44 | 0:61364762ee0e | 210 | |
jmarkel44 | 0:61364762ee0e | 211 | if ( !actingDir ) { |
jmarkel44 | 0:61364762ee0e | 212 | flimit = setpoint + tolerance; |
jmarkel44 | 0:61364762ee0e | 213 | return (value.value >= flimit); |
jmarkel44 | 0:61364762ee0e | 214 | } else { |
jmarkel44 | 0:61364762ee0e | 215 | flimit = setpoint - tolerance; |
jmarkel44 | 0:61364762ee0e | 216 | return (value.value <= flimit); |
jmarkel44 | 0:61364762ee0e | 217 | } |
jmarkel44 | 0:61364762ee0e | 218 | } |
jmarkel44 | 0:61364762ee0e | 219 | |
jmarkel44 | 0:61364762ee0e | 220 | // |
jmarkel44 | 0:61364762ee0e | 221 | // method: underLimit |
jmarkel44 | 0:61364762ee0e | 222 | // description: (see @return) |
jmarkel44 | 0:61364762ee0e | 223 | // |
jmarkel44 | 0:61364762ee0e | 224 | // @param none |
jmarkel44 | 0:61364762ee0e | 225 | // @return true if product is under lower limit for normal mode or |
jmarkel44 | 0:61364762ee0e | 226 | // over the upper limit for reverse mode; false otherwise |
jmarkel44 | 0:61364762ee0e | 227 | // |
jmarkel44 | 0:61364762ee0e | 228 | bool SetpointControl::underLimit(void) |
jmarkel44 | 0:61364762ee0e | 229 | { |
jmarkel44 | 0:61364762ee0e | 230 | ModbusValue value; |
jmarkel44 | 0:61364762ee0e | 231 | ModbusMasterReadRegister( input, &value ); |
jmarkel44 | 0:61364762ee0e | 232 | float flimit; |
jmarkel44 | 0:61364762ee0e | 233 | |
jmarkel44 | 0:61364762ee0e | 234 | if ( !actingDir ) { |
jmarkel44 | 0:61364762ee0e | 235 | flimit = setpoint - tolerance; |
jmarkel44 | 0:61364762ee0e | 236 | return (value.value <= flimit); |
jmarkel44 | 0:61364762ee0e | 237 | } else { |
jmarkel44 | 0:61364762ee0e | 238 | flimit = setpoint + tolerance; |
jmarkel44 | 0:61364762ee0e | 239 | return (value.value >= flimit); |
jmarkel44 | 0:61364762ee0e | 240 | } |
jmarkel44 | 0:61364762ee0e | 241 | } |
jmarkel44 | 0:61364762ee0e | 242 | |
jmarkel44 | 0:61364762ee0e | 243 | // |
jmarkel44 | 0:61364762ee0e | 244 | // method: startFeed() |
jmarkel44 | 0:61364762ee0e | 245 | // description: send ON indication to Output Master for this control's |
jmarkel44 | 0:61364762ee0e | 246 | // relay |
jmarkel44 | 0:61364762ee0e | 247 | // |
jmarkel44 | 0:61364762ee0e | 248 | // @param none |
jmarkel44 | 0:61364762ee0e | 249 | // @return none |
jmarkel44 | 0:61364762ee0e | 250 | // |
jmarkel44 | 0:61364762ee0e | 251 | void SetpointControl::startFeed(void) |
jmarkel44 | 0:61364762ee0e | 252 | { |
jmarkel44 | 0:61364762ee0e | 253 | logInfo("%s: %s attempting to start feed on relay %s\n", |
jmarkel44 | 0:61364762ee0e | 254 | __func__, controlFile.c_str(), output.c_str()); |
jmarkel44 | 0:61364762ee0e | 255 | |
jmarkel44 | 0:61364762ee0e | 256 | if ( isVirtualOutput ) { |
jmarkel44 | 0:61364762ee0e | 257 | ModbusMasterWriteRegister(output, 1.0); |
jmarkel44 | 0:61364762ee0e | 258 | } else { |
jmarkel44 | 0:61364762ee0e | 259 | OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc(); |
jmarkel44 | 0:61364762ee0e | 260 | memset(output_mail, 0, sizeof(OutputControlMsg_t)); |
jmarkel44 | 0:61364762ee0e | 261 | |
jmarkel44 | 0:61364762ee0e | 262 | output_mail->action = ACTION_CONTROL_ON; |
jmarkel44 | 0:61364762ee0e | 263 | output_mail->controlType = CONTROL_SETPOINT; |
jmarkel44 | 0:61364762ee0e | 264 | strncpy(output_mail->input_tag, this->input.c_str(), sizeof(output_mail->input_tag)-1); |
jmarkel44 | 0:61364762ee0e | 265 | strncpy(output_mail->output_tag, this->output.c_str(), sizeof(output_mail->output_tag)-1); |
jmarkel44 | 0:61364762ee0e | 266 | output_mail->priority = this->priority; |
jmarkel44 | 0:61364762ee0e | 267 | strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1); |
jmarkel44 | 0:61364762ee0e | 268 | OutputMasterMailBox.put(output_mail); |
jmarkel44 | 0:61364762ee0e | 269 | } |
jmarkel44 | 0:61364762ee0e | 270 | } |
jmarkel44 | 0:61364762ee0e | 271 | |
jmarkel44 | 0:61364762ee0e | 272 | // |
jmarkel44 | 0:61364762ee0e | 273 | // method: stopFeed |
jmarkel44 | 0:61364762ee0e | 274 | // description: send OFF indication to Output Master for this control's |
jmarkel44 | 0:61364762ee0e | 275 | // relay |
jmarkel44 | 0:61364762ee0e | 276 | // |
jmarkel44 | 0:61364762ee0e | 277 | // @param none |
jmarkel44 | 0:61364762ee0e | 278 | // @return none |
jmarkel44 | 0:61364762ee0e | 279 | // |
jmarkel44 | 0:61364762ee0e | 280 | void SetpointControl::stopFeed(void) |
jmarkel44 | 0:61364762ee0e | 281 | { |
jmarkel44 | 0:61364762ee0e | 282 | logInfo("%s: %s attempting to stop feed on relay %s\n", |
jmarkel44 | 0:61364762ee0e | 283 | __func__, controlFile.c_str(), output.c_str()); |
jmarkel44 | 0:61364762ee0e | 284 | |
jmarkel44 | 0:61364762ee0e | 285 | if ( isVirtualOutput ) { |
jmarkel44 | 0:61364762ee0e | 286 | ModbusMasterWriteRegister(output, 0.0); |
jmarkel44 | 0:61364762ee0e | 287 | } else { |
jmarkel44 | 0:61364762ee0e | 288 | OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc(); |
jmarkel44 | 0:61364762ee0e | 289 | memset(output_mail, 0, sizeof(OutputControlMsg_t)); |
jmarkel44 | 0:61364762ee0e | 290 | |
jmarkel44 | 0:61364762ee0e | 291 | output_mail->action = ACTION_CONTROL_OFF; |
jmarkel44 | 0:61364762ee0e | 292 | output_mail->controlType = CONTROL_SETPOINT; |
jmarkel44 | 0:61364762ee0e | 293 | strncpy(output_mail->input_tag, this->input.c_str(), sizeof(output_mail->input_tag)-1); |
jmarkel44 | 0:61364762ee0e | 294 | strncpy(output_mail->output_tag, this->output.c_str(), sizeof(output_mail->output_tag)-1); |
jmarkel44 | 0:61364762ee0e | 295 | output_mail->priority = this->priority; |
jmarkel44 | 0:61364762ee0e | 296 | strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1); |
jmarkel44 | 0:61364762ee0e | 297 | OutputMasterMailBox.put(output_mail); |
jmarkel44 | 0:61364762ee0e | 298 | } |
jmarkel44 | 0:61364762ee0e | 299 | } |
jmarkel44 | 0:61364762ee0e | 300 | |
jmarkel44 | 0:61364762ee0e | 301 | // |
jmarkel44 | 0:61364762ee0e | 302 | // method: unregisterControl |
jmarkel44 | 0:61364762ee0e | 303 | // description: send OFF indication to Output Master for this control's |
jmarkel44 | 0:61364762ee0e | 304 | // relay |
jmarkel44 | 0:61364762ee0e | 305 | // |
jmarkel44 | 0:61364762ee0e | 306 | // @param none |
jmarkel44 | 0:61364762ee0e | 307 | // @return none |
jmarkel44 | 0:61364762ee0e | 308 | // |
jmarkel44 | 0:61364762ee0e | 309 | void SetpointControl::unregisterControl(void) |
jmarkel44 | 0:61364762ee0e | 310 | { |
jmarkel44 | 0:61364762ee0e | 311 | logInfo("%s: %s attempting to unregister %s\n", |
jmarkel44 | 0:61364762ee0e | 312 | __func__, id.c_str(), controlFile.c_str()); |
jmarkel44 | 0:61364762ee0e | 313 | |
jmarkel44 | 0:61364762ee0e | 314 | if ( isVirtualOutput ) { |
jmarkel44 | 0:61364762ee0e | 315 | ModbusMasterWriteRegister(output, 0.0); |
jmarkel44 | 0:61364762ee0e | 316 | } else { |
jmarkel44 | 0:61364762ee0e | 317 | OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc(); |
jmarkel44 | 0:61364762ee0e | 318 | memset(output_mail, 0, sizeof(OutputControlMsg_t)); |
jmarkel44 | 0:61364762ee0e | 319 | |
jmarkel44 | 0:61364762ee0e | 320 | output_mail->action = ACTION_CONTROL_UNREGISTER; |
jmarkel44 | 0:61364762ee0e | 321 | output_mail->controlType = CONTROL_MANUAL; |
jmarkel44 | 0:61364762ee0e | 322 | output_mail->priority = this->priority; |
jmarkel44 | 0:61364762ee0e | 323 | strncpy(output_mail->output_tag, this->output.c_str(), sizeof(output_mail->output_tag)-1); |
jmarkel44 | 0:61364762ee0e | 324 | strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1); |
jmarkel44 | 0:61364762ee0e | 325 | |
jmarkel44 | 0:61364762ee0e | 326 | OutputMasterMailBox.put(output_mail); |
jmarkel44 | 0:61364762ee0e | 327 | } |
jmarkel44 | 0:61364762ee0e | 328 | } |
jmarkel44 | 0:61364762ee0e | 329 | |
jmarkel44 | 0:61364762ee0e | 330 | // |
jmarkel44 | 0:61364762ee0e | 331 | // method: display |
jmarkel44 | 0:61364762ee0e | 332 | // description: display the control's information |
jmarkel44 | 0:61364762ee0e | 333 | // |
jmarkel44 | 0:61364762ee0e | 334 | // @param none |
jmarkel44 | 0:61364762ee0e | 335 | // @return none |
jmarkel44 | 0:61364762ee0e | 336 | // |
jmarkel44 | 0:61364762ee0e | 337 | void SetpointControl::display(void) |
jmarkel44 | 0:61364762ee0e | 338 | { |
jmarkel44 | 0:61364762ee0e | 339 | // NOTE: this mapping must be 1:1 with "State" enumeration in SetpointControl.h |
jmarkel44 | 0:61364762ee0e | 340 | std::string mapper[] = { "INIT", |
jmarkel44 | 0:61364762ee0e | 341 | "STARTUP", |
jmarkel44 | 0:61364762ee0e | 342 | "CONTROL_OFF", |
jmarkel44 | 0:61364762ee0e | 343 | "CONTROL_ON", |
jmarkel44 | 0:61364762ee0e | 344 | "CONTROL_DISABLE", |
jmarkel44 | 0:61364762ee0e | 345 | "CONTROL_PAUSE", |
jmarkel44 | 0:61364762ee0e | 346 | "CONTROL_MAX" |
jmarkel44 | 0:61364762ee0e | 347 | }; |
jmarkel44 | 0:61364762ee0e | 348 | |
jmarkel44 | 0:61364762ee0e | 349 | |
jmarkel44 | 0:61364762ee0e | 350 | ModbusValue inputValue; |
jmarkel44 | 0:61364762ee0e | 351 | ModbusMasterReadRegister(input, &inputValue); |
jmarkel44 | 0:61364762ee0e | 352 | |
jmarkel44 | 0:61364762ee0e | 353 | printf("\r\n"); |
jmarkel44 | 0:61364762ee0e | 354 | std::cout << std::left << std::setw(10) << std::setfill(' ') << "setpoint: "; |
jmarkel44 | 0:61364762ee0e | 355 | std::cout << std::left << std::setw(40) << std::setfill(' ') << controlFile; |
jmarkel44 | 0:61364762ee0e | 356 | std::cout << std::left << std::setw(20) << std::setfill(' ') << id; |
jmarkel44 | 0:61364762ee0e | 357 | std::cout << std::left << std::setw(6) << std::setfill(' ') << priority; |
jmarkel44 | 0:61364762ee0e | 358 | std::cout << std::left << std::setw(20) << std::setfill(' ') << input; |
jmarkel44 | 0:61364762ee0e | 359 | std::cout << std::left << std::setw(20) << std::setfill(' ') << output; |
jmarkel44 | 0:61364762ee0e | 360 | std::cout << std::left << std::setw(8) << std::setfill(' ') << setpoint; |
jmarkel44 | 0:61364762ee0e | 361 | std::cout << std::left << std::setw(12) << std::setfill(' ') << (actingDir ? "direct" : "indirect"); |
jmarkel44 | 0:61364762ee0e | 362 | std::cout << std::left << std::setw(16) << std::setfill(' ') << mapper[currentState]; |
jmarkel44 | 0:61364762ee0e | 363 | std::cout << std::right << std::setw(8) << std::setfill(' ') << setpoint + tolerance << " <- "; |
jmarkel44 | 0:61364762ee0e | 364 | std::cout << std::left << std::setw(8) << std::setfill(' ') << inputValue.value << " -> "; |
jmarkel44 | 0:61364762ee0e | 365 | std::cout << std::left << std::setw(8) << std::setfill(' ') << setpoint - tolerance; |
jmarkel44 | 0:61364762ee0e | 366 | //std::cout << left << setw(10) << setfill(' ') << highFailsafe << " : " << lowFailsafe; |
jmarkel44 | 0:61364762ee0e | 367 | |
jmarkel44 | 0:61364762ee0e | 368 | std::cout.flush(); |
jmarkel44 | 0:61364762ee0e | 369 | } |