Skip to content

Commit

Permalink
Merge pull request #15 from HevertonLemos/adp
Browse files Browse the repository at this point in the history
Adp
  • Loading branch information
HevertonLemos authored May 26, 2019
2 parents 1cd8f57 + b6de677 commit 2ad7cd5
Show file tree
Hide file tree
Showing 12 changed files with 282 additions and 37 deletions.
30 changes: 16 additions & 14 deletions c37118.1/convert_to_struct.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@
%
% Performed the data received to PMU struct for tests.
%
% Usage: phasors = convert_to_struct(struct_received)
%
% struct_received = list of the frames received of the PMU about test;
%
% Return
% phase: struct of the PMU to tests.
% phasors: struct of the PMU to tests.
%
% Heverton de Lemos 13/10/2017
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function phase = convert_to_struct(struct_received)
function phasors = convert_to_struct(struct_received)

VS1 = VC = VB = VA = [];
IS1 = IC = IB = IA = [];

for i=1:length(struct_received)
pmu = getfield(struct_received{i}.pmu, '1');
pmu = getfield(struct_received(i).pmu, '1');

VA(1,i) = pmu.phasors{1}(1);
VA(2,i) = pmu.phasors{1}(2);
Expand All @@ -39,17 +41,17 @@
rocof(i) = pmu.dfreq;
endfor

phase = struct();
phase.("VA") = VA;
phase.("VB") = VB;
phase.("VC") = VC;
% phase.("VS1") = VS1;
% phase.("IA") = IA;
% phase.("IB") = IB;
% phase.("IC") = IC;
% phase.("IS1") = IS1;
phase.("frequency") = freq;
phase.("rocof") = rocof;
phasors = struct();
phasors.("VA") = VA;
phasors.("VB") = VB;
phasors.("VC") = VC;
% phasors.("VS1") = VS1;
% phasors.("IA") = IA;
% phasors.("IB") = IB;
% phasors.("IC") = IC;
% phasors.("IS1") = IS1;
phasors.("frequency") = freq;
phasors.("rocof") = rocof;

return;
endfunction
50 changes: 50 additions & 0 deletions c37118.1/steady_state/pmu_reference_1ph_freq_delta_freq.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Generate 1ph PMU reference to frequency test steady state.
%
% Usage:
%
% magnitude = Reference phasor magnitude;
% initial_frequency = Initial frequency in the test (Hz);
% delta_frequency = The increase/decrease of frequency in Hz for each step;
% number_of_steps = The total amount of frequency steps;
% time_per_step = The time, in seconds, per step;
% frames_per_second = Reference Fps (frames per second);
% initial_angle = Refenrece initial angle;
%
% Return
% phasor: Struct of the PMU reference to frequency steady state.
%
% Heverton de Lemos 24/10/2017
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function phasor = pmu_reference_1ph_freq_delta_freq(magnitude,
initial_frequency,
delta_frequency,
number_of_steps,
time_per_step,
frames_per_second,
initial_angle)
Va = [];
freq = [];
rocof = [];
% implement check of all input values;
lenght_frame_step = ceil(frames_per_second * time_per_step);
for i=1:number_of_steps
instantaneous_frequency = initial_frequency + (i - 1) * delta_frequency;
tmp_Va = ss_generic(magnitude,
initial_angle,
instantaneous_frequency,
frames_per_second,
lenght_frame_step);

Va = [Va tmp_Va];
freq = [freq ones(1, lenght_frame_step) * instantaneous_frequency];
rocof = [rocof zeros(1, lenght_frame_step)];
endfor

phasor = struct();
phasor.("VA") = Va;
phasor.("frequency") = freq;
phasor.("rocof") = rocof;
return;
endfunction
2 changes: 2 additions & 0 deletions c37118.2/generate_command_frame.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Usage: commands = generate_command_frame(idcode, cmd)
%
% Generate the command frame (IEEE C37.118-2).
%
% idcode = identifier of the script;
Expand Down
2 changes: 2 additions & 0 deletions c37118.2/read_cfg.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Usage: cfg = read_cfg(frame_cfg, frame_size)
%
% Generate the cfg struct IEEE C37.118-2.
%
% frame_cfg = frame of the cfg received;
Expand Down
6 changes: 4 additions & 2 deletions c37118.2/read_data.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Usage: data = read_data(frame_data, frame_size, cfg)
%
% Generate the data struct IEEE C37.118-2.
%
% frame_dat = frame of the data received;
Expand All @@ -17,8 +19,8 @@
data.("frame_size") = typecast( fliplr(frame_data(3:4)), "uint16" );
data.("idcode") = typecast(fliplr(frame_data(5:6)), "uint16");
data.("soc") = ctime( typecast( fliplr(frame_data(7:10)), "uint32" ));
data.("fracsec") = typecast( fliplr([frame_data(11) frame_data(12) frame_data(13) uint8(0)]), "single" ) / typecast( cfg.time_base, "single" );
data.("time_quality") = frame_data(14);
data.("fracsec") = typecast(fliplr([uint8(0) frame_data(12) frame_data(13) frame_data(14)]), "single") / typecast( cfg.time_base, "single" );
data.("time_quality") = frame_data(11);
data.("stat") = typecast( fliplr(frame_data(15:16)), "uint16" );

