Saqib Awan
Published © GPL3+

Hardware-in-the-Loop Simulation with MATLAB and Zyob-Z7

HIL Simulation: MATLAB to generate signal from Picoscope, digitize it using XADC(Zybo), receive it back from PS RAM, plot, and FFT in MATLAB

BeginnerFull instructions provided2 hours146
Hardware-in-the-Loop Simulation with MATLAB and Zyob-Z7

Things used in this project

Story

Read more

Schematics

Final Block Design

This is the system level design diagram

Code

MATLAB GUI CODE

MATLAB
This is the matlab GUI code
classdef HILGUI < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure                 matlab.ui.Figure
        RevisedDesignFlowLabel   matlab.ui.control.Label
        Image2                   matlab.ui.control.Image
        PreviousDesignFlowLabel  matlab.ui.control.Label
        Image                    matlab.ui.control.Image
        Label                    matlab.ui.control.Label
        STOPReceptionButton      matlab.ui.control.Button
        StatusLabel              matlab.ui.control.Label
        StartReceptionButton     matlab.ui.control.Button
        UIAxes2                  matlab.ui.control.UIAxes
        UIAxes                   matlab.ui.control.UIAxes
    end

    
    properties (Access = private)
        VoltageData
        timeData
        sampleCount% Description
        ps2000DeviceObj
        serialObj% Description
        stopFlag = false; % Description
        sampleRate; % Description
    end
    

    % Callbacks that handle component events
    methods (Access = private)

        % Button pushed function: StartReceptionButton
        function StartReceptionButtonPushed(app, event)
                instrreset;
                app.VoltageData =[];
                app.timeData=[];
                app.sampleCount=0;
                app.StatusLabel.Text = 'Siganl generation started...';
                app.sampleRate = 1000000;
                %load picoscope configurations
                PS2000Config;

                %Conncet to picoscope device
                app.ps2000DeviceObj = icdevice('picotech_ps2000_generic.mdd');
                connect(app.ps2000DeviceObj);
                % Signal Generator Configuration
                sigGenGroupObj = get(app.ps2000DeviceObj, 'Signalgenerator');
                sigGenGroupObj = sigGenGroupObj(1);
                set(sigGenGroupObj, 'startFrequency', 50000); % Frequency in Hz
                set(sigGenGroupObj, 'offsetVoltage', 400);
                set(sigGenGroupObj, 'peakToPeakVoltage', 800); % Peak-to-peak voltage in mV
                [status.sigGenSimple] = invoke(sigGenGroupObj, 'setSigGenBuiltInSimple', ...
                ps2000Enuminfo.enPS2000WaveType.PS2000_SINE);

                serialPort = '/dev/ttyUSB1';  % Replace with your correct port
                app.serialObj = serial(serialPort, 'BaudRate', 115200, 'Terminator', 'LF', 'Timeout', 10);
                fopen(app.serialObj);  % Open the serial port

                % Real-time data collection loop
                
                    
                while (ishandle(app.UIAxes) && ~app.stopFlag) % Check if the figure is still open
                data = fgetl(app.serialObj);
        
                if ischar(data)
                voltage = str2double(data);
            
                if ~isnan(voltage)
                app.sampleCount = app.sampleCount + 1;
                app.VoltageData = [app.VoltageData, voltage];
                app.timeData = [app.timeData, app.sampleCount];
                
                % Clear the previous plot and plot the new data
                plot(app.UIAxes, app.timeData, app.VoltageData, '-o');
                xlabel(app.UIAxes, 'Time (samples)');
                ylabel(app.UIAxes, 'Voltage (V)');
                title(app.UIAxes, 'Real-Time Voltage Data');
                drawnow;
                % Perform FFT on VoltageData
                n = length(app.VoltageData); % Number of samples
                if n >= 800 % Minimum number of samples to compute FFT (e.g., 1024)
                    % Compute the FFT
                    Y = fft(app.VoltageData);
                    f = (0:n-1)*(app.sampleRate/n);  % Frequency vector

                    % Plot the FFT (amplitude spectrum)
                    amplitude = abs(Y)/n; % Normalized amplitude
                    frequencyPlot = f(1:floor(n/2)); % Only the positive frequencies
                    amplitudePlot = amplitude(1:floor(n/2));

                    % Plot the FFT on a different axis (frequency domain plot)
                    plot(app.UIAxes2, frequencyPlot, amplitudePlot);
                    xlabel(app.UIAxes2, 'Frequency (Hz)');
                    ylabel(app.UIAxes2, 'Amplitude');
                    title(app.UIAxes2, 'Frequency Spectrum');
                    drawnow;
                end
                end
                end
                end

                app.Label.Text = 'Process has been stopped by user....';
        end

        % Button pushed function: STOPReceptionButton
        function STOPReceptionButtonPushed(app, event)
            app.stopFlag = true;
            instrreset;
        end
    end

    % Component initialization
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Get the file path for locating images
            pathToMLAPP = fileparts(mfilename('fullpath'));

            % Create UIFigure and hide until all components are created
            app.UIFigure = uifigure('Visible', 'off');
            app.UIFigure.Position = [100 100 1663 823];
            app.UIFigure.Name = 'MATLAB App';

            % Create UIAxes
            app.UIAxes = uiaxes(app.UIFigure);
            title(app.UIAxes, 'Voltage Time Plot')
            xlabel(app.UIAxes, 'Sample Count')
            ylabel(app.UIAxes, 'Voltage Values')
            zlabel(app.UIAxes, 'Z')
            app.UIAxes.LineWidth = 0.7;
            app.UIAxes.Box = 'on';
            app.UIAxes.Position = [57 333 428 267];

            % Create UIAxes2
            app.UIAxes2 = uiaxes(app.UIFigure);
            title(app.UIAxes2, 'Title')
            xlabel(app.UIAxes2, 'X')
            ylabel(app.UIAxes2, 'Y')
            zlabel(app.UIAxes2, 'Z')
            app.UIAxes2.Position = [564 324 409 276];

            % Create StartReceptionButton
            app.StartReceptionButton = uibutton(app.UIFigure, 'push');
            app.StartReceptionButton.ButtonPushedFcn = createCallbackFcn(app, @StartReceptionButtonPushed, true);
            app.StartReceptionButton.Icon = fullfile(pathToMLAPP, 'button_blank_blue_14984.png');
            app.StartReceptionButton.BackgroundColor = [1 1 1];
            app.StartReceptionButton.FontSize = 18;
            app.StartReceptionButton.Position = [430 689 226 54];
            app.StartReceptionButton.Text = 'Start Reception';

            % Create StatusLabel
            app.StatusLabel = uilabel(app.UIFigure);
            app.StatusLabel.HorizontalAlignment = 'center';
            app.StatusLabel.FontSize = 18;
            app.StatusLabel.FontColor = [0 1 0];
            app.StatusLabel.Position = [432 617 221 39];
            app.StatusLabel.Text = '';

            % Create STOPReceptionButton
            app.STOPReceptionButton = uibutton(app.UIFigure, 'push');
            app.STOPReceptionButton.ButtonPushedFcn = createCallbackFcn(app, @STOPReceptionButtonPushed, true);
            app.STOPReceptionButton.BackgroundColor = [1 0 0];
            app.STOPReceptionButton.FontSize = 18;
            app.STOPReceptionButton.FontColor = [1 1 1];
            app.STOPReceptionButton.Position = [452 213 182 52];
            app.STOPReceptionButton.Text = 'STOP Reception';

            % Create Label
            app.Label = uilabel(app.UIFigure);
            app.Label.FontSize = 18;
            app.Label.Position = [340 150 406 29];
            app.Label.Text = '';

            % Create Image
            app.Image = uiimage(app.UIFigure);
            app.Image.Position = [760 445 1068 298];
            app.Image.ImageSource = fullfile(pathToMLAPP, 'XADC.jpg');

            % Create PreviousDesignFlowLabel
            app.PreviousDesignFlowLabel = uilabel(app.UIFigure);
            app.PreviousDesignFlowLabel.HorizontalAlignment = 'center';
            app.PreviousDesignFlowLabel.FontSize = 24;
            app.PreviousDesignFlowLabel.FontWeight = 'bold';
            app.PreviousDesignFlowLabel.Position = [1158 757 271 31];
            app.PreviousDesignFlowLabel.Text = 'Previous Design Flow';

            % Create Image2
            app.Image2 = uiimage(app.UIFigure);
            app.Image2.Position = [792 35 1004 299];
            app.Image2.ImageSource = fullfile(pathToMLAPP, 'XADCrev.jpg');

            % Create RevisedDesignFlowLabel
            app.RevisedDesignFlowLabel = uilabel(app.UIFigure);
            app.RevisedDesignFlowLabel.HorizontalAlignment = 'center';
            app.RevisedDesignFlowLabel.FontSize = 24;
            app.RevisedDesignFlowLabel.FontWeight = 'bold';
            app.RevisedDesignFlowLabel.Position = [1175 344 273 54];
            app.RevisedDesignFlowLabel.Text = 'Revised Design Flow';

            % Show the figure after all components are created
            app.UIFigure.Visible = 'on';
        end
    end

    % App creation and deletion
    methods (Access = public)

        % Construct app
        function app = HILGUI

            % Create UIFigure and components
            createComponents(app)

            % Register the app with App Designer
            registerApp(app, app.UIFigure)

            if nargout == 0
                clear app
            end
        end

        % Code that executes before app deletion
        function delete(app)

            % Delete UIFigure when app is deleted
            delete(app.UIFigure)
        end
    end
end

Credits

Saqib Awan
3 projects • 5 followers
Hardware Engineer and Embedded Systems Enthusiast | Passionate about SoCs, RFSoCs, and Cutting-Edge Hardware Design
Contact

Comments

Please log in or sign up to comment.