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@70:7427f4959201, 2016-09-16 (annotated)
- Committer:
- jmarkel44
- Date:
- Fri Sep 16 17:54:51 2016 +0000
- Revision:
- 70:7427f4959201
- Parent:
- 56:225786c56315
- Child:
- 71:34856d21f2bf
increased outputtask size;
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" |
jmarkel44 | 51:66b820f203a5 | 10 | #include "global.h" |
jmarkel44 | 28:c410a61238bb | 11 | #include <string> |
jmarkel44 | 13:c80c283f9db2 | 12 | |
jmarkel44 | 14:cc916fa8dd11 | 13 | extern mDot *GLOBAL_mdot; |
jmarkel44 | 14:cc916fa8dd11 | 14 | |
jmarkel44 | 56:225786c56315 | 15 | // Method: load |
jmarkel44 | 56:225786c56315 | 16 | // Description: open the configuration file and assign data to the |
jmarkel44 | 56:225786c56315 | 17 | // setpoint control object |
jmarkel44 | 56:225786c56315 | 18 | // |
jmarkel44 | 56:225786c56315 | 19 | // @param controlFile -> name of the control file |
jmarkel44 | 56:225786c56315 | 20 | // @return true if data is assigned; false on error |
jmarkel44 | 56:225786c56315 | 21 | |
jmarkel44 | 28:c410a61238bb | 22 | bool SetpointControl::load(string _controlFile) |
jmarkel44 | 20:653923c2f37a | 23 | { |
jmarkel44 | 51:66b820f203a5 | 24 | MbedJSONValue json_value; // JSON parsing element |
jmarkel44 | 51:66b820f203a5 | 25 | controlFile = _controlFile; |
jmarkel44 | 51:66b820f203a5 | 26 | |
jmarkel44 | 56:225786c56315 | 27 | // open and read from the control file |
jmarkel44 | 14:cc916fa8dd11 | 28 | mDot::mdot_file file = GLOBAL_mdot->openUserFile(controlFile.c_str(), mDot::FM_RDONLY); |
jmarkel44 | 51:66b820f203a5 | 29 | if ( file.fd < 0 ) |
jmarkel44 | 28:c410a61238bb | 30 | return false; |
jmarkel44 | 20:653923c2f37a | 31 | |
jmarkel44 | 20:653923c2f37a | 32 | // read the data into a buffer |
jmarkel44 | 28:c410a61238bb | 33 | char dataBuf[1024]; |
jmarkel44 | 28:c410a61238bb | 34 | |
jmarkel44 | 28:c410a61238bb | 35 | int bytes_read = GLOBAL_mdot->readUserFile(file, (void *)dataBuf, sizeof(dataBuf)); |
jmarkel44 | 28:c410a61238bb | 36 | if ( bytes_read != sizeof(dataBuf) ) { |
jmarkel44 | 28:c410a61238bb | 37 | logError("%s: failed to read %d bytes from %s", __func__, sizeof(dataBuf), controlFile.c_str()); |
jmarkel44 | 46:4cb96ab2d1c8 | 38 | // we can't throw exceptions in mbed, so just return false. the calling function will |
jmarkel44 | 51:66b820f203a5 | 39 | // 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 | 28:c410a61238bb | 49 | id = json_value["id"].get<string>(); |
jmarkel44 | 28:c410a61238bb | 50 | name = json_value["name"].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 | 28:c410a61238bb | 54 | productFactor = atof(json_value["prodfact"].get<string>().c_str()); |
jmarkel44 | 28:c410a61238bb | 55 | highAlert = atof(json_value["halert"].get<string>().c_str()); |
jmarkel44 | 28:c410a61238bb | 56 | lowAlert = atof(json_value["lalert"].get<string>().c_str()); |
jmarkel44 | 28:c410a61238bb | 57 | highFailsafe = atof(json_value["hfs"].get<string>().c_str()); |
jmarkel44 | 28:c410a61238bb | 58 | lowFailsafe = atof(json_value["lfs"].get<string>().c_str()); |
jmarkel44 | 28:c410a61238bb | 59 | tol = atof(json_value["tol"].get<string>().c_str()); |
jmarkel44 | 20:653923c2f37a | 60 | |
jmarkel44 | 56:225786c56315 | 61 | return true; // object created successfully |
jmarkel44 | 51:66b820f203a5 | 62 | } |
jmarkel44 | 51:66b820f203a5 | 63 | |
jmarkel44 | 56:225786c56315 | 64 | // Method: registerControl |
jmarkel44 | 56:225786c56315 | 65 | // Description: register the setpoint control with the output task |
jmarkel44 | 56:225786c56315 | 66 | // |
jmarkel44 | 56:225786c56315 | 67 | // @param none |
jmarkel44 | 56:225786c56315 | 68 | // @return none |
jmarkel44 | 56:225786c56315 | 69 | |
jmarkel44 | 51:66b820f203a5 | 70 | void SetpointControl::registerControl(void) |
jmarkel44 | 51:66b820f203a5 | 71 | { |
jmarkel44 | 51:66b820f203a5 | 72 | if ( GLOBAL_outputTask_thread ) { |
jmarkel44 | 56:225786c56315 | 73 | // register our priority with the output master |
jmarkel44 | 51:66b820f203a5 | 74 | } |
jmarkel44 | 51:66b820f203a5 | 75 | } |
jmarkel44 | 51:66b820f203a5 | 76 | |
jmarkel44 | 56:225786c56315 | 77 | // Method: start |
jmarkel44 | 56:225786c56315 | 78 | // Description: start the setpoint control |
jmarkel44 | 56:225786c56315 | 79 | // |
jmarkel44 | 56:225786c56315 | 80 | // @param none |
jmarkel44 | 56:225786c56315 | 81 | // @return none |
jmarkel44 | 56:225786c56315 | 82 | |
jmarkel44 | 51:66b820f203a5 | 83 | void SetpointControl::start(void) |
jmarkel44 | 51:66b820f203a5 | 84 | { |
jmarkel44 | 56:225786c56315 | 85 | // this is the initial state; what else needs to be done?? |
jmarkel44 | 51:66b820f203a5 | 86 | this->currentState = STATE_STARTUP; |
jmarkel44 | 19:9bc8fabeddfa | 87 | } |
jmarkel44 | 19:9bc8fabeddfa | 88 | |
jmarkel44 | 56:225786c56315 | 89 | // Method: update |
jmarkel44 | 56:225786c56315 | 90 | // Description: based on the state of the control, check for |
jmarkel44 | 56:225786c56315 | 91 | // under limit and over limit values, adjust the |
jmarkel44 | 56:225786c56315 | 92 | // state accordingly |
jmarkel44 | 56:225786c56315 | 93 | // |
jmarkel44 | 56:225786c56315 | 94 | // @param none |
jmarkel44 | 56:225786c56315 | 95 | // @return none |
jmarkel44 | 56:225786c56315 | 96 | |
jmarkel44 | 51:66b820f203a5 | 97 | void SetpointControl::update(void) |
jmarkel44 | 51:66b820f203a5 | 98 | { |
jmarkel44 | 51:66b820f203a5 | 99 | switch (this->currentState) { |
jmarkel44 | 51:66b820f203a5 | 100 | case STATE_STARTUP: |
jmarkel44 | 51:66b820f203a5 | 101 | if ( this->underLimit() ) { |
jmarkel44 | 51:66b820f203a5 | 102 | // start the feed right away |
jmarkel44 | 51:66b820f203a5 | 103 | this->startFeed(); |
jmarkel44 | 51:66b820f203a5 | 104 | this->currentState = STATE_CONTROL_ON; |
jmarkel44 | 51:66b820f203a5 | 105 | } else { |
jmarkel44 | 51:66b820f203a5 | 106 | this->currentState = STATE_CONTROL_OFF; |
jmarkel44 | 51:66b820f203a5 | 107 | } |
jmarkel44 | 51:66b820f203a5 | 108 | break; |
jmarkel44 | 51:66b820f203a5 | 109 | case STATE_CONTROL_ON: |
jmarkel44 | 51:66b820f203a5 | 110 | if ( this->overLimit() ) { |
jmarkel44 | 51:66b820f203a5 | 111 | // stop the feed |
jmarkel44 | 51:66b820f203a5 | 112 | this->stopFeed(); |
jmarkel44 | 51:66b820f203a5 | 113 | this->currentState = STATE_CONTROL_OFF; |
jmarkel44 | 51:66b820f203a5 | 114 | } else { |
jmarkel44 | 51:66b820f203a5 | 115 | // do nothing |
jmarkel44 | 51:66b820f203a5 | 116 | } |
jmarkel44 | 51:66b820f203a5 | 117 | break; |
jmarkel44 | 51:66b820f203a5 | 118 | case STATE_CONTROL_OFF: |
jmarkel44 | 51:66b820f203a5 | 119 | if ( this->underLimit() ) { |
jmarkel44 | 51:66b820f203a5 | 120 | // start the feed |
jmarkel44 | 51:66b820f203a5 | 121 | this->startFeed(); |
jmarkel44 | 51:66b820f203a5 | 122 | this->currentState = STATE_CONTROL_ON; |
jmarkel44 | 51:66b820f203a5 | 123 | } else { |
jmarkel44 | 51:66b820f203a5 | 124 | // do nothing |
jmarkel44 | 51:66b820f203a5 | 125 | } |
jmarkel44 | 51:66b820f203a5 | 126 | break; |
jmarkel44 | 56:225786c56315 | 127 | //case STATE_CONTROL_DISABLED: |
jmarkel44 | 56:225786c56315 | 128 | //case STATE_CONTROL_PAUSED: |
jmarkel44 | 51:66b820f203a5 | 129 | default: |
jmarkel44 | 51:66b820f203a5 | 130 | break; |
jmarkel44 | 51:66b820f203a5 | 131 | } |
jmarkel44 | 51:66b820f203a5 | 132 | } |
jmarkel44 | 51:66b820f203a5 | 133 | |
jmarkel44 | 56:225786c56315 | 134 | // Method: overLimit |
jmarkel44 | 56:225786c56315 | 135 | // Description: (see @return) |
jmarkel44 | 56:225786c56315 | 136 | // |
jmarkel44 | 56:225786c56315 | 137 | // @param none |
jmarkel44 | 56:225786c56315 | 138 | // @return true if product is over the upper limit for normal mode |
jmarkel44 | 56:225786c56315 | 139 | // or under the limit for reverse mode; false otherwise |
jmarkel44 | 56:225786c56315 | 140 | |
jmarkel44 | 51:66b820f203a5 | 141 | bool SetpointControl::overLimit(void) |
jmarkel44 | 51:66b820f203a5 | 142 | { |
jmarkel44 | 56:225786c56315 | 143 | // stubbed for now |
jmarkel44 | 51:66b820f203a5 | 144 | return false; |
jmarkel44 | 51:66b820f203a5 | 145 | } |
jmarkel44 | 51:66b820f203a5 | 146 | |
jmarkel44 | 56:225786c56315 | 147 | // Method: underLimit |
jmarkel44 | 56:225786c56315 | 148 | // Description: (see @return) |
jmarkel44 | 56:225786c56315 | 149 | // |
jmarkel44 | 56:225786c56315 | 150 | // @param none |
jmarkel44 | 56:225786c56315 | 151 | // @return true if product is under lower limit for normal mode or |
jmarkel44 | 56:225786c56315 | 152 | // over the upper limit for reverse mode; false otherwise |
jmarkel44 | 56:225786c56315 | 153 | |
jmarkel44 | 51:66b820f203a5 | 154 | bool SetpointControl::underLimit(void) |
jmarkel44 | 51:66b820f203a5 | 155 | { |
jmarkel44 | 56:225786c56315 | 156 | // stubbed for now |
jmarkel44 | 70:7427f4959201 | 157 | return true; |
jmarkel44 | 51:66b820f203a5 | 158 | } |
jmarkel44 | 51:66b820f203a5 | 159 | |
jmarkel44 | 56:225786c56315 | 160 | // Method: startFeed() |
jmarkel44 | 56:225786c56315 | 161 | // Description: send ON indication to Output Master for this control's |
jmarkel44 | 56:225786c56315 | 162 | // relay |
jmarkel44 | 56:225786c56315 | 163 | // |
jmarkel44 | 56:225786c56315 | 164 | // @param none |
jmarkel44 | 56:225786c56315 | 165 | // @return none |
jmarkel44 | 56:225786c56315 | 166 | |
jmarkel44 | 51:66b820f203a5 | 167 | void SetpointControl::startFeed(void) |
jmarkel44 | 51:66b820f203a5 | 168 | { |
jmarkel44 | 56:225786c56315 | 169 | logInfo("%s: %s attempting to start feed on relay %s\n", |
jmarkel44 | 56:225786c56315 | 170 | __func__, controlFile.c_str(), output.c_str()); |
jmarkel44 | 56:225786c56315 | 171 | |
jmarkel44 | 70:7427f4959201 | 172 | OutputControlMsg_t *msg = OutputMasterMailBox.alloc(); |
jmarkel44 | 56:225786c56315 | 173 | |
jmarkel44 | 56:225786c56315 | 174 | // construct the ON message |
jmarkel44 | 70:7427f4959201 | 175 | msg->output = this->output; |
jmarkel44 | 56:225786c56315 | 176 | msg->state = ON; |
jmarkel44 | 70:7427f4959201 | 177 | msg->priority = this->priority; |
jmarkel44 | 56:225786c56315 | 178 | |
jmarkel44 | 70:7427f4959201 | 179 | // ship it to the output task |
jmarkel44 | 56:225786c56315 | 180 | OutputMasterMailBox.put(msg); |
jmarkel44 | 51:66b820f203a5 | 181 | } |
jmarkel44 | 51:66b820f203a5 | 182 | |
jmarkel44 | 56:225786c56315 | 183 | // Method: stopFeed |
jmarkel44 | 56:225786c56315 | 184 | // Description: send OFF indication to Output Master for this control's |
jmarkel44 | 56:225786c56315 | 185 | // relay |
jmarkel44 | 56:225786c56315 | 186 | // |
jmarkel44 | 56:225786c56315 | 187 | // @param none |
jmarkel44 | 56:225786c56315 | 188 | // @return none |
jmarkel44 | 56:225786c56315 | 189 | |
jmarkel44 | 51:66b820f203a5 | 190 | void SetpointControl::stopFeed(void) |
jmarkel44 | 51:66b820f203a5 | 191 | { |
jmarkel44 | 56:225786c56315 | 192 | logInfo("%s: %s attempting to start feed on relay %s\n", |
jmarkel44 | 56:225786c56315 | 193 | __func__, controlFile.c_str(), output.c_str()); |
jmarkel44 | 56:225786c56315 | 194 | |
jmarkel44 | 70:7427f4959201 | 195 | OutputControlMsg_t *msg = OutputMasterMailBox.alloc(); |
jmarkel44 | 56:225786c56315 | 196 | |
jmarkel44 | 56:225786c56315 | 197 | // construct the OFF message |
jmarkel44 | 70:7427f4959201 | 198 | msg->output = this->output; |
jmarkel44 | 56:225786c56315 | 199 | msg->state = OFF; |
jmarkel44 | 56:225786c56315 | 200 | msg->priority = this->priority; |
jmarkel44 | 56:225786c56315 | 201 | |
jmarkel44 | 56:225786c56315 | 202 | // ship it |
jmarkel44 | 56:225786c56315 | 203 | OutputMasterMailBox.put(msg); |
jmarkel44 | 51:66b820f203a5 | 204 | } |