x = 1;
Expand Down
4 changes: 3 additions & 1 deletion communication/connect_socket.m
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% <Description>.
% Usage: socket = connect_socket(local_port, send_ip, send_port)
%
% local_port= local port to open the socket;
% send_ip= addres ip to send data;
% send_port= port to send data;
%
% Return
% socket: socket number to comunication or -1 if connection fail.
%
% <Description>.
%
% Heverton de Lemos 16/10/2017
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand Down
15 changes: 9 additions & 6 deletions communication/receive_cfg.m
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Usage: data = receive_cfg(rcv_sck, type_frame)
%
% UDP Socket for reception of the cfg frame.
%
% rcv_sck= number of the socket open.
% type_frame= 1 = CFG1
% 2 = CFG2
% 3 = CFG3
% rcv_sck = opened socket.
% type_frame = 1 = CFG1
% 2 = CFG2
% 3 = CFG3
%
% Return
% data: frame received by socket.
%
% Heverton de Lemos 16/10/2017
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function data = receive_cfg(rcv_sck, type_frame)

[frame,len] = recv(rcv_sck, 65365);

max_frame_size = 65365;
[frame,len] = recv(rcv_sck, max_frame_size);
switch( type_frame)
case 1
data = read_cfg(frame, len);
Expand Down
14 changes: 10 additions & 4 deletions communication/receive_data.m
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Usage: [data, len] = receive_data(rcv_sck, cfg)
%
% UDP Socket for reception of the data frame.
%
% rcv_sck= number of the socket open.
% cfg= struct cfg
%
% Return
% data: frame received by socket.
% len:
% len: length of last received data. -1 if not received.
%
% Heverton de Lemos 16/10/2017
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [data,len] = receive_data(rcv_sck, cfg)

[frame,len] = recv(rcv_sck, 65365, MSG_DONTWAIT);
function [data, len] = receive_data(rcv_sck, cfg)
data = 0; % avoid exceptions in case of len < 0;
if (isunix())
[frame,len] = recv(rcv_sck, 65365, MSG_DONTWAIT);
else
[frame,len] = recv(rcv_sck, 65365);
endif
if (len != -1)
data = read_data(frame, len, cfg);
endif
Expand Down
6 changes: 4 additions & 2 deletions communication/send_command.m
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Usage: send_command(socket, data)
%
% UDP socket for sending frames.
%
% socket= socket open to send frame.
% data= frame to send.
% socket = socket open to send frame.
% data = frame to send.
%
% Return
%
Expand Down
77 changes: 77 additions & 0 deletions examples/generate_reference.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
clc
clear all

source("../start_lib.m");

start_lib("../");

voltage_magnitude = 115;
current_magnitude = 5;
Initial_frequency = 45;
delta_frequency = 1;
time_per_step = 2; % two seconds
number_of_steps = 11;
initial_angle = 0;
frames_per_second = 50;
time = linspace(0, number_of_steps * time_per_step,
frames_per_second * number_of_steps * time_per_step);
phasor = struct();
tmp_phasor = pmu_reference_1ph_freq_delta_freq(voltage_magnitude,
Initial_frequency,
delta_frequency,
number_of_steps,
time_per_step,
frames_per_second,
initial_angle);
phasor.VA = tmp_phasor.VA;
phasor.frequency = tmp_phasor.frequency;
phasor.rocof = tmp_phasor.rocof;
tmp_phasor = pmu_reference_1ph_freq_delta_freq(voltage_magnitude,
Initial_frequency,
delta_frequency,
number_of_steps,
time_per_step,
frames_per_second,
initial_angle + 120);
phasor.VB = tmp_phasor.VA;
tmp_phasor = pmu_reference_1ph_freq_delta_freq(voltage_magnitude,
Initial_frequency,
delta_frequency,
number_of_steps,
time_per_step,
frames_per_second,
initial_angle - 120);

phasor.VC = tmp_phasor.VA;
tmp_phasor = pmu_reference_1ph_freq_delta_freq(current_magnitude,
Initial_frequency,
delta_frequency,
number_of_steps,
time_per_step,
frames_per_second,
initial_angle);
phasor.IA = tmp_phasor.VA;
tmp_phasor = pmu_reference_1ph_freq_delta_freq(current_magnitude,
Initial_frequency,
delta_frequency,
number_of_steps,
time_per_step,
frames_per_second,
initial_angle + 120);
phasor.IB = tmp_phasor.VA;
tmp_phasor = pmu_reference_1ph_freq_delta_freq(current_magnitude,
Initial_frequency,
delta_frequency,
number_of_steps,
time_per_step,
frames_per_second,
initial_angle - 120);
phasor.IC = tmp_phasor.VA;

plot(time, phasor.VA(1,:));
figure();
plot(time, phasor.VA(2,:));
figure();
plot(time, phasor.frequency);
figure();
plot(time, phasor.rocof);
Loading

0 comments on commit 2ad7cd5

Please sign in to comment.