EZR
Dependencies: CRC16 FreescaleIAP FreescaleWatchdog GGSProtocol LM75B PIMA Parameters PersistentCircularQueue SerialNumberV2COM mbed-dev-watchdog_2016_03_04
Fork of smartRamalKW by
Revision 10:7a1d1abb5172, committed 2015-07-23
- Comitter:
- tpadovani
- Date:
- Thu Jul 23 22:46:18 2015 +0000
- Parent:
- 9:e501499af4ef
- Child:
- 11:8c93e0de58cd
- Commit message:
- Mem?ria de massa de tens?o e comando para ler o rel?gio atual.
Changed in this revision
--- a/CRC16.lib Wed Jul 22 15:09:01 2015 +0000 +++ b/CRC16.lib Thu Jul 23 22:46:18 2015 +0000 @@ -1,1 +1,1 @@ -http://developer.mbed.org/teams/Equipe-Firmware-V2COM/code/CRC16/#45617a5280ed +http://developer.mbed.org/teams/Equipe-Firmware-V2COM/code/CRC16/#07b6475bbd72
--- a/GGSProtocol.lib Wed Jul 22 15:09:01 2015 +0000 +++ b/GGSProtocol.lib Thu Jul 23 22:46:18 2015 +0000 @@ -1,1 +1,1 @@ -http://developer.mbed.org/teams/Equipe-Firmware-V2COM/code/GGSProtocol/#73862506e1d6 +http://developer.mbed.org/teams/Equipe-Firmware-V2COM/code/GGSProtocol/#766dbcb26c70
--- a/Parameters.lib Wed Jul 22 15:09:01 2015 +0000 +++ b/Parameters.lib Thu Jul 23 22:46:18 2015 +0000 @@ -1,1 +1,1 @@ -http://developer.mbed.org/teams/Equipe-Firmware-V2COM/code/Parameters/#83a20c772012 +http://developer.mbed.org/teams/Equipe-Firmware-V2COM/code/Parameters/#4dfa7b322534
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PersistentCircularQueue.lib Thu Jul 23 22:46:18 2015 +0000 @@ -0,0 +1,1 @@ +PersistentCircularQueue#f4c180453b0f
--- a/SerialNumberV2COM.lib Wed Jul 22 15:09:01 2015 +0000 +++ b/SerialNumberV2COM.lib Thu Jul 23 22:46:18 2015 +0000 @@ -1,1 +1,1 @@ -http://developer.mbed.org/teams/Equipe-Firmware-V2COM/code/SerialNumberV2COM/#f0a2d7e52eac +http://developer.mbed.org/teams/Equipe-Firmware-V2COM/code/SerialNumberV2COM/#66b25bfc7c90
--- a/appCommands.cpp Wed Jul 22 15:09:01 2015 +0000 +++ b/appCommands.cpp Thu Jul 23 22:46:18 2015 +0000 @@ -1,12 +1,12 @@ #include "appCommands.h" -void enviaRespostaGGS(unsigned char * dados, int tamanho){ +void enviaRespostaGGS(char * dados, int tamanho){ for(int i=0; i < tamanho; i++){ ggsStream.putc(dados[i]); } } -void executaComando(unsigned char tamanho, unsigned char sequencia, unsigned char tipo, unsigned char * dados){ +void executaComando(unsigned char tamanho, unsigned char sequencia, unsigned char tipo, char * dados){ switch(tipo){ case CMD_LER_VERSAO: cmdLerVersao(); @@ -36,9 +36,15 @@ case CMD_CONFIGURAR_PARAMETROS: cmdConfigurarParametros(dados, tamanho - TAMANHO_MINIMO_PACOTE); break; + case CMD_LER_RELOGIO: + cmdLerRelogio(); + break; case CMD_TENSAO_INSTANTANEA: cmdTensaoInstantanea(dados[0]); break; + case CMD_MM_TENSAO: + cmdMMTensao(dados[0]); + break; case CMD_DETECTA_CARGA: cmdDetectaCarga(); break;
--- a/appCommands.h Wed Jul 22 15:09:01 2015 +0000 +++ b/appCommands.h Thu Jul 23 22:46:18 2015 +0000 @@ -25,7 +25,8 @@ #define RESP_LER_PARAMETROS 0x1c #define CMD_CONFIGURAR_PARAMETROS 0x1d #define RESP_CONFIGURAR_PARAMETROS 0x1e - +#define CMD_LER_RELOGIO 0x1f +#define RESP_LER_RELOGIO 0x20 #define CMD_TENSAO_INSTANTANEA 0x21 #define RESP_TENSAO_INSTANTANEA 0x22 #define CMD_CORRENTE_INSTANTANEA 0x23 @@ -44,15 +45,16 @@ extern ParametersBlock APP_PARAMETERS; extern void softReset(); -void enviaRespostaGGS(unsigned char * dados, int tamanho); -void executaComando(unsigned char tamanho, unsigned char sequencia, unsigned char tipo, unsigned char * dados); +void enviaRespostaGGS(char * dados, int tamanho); +void executaComando(unsigned char tamanho, unsigned char sequencia, unsigned char tipo, char * dados); void cmdLerVersao(); void cmdLerNumeroSerie(); -void cmdGravarNumeroSerie(unsigned char * dados, unsigned char tamanhoDados); +void cmdGravarNumeroSerie(char * dados, unsigned char tamanhoDados); void cmdReset(); void cmdLerParametros(); -void cmdConfigurarParametros(unsigned char * dados, unsigned char tamanhoDados); +void cmdConfigurarParametros(char * dados, unsigned char tamanhoDados); +void cmdLerRelogio(); void cmdCortar(); void cmdReligar(); @@ -60,6 +62,7 @@ void cmdDetectaCarga(); void cmdTensaoInstantanea(unsigned char lineOrLoad); +void cmdMMTensao(unsigned char lineOrLoad); void atualizaEstadoRele(unsigned char estado); unsigned char lerEstadoRele();
--- a/appParameters.cpp Wed Jul 22 15:09:01 2015 +0000 +++ b/appParameters.cpp Thu Jul 23 22:46:18 2015 +0000 @@ -5,35 +5,35 @@ APP_PARAMETERS.estadoRele = ESTADO_RELE_INDEFINIDO; } - if(((unsigned char *)&APP_PARAMETERS.RELIGAR_LARGURA_PULSO_MS)[0] == 0xff && ((unsigned char *)&APP_PARAMETERS.RELIGAR_LARGURA_PULSO_MS)[1] == 0xff){ + if(((char *)&APP_PARAMETERS.RELIGAR_LARGURA_PULSO_MS)[0] == 0xff && ((char *)&APP_PARAMETERS.RELIGAR_LARGURA_PULSO_MS)[1] == 0xff){ APP_PARAMETERS.RELIGAR_LARGURA_PULSO_MS = DEFAULT_RELIGAR_LARGURA_PULSO_MS; } - if(((unsigned char *)&APP_PARAMETERS.CORTAR_LARGURA_PULSO_MS)[0] == 0xff && ((unsigned char *)&APP_PARAMETERS.CORTAR_LARGURA_PULSO_MS)[1] == 0xff){ + if(((char *)&APP_PARAMETERS.CORTAR_LARGURA_PULSO_MS)[0] == 0xff && ((char *)&APP_PARAMETERS.CORTAR_LARGURA_PULSO_MS)[1] == 0xff){ APP_PARAMETERS.CORTAR_LARGURA_PULSO_MS = DEFAULT_CORTAR_LARGURA_PULSO_MS; } - if(((unsigned char *)&APP_PARAMETERS.DELAY_SENSOR_MS)[0] == 0xff && ((unsigned char *)&APP_PARAMETERS.DELAY_SENSOR_MS)[1] == 0xff){ + if(((char *)&APP_PARAMETERS.DELAY_SENSOR_MS)[0] == 0xff && ((char *)&APP_PARAMETERS.DELAY_SENSOR_MS)[1] == 0xff){ APP_PARAMETERS.DELAY_SENSOR_MS = DEFAULT_DELAY_SENSOR_MS; } - if(((unsigned char *)&APP_PARAMETERS.INTERVALO_SILENCIO_S)[0] == 0xff && ((unsigned char *)&APP_PARAMETERS.INTERVALO_SILENCIO_S)[1] == 0xff){ + if(((char *)&APP_PARAMETERS.INTERVALO_SILENCIO_S)[0] == 0xff && ((char *)&APP_PARAMETERS.INTERVALO_SILENCIO_S)[1] == 0xff){ APP_PARAMETERS.INTERVALO_SILENCIO_S = DEFAULT_INTERVALO_SILENCIO_S; } - if(((unsigned char *)&APP_PARAMETERS.INTERVALO_WD_NETWORK_S)[0] == 0xff && ((unsigned char *)&APP_PARAMETERS.INTERVALO_WD_NETWORK_S)[1] == 0xff){ + if(((char *)&APP_PARAMETERS.INTERVALO_WD_NETWORK_S)[0] == 0xff && ((char *)&APP_PARAMETERS.INTERVALO_WD_NETWORK_S)[1] == 0xff){ APP_PARAMETERS.INTERVALO_WD_NETWORK_S = DEFAULT_INTERVALO_WD_NETWORK_S; } - if(((unsigned char *)&APP_PARAMETERS.LIMITE_TENSAO_SENSOR_V)[0] == 0xff && ((unsigned char *)&APP_PARAMETERS.LIMITE_TENSAO_SENSOR_V)[1] == 0xff){ + if(((char *)&APP_PARAMETERS.LIMITE_TENSAO_SENSOR_V)[0] == 0xff && ((char *)&APP_PARAMETERS.LIMITE_TENSAO_SENSOR_V)[1] == 0xff){ APP_PARAMETERS.LIMITE_TENSAO_SENSOR_V = DEFAULT_LIMITE_TENSAO_SENSOR_V; } - if(((unsigned char *)&APP_PARAMETERS.SAMPLES)[0] == 0xff && ((unsigned char *)&APP_PARAMETERS.SAMPLES)[1] == 0xff){ + if(((char *)&APP_PARAMETERS.SAMPLES)[0] == 0xff && ((char *)&APP_PARAMETERS.SAMPLES)[1] == 0xff){ APP_PARAMETERS.SAMPLES = DEFAULT_SAMPLES; } - if(((unsigned char *)&APP_PARAMETERS.SAMPLES_DELAY_US)[0] == 0xff && ((unsigned char *)&APP_PARAMETERS.SAMPLES_DELAY_US)[1] == 0xff){ + if(((char *)&APP_PARAMETERS.SAMPLES_DELAY_US)[0] == 0xff && ((char *)&APP_PARAMETERS.SAMPLES_DELAY_US)[1] == 0xff){ APP_PARAMETERS.SAMPLES_DELAY_US = DEFAULT_SAMPLES_DELAY_US; } @@ -49,4 +49,12 @@ APP_PARAMETERS.LOAD_CHANNEL = DEFAULT_LOAD_CHANNEL; } + if(((char *)&APP_PARAMETERS.INTERVALO_MM_S)[0] == 0xff && ((char *)&APP_PARAMETERS.INTERVALO_MM_S)[1] == 0xff){ + APP_PARAMETERS.INTERVALO_MM_S = DEFAULT_INTERVALO_MM_S; + } + + if(((char *)&APP_PARAMETERS.MM_SIZE)[0] == 0xff && ((char *)&APP_PARAMETERS.MM_SIZE)[1] == 0xff){ + APP_PARAMETERS.MM_SIZE = DEFAULT_MM_SIZE; + } + }
--- a/appParameters.h Wed Jul 22 15:09:01 2015 +0000 +++ b/appParameters.h Thu Jul 23 22:46:18 2015 +0000 @@ -16,13 +16,16 @@ unsigned int SAMPLES_DELAY_US; // Intervalo entre amostras para calculo da tensao RMS (em microssegundos) union{ float floatValue; // Coeficiente angular das amostras de tensao do A/D - unsigned char bytes[4]; + char bytes[4]; } SAMPLES_ANG_COEF; union{ float floatValue; // Coeficiente linear das amostras de tensao do A/D - unsigned char bytes[4]; + char bytes[4]; } SAMPLES_LIN_COEF; unsigned char LOAD_CHANNEL; // Indica o canal (0 ou 1) onde esta ligada a carga, para que as rotinas de corte e religa verifiquem a alimentacao corretamente + unsigned int INTERVALO_MM_S; // Intervalo de leitura de Memoria de Massa em segundos + unsigned int MM_SIZE; // Numero maximo de registros da memoria de massa + char reserved[3]; // Apenas para completar numero de bytes para ficar multiplo de 8 } ParametersBlock; void validateAppParameters();
--- a/cmd/configurarParametros.cpp Wed Jul 22 15:09:01 2015 +0000 +++ b/cmd/configurarParametros.cpp Thu Jul 23 22:46:18 2015 +0000 @@ -4,14 +4,14 @@ #define TAMANHO_INVALIDO 0xff #define ERRO_GRAVACAO 0xfe -void cmdConfigurarParametros(unsigned char * dados, unsigned char tamanhoDados){ +void cmdConfigurarParametros(char * dados, unsigned char tamanhoDados){ if(tamanhoDados != sizeof(APP_PARAMETERS)){ exec.enviaResposta(RESP_CONFIGURAR_PARAMETROS, (char)TAMANHO_INVALIDO); return; } - memcpy((unsigned char *)&APP_PARAMETERS, dados, sizeof(APP_PARAMETERS)); + memcpy((char *)&APP_PARAMETERS, dados, sizeof(APP_PARAMETERS)); if(!param.saveParameters()){ printf("[GGS] Erro gravando parametros\r\n");
--- a/cmd/gravarNumeroSerie.cpp Wed Jul 22 15:09:01 2015 +0000 +++ b/cmd/gravarNumeroSerie.cpp Thu Jul 23 22:46:18 2015 +0000 @@ -12,7 +12,7 @@ #define PAYLOAD_INVALIDO -1 //Payload nao possui 8 bytes #define SUCESSO 0 -void cmdGravarNumeroSerie(unsigned char * dados, unsigned char tamanhoDados){ +void cmdGravarNumeroSerie(char * dados, unsigned char tamanhoDados){ if(tamanhoDados != SERIAL_LENGTH){ exec.enviaResposta(RESP_GRAVAR_NUM_SERIE, (char)PAYLOAD_INVALIDO);
--- a/cmd/lerParametros.cpp Wed Jul 22 15:09:01 2015 +0000 +++ b/cmd/lerParametros.cpp Thu Jul 23 22:46:18 2015 +0000 @@ -1,5 +1,5 @@ #include "appCommands.h" void cmdLerParametros(){ - exec.enviaResposta(RESP_LER_PARAMETROS, (unsigned char *)&APP_PARAMETERS, sizeof(APP_PARAMETERS)); + exec.enviaResposta(RESP_LER_PARAMETROS, (char *)&APP_PARAMETERS, sizeof(APP_PARAMETERS)); }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cmd/lerRelogio.cpp Thu Jul 23 22:46:18 2015 +0000 @@ -0,0 +1,6 @@ +#include "appCommands.h" + +void cmdLerRelogio(){ + unsigned int t = time(NULL); + exec.enviaResposta(RESP_LER_VERSAO, (char*) &t, 4); +}
--- a/cmd/lerVersao.cpp Wed Jul 22 15:09:01 2015 +0000 +++ b/cmd/lerVersao.cpp Thu Jul 23 22:46:18 2015 +0000 @@ -2,5 +2,5 @@ #include "version.h" void cmdLerVersao(){ - exec.enviaResposta(RESP_LER_VERSAO, (unsigned char *)&version, VERSION_LENGTH); + exec.enviaResposta(RESP_LER_VERSAO, (char *)&version, VERSION_LENGTH); }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cmd/mmTensao.cpp Thu Jul 23 22:46:18 2015 +0000 @@ -0,0 +1,24 @@ +#include "appCommands.h" +#include "sensor.h" +#include "memoriaMassa.h" + +#define MM_TENSAO_FALHA 1 + +extern MemoriaMassa mm; + +void cmdMMTensao(unsigned char lineOrLoad){ + int channel; + if(lineOrLoad == APP_PARAMETERS.LOAD_CHANNEL){ + channel = 0; + } else{ + channel = 1; + } + + char* resp = mm.getMM(channel); + if(resp == NULL){ + exec.enviaResposta(RESP_MM_TENSAO, MM_TENSAO_FALHA); + } else{ + exec.enviaResposta(RESP_MM_TENSAO, resp, 8*APP_PARAMETERS.MM_SIZE); + } + free(resp); +}
--- a/cmd/tensaoInstantanea.cpp Wed Jul 22 15:09:01 2015 +0000 +++ b/cmd/tensaoInstantanea.cpp Thu Jul 23 22:46:18 2015 +0000 @@ -8,13 +8,13 @@ } else{ channel = 1; } - float tensao = getTensaoInstantanea(channel); - int tensaomv = tensao*1000.0; - unsigned char resp[4]; - resp[0] = tensaomv & 0xff; - resp[1] = (tensaomv >> 8) & 0xff; - resp[2] = (tensaomv >> 16) & 0xff; - resp[3] = (tensaomv >> 24) & 0xff; - exec.enviaResposta(RESP_TENSAO_INSTANTANEA, resp, 4); + union{ + float tensao; + char bytes[4]; + } resp; + + resp.tensao = getTensaoInstantanea(channel); + + exec.enviaResposta(RESP_TENSAO_INSTANTANEA, resp.bytes, 4); }
--- a/main.cpp Wed Jul 22 15:09:01 2015 +0000 +++ b/main.cpp Thu Jul 23 22:46:18 2015 +0000 @@ -3,8 +3,9 @@ static Watchdog wd; static Ticker tickerWd; SerialNumber sn; -Parameters param ((unsigned char *)&APP_PARAMETERS, sizeof(APP_PARAMETERS)); -CommandExecutor exec(APP_PARAMETERS.INTERVALO_WD_NETWORK_S, APP_PARAMETERS.INTERVALO_SILENCIO_S); +Parameters param ((char *)&APP_PARAMETERS, sizeof(APP_PARAMETERS)); +CommandExecutor exec; +MemoriaMassa mm; int main() { @@ -15,13 +16,17 @@ param.loadParameters(); printParameters(); + + mm.iniciaLeituras(APP_PARAMETERS.INTERVALO_MM_S, APP_PARAMETERS.MM_SIZE); + ggsStream.baud(19200); exec.carregaEndereco(sn.get()); - exec.iniciaExecutorComandos(); + exec.iniciaExecutorComandos(APP_PARAMETERS.INTERVALO_WD_NETWORK_S, APP_PARAMETERS.INTERVALO_SILENCIO_S); detectaCargaSemCorte(); - + while(1){ exec.trataPacoteGGS(); + mm.executaLeitura(); } } @@ -51,7 +56,7 @@ void printParameters(){ printf("Parameters:"); for(int i=0; i < sizeof(APP_PARAMETERS); i++){ - printf(" %02x", ((unsigned char *)&APP_PARAMETERS)[i]); + printf(" %02x", ((char *)&APP_PARAMETERS)[i]); } printf("\r\n"); }
--- a/main.h Wed Jul 22 15:09:01 2015 +0000 +++ b/main.h Thu Jul 23 22:46:18 2015 +0000 @@ -11,6 +11,7 @@ #include "v2hardware.h" #include "ggs.h" #include "sensor.h" +#include "memoriaMassa.h" ParametersBlock APP_PARAMETERS;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/memoriaMassa.cpp Thu Jul 23 22:46:18 2015 +0000 @@ -0,0 +1,36 @@ +#include "memoriaMassa.h" + +void MemoriaMassa::iniciaLeituras(int periodoSec, int size){ + mmPeriodoSec = periodoSec; + queue[0].init(size, 3); + queue[1].init(size, 4); + leituraPeriodica.attach(this, &MemoriaMassa::disparaLeitura, mmPeriodoSec); +} + +void MemoriaMassa::disparaLeitura(){ + lerAgora = true; +} + +void MemoriaMassa::executaLeitura(){ + if(!lerAgora){ + return; + } + + LeituraMM leitura; + leitura.value.floatValue = getTensaoInstantanea(0); + leitura.time = time(NULL); + queue[0].add((char*)&leitura); + + leitura.value.floatValue = getTensaoInstantanea(1); + queue[1].add((char*)&leitura); + + lerAgora = false; +} + +char* MemoriaMassa::getMM(int channel){ + if(channel < 0 || channel > 1){ + printf("[MM] Invalid channel %d\r\n", channel); + return NULL; + } + return queue[channel].getAll(); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/memoriaMassa.h Thu Jul 23 22:46:18 2015 +0000 @@ -0,0 +1,32 @@ +#ifndef MM_H +#define MM_H + +#include "mbed.h" +#include "queue.h" +#include "sensor.h" + +typedef struct{ + unsigned int time; + union{ + float floatValue; + unsigned char bytes[4]; + } value; +} LeituraMM; + +class MemoriaMassa { + + Ticker leituraPeriodica; + int mmPeriodoSec; + bool lerAgora; + + PersistentCircularQueue queue[2]; + + public: + void iniciaLeituras(int periodoSec, int size); + void disparaLeitura(); + void executaLeitura(); + char* getMM(int channel); + +}; + +#endif
--- a/settings.h Wed Jul 22 15:09:01 2015 +0000 +++ b/settings.h Thu Jul 23 22:46:18 2015 +0000 @@ -22,5 +22,7 @@ #define DEFAULT_SAMPLES_ANG_COEF 2937.9035f #define DEFAULT_SAMPLES_LIN_COEF -1466.6749f #define DEFAULT_LOAD_CHANNEL 0 // Canal onde esta ligada a carga +#define DEFAULT_INTERVALO_MM_S 60 +#define DEFAULT_MM_SIZE 10 #endif
--- a/version.h Wed Jul 22 15:09:01 2015 +0000 +++ b/version.h Thu Jul 23 22:46:18 2015 +0000 @@ -5,10 +5,10 @@ #ifdef TEST_MODE #define VERSION_LENGTH 20 - static const char version[] = "SmartRamal v0.4 TST"; + static const char version[] = "SmartRamal v0.5 TST"; #else #define VERSION_LENGTH 16 - static const char version[] = "SmartRamal v0.4"; + static const char version[] = "SmartRamal v0.5"; #endif #endif