![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Versão do protegemed que calcula o tempo em ms da fuga, calcula o numero de onverflow (valores muito baixo) e underflow (valores muito altos). Além disso, calcula um valor médio a partir dos valores capturados e não apenas pela fft.
Dependencies: EthernetInterface mbed-rtos mbed
Codes/EventDetector.cpp@0:c64e1194230b, 2014-07-09 (annotated)
- Committer:
- rebonatto
- Date:
- Wed Jul 09 21:16:23 2014 +0000
- Revision:
- 0:c64e1194230b
- Child:
- 1:917ca6b5d9d9
Vers?o do Protegemed com calculo de tempo de fuga, overflow, underflow e novo valor m?dio (manual).
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rebonatto | 0:c64e1194230b | 1 | /* |
rebonatto | 0:c64e1194230b | 2 | * EventDetector.cpp |
rebonatto | 0:c64e1194230b | 3 | * |
rebonatto | 0:c64e1194230b | 4 | * Created on: |
rebonatto | 0:c64e1194230b | 5 | * Author: |
rebonatto | 0:c64e1194230b | 6 | */ |
rebonatto | 0:c64e1194230b | 7 | |
rebonatto | 0:c64e1194230b | 8 | #include "EventDetector.h" |
rebonatto | 0:c64e1194230b | 9 | #include "CommTCP.h" |
rebonatto | 0:c64e1194230b | 10 | |
rebonatto | 0:c64e1194230b | 11 | #define MARCA 10 |
rebonatto | 0:c64e1194230b | 12 | #define UMCICLO 16.666666667 |
rebonatto | 0:c64e1194230b | 13 | |
rebonatto | 0:c64e1194230b | 14 | CaptureMailbox EventDetector::m_EventMailbox; |
rebonatto | 0:c64e1194230b | 15 | EventDetector EventDetector::m_Detector[NUMBER_OF_CHANNELS] = {0,1,2,3,4,5}; |
rebonatto | 0:c64e1194230b | 16 | static int cont=0; |
rebonatto | 0:c64e1194230b | 17 | static int contliga=0; |
rebonatto | 0:c64e1194230b | 18 | static int contdesl=0; |
rebonatto | 0:c64e1194230b | 19 | |
rebonatto | 0:c64e1194230b | 20 | CaptureEvent::CaptureEvent() |
rebonatto | 0:c64e1194230b | 21 | { |
rebonatto | 0:c64e1194230b | 22 | } |
rebonatto | 0:c64e1194230b | 23 | |
rebonatto | 0:c64e1194230b | 24 | //void CaptureEvent::Setup(char* rfid,int type,int outlet,float mv,float rms,float gain, int offset,float* sin, float* cos) |
rebonatto | 0:c64e1194230b | 25 | 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:c64e1194230b | 26 | { |
rebonatto | 0:c64e1194230b | 27 | memcpy(m_RFID,rfid,9); |
rebonatto | 0:c64e1194230b | 28 | m_Type = type; |
rebonatto | 0:c64e1194230b | 29 | m_OutletNumber = outlet; |
rebonatto | 0:c64e1194230b | 30 | m_MeanValue = mv; |
rebonatto | 0:c64e1194230b | 31 | m_MV2 = mv2; |
rebonatto | 0:c64e1194230b | 32 | m_RMSValue = rms; |
rebonatto | 0:c64e1194230b | 33 | m_Under = under; |
rebonatto | 0:c64e1194230b | 34 | m_Over = over; |
rebonatto | 0:c64e1194230b | 35 | m_Gain = gain; |
rebonatto | 0:c64e1194230b | 36 | m_Offset = offset; |
rebonatto | 0:c64e1194230b | 37 | m_Duration = duration; |
rebonatto | 0:c64e1194230b | 38 | memcpy(m_Sin,sin,Settings::get_MaxHarmonics()*sizeof(float)); |
rebonatto | 0:c64e1194230b | 39 | memcpy(m_Cos,cos,Settings::get_MaxHarmonics()*sizeof(float)); |
rebonatto | 0:c64e1194230b | 40 | } |
rebonatto | 0:c64e1194230b | 41 | |
rebonatto | 0:c64e1194230b | 42 | EventDetector::EventDetector(int ch) |
rebonatto | 0:c64e1194230b | 43 | { |
rebonatto | 0:c64e1194230b | 44 | m_Channel = ch; |
rebonatto | 0:c64e1194230b | 45 | m_OutletTriggered = false; |
rebonatto | 0:c64e1194230b | 46 | m_EventCounter = 0; |
rebonatto | 0:c64e1194230b | 47 | } |
rebonatto | 0:c64e1194230b | 48 | |
rebonatto | 0:c64e1194230b | 49 | //void EventDetector::ProcessEvent(float rmsvalue, int t) |
rebonatto | 0:c64e1194230b | 50 | void EventDetector::ProcessEvent(float rmsvalue, float mv2, int under, int over) |
rebonatto | 0:c64e1194230b | 51 | { |
rebonatto | 0:c64e1194230b | 52 | short gerafuga = 0; |
rebonatto | 0:c64e1194230b | 53 | int tempofuga = 0; |
rebonatto | 0:c64e1194230b | 54 | |
rebonatto | 0:c64e1194230b | 55 | //Thread::wait(500);//TimeDelay(t)); |
rebonatto | 0:c64e1194230b | 56 | //printf("%d time delay\n", TimeDelay(t)); |
rebonatto | 0:c64e1194230b | 57 | if (gerafuga && (m_Channel % 2 == 0) ){ |
rebonatto | 0:c64e1194230b | 58 | /* |
rebonatto | 0:c64e1194230b | 59 | if (cont % MARCA == 0) |
rebonatto | 0:c64e1194230b | 60 | printf("Envia Fuga %d %d\n", cont, TimeDelay(m_Channel)); |
rebonatto | 0:c64e1194230b | 61 | cont++; |
rebonatto | 0:c64e1194230b | 62 | */ |
rebonatto | 0:c64e1194230b | 63 | // Fuga |
rebonatto | 0:c64e1194230b | 64 | m_OutletTriggered = true; |
rebonatto | 0:c64e1194230b | 65 | Capture::Stop(); |
rebonatto | 0:c64e1194230b | 66 | //printf("Passou Stop\n"); |
rebonatto | 0:c64e1194230b | 67 | //CommTCP::RequestAcom(); |
rebonatto | 0:c64e1194230b | 68 | OnTrigger(rmsvalue, mv2, under, over, 0); //TODO: must change the parameter of this function call |
rebonatto | 0:c64e1194230b | 69 | //printf("Passou OnTriger\n"); |
rebonatto | 0:c64e1194230b | 70 | Capture::Start(); |
rebonatto | 0:c64e1194230b | 71 | //printf("Passou Start\n"); |
rebonatto | 0:c64e1194230b | 72 | m_EventCounter = 0; |
rebonatto | 0:c64e1194230b | 73 | |
rebonatto | 0:c64e1194230b | 74 | Thread::wait(2); |
rebonatto | 0:c64e1194230b | 75 | // Termino |
rebonatto | 0:c64e1194230b | 76 | /* |
rebonatto | 0:c64e1194230b | 77 | if (cont % MARCA == 0) |
rebonatto | 0:c64e1194230b | 78 | printf("Envia Termino\n"); |
rebonatto | 0:c64e1194230b | 79 | */ |
rebonatto | 0:c64e1194230b | 80 | m_OutletTriggered = false; |
rebonatto | 0:c64e1194230b | 81 | Capture::Stop(); |
rebonatto | 0:c64e1194230b | 82 | //printf("Passou Stop\n"); |
rebonatto | 0:c64e1194230b | 83 | OnTrigger(rmsvalue, mv2, under, over, 0);//TODO: must change the parameter of this function call |
rebonatto | 0:c64e1194230b | 84 | //printf("Passou onTrigger\n"); |
rebonatto | 0:c64e1194230b | 85 | Capture::Start(); |
rebonatto | 0:c64e1194230b | 86 | //printf("Passou Start\n"); |
rebonatto | 0:c64e1194230b | 87 | m_EventCounter = 0; |
rebonatto | 0:c64e1194230b | 88 | |
rebonatto | 0:c64e1194230b | 89 | Thread::wait(2); |
rebonatto | 0:c64e1194230b | 90 | } |
rebonatto | 0:c64e1194230b | 91 | else{ |
rebonatto | 0:c64e1194230b | 92 | if(rmsvalue > Settings::get_Limit(m_Channel)) |
rebonatto | 0:c64e1194230b | 93 | { |
rebonatto | 0:c64e1194230b | 94 | if(!m_OutletTriggered) |
rebonatto | 0:c64e1194230b | 95 | { |
rebonatto | 0:c64e1194230b | 96 | if(m_EventCounter < Settings::get_EventLimit()) |
rebonatto | 0:c64e1194230b | 97 | { |
rebonatto | 0:c64e1194230b | 98 | m_EventCounter++; |
rebonatto | 0:c64e1194230b | 99 | } |
rebonatto | 0:c64e1194230b | 100 | else |
rebonatto | 0:c64e1194230b | 101 | { |
rebonatto | 0:c64e1194230b | 102 | //printf("Deu evento de liga ou Fuga\n"); |
rebonatto | 0:c64e1194230b | 103 | if (m_Channel % 2 == 1) // Canais impares sao de diferencial |
rebonatto | 0:c64e1194230b | 104 | m_tempo.start(); |
rebonatto | 0:c64e1194230b | 105 | m_OutletTriggered = true; |
rebonatto | 0:c64e1194230b | 106 | Capture::Stop(); |
rebonatto | 0:c64e1194230b | 107 | OnTrigger(rmsvalue, mv2, under, over, 0); //TODO: must change the parameter of this function call |
rebonatto | 0:c64e1194230b | 108 | //CommTCP::RequestAcom(); |
rebonatto | 0:c64e1194230b | 109 | //zzz |
rebonatto | 0:c64e1194230b | 110 | Capture::Start(); |
rebonatto | 0:c64e1194230b | 111 | m_EventCounter = 0; |
rebonatto | 0:c64e1194230b | 112 | } |
rebonatto | 0:c64e1194230b | 113 | } |
rebonatto | 0:c64e1194230b | 114 | else |
rebonatto | 0:c64e1194230b | 115 | m_EventCounter = 0; |
rebonatto | 0:c64e1194230b | 116 | } |
rebonatto | 0:c64e1194230b | 117 | else |
rebonatto | 0:c64e1194230b | 118 | { |
rebonatto | 0:c64e1194230b | 119 | if(m_OutletTriggered) |
rebonatto | 0:c64e1194230b | 120 | { |
rebonatto | 0:c64e1194230b | 121 | if(m_EventCounter < Settings::get_EventLimit()) |
rebonatto | 0:c64e1194230b | 122 | { |
rebonatto | 0:c64e1194230b | 123 | m_EventCounter++; |
rebonatto | 0:c64e1194230b | 124 | } |
rebonatto | 0:c64e1194230b | 125 | else |
rebonatto | 0:c64e1194230b | 126 | { |
rebonatto | 0:c64e1194230b | 127 | //printf("Terminou evento de liga ou Fuga\n"); |
rebonatto | 0:c64e1194230b | 128 | if (m_Channel % 2 == 1){ // Canais impares sao de diferencial |
rebonatto | 0:c64e1194230b | 129 | //m_tempo.stop(); |
rebonatto | 0:c64e1194230b | 130 | tempofuga = (int) (Settings::get_EventLimit() * UMCICLO) + m_tempo.read_ms(); |
rebonatto | 0:c64e1194230b | 131 | //printf("Limite %d\n", (int) (Settings::get_EventLimit() * UMCICLO)); |
rebonatto | 0:c64e1194230b | 132 | m_tempo.reset(); |
rebonatto | 0:c64e1194230b | 133 | } |
rebonatto | 0:c64e1194230b | 134 | else |
rebonatto | 0:c64e1194230b | 135 | tempofuga = 0; |
rebonatto | 0:c64e1194230b | 136 | m_OutletTriggered = false; |
rebonatto | 0:c64e1194230b | 137 | Capture::Stop(); |
rebonatto | 0:c64e1194230b | 138 | OnTrigger(rmsvalue, mv2, under, over, tempofuga);//TODO: must change the parameter of this function call |
rebonatto | 0:c64e1194230b | 139 | //printf("===> contliga %d contdesliga %d\n", contliga, contdesl); |
rebonatto | 0:c64e1194230b | 140 | //zzz |
rebonatto | 0:c64e1194230b | 141 | Capture::Start(); |
rebonatto | 0:c64e1194230b | 142 | m_EventCounter = 0; |
rebonatto | 0:c64e1194230b | 143 | } |
rebonatto | 0:c64e1194230b | 144 | } |
rebonatto | 0:c64e1194230b | 145 | else |
rebonatto | 0:c64e1194230b | 146 | m_EventCounter = 0; |
rebonatto | 0:c64e1194230b | 147 | } |
rebonatto | 0:c64e1194230b | 148 | } // Final gerafuga |
rebonatto | 0:c64e1194230b | 149 | } |
rebonatto | 0:c64e1194230b | 150 | |
rebonatto | 0:c64e1194230b | 151 | void EventDetector::ShowValues(CaptureEvent* e) |
rebonatto | 0:c64e1194230b | 152 | { |
rebonatto | 0:c64e1194230b | 153 | printf("RFID: %s\n", e->get_RFID()); |
rebonatto | 0:c64e1194230b | 154 | printf("type: %d\n", e->get_Type()); |
rebonatto | 0:c64e1194230b | 155 | printf("OutletNr: %d\n", e->get_OutletNumber()); |
rebonatto | 0:c64e1194230b | 156 | printf("MeanValue: %f\n", e->get_MeanValue()); |
rebonatto | 0:c64e1194230b | 157 | printf("MV2: %f\n", e->get_MV2()); |
rebonatto | 0:c64e1194230b | 158 | printf("RMSValue: %f\n", e->get_RMSValue()); |
rebonatto | 0:c64e1194230b | 159 | printf("Underflow: %d\n", e->get_Under()); |
rebonatto | 0:c64e1194230b | 160 | printf("Overflow: %d\n", e->get_Over()); |
rebonatto | 0:c64e1194230b | 161 | printf("Gain: %f\n", e->get_Gain()); |
rebonatto | 0:c64e1194230b | 162 | printf("Offset: %d\n", e->get_Offset()); |
rebonatto | 0:c64e1194230b | 163 | printf("Duration: %d\n", e->get_Duration()); |
rebonatto | 0:c64e1194230b | 164 | |
rebonatto | 0:c64e1194230b | 165 | int i; |
rebonatto | 0:c64e1194230b | 166 | for(i=0;i<12;i++) |
rebonatto | 0:c64e1194230b | 167 | { |
rebonatto | 0:c64e1194230b | 168 | printf("Harm %d Sen %f Cos %f\n ", i, e->get_SineValue(i), e->get_CossineValue(i)); |
rebonatto | 0:c64e1194230b | 169 | } |
rebonatto | 0:c64e1194230b | 170 | |
rebonatto | 0:c64e1194230b | 171 | } |
rebonatto | 0:c64e1194230b | 172 | |
rebonatto | 0:c64e1194230b | 173 | //void EventDetector::OnTrigger(float rmsvalue) |
rebonatto | 0:c64e1194230b | 174 | void EventDetector::OnTrigger(float rmsvalue, float mv2, int under, int over, int duration) |
rebonatto | 0:c64e1194230b | 175 | { |
rebonatto | 0:c64e1194230b | 176 | SendMessage(0,rmsvalue, mv2, under, over, duration); |
rebonatto | 0:c64e1194230b | 177 | //printf("Ontrigger::Enviou mensagem\n"); |
rebonatto | 0:c64e1194230b | 178 | /* |
rebonatto | 0:c64e1194230b | 179 | if(m_OutletTriggered) |
rebonatto | 0:c64e1194230b | 180 | { |
rebonatto | 0:c64e1194230b | 181 | for(int i=0;i<Settings::get_MaxChannels();i++) |
rebonatto | 0:c64e1194230b | 182 | { |
rebonatto | 0:c64e1194230b | 183 | //Thread::wait(300); |
rebonatto | 0:c64e1194230b | 184 | if(i != m_Channel){ |
rebonatto | 0:c64e1194230b | 185 | //printf("Ontrigger::Enviou acompanhamento %d\n", i); |
rebonatto | 0:c64e1194230b | 186 | get_Detector(i).ExternalTrigger(); |
rebonatto | 0:c64e1194230b | 187 | } |
rebonatto | 0:c64e1194230b | 188 | } |
rebonatto | 0:c64e1194230b | 189 | //printf("Ontrigger::Enviou acompanhamentos %d\n"); |
rebonatto | 0:c64e1194230b | 190 | } |
rebonatto | 0:c64e1194230b | 191 | */ |
rebonatto | 0:c64e1194230b | 192 | } |
rebonatto | 0:c64e1194230b | 193 | |
rebonatto | 0:c64e1194230b | 194 | void EventDetector::ExternalTrigger() |
rebonatto | 0:c64e1194230b | 195 | { |
rebonatto | 0:c64e1194230b | 196 | // Ajustar valores de mv2, under, over e duration na chamada!!! |
rebonatto | 0:c64e1194230b | 197 | SendMessage(1, 0, 0, 0, 0, 0); |
rebonatto | 0:c64e1194230b | 198 | } |
rebonatto | 0:c64e1194230b | 199 | |
rebonatto | 0:c64e1194230b | 200 | //void EventDetector::SendMessage(int ext,float rmsvalue) |
rebonatto | 0:c64e1194230b | 201 | void EventDetector::SendMessage(int ext, float rmsvalue, float mv2, int under, int over, int duration) |
rebonatto | 0:c64e1194230b | 202 | { |
rebonatto | 0:c64e1194230b | 203 | //Here we must alloc a CaptureEvent object from mailbox pool, |
rebonatto | 0:c64e1194230b | 204 | CaptureEvent* event = GetMailbox().alloc(); |
rebonatto | 0:c64e1194230b | 205 | //printf("Capturou evento\n"); |
rebonatto | 0:c64e1194230b | 206 | //then initialize the object properly |
rebonatto | 0:c64e1194230b | 207 | |
rebonatto | 0:c64e1194230b | 208 | unsigned short int buf[NUMBER_OF_SAMPLES]; |
rebonatto | 0:c64e1194230b | 209 | Capture::CopyBuffer(m_Channel,buf); |
rebonatto | 0:c64e1194230b | 210 | |
rebonatto | 0:c64e1194230b | 211 | if(ext) |
rebonatto | 0:c64e1194230b | 212 | { |
rebonatto | 0:c64e1194230b | 213 | rmsvalue = 0;//SignalProcessor::CalculateRMS(buf,m_Channel); |
rebonatto | 0:c64e1194230b | 214 | } |
rebonatto | 0:c64e1194230b | 215 | |
rebonatto | 0:c64e1194230b | 216 | float seno[NUMBER_OF_HARMONICS+1],coss[NUMBER_OF_HARMONICS+1],mv; |
rebonatto | 0:c64e1194230b | 217 | //SignalProcessor::CalculateFFT(buf,sen,cos,&mv,1); |
rebonatto | 0:c64e1194230b | 218 | |
rebonatto | 0:c64e1194230b | 219 | int type=0,outlet_number=0, aux=0; |
rebonatto | 0:c64e1194230b | 220 | aux = Settings::get_OutletNumber(m_Channel); |
rebonatto | 0:c64e1194230b | 221 | outlet_number = Settings::get_Outlet(aux); |
rebonatto | 0:c64e1194230b | 222 | //Temporario |
rebonatto | 0:c64e1194230b | 223 | char rfid[9] = "1234560"; |
rebonatto | 0:c64e1194230b | 224 | rfid[7] = (char)outlet_number + '0'; |
rebonatto | 0:c64e1194230b | 225 | |
rebonatto | 0:c64e1194230b | 226 | if(Settings::get_Purpose(m_Channel) == 'p') // phase channel |
rebonatto | 0:c64e1194230b | 227 | { |
rebonatto | 0:c64e1194230b | 228 | if(ext!=0) |
rebonatto | 0:c64e1194230b | 229 | type = 3; |
rebonatto | 0:c64e1194230b | 230 | else |
rebonatto | 0:c64e1194230b | 231 | { |
rebonatto | 0:c64e1194230b | 232 | if(m_OutletTriggered) |
rebonatto | 0:c64e1194230b | 233 | type = 4; // power on event |
rebonatto | 0:c64e1194230b | 234 | else |
rebonatto | 0:c64e1194230b | 235 | type = 5; // power off event |
rebonatto | 0:c64e1194230b | 236 | } |
rebonatto | 0:c64e1194230b | 237 | } |
rebonatto | 0:c64e1194230b | 238 | if(Settings::get_Purpose(m_Channel) == 'd') // diferential channel (leakage) |
rebonatto | 0:c64e1194230b | 239 | { |
rebonatto | 0:c64e1194230b | 240 | if(ext!=0) |
rebonatto | 0:c64e1194230b | 241 | type = 2; |
rebonatto | 0:c64e1194230b | 242 | else |
rebonatto | 0:c64e1194230b | 243 | { |
rebonatto | 0:c64e1194230b | 244 | if(m_OutletTriggered) |
rebonatto | 0:c64e1194230b | 245 | type = 1; // start leakage event |
rebonatto | 0:c64e1194230b | 246 | else |
rebonatto | 0:c64e1194230b | 247 | type = 6; // stop leakage event |
rebonatto | 0:c64e1194230b | 248 | } |
rebonatto | 0:c64e1194230b | 249 | } |
rebonatto | 0:c64e1194230b | 250 | |
rebonatto | 0:c64e1194230b | 251 | if (type == 1 || type == 2 || type == 4) // Calula FFT s nos eventos de fuga, acompanhamento de fuga (diferencial) e liga |
rebonatto | 0:c64e1194230b | 252 | SignalProcessor::CalculateFFT(buf,seno,coss,&mv,1, m_Channel); |
rebonatto | 0:c64e1194230b | 253 | /* |
rebonatto | 0:c64e1194230b | 254 | for(int y=0; y < 12; y++) |
rebonatto | 0:c64e1194230b | 255 | printf("[%d] sen %f cos %f\n", y, seno[y], coss[y]); |
rebonatto | 0:c64e1194230b | 256 | */ |
rebonatto | 0:c64e1194230b | 257 | else |
rebonatto | 0:c64e1194230b | 258 | for(int i=0; i < Settings::get_MaxHarmonics(); i++) |
rebonatto | 0:c64e1194230b | 259 | seno[i] = coss[i] = 0; |
rebonatto | 0:c64e1194230b | 260 | |
rebonatto | 0:c64e1194230b | 261 | //event->Setup(rfid,type,outlet_number,mv,rmsvalue,Settings::get_Gain(m_Channel),Settings::get_Offset(m_Channel),seno,coss); |
rebonatto | 0:c64e1194230b | 262 | |
rebonatto | 0:c64e1194230b | 263 | 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:c64e1194230b | 264 | |
rebonatto | 0:c64e1194230b | 265 | //printf("\n\nDuration: %d\n\n", duration); |
rebonatto | 0:c64e1194230b | 266 | //ShowValues(event); |
rebonatto | 0:c64e1194230b | 267 | |
rebonatto | 0:c64e1194230b | 268 | //and finally place the object in the mailbox queue. |
rebonatto | 0:c64e1194230b | 269 | GetMailbox().put(event); |
rebonatto | 0:c64e1194230b | 270 | //printf("Deu put no evento no mailBox\n"); |
rebonatto | 0:c64e1194230b | 271 | } |
rebonatto | 0:c64e1194230b | 272 | |
rebonatto | 0:c64e1194230b | 273 | int EventDetector::TimeDelay(int t){ |
rebonatto | 0:c64e1194230b | 274 | switch (t){ |
rebonatto | 0:c64e1194230b | 275 | case 0: return 300; |
rebonatto | 0:c64e1194230b | 276 | case 1: return 250; |
rebonatto | 0:c64e1194230b | 277 | case 2: return 200; |
rebonatto | 0:c64e1194230b | 278 | case 3: return 250; |
rebonatto | 0:c64e1194230b | 279 | case 4: return 200; |
rebonatto | 0:c64e1194230b | 280 | default: return 150; |
rebonatto | 0:c64e1194230b | 281 | } |
rebonatto | 0:c64e1194230b | 282 | } |