Erick / Mbed 2 deprecated ICE_BLE_TEST

Dependencies:   NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed

Fork of ICE by Erick

Revision:
269:97243a7f56ba
Parent:
262:696cd48bb04a
Child:
271:19948511cd3f
--- a/src/ConfigurationHandler/Controls/FailsafeControl.cpp	Tue Oct 25 13:56:14 2016 +0000
+++ b/src/ConfigurationHandler/Controls/FailsafeControl.cpp	Wed Oct 26 18:30:08 2016 +0000
@@ -92,14 +92,17 @@
             break;
         case STATE_START:
             // control has been started, do some checking
-            if ( this->aboveHighFailsafe() ) {
+            if ( this->sensorError() ) {
+                this->currentState = STATE_CONTROL_SENSOR_ERROR;
+                sendMailToOutput(ACTION_CONTROL_OFF);
+            } else if ( this->aboveHighFailsafe() ) {
                 // start high failsafe duty cycle
-                this->currentState = STATE_CONTROL_HFS_ON;
+                this->currentState = STATE_CONTROL_ON_HFS;
                 sendMailToOutput(ACTION_CONTROL_ON);
                 this->startHfsDutyTimer();
             } else if ( this->belowLowFailsafe() ) {
                 // start low failsafe duty cycle
-                this->currentState = STATE_CONTROL_LFS_ON;
+                this->currentState = STATE_CONTROL_ON_LFS;
                 sendMailToOutput(ACTION_CONTROL_ON);
                 this->startLfsDutyTimer();
             } else {
@@ -108,69 +111,91 @@
             break;
         case STATE_CONTROL_OFF:
             // control is acting normal, within bounds
-            if ( this->aboveHighFailsafe() ) {
+            if ( this->sensorError() ) {
+                this->currentState = STATE_CONTROL_SENSOR_ERROR;
+                sendMailToOutput(ACTION_CONTROL_OFF);
+            } else if ( this->aboveHighFailsafe() ) {
                 // restart the high failsafe duty cycle
-                this->currentState = STATE_CONTROL_HFS_ON;
+                this->currentState = STATE_CONTROL_ON_HFS;
                 sendMailToOutput(ACTION_CONTROL_ON);
                 this->startHfsDutyTimer();
             } else if ( this->belowLowFailsafe() ) {
                 // restart the low failsafe duty cycle
-                this->currentState = STATE_CONTROL_LFS_ON;
+                this->currentState = STATE_CONTROL_ON_LFS;
                 sendMailToOutput(ACTION_CONTROL_ON);
                 this->startLfsDutyTimer();
             } else {
                 // do nothing
             }
             break;
-        case STATE_CONTROL_LFS_ON:
+        case STATE_CONTROL_ON_LFS:
             // control is in low-failsafe with duty cycle ON
-            if ( !this->belowLowFailsafe() ) {
+            if ( this->sensorError() ) {
+                this->currentState = STATE_CONTROL_SENSOR_ERROR;
+                sendMailToOutput(ACTION_CONTROL_OFF);
+            } else if ( !this->belowLowFailsafe() ) {
                 this->currentState = STATE_CONTROL_OFF;
                 this->unregisterControl();
             } else if ( this->dutyOnExpired() ) {
-                this->currentState = STATE_CONTROL_LFS_OFF;
+                this->currentState = STATE_CONTROL_OFF_LFS;
                 sendMailToOutput(ACTION_CONTROL_OFF);
             } else {
                 // do nothing
             }
             break;
-        case STATE_CONTROL_LFS_OFF:
+        case STATE_CONTROL_OFF_LFS:
             // control is in low-failsafe with duty cycle OFF
-            if ( !this->belowLowFailsafe() ) {
+            if ( this->sensorError() ) {
+                this->currentState = STATE_CONTROL_SENSOR_ERROR;
+                sendMailToOutput(ACTION_CONTROL_OFF);
+            } else if ( !this->belowLowFailsafe() ) {
                 this->currentState = STATE_CONTROL_OFF;
                 this->unregisterControl();
             } else if ( this->dutyOffExpired() ) {
-                this->currentState = STATE_CONTROL_LFS_ON;
+                this->currentState = STATE_CONTROL_ON_LFS;
                 this->startLfsDutyTimer();
             } else {
                 // do nothing
             }
             break;
-        case STATE_CONTROL_HFS_ON:
+        case STATE_CONTROL_ON_HFS:
             // control is in high-failsafe with duty cycle ON
-            if ( !this->aboveHighFailsafe() ) {
+            if ( this->sensorError() ) {
+                this->currentState = STATE_CONTROL_SENSOR_ERROR;
+                sendMailToOutput(ACTION_CONTROL_OFF);
+            } else if ( !this->aboveHighFailsafe() ) {
                 this->currentState = STATE_CONTROL_OFF;
                 this->unregisterControl();
             } else if ( this->dutyOnExpired() ) {
-                this->currentState = STATE_CONTROL_HFS_OFF;
+                this->currentState = STATE_CONTROL_OFF_HFS;
                 sendMailToOutput(ACTION_CONTROL_OFF);
             } else {
                 // do nothing
             }
             break;
-        case STATE_CONTROL_HFS_OFF:
+        case STATE_CONTROL_OFF_HFS:
             // control is in high-failsafe with cuty cycle OFF
-            if ( !this->aboveHighFailsafe() ) {
+            if ( this->sensorError() ) {
+                this->currentState = STATE_CONTROL_SENSOR_ERROR;
+                sendMailToOutput(ACTION_CONTROL_OFF);
+            } else if ( !this->aboveHighFailsafe() ) {
                 this->currentState = STATE_CONTROL_OFF;
                 this->unregisterControl();
             } else if ( this->dutyOffExpired() ) {
-                this->currentState = STATE_CONTROL_LFS_ON;
+                this->currentState = STATE_CONTROL_ON_LFS;
                 sendMailToOutput(ACTION_CONTROL_ON);
                 this->startLfsDutyTimer();
             } else {
                 // do nothing
             }
             break;
+        case STATE_CONTROL_SENSOR_ERROR:
+            if ( !this->sensorError() ) {
+                this->currentState = STATE_CONTROL_OFF;
+            } else {
+                // do nothing
+            }
+            break;
         default:
             break;
     }
@@ -318,6 +343,25 @@
     OutputMasterMailBox.put(output_mail);
 }
 
+//
+// method:          sensorError
+// description:     return true if a sensor's error flag it set
+//
+// @param[in]       none
+// @param[out]      none
+// @return          true if sensor error
+//
+bool FailsafeControl::sensorError(void)
+{
+    ModbusValue value;
+    ModbusMasterReadRegister(input, &value);
+    if ( value.errflag ) {
+        logInfo("%s: %s is in error\n", __func__, input.c_str());
+        return true;
+    }
+    return false;
+}
+
 
 //
 // method:          unregisterControl
@@ -354,11 +398,12 @@
 {
     const char *mapper[] = { "INIT",
                              "START",
-                             "CONTROL_OFF",
-                             "LFS_ON",
-                             "LFS_OFF",
-                             "HFS_ON",
-                             "HFS_OFF",
+                             "OFF",
+                             "ON_LFS",
+                             "OFF_LFS",
+                             "ON_HFS",
+                             "OFF_HFS",
+                             "SENSOR_ERR",
                              "invalid"
                            };