Skip to content

Commit

Permalink
Corrigido a versão de firmware do ESP32;
Browse files Browse the repository at this point in the history
- Criado função para cálculo do RMS
- Função para avaliar se o buffer de leitura está cheio;
- Organização da chamada das funções
- Organização do salvamento no buffer;
- Adicionado max e min no buffer, para facilitar o cálculo de offset DC que deve ser subtraido do sinal;
- Criado estrutura para armzenamento das variáveis principais do Smart Meter;
- Criado buffer do tipo ring, para salvar os dados de 'time series' do Smart MEter;
- Criado função para inserir no vetor de Smart Meter time series, os dados atuais que foram calculados;
- Alteração da lib de wifi;
- Melhorias gerais do código
  • Loading branch information
kelvincesar committed Oct 30, 2020
1 parent f884ea5 commit ffeef87
Show file tree
Hide file tree
Showing 14 changed files with 416 additions and 366 deletions.
17 changes: 0 additions & 17 deletions lib/buffer/buffer.h

This file was deleted.

46 changes: 45 additions & 1 deletion lib/buffer/buffer.c → lib/data_buffers/data_buffers.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,40 @@
#include "buffer.h" // Include buffer header
#include "data_buffers.h" // Include buffer header

TimeSerieSM sm_ts;

// Function used to insert data into timeseries struct
int sm_push (SmartMeter *data){
uint16_t pt = sm_ts.pointer;

// Store data into timeseries data block
sm_ts.data[pt].v_rms = data->v_rms;
sm_ts.data[pt].i_rms = data->i_rms;
sm_ts.data[pt].aparrent_power = data->aparrent_power;
sm_ts.data[pt].active_power = data->active_power;
sm_ts.data[pt].reactive_power = data->reactive_power;
sm_ts.data[pt].frequency = data->frequency;
sm_ts.data[pt].fp = data->fp;
sm_ts.data[pt].THD_V = data->THD_V;
sm_ts.data[pt].THD_I = data->THD_I;
sm_ts.pointer++;

// Verify if timeserires size is not full
if(sm_ts.pointer >= SM_TIMESERIE_SIZE){
sm_ts.pointer = 0;
}
return 1;
}












// Function used to insert data into buffer and contabilize it size;
int buffer_push (Buffer *buf, int16_t value){
Expand All @@ -8,14 +44,22 @@ int buffer_push (Buffer *buf, int16_t value){
}
// Insert data into buffer and increment it counter
buf->data[buf->size] = value;
// Compute max and min values
buf->max = (value > buf->max ) ? value : buf->max;
buf->min = (value < buf->min ) ? value : buf->min;

buf->size++;


return 1;
}
void buffer_clean (Buffer *buf){
for (uint16_t i = 0; i < buf->size ; i++){
buf->data[i] = 0;
}
buf->size = 0;
buf->max = 0;
buf->min = 0;
}
int is_buffer_full(Buffer *buf){
// Verify if buffer size is not full
Expand Down
44 changes: 44 additions & 0 deletions lib/data_buffers/data_buffers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Inclusion guard, to prevent multiple includes of the same header
#ifndef BUFFER_H
#define BUFFER_H

#include <stdint.h> // C standard library
#define BUFFER_SIZE 2048 // Define buffer size for current and voltage
#define SM_TIMESERIE_SIZE 32 // Define smart meter main measures timeseires size

// # Buffer struct
typedef struct {
int16_t data[BUFFER_SIZE]; // Array to store values
uint16_t size; // Store array size
uint16_t max; // Store max value in array
uint16_t min; // Store min value in array
} Buffer;

// # Smart Meter main data block struct
typedef struct {
float v_rms;
float i_rms;
float aparrent_power;
float active_power;
float reactive_power;
float frequency;
float fp;
float THD_V;
float THD_I;
uint32_t timestamp;
} SmartMeter;

// # Smart Meter timeseries block
typedef struct {
SmartMeter data[SM_TIMESERIE_SIZE];
uint16_t pointer;
} TimeSerieSM;

// # Buffer functions
int buffer_push (Buffer *buf, int16_t value);
int is_buffer_full(Buffer *buf);
void buffer_clean (Buffer *buf);

// # Timeseries functions
int sm_push (SmartMeter *buf);
#endif
21 changes: 11 additions & 10 deletions lib/goertzel/goertzel.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,27 @@ int goertzel (Buffer *buf, GoertzelState *goertz, uint16_t target_freq, uint16_t
uint8_t enable_full_dft = config > 0x1;
float w, c_real, c_img, coeff;
float hann_const = 1;
float scale_factor = buf->size / 4;
float scale_factor = buf->size / 2;

// # Calculate k constant
k = (unsigned int) (0.5 + (buf->size*target_freq/sample_rate));
k = (unsigned int) floor((0.5 + (buf->size*target_freq/sample_rate)));


// # Calculate omega
w = (k * 2.0 * PI_VALUE) / (buf->size); // Omega
w = (float) (k * 2.0 * M_PI) / (buf->size); // Omega

// # Goertzel coeffiencts
c_real = cosf(w); // Real coeff
coeff = 2 * c_real; // Pre-compute coeff;
coeff = 2 * c_real; // Pre-compute coeff;

// Verifica se o cálculo completo da DFT está ativo
if(enable_full_dft){
c_img = sinf(w); // Img coeff
}
// Cálcula a constante de Hanning para utilizar aplicar no sinal
if(enable_hanning){
hann_const = 2 * PI_VALUE / (buf->size - 1); // Compute hanning constant
hann_const = 2 * M_PI / (buf->size - 1); // Compute hanning constant
//printf(" # Hanning window enabled");
}


Expand All @@ -61,7 +62,7 @@ int goertzel (Buffer *buf, GoertzelState *goertz, uint16_t target_freq, uint16_t
// # Process samples
for (uint16_t n = 0; n < buf->size; n++){
if(enable_hanning)
y = (float) buf->data[n] * (0.5 - 0.5*cosf(hann_const*n));
y = (float) buf->data[n] * (1 - cosf(hann_const*n)) / 2;
else
y = (float) buf->data[n];
y += coeff*y_1 - y_2;
Expand All @@ -72,19 +73,19 @@ int goertzel (Buffer *buf, GoertzelState *goertz, uint16_t target_freq, uint16_t

if(enable_full_dft){
// # Calculate real, imaginary and amplitude
goertz->DFT_r = (y_1 - y_2 * c_real) / (scale_factor);
goertz->DFT_r = (y_1 * c_real - y_2) / (scale_factor);
goertz->DFT_i = (y_2 * c_img) / (scale_factor);
goertz->DFT_m = fast_sqrt(goertz->DFT_r*goertz->DFT_r + goertz->DFT_i*goertz->DFT_i);

// # Calculate the angle <!>
if(goertz->DFT_r > 0) {
goertz->DFT_arg = atanf(goertz->DFT_i / goertz->DFT_r);
} else if(goertz->DFT_r < 0) {
goertz->DFT_arg = PI_VALUE + atanf(goertz->DFT_i/goertz->DFT_r);
goertz->DFT_arg = M_PI + atanf(goertz->DFT_i/goertz->DFT_r);
} else if(goertz->DFT_r == 0 && goertz->DFT_i > 0) {
goertz->DFT_arg = PI_2;
goertz->DFT_arg = M_PI_2;
} else if(goertz->DFT_r == 0 && goertz->DFT_i < 0) {
goertz->DFT_arg = -PI_2;
goertz->DFT_arg = -M_PI_2;
}
} else {
goertz->DFT_r = 0;
Expand Down
3 changes: 2 additions & 1 deletion lib/goertzel/goertzel.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include <stdint.h> // C standard library
#include <math.h>
#include "buffer.h"
#include "data_buffers.h"
//#include <stdio.h>
#define PI_VALUE 3.141592653
#define PI_2 1.570796327
Expand All @@ -21,4 +21,5 @@ typedef struct {

// Functions
int goertzel (Buffer *buf, GoertzelState *goertz, uint16_t target_freq, uint16_t sample_rate, uint8_t config);
float fast_sqrt(float x);
#endif
7 changes: 7 additions & 0 deletions lib/smartmeter/smartmeter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Inclusion guard, to prevent multiple includes of the same header
#ifndef SMARTMETER_H
#define SMARTMETER_H

#include <stdint.h> // C standard library

#endif
175 changes: 0 additions & 175 deletions lib/wifi/wifi.c

This file was deleted.

Loading

0 comments on commit ffeef87

Please sign in to comment.