Commit 0a0aa4a2 authored by Lindenberg, Arthur-Vincent's avatar Lindenberg, Arthur-Vincent
Browse files

oscilloscope example developed

parent be22070e
%% alter path to your python (3.8 is tested) distribution here
py_path = 'C:\Users\Arthur-Vincent\AppData\Local\Programs\Python\Python38\python.exe';
broker_ip = '127.0.0.1';
%% Init components
%oscilloscope properties --> delivering mqtt string for RedPitaya Inputs
%mqtt stuff
addpath('../../lib');
addpath('../../lib/redpitaya');
op = OscilloscopeProperties(); % get an object of the OscilloscopeProperties class
% Two options to set sample rate
op = op.set_sample_rate_by_div(1.01e-4); % Set time per Oszilloscope-Div
%op.SampleRate = 15.6e6; % Set samplerate in MHz
op.TriggerDelay =+8192; %Delay in samples (0 delay -> Start writing in the center of the buffer)
op.TriggerLevel = .001; % Trigger level in Volt
test_message = op.get_mqtt_message();
%test_message = '[OSC]\ntrigger_delay=100\ndecimation=1\ntrigger_level=0.2\ntrigger_ch=CH_1\ntrigger_src=CH_1_PE';
myMqtt = mqtt_py_matlab(py_path);
myMqtt.connect_mqtt(broker_ip);
% Subscription to RP1 with wildcard (#)
myMqtt.sub_mqtt_topics(broker_ip, "RP1/#");
% Subscription without wildcard
myMqtt.sub_mqtt_topics(broker_ip, "RP1/CONFIGURE/RECONFIG");
myMqtt.sub_mqtt_topics(broker_ip, "RP1/raw/CH1");
pause(2);
% Publish Redpitaya API message
myMqtt.pub_mqtt(broker_ip, "RP1/CONFIGURE/RECONFIG", test_message);
% Read the last published message
fprintf('%s', myMqtt.get_payload_str("RP1/CONFIGURE/RECONFIG"));
% get max length of x axis
x_axis = op.get_ordinate();
pause(2);
% Start oszilloscope
myMqtt.pub_mqtt(broker_ip, "RP1/CONFIGURE/START", test_message);
%% Filter
Hd = fir_highpass2k;
%% Signal aquisition
%while(1)
% Read input 1
arr = myMqtt.get_payload_float("RP1/raw/CH1");
y_mat = cell2mat(cell(arr));
%filter input 1
y_filt = filter(Hd,y_mat);
% Plot input 1
subplot (2,1,1)
plot(x_axis, y_mat);
grid on;
% Plot filtered input 1
subplot (2,1,2)
plot(x_axis, y_filt);
grid on;
% Stop the aquisition
myMqtt.pub_mqtt(broker_ip, "RP1/CONFIGURE/RECONFIG", "STOP");
%end
function Hd = fir_highpass2k
%FIR_HIGHPASS2K Returns a discrete-time filter object.
% MATLAB Code
% Generated by MATLAB(R) 9.8 and Signal Processing Toolbox 8.4.
% Generated on: 15-Jun-2021 17:24:37
% Equiripple Highpass filter designed using the FIRPM function.
% All frequency values are in Hz.
Fs = 2000; % Sampling Frequency
N = 300; % Order
Fstop = 0.1; % Stopband Frequency
Fpass = 1; % Passband Frequency
Wstop = 1; % Stopband Weight
Wpass = 1; % Passband Weight
dens = 20; % Density Factor
% Calculate the coefficients using the FIRPM function.
b = firpm(N, [0 Fstop Fpass Fs/2]/(Fs/2), [0 0 1 1], [Wstop Wpass], ...
{dens});
Hd = dfilt.dffir(b);
% [EOF]
%% Define Red Pitaya as TCP/IP object
clear all
close all
clc
IP= '169.254.180.42'; % Input IP of your Red Pitaya...
port = 5000;
tcpipObj = tcpip(IP, port);
tcpipObj.InputBufferSize = 16384*32;
%% Open connection with your Red Pitaya
fopen(tcpipObj);
tcpipObj.Terminator = 'CR/LF';
flushinput(tcpipObj);
flushoutput(tcpipObj);
% Set decimation vale (sampling rate) in respect to you
% acquired signal frequency
fprintf(tcpipObj,'ACQ:RST');
fprintf(tcpipObj,'ACQ:DEC 8');
fprintf(tcpipObj,'ACQ:TRIG:LEV 0');
% there is an option to select coupling when using SIGNALlab 250-12
% fprintf(tcpipObj,'ACQ:SOUR1:COUP AC'); % enables AC coupling on channel 1
% by default LOW level gain is selected
% fprintf(tcpipObj,'ACQ:SOUR1:GAIN LV'); % user can switch gain using this command
% Set trigger delay to 0 samples
% 0 samples delay set trigger to center of the buffer
% Signal on your graph will have trigger in the center (symmetrical)
% Samples from left to the center are samples before trigger
% Samples from center to the right are samples after trigger
fprintf(tcpipObj,'ACQ:TRIG:DLY 0');
%% Start & Trigg
% Trigger source setting must be after ACQ:START
% Set trigger to source 1 positive edge
fprintf(tcpipObj,'ACQ:START');
% After acquisition is started some time delay is needed in order to acquire fresh samples in to buffer
% Here we have used time delay of one second but you can calculate exact value taking in to account buffer
% length and smaling rate
pause(1)
fprintf(tcpipObj,'ACQ:TRIG CH1_PE');
% Wait for trigger
% Until trigger is true wait with acquiring
% Be aware of while loop if trigger is not achieved
% Ctrl+C will stop code executing in Matlab
while 1
trig_rsp=query(tcpipObj,'ACQ:TRIG:STAT?')
if strcmp('TD',trig_rsp(1:2)) % Read only TD
break
end
end
% Read data from buffer
signal_str=query(tcpipObj,'ACQ:SOUR1:DATA?');
signal_str_2=query(tcpipObj,'ACQ:SOUR2:DATA?');
% Convert values to numbers.% First character in string is “{“
% and 2 latest are empty spaces and last is “}”.
signal_num=str2num(signal_str(1,2:length(signal_str)-3));
signal_num_2=str2num(signal_str_2(1,2:length(signal_str_2)-3));
plot(signal_num)
hold on
plot(signal_num_2,'r')
grid on
ylabel('Voltage / V')
xlabel('samples')
fclose(tcpipObj)
\ No newline at end of file
......@@ -8,6 +8,7 @@ myMqtt = mqtt_py_matlab(py_path);
myMqtt.connect_mqtt(broker_ip);
pause(1);
%% sending and receiving a string value
myMqtt.sub_mqtt_topics(broker_ip, "hello/test");
myMqtt.pub_mqtt(broker_ip, "hello/test", "WORLD!");
......@@ -15,6 +16,7 @@ pause(1);
fprintf('%s', myMqtt.get_payload_str("hello/test"));
%% sending and receiving a binary value
signal = rand(1, 2000)';
signal_b = typecast(single(signal), 'uint8');
......
classdef OscilloscopeProperties
%OSCILLOSCOPEPROPERTIES Summary of this class goes here
% Detailed explanation goes here
properties (Constant)
sample_rates = [125e6, 15.6e6, 1.953e6, 122.07e3, 15.258e3, 1.907e3];
rp_decimation = [1, 8, 64, 1024, 8192, 65536];
end
properties
SampleRate = 125e6;
TriggerDelay = 0;
TriggerLevel = .2;
TriggerCh = 'CH_1';
TriggerSrc = 'CH_1_PE';
Ch1Mode = 'DC';
Ch2Mode = 'DC';
end
methods
function obj = OscilloscopeProperties()
%OSCILLOSCOPEPROPERTIES Construct an instance of this class
end
function mqtt_message = get_mqtt_message(obj)
%METHOD1 Summary of this method goes here
% Detailed explanation goes here
mqtt_message = ['[OSC]\n',...
'trigger_delay=', obj.get_trigger_delay_rp(),'\n',...
'decimation=', obj.get_decimation_rp(),'\n',...
'trigger_level=', num2str(obj.TriggerLevel),'\n',...
'trigger_ch=', obj.TriggerCh,'\n',...
'trigger_src=', obj.TriggerSrc,'\n',...
'ch1_mode=', obj.Ch1Mode,'\n',...
'ch2_mode=', obj.Ch2Mode];% obj.Property1 + inputArg;
end
function rp_sample_rate = get_sample_rate_rp(obj)
[~, idx] = min(abs(obj.sample_rates - obj.SampleRate));
rp_sample_rate = obj.sample_rates(idx);
end
function rp_sample_time = get_sample_time_rp(obj)
rp_sample_time = 16384/obj.get_sample_rate_rp();
end
function dec_rp = get_decimation_rp(obj)
idx = find(obj.sample_rates == obj.get_sample_rate_rp(), 1);
dec_rp = num2str(obj.rp_decimation(idx));
end
function trg_del_rp = get_trigger_delay_rp(obj)
trg_del_smpl_num = obj.TriggerDelay*obj.SampleRate;
trg_del_smpl_num = max(-8191, min(8192, trg_del_smpl_num));
trg_del_rp = num2str(round(trg_del_smpl_num, 0));
end
function obj = set_sample_rate_by_div(obj, time_per_div)
window_lengths = (8192*2+1)./(obj.sample_rates);
div_lengths = window_lengths/10;
[~, idx] = min(abs(div_lengths - time_per_div));
obj.SampleRate = obj.sample_rates(idx);
end
function ordinate = get_ordinate(obj)
trg_del = double(string(obj.get_trigger_delay_rp()));
o_start = (-8191+trg_del)/obj.get_sample_rate_rp();
o_stop = (8192+trg_del)/obj.get_sample_rate_rp();
ordinate = o_start:1/obj.get_sample_rate_rp():o_stop;
end
end
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment