Versão estável sem DMA e FFT. 128 amostras.

Dependencies:   EthernetInterface NTPClient mbed-rtos mbed

Committer:
rebonatto
Date:
Tue Jan 05 11:47:35 2016 +0000
Revision:
0:fac116e94d44
Vers?o est?vel sem DMA e FFT. 128 amostras.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rebonatto 0:fac116e94d44 1 /*
rebonatto 0:fac116e94d44 2 * EventDetector.cpp
rebonatto 0:fac116e94d44 3 *
rebonatto 0:fac116e94d44 4 * Created on:
rebonatto 0:fac116e94d44 5 * Author:
rebonatto 0:fac116e94d44 6 */
rebonatto 0:fac116e94d44 7
rebonatto 0:fac116e94d44 8 #include "EthernetInterface.h"
rebonatto 0:fac116e94d44 9 #include "Settings.h"
rebonatto 0:fac116e94d44 10 #include "Http_post.h"
rebonatto 0:fac116e94d44 11
rebonatto 0:fac116e94d44 12 #include "EventDetector.h"
rebonatto 0:fac116e94d44 13
rebonatto 0:fac116e94d44 14
rebonatto 0:fac116e94d44 15 #define MARCA 10
rebonatto 0:fac116e94d44 16 #define GERAFUGA 0
rebonatto 0:fac116e94d44 17 #define UMCICLO 16.666666667
rebonatto 0:fac116e94d44 18
rebonatto 0:fac116e94d44 19 CaptureMailbox EventDetector::m_EventMailbox;
rebonatto 0:fac116e94d44 20 EventDetector EventDetector::m_Detector[NUMBER_OF_CHANNELS] = {0,1,2,3,4,5};
rebonatto 0:fac116e94d44 21
rebonatto 0:fac116e94d44 22 CaptureEvent::CaptureEvent()
rebonatto 0:fac116e94d44 23 {
rebonatto 0:fac116e94d44 24 }
rebonatto 0:fac116e94d44 25
rebonatto 0:fac116e94d44 26 //void CaptureEvent::Setup(char* rfid,int type,int outlet,float mv,float rms,float gain, int offset,float* sin, float* cos)
rebonatto 0:fac116e94d44 27 void CaptureEvent::Setup(char* rfid, int type, int outlet, float mv, float mv2, float rms, int under, int over, float gain, int offset, int duration, short int *samples)
rebonatto 0:fac116e94d44 28 {
rebonatto 0:fac116e94d44 29 memcpy(m_RFID,rfid,9);
rebonatto 0:fac116e94d44 30 m_Type = type;
rebonatto 0:fac116e94d44 31 m_OutletNumber = outlet;
rebonatto 0:fac116e94d44 32 m_MeanValue = mv;
rebonatto 0:fac116e94d44 33 m_MV2 = mv2;
rebonatto 0:fac116e94d44 34 m_RMSValue = rms;
rebonatto 0:fac116e94d44 35 m_Under = under;
rebonatto 0:fac116e94d44 36 m_Over = over;
rebonatto 0:fac116e94d44 37 m_Gain = gain;
rebonatto 0:fac116e94d44 38 m_Offset = offset;
rebonatto 0:fac116e94d44 39 m_Duration = duration;
rebonatto 0:fac116e94d44 40 memcpy(m_Samples, samples, (sizeof(short int) * NUMBER_OF_SAMPLES) );
rebonatto 0:fac116e94d44 41 }
rebonatto 0:fac116e94d44 42
rebonatto 0:fac116e94d44 43 EventDetector::EventDetector(int ch)
rebonatto 0:fac116e94d44 44 {
rebonatto 0:fac116e94d44 45 m_Channel = ch;
rebonatto 0:fac116e94d44 46 m_OutletTriggered = false;
rebonatto 0:fac116e94d44 47 m_EventCounter = 0;
rebonatto 0:fac116e94d44 48 }
rebonatto 0:fac116e94d44 49
rebonatto 0:fac116e94d44 50 //void EventDetector::ProcessEvent(float rmsvalue, int t)
rebonatto 0:fac116e94d44 51 void EventDetector::ProcessEvent(float rmsvalue, float mv2, int under, int over)
rebonatto 0:fac116e94d44 52 {
rebonatto 0:fac116e94d44 53 int i, tempofuga = 0;
rebonatto 0:fac116e94d44 54 short int aux;
rebonatto 0:fac116e94d44 55 //short int buf[NUMBER_OF_SAMPLES];
rebonatto 0:fac116e94d44 56 float newvm;
rebonatto 0:fac116e94d44 57
rebonatto 0:fac116e94d44 58 if(rmsvalue > Settings::get_Limit(m_Channel))
rebonatto 0:fac116e94d44 59 {
rebonatto 0:fac116e94d44 60 /* Retira o VM das amostras */
rebonatto 0:fac116e94d44 61 //pega os dados da captura atual
rebonatto 0:fac116e94d44 62 newvm = 0;
rebonatto 0:fac116e94d44 63 //Capture::CopyBuffer(m_Channel,buf);
rebonatto 0:fac116e94d44 64
rebonatto 0:fac116e94d44 65 /* Retira o valorMedio de todas as amostras */
rebonatto 0:fac116e94d44 66 for(i=0; i < NUMBER_OF_SAMPLES; i++){
rebonatto 0:fac116e94d44 67 aux = Capture::GetValue(i, m_Channel) - mv2;
rebonatto 0:fac116e94d44 68 Capture::PutValue(i, m_Channel, aux) ;
rebonatto 0:fac116e94d44 69 newvm += aux;
rebonatto 0:fac116e94d44 70 }
rebonatto 0:fac116e94d44 71
rebonatto 0:fac116e94d44 72 if ( m_EventCounter == Settings::get_EventLimit() ){
rebonatto 0:fac116e94d44 73 mv2 = newvm / NUMBER_OF_SAMPLES;
rebonatto 0:fac116e94d44 74 //printf("Novo valor medio %f RMS original %f\n", mv2, rmsvalue);
rebonatto 0:fac116e94d44 75 }
rebonatto 0:fac116e94d44 76
rebonatto 0:fac116e94d44 77 rmsvalue = SignalProcessor::CalculateRMS( m_Channel );
rebonatto 0:fac116e94d44 78
rebonatto 0:fac116e94d44 79 if(!m_OutletTriggered)
rebonatto 0:fac116e94d44 80 {
rebonatto 0:fac116e94d44 81 if (rmsvalue > Settings::get_Limit(m_Channel)) {
rebonatto 0:fac116e94d44 82 if(m_EventCounter < Settings::get_EventLimit() )
rebonatto 0:fac116e94d44 83 {
rebonatto 0:fac116e94d44 84 m_EventCounter++;
rebonatto 0:fac116e94d44 85 }
rebonatto 0:fac116e94d44 86 else
rebonatto 0:fac116e94d44 87 {
rebonatto 0:fac116e94d44 88 //printf("Deu evento de liga ou Fuga\n");
rebonatto 0:fac116e94d44 89 if (m_Channel % 2 == 1) // Canais impares sao de diferencial
rebonatto 0:fac116e94d44 90 m_tempo.start();
rebonatto 0:fac116e94d44 91 m_OutletTriggered = true;
rebonatto 0:fac116e94d44 92
rebonatto 0:fac116e94d44 93 SendMessage(0, rmsvalue, mv2, under, over, 0);
rebonatto 0:fac116e94d44 94
rebonatto 0:fac116e94d44 95 //OnTrigger(buf, rmsvalue, mv2, under, over, 0); //TODO: must change the parameter of this function call
rebonatto 0:fac116e94d44 96
rebonatto 0:fac116e94d44 97 m_EventCounter = 0;
rebonatto 0:fac116e94d44 98 }
rebonatto 0:fac116e94d44 99 }
rebonatto 0:fac116e94d44 100 }
rebonatto 0:fac116e94d44 101 else
rebonatto 0:fac116e94d44 102 m_EventCounter = 0;
rebonatto 0:fac116e94d44 103 }
rebonatto 0:fac116e94d44 104 else
rebonatto 0:fac116e94d44 105 {
rebonatto 0:fac116e94d44 106 if(m_OutletTriggered)
rebonatto 0:fac116e94d44 107 {
rebonatto 0:fac116e94d44 108 if(m_EventCounter < Settings::get_EventLimit())
rebonatto 0:fac116e94d44 109 {
rebonatto 0:fac116e94d44 110 m_EventCounter++;
rebonatto 0:fac116e94d44 111 }
rebonatto 0:fac116e94d44 112 else
rebonatto 0:fac116e94d44 113 {
rebonatto 0:fac116e94d44 114 //printf("Terminou evento de liga ou Fuga\n");
rebonatto 0:fac116e94d44 115 if (m_Channel % 2 == 1){ // Canais impares sao de diferencial
rebonatto 0:fac116e94d44 116 //m_tempo.stop();
rebonatto 0:fac116e94d44 117 tempofuga = (int) (Settings::get_EventLimit() * UMCICLO) + m_tempo.read_ms();
rebonatto 0:fac116e94d44 118 //printf("Limite %d\n", (int) (Settings::get_EventLimit() * UMCICLO));
rebonatto 0:fac116e94d44 119 m_tempo.reset();
rebonatto 0:fac116e94d44 120 }
rebonatto 0:fac116e94d44 121 else
rebonatto 0:fac116e94d44 122 tempofuga = 0;
rebonatto 0:fac116e94d44 123 m_OutletTriggered = false;
rebonatto 0:fac116e94d44 124
rebonatto 0:fac116e94d44 125 SendMessage(0, rmsvalue, mv2, under, over, tempofuga);
rebonatto 0:fac116e94d44 126 //OnTrigger(buf, rmsvalue, mv2, under, over, tempofuga);//TODO: must change the parameter of this function call
rebonatto 0:fac116e94d44 127 //printf("===> contliga %d contdesliga %d\n", contliga, contdesl);
rebonatto 0:fac116e94d44 128
rebonatto 0:fac116e94d44 129 m_EventCounter = 0;
rebonatto 0:fac116e94d44 130 }
rebonatto 0:fac116e94d44 131 }
rebonatto 0:fac116e94d44 132 else
rebonatto 0:fac116e94d44 133 m_EventCounter = 0;
rebonatto 0:fac116e94d44 134 }
rebonatto 0:fac116e94d44 135 }
rebonatto 0:fac116e94d44 136
rebonatto 0:fac116e94d44 137 void EventDetector::ShowValues(CaptureEvent* e)
rebonatto 0:fac116e94d44 138 {
rebonatto 0:fac116e94d44 139 int i;
rebonatto 0:fac116e94d44 140
rebonatto 0:fac116e94d44 141 printf("RFID: %s\n", e->get_RFID());
rebonatto 0:fac116e94d44 142 printf("type: %d\n", e->get_Type());
rebonatto 0:fac116e94d44 143 printf("OutletNr: %d\n", e->get_OutletNumber());
rebonatto 0:fac116e94d44 144 printf("MeanValue: %f\n", e->get_MeanValue());
rebonatto 0:fac116e94d44 145 printf("MV2: %f\n", e->get_MV2());
rebonatto 0:fac116e94d44 146 printf("RMSValue: %f\n", e->get_RMSValue());
rebonatto 0:fac116e94d44 147 printf("Underflow: %d\n", e->get_Under());
rebonatto 0:fac116e94d44 148 printf("Overflow: %d\n", e->get_Over());
rebonatto 0:fac116e94d44 149 printf("Gain: %f\n", e->get_Gain());
rebonatto 0:fac116e94d44 150 printf("Offset: %d\n", e->get_Offset());
rebonatto 0:fac116e94d44 151 printf("Duration: %d\n", e->get_Duration());
rebonatto 0:fac116e94d44 152
rebonatto 0:fac116e94d44 153
rebonatto 0:fac116e94d44 154 for(i=0;i<NUMBER_OF_SAMPLES;i++)
rebonatto 0:fac116e94d44 155 {
rebonatto 0:fac116e94d44 156 printf("%d,",e->get_SampleValue(i));
rebonatto 0:fac116e94d44 157 }
rebonatto 0:fac116e94d44 158
rebonatto 0:fac116e94d44 159 }
rebonatto 0:fac116e94d44 160
rebonatto 0:fac116e94d44 161 void EventDetector::ExternalTrigger()
rebonatto 0:fac116e94d44 162 {
rebonatto 0:fac116e94d44 163 // Ajustar valores de mv2, under, over e duration na chamada!!!
rebonatto 0:fac116e94d44 164 SendMessage(1, 0, 0, 0, 0, 0);
rebonatto 0:fac116e94d44 165 }
rebonatto 0:fac116e94d44 166
rebonatto 0:fac116e94d44 167 //void EventDetector::SendMessage(int ext,float rmsvalue)
rebonatto 0:fac116e94d44 168 void EventDetector::SendMessage(int ext, float rmsvalue, float mv2, int under, int over, int duration)
rebonatto 0:fac116e94d44 169 {
rebonatto 0:fac116e94d44 170 int flagrfid = 0;
rebonatto 0:fac116e94d44 171 short int buf[NUMBER_OF_SAMPLES];
rebonatto 0:fac116e94d44 172
rebonatto 0:fac116e94d44 173 //printf("Chegou SendMessage\n");
rebonatto 0:fac116e94d44 174 //Here we must alloc a CaptureEvent object from mailbox pool,
rebonatto 0:fac116e94d44 175
rebonatto 0:fac116e94d44 176 //CaptureEvent* event;
rebonatto 0:fac116e94d44 177 CaptureEvent* event = GetMailbox().alloc();
rebonatto 0:fac116e94d44 178
rebonatto 0:fac116e94d44 179
rebonatto 0:fac116e94d44 180 //printf("Capturou evento\n");
rebonatto 0:fac116e94d44 181 //then initialize the object properly
rebonatto 0:fac116e94d44 182
rebonatto 0:fac116e94d44 183 /* O buffer foi coletado no ProcessEvent para retirada do VM
rebonatto 0:fac116e94d44 184 unsigned short int buf[NUMBER_OF_SAMPLES];
rebonatto 0:fac116e94d44 185 Capture::CopyBuffer(m_Channel,buf);
rebonatto 0:fac116e94d44 186 */
rebonatto 0:fac116e94d44 187 if(ext)
rebonatto 0:fac116e94d44 188 {
rebonatto 0:fac116e94d44 189 rmsvalue = 0;//SignalProcessor::CalculateRMS(buf,m_Channel);
rebonatto 0:fac116e94d44 190 }
rebonatto 0:fac116e94d44 191
rebonatto 0:fac116e94d44 192 float mv;
rebonatto 0:fac116e94d44 193
rebonatto 0:fac116e94d44 194 int type=0,outlet_number=0, aux=0;
rebonatto 0:fac116e94d44 195 aux = Settings::get_OutletNumber(m_Channel);
rebonatto 0:fac116e94d44 196 outlet_number = Settings::get_Outlet(aux);
rebonatto 0:fac116e94d44 197
rebonatto 0:fac116e94d44 198 //Temporario - Sem leitor de RFID
rebonatto 0:fac116e94d44 199 //char rfid[9] = "1234560";
rebonatto 0:fac116e94d44 200 //rfid[7] = (char)outlet_number + '0';
rebonatto 0:fac116e94d44 201
rebonatto 0:fac116e94d44 202 // Com leitor de RFID
rebonatto 0:fac116e94d44 203 char rfid[9], aux2[5];
rebonatto 0:fac116e94d44 204 //rfid[0] = '\0';
rebonatto 0:fac116e94d44 205 //send hitag request
rebonatto 0:fac116e94d44 206 //capture hitag response
rebonatto 0:fac116e94d44 207 //printf("OUTLET=%d\n",outlet_index+1);
rebonatto 0:fac116e94d44 208
rebonatto 0:fac116e94d44 209
rebonatto 0:fac116e94d44 210 if (Settings::get_ReadRfid()){
rebonatto 0:fac116e94d44 211
rebonatto 0:fac116e94d44 212 Capture::ReadRFID(outlet_number,rfid);
rebonatto 0:fac116e94d44 213
rebonatto 0:fac116e94d44 214 if (strcmp(rfid, "00000000") != 0){ // get rfid number
rebonatto 0:fac116e94d44 215 //printf("Leu RFID [%s]\n", rfid);
rebonatto 0:fac116e94d44 216 flagrfid = 1;
rebonatto 0:fac116e94d44 217 }
rebonatto 0:fac116e94d44 218 }
rebonatto 0:fac116e94d44 219 if (! flagrfid){
rebonatto 0:fac116e94d44 220 strcpy(rfid, "FFFF");
rebonatto 0:fac116e94d44 221 sprintf(aux2, "%04d", outlet_number);
rebonatto 0:fac116e94d44 222 strcat(rfid, aux2);
rebonatto 0:fac116e94d44 223 //rfid[7] = (char)outlet_number + '0';
rebonatto 0:fac116e94d44 224 }
rebonatto 0:fac116e94d44 225 //printf("#%s#\n", rfid);
rebonatto 0:fac116e94d44 226
rebonatto 0:fac116e94d44 227 if(Settings::get_Purpose(m_Channel) == 'p') // phase channel
rebonatto 0:fac116e94d44 228 {
rebonatto 0:fac116e94d44 229 if(ext!=0)
rebonatto 0:fac116e94d44 230 type = 3;
rebonatto 0:fac116e94d44 231 else
rebonatto 0:fac116e94d44 232 {
rebonatto 0:fac116e94d44 233 if(m_OutletTriggered)
rebonatto 0:fac116e94d44 234 type = 4; // power on event
rebonatto 0:fac116e94d44 235 else
rebonatto 0:fac116e94d44 236 type = 5; // power off event
rebonatto 0:fac116e94d44 237 }
rebonatto 0:fac116e94d44 238 }
rebonatto 0:fac116e94d44 239 if(Settings::get_Purpose(m_Channel) == 'd') // diferential channel (leakage)
rebonatto 0:fac116e94d44 240 {
rebonatto 0:fac116e94d44 241 if(ext!=0)
rebonatto 0:fac116e94d44 242 type = 2;
rebonatto 0:fac116e94d44 243 else
rebonatto 0:fac116e94d44 244 {
rebonatto 0:fac116e94d44 245 if(m_OutletTriggered)
rebonatto 0:fac116e94d44 246 type = 1; // start leakage event
rebonatto 0:fac116e94d44 247 else
rebonatto 0:fac116e94d44 248 type = 6; // stop leakage event
rebonatto 0:fac116e94d44 249 }
rebonatto 0:fac116e94d44 250 }
rebonatto 0:fac116e94d44 251
rebonatto 0:fac116e94d44 252 if (type == 1)
rebonatto 0:fac116e94d44 253 printf("Fuga na tomada %d com rms %f\n", outlet_number, rmsvalue);
rebonatto 0:fac116e94d44 254
rebonatto 0:fac116e94d44 255 if (type == 4)
rebonatto 0:fac116e94d44 256 printf("Liga na tomada %d com rms %f\n", outlet_number, rmsvalue);
rebonatto 0:fac116e94d44 257
rebonatto 0:fac116e94d44 258 if (type == 5)
rebonatto 0:fac116e94d44 259 printf("Desliga na tomada %d com rms %f\n", outlet_number, rmsvalue);
rebonatto 0:fac116e94d44 260
rebonatto 0:fac116e94d44 261 if (type == 6)
rebonatto 0:fac116e94d44 262 printf("Final de fuga na tomada %d com rms %f\n", outlet_number, rmsvalue);
rebonatto 0:fac116e94d44 263
rebonatto 0:fac116e94d44 264 //event->Setup(rfid,type,outlet_number,mv,rmsvalue,Settings::get_Gain(m_Channel),Settings::get_Offset(m_Channel),seno,coss);
rebonatto 0:fac116e94d44 265
rebonatto 0:fac116e94d44 266 //printf("Chegou montar evento\n");
rebonatto 0:fac116e94d44 267
rebonatto 0:fac116e94d44 268 Capture::CopyBuffer( m_Channel, buf );
rebonatto 0:fac116e94d44 269
rebonatto 0:fac116e94d44 270 event->Setup(rfid,type,outlet_number,mv,mv2, rmsvalue,under, over, Settings::get_Gain(m_Channel),Settings::get_Offset(m_Channel),duration, buf);
rebonatto 0:fac116e94d44 271
rebonatto 0:fac116e94d44 272 //printf("\n\nDuration: %d\n\n", duration);
rebonatto 0:fac116e94d44 273 //ShowValues(event);
rebonatto 0:fac116e94d44 274
rebonatto 0:fac116e94d44 275 //and finally place the object in the mailbox queue.
rebonatto 0:fac116e94d44 276 GetMailbox().put(event);
rebonatto 0:fac116e94d44 277
rebonatto 0:fac116e94d44 278 //printf("Deu put no evento no mailBox\n");
rebonatto 0:fac116e94d44 279 }
rebonatto 0:fac116e94d44 280
rebonatto 0:fac116e94d44 281 int EventDetector::TimeDelay(int t){
rebonatto 0:fac116e94d44 282 switch (t){
rebonatto 0:fac116e94d44 283 case 0: return 300;
rebonatto 0:fac116e94d44 284 case 1: return 250;
rebonatto 0:fac116e94d44 285 case 2: return 200;
rebonatto 0:fac116e94d44 286 case 3: return 250;
rebonatto 0:fac116e94d44 287 case 4: return 200;
rebonatto 0:fac116e94d44 288 default: return 150;
rebonatto 0:fac116e94d44 289 }
rebonatto 0:fac116e94d44 290 }