EZR

Dependencies:   CRC16 FreescaleIAP FreescaleWatchdog GGSProtocol LM75B PIMA Parameters PersistentCircularQueue SerialNumberV2COM mbed-dev-watchdog_2016_03_04

Fork of smartRamalKW by Equipe Firmware V2COM

Revision:
22:b66e36356dd4
Parent:
16:cae76dbd681f
Child:
23:85202a52482d
--- a/sensor.cpp	Tue Oct 13 21:22:27 2015 +0000
+++ b/sensor.cpp	Mon Oct 19 20:34:15 2015 +0000
@@ -1,6 +1,7 @@
 #include "sensor.h"
 
 extern AnalogIn voltage[];
+extern DigitalOut ledLoad;
 extern ParametersBlock APP_PARAMETERS;
 
 Ticker tickerSamples;
@@ -11,7 +12,7 @@
 bool finished;
 bool timeout;
 
-float getTensaoInstantanea(unsigned char channel){
+float getInstVoltage(unsigned char channel){
     currentChannel = channel;
     currentSample = 0;
     finished = false;
@@ -23,7 +24,7 @@
         wait(0.1f);
     }
     
-    float resp = calculateRMS();
+    float resp = calculateRMS(APP_PARAMETERS.SAMPLES_VOLTAGE_ANG_COEF.floatValue, APP_PARAMETERS.SAMPLES_VOLTAGE_LIN_COEF.floatValue);
     float limit = 0;
     if(channel == 0){
         limit = APP_PARAMETERS.LIMITE_TENSAO_ZERO_CH0_V;
@@ -37,10 +38,37 @@
     }
 }
 
-bool getEstadoSensor(unsigned char channel){
-    if(getTensaoInstantanea(channel) > APP_PARAMETERS.LIMITE_TENSAO_SENSOR_V){
+float getInstCurrent(){
+    currentChannel = 2;
+    currentSample = 0;
+    finished = false;
+    timeout = false;
+    tickerSamples.attach_us(&readSample, APP_PARAMETERS.SAMPLES_DELAY_US);
+    samplesTimeout.attach_us(&timeoutReadingSamples, (DEFAULT_SAMPLES + 2) * APP_PARAMETERS.SAMPLES_DELAY_US);
+    
+    while(!finished && !timeout){
+        wait(0.1f);
+    }
+    
+    float resp = calculateRMS(APP_PARAMETERS.SAMPLES_CURRENT_ANG_COEF.floatValue, APP_PARAMETERS.SAMPLES_CURRENT_LIN_COEF.floatValue);
+    float limit = APP_PARAMETERS.LIMITE_CORRENTE_ZERO_A;
+    if(resp < limit){
+        return 0;
+    } else{
+        return resp;
+    }
+}
+
+bool getSensorState(unsigned char channel){
+    if(getInstVoltage(channel) > APP_PARAMETERS.LIMITE_TENSAO_SENSOR_V){
+        if(channel == APP_PARAMETERS.LOAD_CHANNEL){
+            ledLoad = LED_ON;
+        }
         return SENSOR_COM_FORNECIMENTO;
     } else{
+        if(channel == APP_PARAMETERS.LOAD_CHANNEL){
+            ledLoad = LED_OFF;
+        }
         return SENSOR_SEM_FORNECIMENTO;
     }
 }
@@ -59,10 +87,10 @@
     timeout = true;
 }
 
-float calculateRMS(){
+float calculateRMS(float angCoef, float linCoef){
     float rms = 0;
     for(int i=0; i < DEFAULT_SAMPLES; i++){
-        sample[i] = abs(sample[i]*APP_PARAMETERS.SAMPLES_ANG_COEF.floatValue + APP_PARAMETERS.SAMPLES_LIN_COEF.floatValue);
+        sample[i] = abs(sample[i]*angCoef + linCoef);
         rms += sample[i]*sample[i];
     }
     rms = rms / DEFAULT_SAMPLES;