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: CRC16 FreescaleIAP FreescaleWatchdog GGSProtocol LM75B PIMA Parameters PersistentCircularQueue SerialNumberV2COM mbed-dev-watchdog_2016_03_04
Fork of smartRamalKW by
sensor.cpp
00001 #include "sensor.h" 00002 00003 extern AnalogIn voltage[]; 00004 extern DigitalOut ledLoad; 00005 extern ParametersBlock APP_PARAMETERS; 00006 00007 Ticker tickerSamples; 00008 Timeout samplesTimeout; 00009 unsigned char currentChannel; 00010 float sample[DEFAULT_SAMPLES]; 00011 int currentSample; 00012 bool finished; 00013 bool timeout; 00014 00015 float getInstVoltage(unsigned char channel){ 00016 currentChannel = channel; 00017 currentSample = 0; 00018 finished = false; 00019 timeout = false; 00020 tickerSamples.attach_us(&readSample, APP_PARAMETERS.SAMPLES_DELAY_US); 00021 samplesTimeout.attach_us(&timeoutReadingSamples, (DEFAULT_SAMPLES + 2) * APP_PARAMETERS.SAMPLES_DELAY_US); 00022 00023 while(!finished && !timeout){ 00024 wait(0.1f); 00025 } 00026 00027 float resp = calculateRMS(APP_PARAMETERS.SAMPLES_VOLTAGE_ANG_COEF.floatValue, APP_PARAMETERS.SAMPLES_VOLTAGE_LIN_COEF.floatValue); 00028 float limit = 0; 00029 if(channel == 0){ 00030 limit = APP_PARAMETERS.LIMITE_TENSAO_ZERO_CH0_V; 00031 } else{ 00032 limit = APP_PARAMETERS.LIMITE_TENSAO_ZERO_CH1_V; 00033 } 00034 if(resp < limit){ 00035 return 0; 00036 } else{ 00037 return resp; 00038 } 00039 } 00040 00041 float getInstCurrent(){ 00042 currentChannel = 2; 00043 currentSample = 0; 00044 finished = false; 00045 timeout = false; 00046 tickerSamples.attach_us(&readSample, APP_PARAMETERS.SAMPLES_DELAY_US); 00047 samplesTimeout.attach_us(&timeoutReadingSamples, (DEFAULT_SAMPLES + 2) * APP_PARAMETERS.SAMPLES_DELAY_US); 00048 00049 while(!finished && !timeout){ 00050 wait(0.1f); 00051 } 00052 00053 float resp = calculateRMS(APP_PARAMETERS.SAMPLES_CURRENT_ANG_COEF.floatValue, APP_PARAMETERS.SAMPLES_CURRENT_LIN_COEF.floatValue); 00054 float limit = APP_PARAMETERS.LIMITE_CORRENTE_ZERO_A.floatValue; 00055 if(resp < limit){ 00056 return 0; 00057 } else{ 00058 return resp; 00059 } 00060 } 00061 00062 bool getSensorState(unsigned char channel){ 00063 unsigned char lineChannel = channel == 1 ? 0 : 1; 00064 float line = getInstVoltage(lineChannel); 00065 float load = getInstVoltage(channel); 00066 00067 if((line - load) > (line*APP_PARAMETERS.LIMITE_TENSAO_SENSOR_V)/100.0){ 00068 ledLoad = LED_ON; 00069 return SENSOR_COM_FORNECIMENTO; 00070 } else if((line - load) < -(line*APP_PARAMETERS.LIMITE_TENSAO_SENSOR_V)/100.0){ 00071 ledLoad = LED_ON; 00072 return SENSOR_SEM_FORNECIMENTO; 00073 } else{ 00074 ledLoad = LED_OFF; 00075 return SENSOR_SEM_FORNECIMENTO; 00076 } 00077 } 00078 00079 bool checkVoltageReturn(unsigned char channel){ 00080 unsigned char lineChannel = channel == 1 ? 0 : 1; 00081 float line = getInstVoltage(lineChannel); 00082 float load = getInstVoltage(channel); 00083 00084 if(load > (line + (line*APP_PARAMETERS.LIMITE_TENSAO_SENSOR_V)/100.0)){ 00085 return SENSOR_COM_FORNECIMENTO; 00086 } else{ 00087 return SENSOR_SEM_FORNECIMENTO; 00088 } 00089 } 00090 00091 void readSample(){ 00092 sample[currentSample++] = voltage[currentChannel]; 00093 if(currentSample == DEFAULT_SAMPLES){ 00094 finished = true; 00095 samplesTimeout.detach(); 00096 tickerSamples.detach(); 00097 } 00098 } 00099 00100 void timeoutReadingSamples(){ 00101 tickerSamples.detach(); 00102 timeout = true; 00103 } 00104 00105 float calculateRMS(float angCoef, float linCoef){ 00106 float rms = 0; 00107 for(int i=0; i < DEFAULT_SAMPLES; i++){ 00108 sample[i] = abs(sample[i]*angCoef + linCoef); 00109 rms += sample[i]*sample[i]; 00110 } 00111 rms = rms / DEFAULT_SAMPLES; 00112 return sqrt(rms); 00113 }
Generated on Thu Jul 14 2022 00:24:57 by
1.7.2
