Erick / Mbed 2 deprecated ICE_BLE_TEST

Dependencies:   NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed

Fork of ICE by Erick

Committer:
jmarkel44
Date:
Tue Sep 20 17:39:56 2016 +0000
Revision:
89:55ac65d7f206
Parent:
87:c466bde76fa0
Child:
90:7d9731dec0da
setpoint control updates;

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 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 priority = atoi(json_value["priority"].get<string>().c_str());
jmarkel44 28:c410a61238bb 51 input = json_value["input"].get<string>();
jmarkel44 28:c410a61238bb 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 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 20:653923c2f37a 59
jmarkel44 56:225786c56315 60 return true; // object created successfully
jmarkel44 51:66b820f203a5 61 }
jmarkel44 51:66b820f203a5 62
jmarkel44 56:225786c56315 63 // Method: start
jmarkel44 56:225786c56315 64 // Description: start the setpoint control
jmarkel44 56:225786c56315 65 //
jmarkel44 56:225786c56315 66 // @param none
jmarkel44 56:225786c56315 67 // @return none
jmarkel44 56:225786c56315 68
jmarkel44 51:66b820f203a5 69 void SetpointControl::start(void)
jmarkel44 51:66b820f203a5 70 {
jmarkel44 56:225786c56315 71 // this is the initial state; what else needs to be done??
jmarkel44 51:66b820f203a5 72 this->currentState = STATE_STARTUP;
jmarkel44 19:9bc8fabeddfa 73 }
jmarkel44 19:9bc8fabeddfa 74
jmarkel44 56:225786c56315 75 // Method: update
jmarkel44 56:225786c56315 76 // Description: based on the state of the control, check for
jmarkel44 56:225786c56315 77 // under limit and over limit values, adjust the
jmarkel44 56:225786c56315 78 // state accordingly
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::update(void)
jmarkel44 51:66b820f203a5 84 {
jmarkel44 51:66b820f203a5 85 switch (this->currentState) {
jmarkel44 51:66b820f203a5 86 case STATE_STARTUP:
jmarkel44 51:66b820f203a5 87 if ( this->underLimit() ) {
jmarkel44 51:66b820f203a5 88 // start the feed right away
jmarkel44 51:66b820f203a5 89 this->startFeed();
jmarkel44 51:66b820f203a5 90 this->currentState = STATE_CONTROL_ON;
jmarkel44 51:66b820f203a5 91 } else {
jmarkel44 51:66b820f203a5 92 this->currentState = STATE_CONTROL_OFF;
jmarkel44 51:66b820f203a5 93 }
jmarkel44 51:66b820f203a5 94 break;
jmarkel44 51:66b820f203a5 95 case STATE_CONTROL_ON:
jmarkel44 51:66b820f203a5 96 if ( this->overLimit() ) {
jmarkel44 51:66b820f203a5 97 // stop the feed
jmarkel44 51:66b820f203a5 98 this->stopFeed();
jmarkel44 51:66b820f203a5 99 this->currentState = STATE_CONTROL_OFF;
jmarkel44 51:66b820f203a5 100 } else {
jmarkel44 51:66b820f203a5 101 // do nothing
jmarkel44 51:66b820f203a5 102 }
jmarkel44 51:66b820f203a5 103 break;
jmarkel44 51:66b820f203a5 104 case STATE_CONTROL_OFF:
jmarkel44 51:66b820f203a5 105 if ( this->underLimit() ) {
jmarkel44 51:66b820f203a5 106 // start the feed
jmarkel44 51:66b820f203a5 107 this->startFeed();
jmarkel44 51:66b820f203a5 108 this->currentState = STATE_CONTROL_ON;
jmarkel44 51:66b820f203a5 109 } else {
jmarkel44 51:66b820f203a5 110 // do nothing
jmarkel44 51:66b820f203a5 111 }
jmarkel44 51:66b820f203a5 112 break;
jmarkel44 56:225786c56315 113 //case STATE_CONTROL_DISABLED:
jmarkel44 56:225786c56315 114 //case STATE_CONTROL_PAUSED:
jmarkel44 51:66b820f203a5 115 default:
jmarkel44 51:66b820f203a5 116 break;
jmarkel44 51:66b820f203a5 117 }
jmarkel44 51:66b820f203a5 118 }
jmarkel44 51:66b820f203a5 119
jmarkel44 56:225786c56315 120 // Method: overLimit
jmarkel44 56:225786c56315 121 // Description: (see @return)
jmarkel44 56:225786c56315 122 //
jmarkel44 56:225786c56315 123 // @param none
jmarkel44 56:225786c56315 124 // @return true if product is over the upper limit for normal mode
jmarkel44 56:225786c56315 125 // or under the limit for reverse mode; false otherwise
jmarkel44 56:225786c56315 126
jmarkel44 51:66b820f203a5 127 bool SetpointControl::overLimit(void)
jmarkel44 51:66b820f203a5 128 {
jmarkel44 51:66b820f203a5 129 return false;
jmarkel44 51:66b820f203a5 130 }
jmarkel44 51:66b820f203a5 131
jmarkel44 56:225786c56315 132 // Method: underLimit
jmarkel44 56:225786c56315 133 // Description: (see @return)
jmarkel44 56:225786c56315 134 //
jmarkel44 56:225786c56315 135 // @param none
jmarkel44 56:225786c56315 136 // @return true if product is under lower limit for normal mode or
jmarkel44 56:225786c56315 137 // over the upper limit for reverse mode; false otherwise
jmarkel44 56:225786c56315 138
jmarkel44 51:66b820f203a5 139 bool SetpointControl::underLimit(void)
jmarkel44 51:66b820f203a5 140 {
jmarkel44 87:c466bde76fa0 141 return false;
jmarkel44 51:66b820f203a5 142 }
jmarkel44 51:66b820f203a5 143
jmarkel44 56:225786c56315 144 // Method: startFeed()
jmarkel44 56:225786c56315 145 // Description: send ON indication to Output Master for this control's
jmarkel44 56:225786c56315 146 // relay
jmarkel44 56:225786c56315 147 //
jmarkel44 56:225786c56315 148 // @param none
jmarkel44 56:225786c56315 149 // @return none
jmarkel44 56:225786c56315 150
jmarkel44 51:66b820f203a5 151 void SetpointControl::startFeed(void)
jmarkel44 51:66b820f203a5 152 {
jmarkel44 56:225786c56315 153 logInfo("%s: %s attempting to start feed on relay %s\n",
jmarkel44 56:225786c56315 154 __func__, controlFile.c_str(), output.c_str());
jmarkel44 56:225786c56315 155
jmarkel44 71:34856d21f2bf 156 OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc();
jmarkel44 71:34856d21f2bf 157 memset(output_mail, 0, sizeof(OutputControlMsg_t));
jmarkel44 71:34856d21f2bf 158 output_mail->action = ACTION_CONTROL_ON;
jmarkel44 71:34856d21f2bf 159 output_mail->priority = this->priority;
jmarkel44 71:34856d21f2bf 160 strncpy(output_mail->output, this->output.c_str(), sizeof(output_mail->output)-1);
jmarkel44 71:34856d21f2bf 161 strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1);
jmarkel44 71:34856d21f2bf 162 OutputMasterMailBox.put(output_mail);
jmarkel44 51:66b820f203a5 163 }
jmarkel44 51:66b820f203a5 164
jmarkel44 56:225786c56315 165 // Method: stopFeed
jmarkel44 56:225786c56315 166 // Description: send OFF indication to Output Master for this control's
jmarkel44 56:225786c56315 167 // relay
jmarkel44 56:225786c56315 168 //
jmarkel44 56:225786c56315 169 // @param none
jmarkel44 56:225786c56315 170 // @return none
jmarkel44 56:225786c56315 171
jmarkel44 51:66b820f203a5 172 void SetpointControl::stopFeed(void)
jmarkel44 51:66b820f203a5 173 {
jmarkel44 71:34856d21f2bf 174 logInfo("%s: %s attempting to stop feed on relay %s\n",
jmarkel44 56:225786c56315 175 __func__, controlFile.c_str(), output.c_str());
jmarkel44 56:225786c56315 176
jmarkel44 71:34856d21f2bf 177 OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc();
jmarkel44 71:34856d21f2bf 178 memset(output_mail, 0, sizeof(OutputControlMsg_t));
jmarkel44 71:34856d21f2bf 179 output_mail->action = ACTION_CONTROL_OFF;
jmarkel44 71:34856d21f2bf 180 output_mail->priority = this->priority;
jmarkel44 71:34856d21f2bf 181 strncpy(output_mail->output, this->output.c_str(), sizeof(output_mail->output)-1);
jmarkel44 71:34856d21f2bf 182 strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1);
jmarkel44 71:34856d21f2bf 183 OutputMasterMailBox.put(output_mail);
jmarkel44 72:3754b352f156 184 }
jmarkel44 72:3754b352f156 185
jmarkel44 72:3754b352f156 186 // Method: unregisterControl
jmarkel44 72:3754b352f156 187 // Description: send OFF indication to Output Master for this control's
jmarkel44 72:3754b352f156 188 // relay
jmarkel44 72:3754b352f156 189 //
jmarkel44 72:3754b352f156 190 // @param none
jmarkel44 72:3754b352f156 191 // @return none
jmarkel44 72:3754b352f156 192 void SetpointControl::unregisterControl(void)
jmarkel44 72:3754b352f156 193 {
jmarkel44 72:3754b352f156 194 logInfo("%s: %s attempting to stop feed on relay %s\n",
jmarkel44 72:3754b352f156 195 __func__, controlFile.c_str(), output.c_str());
jmarkel44 72:3754b352f156 196
jmarkel44 72:3754b352f156 197 OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc();
jmarkel44 72:3754b352f156 198 memset(output_mail, 0, sizeof(OutputControlMsg_t));
jmarkel44 72:3754b352f156 199 output_mail->action = ACTION_CONTROL_UNREGISTER;
jmarkel44 72:3754b352f156 200 output_mail->priority = this->priority;
jmarkel44 72:3754b352f156 201 strncpy(output_mail->output, this->output.c_str(), sizeof(output_mail->output)-1);
jmarkel44 72:3754b352f156 202 strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1);
jmarkel44 72:3754b352f156 203 OutputMasterMailBox.put(output_mail);
jmarkel44 51:66b820f203a5 204 }