Versão sem FFT e aquisição por DMA. 256 amostras.

Dependencies:   EthernetInterface NTPClient mbed-rtos mbed

Committer:
rebonatto
Date:
Tue Jan 05 11:45:44 2016 +0000
Revision:
0:e57bc370d339
Vers?o est?vel sem calculo de FFT. Aquisi??o por DMA. Usa 256 amostras.

Who changed what in which revision?

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