Erick / Mbed 2 deprecated ICE_BLE_TEST

Dependencies:   NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed

Fork of ICE by Erick

Committer:
jmarkel44
Date:
Mon Oct 24 13:07:52 2016 +0000
Revision:
260:fe726583ba1d
Parent:
253:ae850c19cf81
Child:
276:851554207c77
working on failsafes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jmarkel44 96:807f04bd5256 1 /******************************************************************************
jmarkel44 96:807f04bd5256 2 *
jmarkel44 96:807f04bd5256 3 * File: ManualControl.cpp
jmarkel44 96:807f04bd5256 4 * Desciption: ICE Manual Control Class implementation
jmarkel44 96:807f04bd5256 5 *
jmarkel44 96:807f04bd5256 6 *****************************************************************************/
jmarkel44 96:807f04bd5256 7 #include "ManualControl.h"
jmarkel44 96:807f04bd5256 8 #include "mDot.h"
jmarkel44 242:3b0086a6d625 9 #include "cJSON.h"
jmarkel44 96:807f04bd5256 10 #include "ModbusMasterApi.h"
jmarkel44 96:807f04bd5256 11 #include "global.h"
jmarkel44 96:807f04bd5256 12 #include <string>
jmarkel44 205:3c84af5f711f 13 #include <iostream>
jmarkel44 205:3c84af5f711f 14 #include <iomanip>
jmarkel44 96:807f04bd5256 15
jmarkel44 96:807f04bd5256 16 extern mDot *GLOBAL_mdot;
jmarkel44 96:807f04bd5256 17
jmarkel44 217:d5a2ff093319 18 //
jmarkel44 192:052a419837fa 19 // method: load
jmarkel44 192:052a419837fa 20 // description: open the configuration file and assign data to the
jmarkel44 96:807f04bd5256 21 // setpoint control object
jmarkel44 96:807f04bd5256 22 //
jmarkel44 96:807f04bd5256 23 // @param controlFile -> name of the control file
jmarkel44 96:807f04bd5256 24 // @return true if data is assigned; false on error
jmarkel44 217:d5a2ff093319 25 //
jmarkel44 96:807f04bd5256 26 bool ManualControl::load(string _controlFile)
jmarkel44 96:807f04bd5256 27 {
jmarkel44 97:5cf6ab71dcd0 28 controlFile = _controlFile;
jmarkel44 97:5cf6ab71dcd0 29
jmarkel44 97:5cf6ab71dcd0 30 // open and read from the control file
jmarkel44 97:5cf6ab71dcd0 31 mDot::mdot_file file = GLOBAL_mdot->openUserFile(controlFile.c_str(), mDot::FM_RDONLY);
jmarkel44 97:5cf6ab71dcd0 32 if ( file.fd < 0 )
jmarkel44 97:5cf6ab71dcd0 33 return false;
jmarkel44 97:5cf6ab71dcd0 34
jmarkel44 97:5cf6ab71dcd0 35 // read the data into a buffer
jmarkel44 177:9ec90c8e3ce1 36 char dataBuf[MAX_FILE_SIZE];
jmarkel44 97:5cf6ab71dcd0 37
jmarkel44 97:5cf6ab71dcd0 38 int bytes_read = GLOBAL_mdot->readUserFile(file, (void *)dataBuf, sizeof(dataBuf));
jmarkel44 97:5cf6ab71dcd0 39 if ( bytes_read != sizeof(dataBuf) ) {
jmarkel44 97:5cf6ab71dcd0 40 logError("%s: failed to read %d bytes from %s", __func__, sizeof(dataBuf), controlFile.c_str());
jmarkel44 97:5cf6ab71dcd0 41 // caller should destroy the object
jmarkel44 97:5cf6ab71dcd0 42 return false;
jmarkel44 97:5cf6ab71dcd0 43 }
jmarkel44 97:5cf6ab71dcd0 44
jmarkel44 97:5cf6ab71dcd0 45 // close the file
jmarkel44 97:5cf6ab71dcd0 46 GLOBAL_mdot->closeUserFile(file);
jmarkel44 97:5cf6ab71dcd0 47
jmarkel44 97:5cf6ab71dcd0 48 // parse the json data
jmarkel44 242:3b0086a6d625 49 cJSON * root = cJSON_Parse(dataBuf);
jmarkel44 192:052a419837fa 50
jmarkel44 260:fe726583ba1d 51 if ( !cJSON_HasObjectItem(root, "id") ||
jmarkel44 260:fe726583ba1d 52 !cJSON_HasObjectItem(root, "output") ||
jmarkel44 260:fe726583ba1d 53 !cJSON_HasObjectItem(root, "type") ||
jmarkel44 260:fe726583ba1d 54 !cJSON_HasObjectItem(root, "priority") ||
jmarkel44 260:fe726583ba1d 55 !cJSON_HasObjectItem(root, "duration") ||
jmarkel44 260:fe726583ba1d 56 !cJSON_HasObjectItem(root, "setpoint") ||
jmarkel44 260:fe726583ba1d 57 !cJSON_HasObjectItem(root, "state") ||
jmarkel44 260:fe726583ba1d 58 !cJSON_HasObjectItem(root, "percent") ) {
jmarkel44 260:fe726583ba1d 59 logError("Manual control is missing expected tags\n");
jmarkel44 260:fe726583ba1d 60 return false;
jmarkel44 260:fe726583ba1d 61 }
jmarkel44 260:fe726583ba1d 62
jmarkel44 242:3b0086a6d625 63 id = cJSON_GetObjectItem(root,"id")->valuestring;
jmarkel44 242:3b0086a6d625 64 output = cJSON_GetObjectItem(root,"output")->valuestring;
jmarkel44 242:3b0086a6d625 65 type = atoi(cJSON_GetObjectItem(root,"type")->valuestring);
jmarkel44 242:3b0086a6d625 66 priority = atoi(cJSON_GetObjectItem(root, "priority")->valuestring);
jmarkel44 242:3b0086a6d625 67 duration = atoi(cJSON_GetObjectItem(root, "duration")->valuestring);
jmarkel44 242:3b0086a6d625 68 setpoint = atof(cJSON_GetObjectItem(root, "setpoint")->valuestring);
jmarkel44 242:3b0086a6d625 69 state = atoi(cJSON_GetObjectItem(root, "state")->valuestring);
jmarkel44 242:3b0086a6d625 70 percent = atoi(cJSON_GetObjectItem(root, "percent")->valuestring);
jmarkel44 97:5cf6ab71dcd0 71
jmarkel44 96:807f04bd5256 72 return true;
jmarkel44 96:807f04bd5256 73 }
jmarkel44 96:807f04bd5256 74
jmarkel44 217:d5a2ff093319 75 //
jmarkel44 192:052a419837fa 76 // method: start
jmarkel44 192:052a419837fa 77 // description: start the manual control
jmarkel44 96:807f04bd5256 78 //
jmarkel44 96:807f04bd5256 79 // @param none
jmarkel44 96:807f04bd5256 80 // @return none
jmarkel44 217:d5a2ff093319 81 //
jmarkel44 97:5cf6ab71dcd0 82 void ManualControl::start(void)
jmarkel44 96:807f04bd5256 83 {
jmarkel44 97:5cf6ab71dcd0 84 currentState = STATE_STARTUP;
jmarkel44 96:807f04bd5256 85 }
jmarkel44 96:807f04bd5256 86
jmarkel44 217:d5a2ff093319 87 //
jmarkel44 192:052a419837fa 88 // method: update
jmarkel44 260:fe726583ba1d 89 // description: the setpoint control's state machine
jmarkel44 96:807f04bd5256 90 //
jmarkel44 96:807f04bd5256 91 // @param none
jmarkel44 96:807f04bd5256 92 // @return none
jmarkel44 217:d5a2ff093319 93 //
jmarkel44 96:807f04bd5256 94 int ManualControl::update(void)
jmarkel44 96:807f04bd5256 95 {
jmarkel44 97:5cf6ab71dcd0 96 int rc = 0;
jmarkel44 97:5cf6ab71dcd0 97 switch ( this->currentState ) {
jmarkel44 131:a290a3934132 98 case STATE_INIT:
jmarkel44 131:a290a3934132 99 // do nothing
jmarkel44 131:a290a3934132 100 break;
jmarkel44 97:5cf6ab71dcd0 101 case STATE_STARTUP:
jmarkel44 97:5cf6ab71dcd0 102 if ( state ) {
jmarkel44 97:5cf6ab71dcd0 103 this->currentState = STATE_CONTROL_ON;
jmarkel44 97:5cf6ab71dcd0 104 } else {
jmarkel44 97:5cf6ab71dcd0 105 this->currentState = STATE_CONTROL_OFF;
jmarkel44 97:5cf6ab71dcd0 106 }
jmarkel44 164:7cecd731882e 107 this->powerOutput();
jmarkel44 97:5cf6ab71dcd0 108 break;
jmarkel44 97:5cf6ab71dcd0 109 case STATE_CONTROL_ON:
jmarkel44 97:5cf6ab71dcd0 110 if ( !state ) {
jmarkel44 97:5cf6ab71dcd0 111 this->currentState = STATE_CONTROL_OFF;
jmarkel44 164:7cecd731882e 112 this->powerOutput();
jmarkel44 97:5cf6ab71dcd0 113 }
jmarkel44 97:5cf6ab71dcd0 114 break;
jmarkel44 97:5cf6ab71dcd0 115 case STATE_CONTROL_OFF:
jmarkel44 97:5cf6ab71dcd0 116 if ( state ) {
jmarkel44 97:5cf6ab71dcd0 117 this->currentState = STATE_CONTROL_ON;
jmarkel44 164:7cecd731882e 118 this->powerOutput();
jmarkel44 97:5cf6ab71dcd0 119 }
jmarkel44 97:5cf6ab71dcd0 120 break;
jmarkel44 97:5cf6ab71dcd0 121 default:
jmarkel44 97:5cf6ab71dcd0 122 logError("%s unknown state %d\n", __func__, this->currentState);
jmarkel44 97:5cf6ab71dcd0 123 rc = -1;
jmarkel44 97:5cf6ab71dcd0 124 break;
jmarkel44 97:5cf6ab71dcd0 125 }
jmarkel44 97:5cf6ab71dcd0 126 return rc;
jmarkel44 96:807f04bd5256 127 }
jmarkel44 96:807f04bd5256 128
jmarkel44 217:d5a2ff093319 129 //
jmarkel44 192:052a419837fa 130 // method: unregisterControl
jmarkel44 192:052a419837fa 131 // description: unregister this control with the output master
jmarkel44 96:807f04bd5256 132 //
jmarkel44 96:807f04bd5256 133 // @param none
jmarkel44 96:807f04bd5256 134 // @return none
jmarkel44 217:d5a2ff093319 135 //
jmarkel44 96:807f04bd5256 136 int ManualControl::unregisterControl(void)
jmarkel44 96:807f04bd5256 137 {
jmarkel44 115:1558e01d04c6 138 logInfo("%s: Attempting to unregister %s\n",
jmarkel44 115:1558e01d04c6 139 __func__, controlFile.c_str());
jmarkel44 97:5cf6ab71dcd0 140
jmarkel44 97:5cf6ab71dcd0 141 OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc();
jmarkel44 97:5cf6ab71dcd0 142 memset(output_mail, 0, sizeof(OutputControlMsg_t));
jmarkel44 192:052a419837fa 143
jmarkel44 121:650205ffa656 144 output_mail->controlType = CONTROL_MANUAL;
jmarkel44 121:650205ffa656 145 output_mail->action = ACTION_CONTROL_UNREGISTER;
jmarkel44 121:650205ffa656 146 output_mail->priority = this->priority;
jmarkel44 115:1558e01d04c6 147 strncpy(output_mail->output_tag, this->output.c_str(), sizeof(output_mail->output_tag)-1);
jmarkel44 97:5cf6ab71dcd0 148 strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1);
jmarkel44 192:052a419837fa 149
jmarkel44 97:5cf6ab71dcd0 150 OutputMasterMailBox.put(output_mail);
jmarkel44 97:5cf6ab71dcd0 151 return 0;
jmarkel44 97:5cf6ab71dcd0 152 }
jmarkel44 97:5cf6ab71dcd0 153
jmarkel44 192:052a419837fa 154 //
jmarkel44 192:052a419837fa 155 // method: powerOutput
jmarkel44 192:052a419837fa 156 // description: send a message to the output thread to power the relay
jmarkel44 192:052a419837fa 157 //
jmarkel44 192:052a419837fa 158 // @param none
jmarkel44 192:052a419837fa 159 // @return -1 on error; 0 otherwise
jmarkel44 192:052a419837fa 160 //
jmarkel44 164:7cecd731882e 161 int ManualControl::powerOutput(void)
jmarkel44 97:5cf6ab71dcd0 162 {
jmarkel44 97:5cf6ab71dcd0 163 printf("%s: %s attempting to manually turn %s relay %s\n",
jmarkel44 97:5cf6ab71dcd0 164 __func__, controlFile.c_str(), (state) ? "ON" : "OFF", id.c_str());
jmarkel44 97:5cf6ab71dcd0 165
jmarkel44 97:5cf6ab71dcd0 166 OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc();
jmarkel44 97:5cf6ab71dcd0 167 memset(output_mail, 0, sizeof(OutputControlMsg_t));
jmarkel44 192:052a419837fa 168
jmarkel44 121:650205ffa656 169 output_mail->controlType = CONTROL_MANUAL;
jmarkel44 115:1558e01d04c6 170 output_mail->action = (state) ? ACTION_CONTROL_ON : ACTION_CONTROL_OFF;
jmarkel44 115:1558e01d04c6 171 output_mail->priority = this->priority;
jmarkel44 115:1558e01d04c6 172 strncpy(output_mail->output_tag, this->output.c_str(), sizeof(output_mail->output_tag)-1);
jmarkel44 97:5cf6ab71dcd0 173 strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1);
jmarkel44 192:052a419837fa 174
jmarkel44 97:5cf6ab71dcd0 175 OutputMasterMailBox.put(output_mail);
jmarkel44 96:807f04bd5256 176 return 0;
jmarkel44 96:807f04bd5256 177 }
jmarkel44 96:807f04bd5256 178
jmarkel44 192:052a419837fa 179 //
jmarkel44 192:052a419837fa 180 // method: display
jmarkel44 192:052a419837fa 181 // description: display the pertinents
jmarkel44 192:052a419837fa 182 //
jmarkel44 192:052a419837fa 183 // @param none
jmarkel44 192:052a419837fa 184 // @return none
jmarkel44 192:052a419837fa 185 //
jmarkel44 96:807f04bd5256 186 void ManualControl::display(void)
jmarkel44 260:fe726583ba1d 187 {
jmarkel44 131:a290a3934132 188 string mapper[] = { "INIT",
jmarkel44 131:a290a3934132 189 "STARTUP",
jmarkel44 97:5cf6ab71dcd0 190 "CONTROL_ON",
jmarkel44 97:5cf6ab71dcd0 191 "CONTROL_OFF"
jmarkel44 97:5cf6ab71dcd0 192 };
jmarkel44 260:fe726583ba1d 193
jmarkel44 97:5cf6ab71dcd0 194 printf("\r\n");
jmarkel44 205:3c84af5f711f 195 std::cout << left << setw(10) << setfill(' ') << "manual: ";
jmarkel44 253:ae850c19cf81 196 std::cout << left << setw(40) << setfill(' ') << controlFile;
jmarkel44 205:3c84af5f711f 197 std::cout << left << setw(20) << setfill(' ') << id;
jmarkel44 205:3c84af5f711f 198 std::cout << left << setw(20) << setfill(' ') << output;
jmarkel44 205:3c84af5f711f 199 std::cout << left << setw(6) << setfill(' ') << type;
jmarkel44 205:3c84af5f711f 200 std::cout << left << setw(6) << setfill(' ') << priority;
jmarkel44 205:3c84af5f711f 201 std::cout << left << setw(8) << setfill(' ') << duration;
jmarkel44 205:3c84af5f711f 202 std::cout << left << setw(8) << setfill(' ') << percent;
jmarkel44 205:3c84af5f711f 203 std::cout << left << setw(16) << setfill(' ') << mapper[currentState];
jmarkel44 205:3c84af5f711f 204 std::cout.flush();
jmarkel44 96:807f04bd5256 205 }