Waner Silva
Published © GPL3+

Oscillogram for Utility Grid

The oscillogram monitor the utility grid, recording the amplitude and frequency values and record the voltage waveform during a disturbance.

AdvancedShowcase (no instructions)12 hours1,210
Oscillogram for Utility Grid

Things used in this project

Story

Read more

Schematics

Voltage measurement circuit

Code

TM4C1294_voltage_monitor.zip

C/C++
Microcontroller code. Is necessary the library TivaWare™ for C Series TI, for LaunchPad.
http://www.ti.com/tool/SW-TM4C
No preview (download only).

voltage_monitor.c

C/C++
Program code, which run on pc (was used linux). This code was tested too in raspberry pi and beaglebone.
//gcc voltage_monitor.c -o voltage_monitor -lm -lpthread
#include <unistd.h>
#include <stdio.h>
#include <time.h>
#include <errno.h>
#include <fcntl.h> 
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <signal.h>
#include <limits.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <pthread.h>
#include <net/if.h>
#include <pthread.h>
#include <math.h>

#define SIZE_BUFFER 8

char buffer_tx[] = {0,0,0,0,0,0,0,0};
char buffer_rx[] = {0,0,0,0,0,0,0,0};

time_t timer;
struct tm *horarioLocal;
int dia, mes, ano, hora, min, sec;

volatile unsigned short int msg_receive = 0, nbytes = 0;
volatile unsigned short int running = 1;
unsigned short int nwrite;
unsigned short int nread;
pthread_t thread_id;
char *portname = "/dev/ttyACM0";
int handle;	
int n = SIZE_BUFFER;

void signal_Handler(int sign){
	int o_exit = 0;
	switch (sign){
		case SIGINT:{
			o_exit = 1;
			break;
		}
		case SIGTERM:{
			o_exit = 1;
			break;
		}
		default:{
			break;
		}
	}

	if (o_exit == 1)
	running = 0;
}

void *uart_msg_receive(void *t){
	while(running){
		for(n = 0; n < SIZE_BUFFER; n++){
			nread = read(handle,&buffer_rx[n],1);
			/*if(nread < 0){
				printf("Read error\n");
			}else{
				printf("Data (%d) read = %d \n",n,buffer_rx[n]);
			}*/		
		}		
        msg_receive = 1;
        usleep(1000);
        while(msg_receive);
    }
}

int set_interface_attribs(int fd, int speed){
    struct termios tty;
    if (tcgetattr(fd, &tty) < 0) {
        printf("Error from tcgetattr: %s\n", strerror(errno));
        return -1;
    }
    cfsetospeed(&tty, (speed_t)speed);
    cfsetispeed(&tty, (speed_t)speed);
    tty.c_cflag |= (CLOCAL | CREAD);	/* ignore modem controls */
    tty.c_cflag &= ~CSIZE;
    tty.c_cflag |= CS8;					/* 8-bit characters */
    tty.c_cflag &= ~PARENB;				/* no parity bit */
    tty.c_cflag &= ~CSTOPB;				/* only need 1 stop bit */
    tty.c_cflag &= ~CRTSCTS;			/* no hardware flowcontrol */
    /* setup for non-canonical mode */
    tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
    tty.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
    tty.c_oflag &= ~OPOST;
    /* fetch bytes as they become available */
    tty.c_cc[VMIN] = 1;
    tty.c_cc[VTIME] = 1;
    if (tcsetattr(fd, TCSANOW, &tty) != 0) {
        printf("Error from tcsetattr: %s\n", strerror(errno));
        return -1;
    }
    return 0;
}

void set_mincount(int fd, int mcount){
    struct termios tty;
    if (tcgetattr(fd, &tty) < 0) {
        printf("Error tcgetattr: %s\n", strerror(errno));
        return;
    }
    tty.c_cc[VMIN] = mcount ? 1 : 0;
    tty.c_cc[VTIME] = 5;        /* half second timer */
    if (tcsetattr(fd, TCSANOW, &tty) < 0)
        printf("Error tcsetattr: %s\n", strerror(errno));
}

void update_time(void){
    time(&timer);
	horarioLocal = localtime(&timer);
	dia = horarioLocal->tm_mday;
	mes = horarioLocal->tm_mon + 1;
	ano = horarioLocal->tm_year + 1900;
	hora = horarioLocal->tm_hour;
	min  = horarioLocal->tm_min;
	sec  = horarioLocal->tm_sec;
}


int main(void){	

	/////
	unsigned int i = 0;
	unsigned char c = 0, wave_complete = 0;
	float v_vector[2048], f = 0, v = 0;
	/////
	char query[512];
	FILE *dataloger;

	handle = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
    if (handle < 0) {
		printf("Error opening %s: %s\n", portname, strerror(errno));
        return -1;
    }    
    set_interface_attribs(handle, B115200);// baudrate 115200, 8 bits, no parity, 1 stop bit
   	
	if( pthread_create(&thread_id, NULL, &uart_msg_receive, NULL) ){
		printf("\nErro thread\n");
    }
    
	for(i = 0; i < 2048; i++){
		v_vector[i] = 0.0;
	}
	
	signal(SIGINT,  signal_Handler);
    signal(SIGTERM, signal_Handler);
	time(NULL);
	
	while (running){
		if(msg_receive){
			if( (buffer_rx[0] & 0x0010) == 0x0010 ){
				i = ((buffer_rx[0] & 0x0007) << 8) + (buffer_rx[1] & 0x00FF);
				for(c = 2; c < 8; c++){
					if(i < 2048){
						if( (buffer_rx[c] & 0x80) == 0x80)
							v_vector[i] = -0.01*( (float)((0xFF ^ buffer_rx[c]) + 1) );
						else
							v_vector[i] = 0.01*((float)buffer_rx[c]);
						i++;
					}				
				}
				
				if(i == 2048){					
					wave_complete = 1;
				}
			}

			if( (buffer_rx[0] & 0x0020) == 0x0020 ){
				if( (buffer_rx[2] & 0x80) == 0x80)
					v = -0.01*( (float)((( 0xFF ^ buffer_rx[2])<<8) + ( 0xFF ^ buffer_rx[3]) + 1 ) );
				else
					v = 0.01*( (float)((buffer_rx[2]<<8) + (buffer_rx[3]) ) );

				if( (buffer_rx[4] & 0x80) == 0x80)
					f = -0.01*( (float)((( 0xFF ^ buffer_rx[4])<<8) + ( 0xFF ^ buffer_rx[5]) + 1 ) );
				else
					f = 0.01*( (float)((buffer_rx[4]<<8) + (buffer_rx[5]) ) );
				
				update_time();

				dataloger = fopen("dataloger_1.txt","a");
				if(dataloger == NULL){
					printf("\nErro dataloger\n!");
					exit(1);
				}			
				sprintf(query,"%d,%d,%d,%d,%d,%d,%.2f,%.2f\n",dia,mes,ano,hora,min,sec,v,f);
				fprintf(dataloger,"%s",query);			
				
				fclose(dataloger);				
			}

			memset(buffer_rx,0,sizeof(buffer_rx));
			msg_receive = 0;
		}
		
		if(wave_complete){
			update_time();

			dataloger = fopen("dataloger_0.txt","a");
			if(dataloger == NULL){
				printf("\nErro dataloger\n!");
                exit(1);
			}			
			for(i = 0; i < 2048; i++){				
				sprintf(query,"%d,%d,%d,%d,%d,%d,%d,%.3f\n",dia,mes,ano,hora,min,sec,i,v_vector[i]);
				fprintf(dataloger,"%s",query);			
			}
			fclose(dataloger);
			memset(buffer_tx,0,sizeof(buffer_rx));
			buffer_tx[0] = 0xF1;
			nwrite = write(handle,buffer_tx,8);
			tcdrain(handle);
			for(i = 0; i < 2048; i++){
				v_vector[i] = 0.0;
			}
			wave_complete = 0;
		}
		
		usleep(5000);
	}
	close(handle);
	printf("\nStop program\n");
    return 0;	
}

Credits

Waner Silva

Waner Silva

6 projects • 13 followers
I am electronic enginer

Comments