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/FailsafeControl.cpp@262:696cd48bb04a, 2016-10-24 (annotated)
- Committer:
- jmarkel44
- Date:
- Mon Oct 24 21:09:31 2016 +0000
- Revision:
- 262:696cd48bb04a
- Parent:
- 261:4e9a588c938e
- Child:
- 269:97243a7f56ba
general high and low failsafe controls working;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| jmarkel44 | 250:1cd8ec63e9e9 | 1 | /****************************************************************************** |
| jmarkel44 | 250:1cd8ec63e9e9 | 2 | * |
| jmarkel44 | 250:1cd8ec63e9e9 | 3 | * File: CompositeControl.cpp |
| jmarkel44 | 250:1cd8ec63e9e9 | 4 | * Desciption: ICE Composite Control Class implementation |
| jmarkel44 | 250:1cd8ec63e9e9 | 5 | * |
| jmarkel44 | 250:1cd8ec63e9e9 | 6 | *****************************************************************************/ |
| jmarkel44 | 250:1cd8ec63e9e9 | 7 | #include "FailsafeControl.h" |
| jmarkel44 | 250:1cd8ec63e9e9 | 8 | #include "cJSON.h" |
| jmarkel44 | 250:1cd8ec63e9e9 | 9 | #include "mDot.h" |
| jmarkel44 | 250:1cd8ec63e9e9 | 10 | #include "global.h" |
| jmarkel44 | 252:3c9863f951b7 | 11 | #include "ModbusMasterApi.h" |
| jmarkel44 | 250:1cd8ec63e9e9 | 12 | #include <string> |
| jmarkel44 | 250:1cd8ec63e9e9 | 13 | #include <iostream> |
| jmarkel44 | 250:1cd8ec63e9e9 | 14 | #include <iomanip> |
| jmarkel44 | 250:1cd8ec63e9e9 | 15 | |
| jmarkel44 | 250:1cd8ec63e9e9 | 16 | extern mDot *GLOBAL_mdot; |
| jmarkel44 | 250:1cd8ec63e9e9 | 17 | |
| jmarkel44 | 250:1cd8ec63e9e9 | 18 | // |
| jmarkel44 | 250:1cd8ec63e9e9 | 19 | // method: load |
| jmarkel44 | 250:1cd8ec63e9e9 | 20 | // description: load a composite control |
| jmarkel44 | 250:1cd8ec63e9e9 | 21 | // |
| jmarkel44 | 250:1cd8ec63e9e9 | 22 | // @param none |
| jmarkel44 | 250:1cd8ec63e9e9 | 23 | // @return none |
| jmarkel44 | 250:1cd8ec63e9e9 | 24 | // |
| jmarkel44 | 250:1cd8ec63e9e9 | 25 | bool FailsafeControl::load(std::string _controlFile) |
| jmarkel44 | 250:1cd8ec63e9e9 | 26 | { |
| jmarkel44 | 250:1cd8ec63e9e9 | 27 | controlFile = _controlFile; |
| jmarkel44 | 250:1cd8ec63e9e9 | 28 | |
| jmarkel44 | 250:1cd8ec63e9e9 | 29 | // open and read from the control file |
| jmarkel44 | 250:1cd8ec63e9e9 | 30 | mDot::mdot_file file = GLOBAL_mdot->openUserFile(controlFile.c_str(), mDot::FM_RDONLY); |
| jmarkel44 | 250:1cd8ec63e9e9 | 31 | if ( file.fd < 0 ) { |
| jmarkel44 | 250:1cd8ec63e9e9 | 32 | logError("%s: failed to open %s\n", __func__, controlFile.c_str()); |
| jmarkel44 | 250:1cd8ec63e9e9 | 33 | return false; |
| jmarkel44 | 250:1cd8ec63e9e9 | 34 | } |
| jmarkel44 | 250:1cd8ec63e9e9 | 35 | |
| jmarkel44 | 250:1cd8ec63e9e9 | 36 | // read the data into a buffer |
| jmarkel44 | 250:1cd8ec63e9e9 | 37 | char dataBuf[MAX_FILE_SIZE]; |
| jmarkel44 | 250:1cd8ec63e9e9 | 38 | |
| jmarkel44 | 250:1cd8ec63e9e9 | 39 | int bytes_read = GLOBAL_mdot->readUserFile(file, (void *)dataBuf, sizeof(dataBuf)); |
| jmarkel44 | 250:1cd8ec63e9e9 | 40 | if ( bytes_read != sizeof(dataBuf) ) { |
| jmarkel44 | 250:1cd8ec63e9e9 | 41 | logError("%s: failed to read %d bytes from %s", __func__, sizeof(dataBuf), controlFile.c_str()); |
| jmarkel44 | 250:1cd8ec63e9e9 | 42 | // caller should destroy the object |
| jmarkel44 | 250:1cd8ec63e9e9 | 43 | return false; |
| jmarkel44 | 250:1cd8ec63e9e9 | 44 | } |
| jmarkel44 | 250:1cd8ec63e9e9 | 45 | |
| jmarkel44 | 250:1cd8ec63e9e9 | 46 | // close the file |
| jmarkel44 | 250:1cd8ec63e9e9 | 47 | GLOBAL_mdot->closeUserFile(file); |
| jmarkel44 | 250:1cd8ec63e9e9 | 48 | |
| jmarkel44 | 250:1cd8ec63e9e9 | 49 | // parse the data |
| jmarkel44 | 250:1cd8ec63e9e9 | 50 | cJSON * root = cJSON_Parse(dataBuf); |
| jmarkel44 | 250:1cd8ec63e9e9 | 51 | |
| jmarkel44 | 250:1cd8ec63e9e9 | 52 | id = cJSON_GetObjectItem(root, "id")->valuestring; |
| jmarkel44 | 250:1cd8ec63e9e9 | 53 | priority = atoi(cJSON_GetObjectItem(root, "priority")->valuestring); |
| jmarkel44 | 250:1cd8ec63e9e9 | 54 | input = cJSON_GetObjectItem(root, "input")->valuestring; |
| jmarkel44 | 250:1cd8ec63e9e9 | 55 | output = cJSON_GetObjectItem(root, "output")->valuestring; |
| jmarkel44 | 250:1cd8ec63e9e9 | 56 | |
| jmarkel44 | 250:1cd8ec63e9e9 | 57 | hfs_data.value = atof(cJSON_GetObjectItem(root, "hfsValue")->valuestring); |
| jmarkel44 | 250:1cd8ec63e9e9 | 58 | hfs_data.dutyCycle = atoi(cJSON_GetObjectItem(root, "hfsDutyCycle")->valuestring); |
| jmarkel44 | 250:1cd8ec63e9e9 | 59 | hfs_data.interval = atoi(cJSON_GetObjectItem(root, "hfsInterval")->valuestring); |
| jmarkel44 | 250:1cd8ec63e9e9 | 60 | |
| jmarkel44 | 250:1cd8ec63e9e9 | 61 | lfs_data.value = atof(cJSON_GetObjectItem(root, "lfsValue")->valuestring); |
| jmarkel44 | 250:1cd8ec63e9e9 | 62 | lfs_data.dutyCycle = atoi(cJSON_GetObjectItem(root, "lfsDutyCycle")->valuestring); |
| jmarkel44 | 250:1cd8ec63e9e9 | 63 | lfs_data.interval = atoi(cJSON_GetObjectItem(root, "lfsInterval")->valuestring); |
| jmarkel44 | 250:1cd8ec63e9e9 | 64 | |
| jmarkel44 | 250:1cd8ec63e9e9 | 65 | return true; |
| jmarkel44 | 250:1cd8ec63e9e9 | 66 | } |
| jmarkel44 | 250:1cd8ec63e9e9 | 67 | |
| jmarkel44 | 250:1cd8ec63e9e9 | 68 | // |
| jmarkel44 | 250:1cd8ec63e9e9 | 69 | // method: start |
| jmarkel44 | 250:1cd8ec63e9e9 | 70 | // description: start the failsafe control |
| jmarkel44 | 250:1cd8ec63e9e9 | 71 | // |
| jmarkel44 | 250:1cd8ec63e9e9 | 72 | // @param none |
| jmarkel44 | 250:1cd8ec63e9e9 | 73 | // @return none |
| jmarkel44 | 250:1cd8ec63e9e9 | 74 | // |
| jmarkel44 | 250:1cd8ec63e9e9 | 75 | void FailsafeControl::start(void) |
| jmarkel44 | 250:1cd8ec63e9e9 | 76 | { |
| jmarkel44 | 250:1cd8ec63e9e9 | 77 | currentState = STATE_START; |
| jmarkel44 | 250:1cd8ec63e9e9 | 78 | } |
| jmarkel44 | 250:1cd8ec63e9e9 | 79 | |
| jmarkel44 | 250:1cd8ec63e9e9 | 80 | // |
| jmarkel44 | 250:1cd8ec63e9e9 | 81 | // method: update |
| jmarkel44 | 252:3c9863f951b7 | 82 | // description: update the faisafe control using the FSM |
| jmarkel44 | 250:1cd8ec63e9e9 | 83 | // |
| jmarkel44 | 250:1cd8ec63e9e9 | 84 | // @param none |
| jmarkel44 | 250:1cd8ec63e9e9 | 85 | // @return none |
| jmarkel44 | 250:1cd8ec63e9e9 | 86 | // |
| jmarkel44 | 250:1cd8ec63e9e9 | 87 | void FailsafeControl::update(void) |
| jmarkel44 | 250:1cd8ec63e9e9 | 88 | { |
| jmarkel44 | 260:fe726583ba1d | 89 | switch (this->currentState) { |
| jmarkel44 | 250:1cd8ec63e9e9 | 90 | case STATE_INIT: |
| jmarkel44 | 261:4e9a588c938e | 91 | // do nothing - control must be started |
| jmarkel44 | 250:1cd8ec63e9e9 | 92 | break; |
| jmarkel44 | 250:1cd8ec63e9e9 | 93 | case STATE_START: |
| jmarkel44 | 260:fe726583ba1d | 94 | // control has been started, do some checking |
| jmarkel44 | 252:3c9863f951b7 | 95 | if ( this->aboveHighFailsafe() ) { |
| jmarkel44 | 262:696cd48bb04a | 96 | // start high failsafe duty cycle |
| jmarkel44 | 260:fe726583ba1d | 97 | this->currentState = STATE_CONTROL_HFS_ON; |
| jmarkel44 | 262:696cd48bb04a | 98 | sendMailToOutput(ACTION_CONTROL_ON); |
| jmarkel44 | 261:4e9a588c938e | 99 | this->startHfsDutyTimer(); |
| jmarkel44 | 262:696cd48bb04a | 100 | } else if ( this->belowLowFailsafe() ) { |
| jmarkel44 | 262:696cd48bb04a | 101 | // start low failsafe duty cycle |
| jmarkel44 | 260:fe726583ba1d | 102 | this->currentState = STATE_CONTROL_LFS_ON; |
| jmarkel44 | 262:696cd48bb04a | 103 | sendMailToOutput(ACTION_CONTROL_ON); |
| jmarkel44 | 262:696cd48bb04a | 104 | this->startLfsDutyTimer(); |
| jmarkel44 | 262:696cd48bb04a | 105 | } else { |
| jmarkel44 | 260:fe726583ba1d | 106 | this->currentState = STATE_CONTROL_OFF; |
| jmarkel44 | 252:3c9863f951b7 | 107 | } |
| jmarkel44 | 252:3c9863f951b7 | 108 | break; |
| jmarkel44 | 250:1cd8ec63e9e9 | 109 | case STATE_CONTROL_OFF: |
| jmarkel44 | 260:fe726583ba1d | 110 | // control is acting normal, within bounds |
| jmarkel44 | 252:3c9863f951b7 | 111 | if ( this->aboveHighFailsafe() ) { |
| jmarkel44 | 262:696cd48bb04a | 112 | // restart the high failsafe duty cycle |
| jmarkel44 | 260:fe726583ba1d | 113 | this->currentState = STATE_CONTROL_HFS_ON; |
| jmarkel44 | 262:696cd48bb04a | 114 | sendMailToOutput(ACTION_CONTROL_ON); |
| jmarkel44 | 262:696cd48bb04a | 115 | this->startHfsDutyTimer(); |
| jmarkel44 | 252:3c9863f951b7 | 116 | } else if ( this->belowLowFailsafe() ) { |
| jmarkel44 | 262:696cd48bb04a | 117 | // restart the low failsafe duty cycle |
| jmarkel44 | 260:fe726583ba1d | 118 | this->currentState = STATE_CONTROL_LFS_ON; |
| jmarkel44 | 262:696cd48bb04a | 119 | sendMailToOutput(ACTION_CONTROL_ON); |
| jmarkel44 | 262:696cd48bb04a | 120 | this->startLfsDutyTimer(); |
| jmarkel44 | 252:3c9863f951b7 | 121 | } else { |
| jmarkel44 | 252:3c9863f951b7 | 122 | // do nothing |
| jmarkel44 | 252:3c9863f951b7 | 123 | } |
| jmarkel44 | 252:3c9863f951b7 | 124 | break; |
| jmarkel44 | 250:1cd8ec63e9e9 | 125 | case STATE_CONTROL_LFS_ON: |
| jmarkel44 | 260:fe726583ba1d | 126 | // control is in low-failsafe with duty cycle ON |
| jmarkel44 | 252:3c9863f951b7 | 127 | if ( !this->belowLowFailsafe() ) { |
| jmarkel44 | 260:fe726583ba1d | 128 | this->currentState = STATE_CONTROL_OFF; |
| jmarkel44 | 262:696cd48bb04a | 129 | this->unregisterControl(); |
| jmarkel44 | 260:fe726583ba1d | 130 | } else if ( this->dutyOnExpired() ) { |
| jmarkel44 | 260:fe726583ba1d | 131 | this->currentState = STATE_CONTROL_LFS_OFF; |
| jmarkel44 | 262:696cd48bb04a | 132 | sendMailToOutput(ACTION_CONTROL_OFF); |
| jmarkel44 | 252:3c9863f951b7 | 133 | } else { |
| jmarkel44 | 260:fe726583ba1d | 134 | // do nothing |
| jmarkel44 | 252:3c9863f951b7 | 135 | } |
| jmarkel44 | 252:3c9863f951b7 | 136 | break; |
| jmarkel44 | 250:1cd8ec63e9e9 | 137 | case STATE_CONTROL_LFS_OFF: |
| jmarkel44 | 260:fe726583ba1d | 138 | // control is in low-failsafe with duty cycle OFF |
| jmarkel44 | 252:3c9863f951b7 | 139 | if ( !this->belowLowFailsafe() ) { |
| jmarkel44 | 260:fe726583ba1d | 140 | this->currentState = STATE_CONTROL_OFF; |
| jmarkel44 | 262:696cd48bb04a | 141 | this->unregisterControl(); |
| jmarkel44 | 260:fe726583ba1d | 142 | } else if ( this->dutyOffExpired() ) { |
| jmarkel44 | 260:fe726583ba1d | 143 | this->currentState = STATE_CONTROL_LFS_ON; |
| jmarkel44 | 262:696cd48bb04a | 144 | this->startLfsDutyTimer(); |
| jmarkel44 | 252:3c9863f951b7 | 145 | } else { |
| jmarkel44 | 260:fe726583ba1d | 146 | // do nothing |
| jmarkel44 | 252:3c9863f951b7 | 147 | } |
| jmarkel44 | 252:3c9863f951b7 | 148 | break; |
| jmarkel44 | 250:1cd8ec63e9e9 | 149 | case STATE_CONTROL_HFS_ON: |
| jmarkel44 | 260:fe726583ba1d | 150 | // control is in high-failsafe with duty cycle ON |
| jmarkel44 | 252:3c9863f951b7 | 151 | if ( !this->aboveHighFailsafe() ) { |
| jmarkel44 | 260:fe726583ba1d | 152 | this->currentState = STATE_CONTROL_OFF; |
| jmarkel44 | 262:696cd48bb04a | 153 | this->unregisterControl(); |
| jmarkel44 | 260:fe726583ba1d | 154 | } else if ( this->dutyOnExpired() ) { |
| jmarkel44 | 260:fe726583ba1d | 155 | this->currentState = STATE_CONTROL_HFS_OFF; |
| jmarkel44 | 262:696cd48bb04a | 156 | sendMailToOutput(ACTION_CONTROL_OFF); |
| jmarkel44 | 260:fe726583ba1d | 157 | } else { |
| jmarkel44 | 260:fe726583ba1d | 158 | // do nothing |
| jmarkel44 | 252:3c9863f951b7 | 159 | } |
| jmarkel44 | 252:3c9863f951b7 | 160 | break; |
| jmarkel44 | 250:1cd8ec63e9e9 | 161 | case STATE_CONTROL_HFS_OFF: |
| jmarkel44 | 260:fe726583ba1d | 162 | // control is in high-failsafe with cuty cycle OFF |
| jmarkel44 | 252:3c9863f951b7 | 163 | if ( !this->aboveHighFailsafe() ) { |
| jmarkel44 | 260:fe726583ba1d | 164 | this->currentState = STATE_CONTROL_OFF; |
| jmarkel44 | 262:696cd48bb04a | 165 | this->unregisterControl(); |
| jmarkel44 | 260:fe726583ba1d | 166 | } else if ( this->dutyOffExpired() ) { |
| jmarkel44 | 260:fe726583ba1d | 167 | this->currentState = STATE_CONTROL_LFS_ON; |
| jmarkel44 | 262:696cd48bb04a | 168 | sendMailToOutput(ACTION_CONTROL_ON); |
| jmarkel44 | 262:696cd48bb04a | 169 | this->startLfsDutyTimer(); |
| jmarkel44 | 261:4e9a588c938e | 170 | } else { |
| jmarkel44 | 260:fe726583ba1d | 171 | // do nothing |
| jmarkel44 | 252:3c9863f951b7 | 172 | } |
| jmarkel44 | 252:3c9863f951b7 | 173 | break; |
| jmarkel44 | 250:1cd8ec63e9e9 | 174 | default: |
| jmarkel44 | 250:1cd8ec63e9e9 | 175 | break; |
| jmarkel44 | 250:1cd8ec63e9e9 | 176 | } |
| jmarkel44 | 250:1cd8ec63e9e9 | 177 | } |
| jmarkel44 | 250:1cd8ec63e9e9 | 178 | |
| jmarkel44 | 252:3c9863f951b7 | 179 | bool FailsafeControl::belowLowFailsafe(void) |
| jmarkel44 | 252:3c9863f951b7 | 180 | { |
| jmarkel44 | 252:3c9863f951b7 | 181 | // read the modbus input |
| jmarkel44 | 252:3c9863f951b7 | 182 | ModbusValue value; |
| jmarkel44 | 252:3c9863f951b7 | 183 | ModbusMasterReadRegister(input, &value); |
| jmarkel44 | 252:3c9863f951b7 | 184 | #if 0 |
| jmarkel44 | 252:3c9863f951b7 | 185 | if ( value.errflag ) { |
| jmarkel44 | 252:3c9863f951b7 | 186 | logError("%s: error reading %s:", __func__, input); |
| jmarkel44 | 252:3c9863f951b7 | 187 | return false; |
| jmarkel44 | 252:3c9863f951b7 | 188 | } |
| jmarkel44 | 252:3c9863f951b7 | 189 | #endif |
| jmarkel44 | 252:3c9863f951b7 | 190 | return ( value.value <= lfs_data.value ); |
| jmarkel44 | 252:3c9863f951b7 | 191 | } |
| jmarkel44 | 252:3c9863f951b7 | 192 | |
| jmarkel44 | 252:3c9863f951b7 | 193 | bool FailsafeControl::aboveHighFailsafe(void) |
| jmarkel44 | 252:3c9863f951b7 | 194 | { |
| jmarkel44 | 252:3c9863f951b7 | 195 | // read the modbus input |
| jmarkel44 | 252:3c9863f951b7 | 196 | ModbusValue value; |
| jmarkel44 | 252:3c9863f951b7 | 197 | ModbusMasterReadRegister(input, &value); |
| jmarkel44 | 252:3c9863f951b7 | 198 | // TODO: check the error flag |
| jmarkel44 | 252:3c9863f951b7 | 199 | #if 0 |
| jmarkel44 | 252:3c9863f951b7 | 200 | if ( value.errflag ) { |
| jmarkel44 | 252:3c9863f951b7 | 201 | logError("%s: error reading %s:", __func__, input); |
| jmarkel44 | 252:3c9863f951b7 | 202 | return false; |
| jmarkel44 | 252:3c9863f951b7 | 203 | } |
| jmarkel44 | 252:3c9863f951b7 | 204 | #endif |
| jmarkel44 | 252:3c9863f951b7 | 205 | return ( value.value >= hfs_data.value ); |
| jmarkel44 | 252:3c9863f951b7 | 206 | } |
| jmarkel44 | 252:3c9863f951b7 | 207 | |
| jmarkel44 | 261:4e9a588c938e | 208 | // |
| jmarkel44 | 261:4e9a588c938e | 209 | // method: startHfsDutyTimer |
| jmarkel44 | 261:4e9a588c938e | 210 | // description: start the high failsafe duty timer |
| jmarkel44 | 261:4e9a588c938e | 211 | // |
| jmarkel44 | 261:4e9a588c938e | 212 | // @param none |
| jmarkel44 | 261:4e9a588c938e | 213 | // @return none |
| jmarkel44 | 261:4e9a588c938e | 214 | // |
| jmarkel44 | 261:4e9a588c938e | 215 | void FailsafeControl::startHfsDutyTimer(void) |
| jmarkel44 | 252:3c9863f951b7 | 216 | { |
| jmarkel44 | 261:4e9a588c938e | 217 | unsigned long currentTime = time(NULL); |
| jmarkel44 | 261:4e9a588c938e | 218 | unsigned long period = hfs_data.interval * 60; |
| jmarkel44 | 261:4e9a588c938e | 219 | |
| jmarkel44 | 261:4e9a588c938e | 220 | duty_timer.offTime = currentTime + ((double)period * ((double)hfs_data.dutyCycle/100.0)); |
| jmarkel44 | 261:4e9a588c938e | 221 | duty_timer.expirationTime = currentTime + period; |
| jmarkel44 | 262:696cd48bb04a | 222 | |
| jmarkel44 | 261:4e9a588c938e | 223 | printf("\r%s: currentTime = %lu\n", __func__, currentTime); |
| jmarkel44 | 261:4e9a588c938e | 224 | printf("\r%s: off Time = %lu\n", __func__, duty_timer.offTime); |
| jmarkel44 | 261:4e9a588c938e | 225 | printf("\r%s: expiration = %lu\n", __func__, duty_timer.expirationTime); |
| jmarkel44 | 261:4e9a588c938e | 226 | } |
| jmarkel44 | 261:4e9a588c938e | 227 | |
| jmarkel44 | 261:4e9a588c938e | 228 | // |
| jmarkel44 | 261:4e9a588c938e | 229 | // method: stopHfsDutyTimer |
| jmarkel44 | 261:4e9a588c938e | 230 | // description: stop the high failsafe duty timer |
| jmarkel44 | 261:4e9a588c938e | 231 | // |
| jmarkel44 | 261:4e9a588c938e | 232 | // @param none |
| jmarkel44 | 261:4e9a588c938e | 233 | // @return none |
| jmarkel44 | 261:4e9a588c938e | 234 | // |
| jmarkel44 | 261:4e9a588c938e | 235 | void FailsafeControl::stopHfsDutyTimer(void) |
| jmarkel44 | 261:4e9a588c938e | 236 | { |
| jmarkel44 | 261:4e9a588c938e | 237 | printf("\r%s invoked\n", __func__); |
| jmarkel44 | 261:4e9a588c938e | 238 | memset(&duty_timer, 0, sizeof(duty_timer)); |
| jmarkel44 | 252:3c9863f951b7 | 239 | } |
| jmarkel44 | 252:3c9863f951b7 | 240 | |
| jmarkel44 | 261:4e9a588c938e | 241 | // |
| jmarkel44 | 261:4e9a588c938e | 242 | // method: startLfsDutyTimer |
| jmarkel44 | 261:4e9a588c938e | 243 | // descrption: start the low failsafe duty-timer |
| jmarkel44 | 261:4e9a588c938e | 244 | // |
| jmarkel44 | 261:4e9a588c938e | 245 | // @param none |
| jmarkel44 | 261:4e9a588c938e | 246 | // @return none |
| jmarkel44 | 261:4e9a588c938e | 247 | // |
| jmarkel44 | 261:4e9a588c938e | 248 | void FailsafeControl::startLfsDutyTimer(void) |
| jmarkel44 | 261:4e9a588c938e | 249 | { |
| jmarkel44 | 261:4e9a588c938e | 250 | unsigned long currentTime = time(NULL); |
| jmarkel44 | 261:4e9a588c938e | 251 | unsigned long period = lfs_data.interval * 60; |
| jmarkel44 | 261:4e9a588c938e | 252 | |
| jmarkel44 | 261:4e9a588c938e | 253 | duty_timer.offTime = (double)period * ((double)lfs_data.dutyCycle/100.0); |
| jmarkel44 | 261:4e9a588c938e | 254 | duty_timer.expirationTime = currentTime + period; |
| jmarkel44 | 261:4e9a588c938e | 255 | } |
| jmarkel44 | 261:4e9a588c938e | 256 | |
| jmarkel44 | 261:4e9a588c938e | 257 | // |
| jmarkel44 | 261:4e9a588c938e | 258 | // method: stopLfsDutyTimer |
| jmarkel44 | 261:4e9a588c938e | 259 | // description: stop the low failsafe duty-timer |
| jmarkel44 | 261:4e9a588c938e | 260 | // |
| jmarkel44 | 261:4e9a588c938e | 261 | // @param none |
| jmarkel44 | 261:4e9a588c938e | 262 | // @return none |
| jmarkel44 | 261:4e9a588c938e | 263 | // |
| jmarkel44 | 261:4e9a588c938e | 264 | void FailsafeControl::stopLfsDutyTimer(void) |
| jmarkel44 | 252:3c9863f951b7 | 265 | { |
| jmarkel44 | 262:696cd48bb04a | 266 | printf("\r%s invoked\n", __func__); |
| jmarkel44 | 262:696cd48bb04a | 267 | memset(&duty_timer, 0, sizeof(duty_timer)); |
| jmarkel44 | 261:4e9a588c938e | 268 | } |
| jmarkel44 | 261:4e9a588c938e | 269 | |
| jmarkel44 | 261:4e9a588c938e | 270 | // |
| jmarkel44 | 261:4e9a588c938e | 271 | // method: dutyOnExpired |
| jmarkel44 | 261:4e9a588c938e | 272 | // description: returns true if ON cycle has expired; false otherwise |
| jmarkel44 | 261:4e9a588c938e | 273 | // |
| jmarkel44 | 261:4e9a588c938e | 274 | // @param none |
| jmarkel44 | 261:4e9a588c938e | 275 | // @return none |
| jmarkel44 | 261:4e9a588c938e | 276 | // |
| jmarkel44 | 261:4e9a588c938e | 277 | bool FailsafeControl::dutyOnExpired(void) |
| jmarkel44 | 261:4e9a588c938e | 278 | { |
| jmarkel44 | 261:4e9a588c938e | 279 | return (duty_timer.offTime < time(NULL)); |
| jmarkel44 | 261:4e9a588c938e | 280 | } |
| jmarkel44 | 261:4e9a588c938e | 281 | |
| jmarkel44 | 261:4e9a588c938e | 282 | // |
| jmarkel44 | 261:4e9a588c938e | 283 | // method: dutyOffExpired |
| jmarkel44 | 261:4e9a588c938e | 284 | // description: returns true if OFF cycle has expired; false otherwise |
| jmarkel44 | 261:4e9a588c938e | 285 | // |
| jmarkel44 | 261:4e9a588c938e | 286 | // @param none |
| jmarkel44 | 261:4e9a588c938e | 287 | // @return none |
| jmarkel44 | 261:4e9a588c938e | 288 | // |
| jmarkel44 | 261:4e9a588c938e | 289 | bool FailsafeControl::dutyOffExpired(void) |
| jmarkel44 | 261:4e9a588c938e | 290 | { |
| jmarkel44 | 262:696cd48bb04a | 291 | return (duty_timer.expirationTime < time(NULL)); |
| jmarkel44 | 252:3c9863f951b7 | 292 | } |
| jmarkel44 | 252:3c9863f951b7 | 293 | |
| jmarkel44 | 252:3c9863f951b7 | 294 | // |
| jmarkel44 | 260:fe726583ba1d | 295 | // method: sendMailToOutput |
| jmarkel44 | 252:3c9863f951b7 | 296 | // description: send mail to the output task |
| jmarkel44 | 252:3c9863f951b7 | 297 | // |
| jmarkel44 | 252:3c9863f951b7 | 298 | // @param io_tag -> input/output tag |
| jmarkel44 | 252:3c9863f951b7 | 299 | // @param action -> ON, OFF, UNREGISTER |
| jmarkel44 | 252:3c9863f951b7 | 300 | // @return none |
| jmarkel44 | 252:3c9863f951b7 | 301 | // |
| jmarkel44 | 260:fe726583ba1d | 302 | void FailsafeControl::sendMailToOutput(OutputAction action) |
| jmarkel44 | 252:3c9863f951b7 | 303 | { |
| jmarkel44 | 252:3c9863f951b7 | 304 | logInfo("%s: failsafe control attempting to send action %d\n", |
| jmarkel44 | 252:3c9863f951b7 | 305 | __func__, action); |
| jmarkel44 | 252:3c9863f951b7 | 306 | |
| jmarkel44 | 252:3c9863f951b7 | 307 | OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc(); |
| jmarkel44 | 252:3c9863f951b7 | 308 | memset(output_mail, 0, sizeof(OutputControlMsg_t)); |
| jmarkel44 | 252:3c9863f951b7 | 309 | |
| jmarkel44 | 252:3c9863f951b7 | 310 | output_mail->action = action; |
| jmarkel44 | 252:3c9863f951b7 | 311 | output_mail->controlType = CONTROL_FAILSAFE; |
| jmarkel44 | 252:3c9863f951b7 | 312 | output_mail->priority = this->priority; |
| jmarkel44 | 252:3c9863f951b7 | 313 | |
| jmarkel44 | 252:3c9863f951b7 | 314 | strncpy(output_mail->input_tag, this->input.c_str(), sizeof(output_mail->input_tag)-1); |
| jmarkel44 | 252:3c9863f951b7 | 315 | strncpy(output_mail->output_tag, this->output.c_str(), sizeof(output_mail->output_tag)-1); |
| jmarkel44 | 252:3c9863f951b7 | 316 | strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1); |
| jmarkel44 | 252:3c9863f951b7 | 317 | |
| jmarkel44 | 252:3c9863f951b7 | 318 | OutputMasterMailBox.put(output_mail); |
| jmarkel44 | 252:3c9863f951b7 | 319 | } |
| jmarkel44 | 252:3c9863f951b7 | 320 | |
| jmarkel44 | 252:3c9863f951b7 | 321 | |
| jmarkel44 | 250:1cd8ec63e9e9 | 322 | // |
| jmarkel44 | 260:fe726583ba1d | 323 | // method: unregisterControl |
| jmarkel44 | 260:fe726583ba1d | 324 | // description: unregister this control with the output task |
| jmarkel44 | 260:fe726583ba1d | 325 | // |
| jmarkel44 | 260:fe726583ba1d | 326 | // @param none |
| jmarkel44 | 260:fe726583ba1d | 327 | // @return none |
| jmarkel44 | 260:fe726583ba1d | 328 | // |
| jmarkel44 | 260:fe726583ba1d | 329 | void FailsafeControl::unregisterControl(void) |
| jmarkel44 | 260:fe726583ba1d | 330 | { |
| jmarkel44 | 260:fe726583ba1d | 331 | logInfo("%s: %s attempting to unregister %s\n", |
| jmarkel44 | 260:fe726583ba1d | 332 | __func__, controlFile.c_str()); |
| jmarkel44 | 260:fe726583ba1d | 333 | |
| jmarkel44 | 260:fe726583ba1d | 334 | OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc(); |
| jmarkel44 | 260:fe726583ba1d | 335 | memset(output_mail, 0, sizeof(OutputControlMsg_t)); |
| jmarkel44 | 260:fe726583ba1d | 336 | |
| jmarkel44 | 260:fe726583ba1d | 337 | output_mail->action = ACTION_CONTROL_UNREGISTER; |
| jmarkel44 | 260:fe726583ba1d | 338 | output_mail->controlType = CONTROL_FAILSAFE; |
| jmarkel44 | 260:fe726583ba1d | 339 | output_mail->priority = this->priority; |
| jmarkel44 | 260:fe726583ba1d | 340 | strncpy(output_mail->output_tag, this->output.c_str(), sizeof(output_mail->output_tag)-1); |
| jmarkel44 | 260:fe726583ba1d | 341 | strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1); |
| jmarkel44 | 260:fe726583ba1d | 342 | |
| jmarkel44 | 260:fe726583ba1d | 343 | OutputMasterMailBox.put(output_mail); |
| jmarkel44 | 260:fe726583ba1d | 344 | } |
| jmarkel44 | 260:fe726583ba1d | 345 | |
| jmarkel44 | 260:fe726583ba1d | 346 | // |
| jmarkel44 | 250:1cd8ec63e9e9 | 347 | // method: display |
| jmarkel44 | 250:1cd8ec63e9e9 | 348 | // description: display the pertinents |
| jmarkel44 | 250:1cd8ec63e9e9 | 349 | // |
| jmarkel44 | 250:1cd8ec63e9e9 | 350 | // @param none |
| jmarkel44 | 250:1cd8ec63e9e9 | 351 | // @return none |
| jmarkel44 | 250:1cd8ec63e9e9 | 352 | // |
| jmarkel44 | 250:1cd8ec63e9e9 | 353 | void FailsafeControl::display(void) |
| jmarkel44 | 250:1cd8ec63e9e9 | 354 | { |
| jmarkel44 | 250:1cd8ec63e9e9 | 355 | const char *mapper[] = { "INIT", |
| jmarkel44 | 250:1cd8ec63e9e9 | 356 | "START", |
| jmarkel44 | 250:1cd8ec63e9e9 | 357 | "CONTROL_OFF", |
| jmarkel44 | 250:1cd8ec63e9e9 | 358 | "LFS_ON", |
| jmarkel44 | 250:1cd8ec63e9e9 | 359 | "LFS_OFF", |
| jmarkel44 | 250:1cd8ec63e9e9 | 360 | "HFS_ON", |
| jmarkel44 | 250:1cd8ec63e9e9 | 361 | "HFS_OFF", |
| jmarkel44 | 250:1cd8ec63e9e9 | 362 | "invalid" |
| jmarkel44 | 250:1cd8ec63e9e9 | 363 | }; |
| jmarkel44 | 250:1cd8ec63e9e9 | 364 | |
| jmarkel44 | 250:1cd8ec63e9e9 | 365 | printf("\r\n"); |
| jmarkel44 | 250:1cd8ec63e9e9 | 366 | std::cout << left << setw(10) << setfill(' ') << "failsafe: "; |
| jmarkel44 | 253:ae850c19cf81 | 367 | std::cout << left << setw(40) << setfill(' ') << controlFile; |
| jmarkel44 | 250:1cd8ec63e9e9 | 368 | std::cout << left << setw(20) << setfill(' ') << id; |
| jmarkel44 | 250:1cd8ec63e9e9 | 369 | std::cout << left << setw(6) << setfill(' ') << priority; |
| jmarkel44 | 250:1cd8ec63e9e9 | 370 | std::cout << left << setw(20) << setfill(' ') << input; |
| jmarkel44 | 250:1cd8ec63e9e9 | 371 | std::cout << left << setw(20) << setfill(' ') << output; |
| jmarkel44 | 250:1cd8ec63e9e9 | 372 | std::cout << left << setw(16) << setfill(' ') << mapper[currentState]; |
| jmarkel44 | 252:3c9863f951b7 | 373 | std::cout << left << setw(12) << setfill(' ') << "lfs-> " |
| jmarkel44 | 252:3c9863f951b7 | 374 | << lfs_data.value << ":" << lfs_data.dutyCycle << ":" << lfs_data.interval; |
| jmarkel44 | 252:3c9863f951b7 | 375 | std::cout << right << setw(12) << setfill(' ') << "hfs-> " |
| jmarkel44 | 252:3c9863f951b7 | 376 | << hfs_data.value << ":" << hfs_data.dutyCycle << ":" << hfs_data.interval; |
| jmarkel44 | 250:1cd8ec63e9e9 | 377 | |
| jmarkel44 | 250:1cd8ec63e9e9 | 378 | std::cout.flush(); |
| jmarkel44 | 252:3c9863f951b7 | 379 | } |
