Versão limpa em 04/09/2014. Telnet funcionando.

Dependencies:   EthernetInterface mbed-rtos mbed NTPClient

Committer:
rebonatto
Date:
Mon Jan 18 18:26:03 2016 +0000
Revision:
43:69afea5f5a4d
Parent:
42:d6f9ce115eaf
Aquisi??o sem DMA, 256 amostras.; Buffer simples com  copia de buffer para calculo da FFT.; Com telnetr e TFTP ativos. Telnet funcionando sem travar.; Vers?o com WhatDog.

Who changed what in which revision?

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