Code covered by the BSD License  

Highlights from
MATLAB GUI Example for Agilent N8241A Arbitrary Waveform Generator

image thumbnail

MATLAB GUI Example for Agilent N8241A Arbitrary Waveform Generator

by

 

16 Sep 2007 (Updated )

Agilent N8241A MATLAB GUI

agilentFcnGen(visaResourceString)
function agilentFcnGen(visaResourceString)
% AGILENTFCNGEN  Controls an Agilent N8241A Arbitrary Waveform Generator
%  agilentFcnGen(S) opens a GUI for controlling an Agilent N8241A AWG,
%      where S is the VISA resource string for the instrument. 
%
%  agilentFcnGen('simulate') uses a "simulated instrument" (an additional
%      figure window that displays the expected output and status of the
%      AWG).
%  
%  Examples:
%   % Starts a GUI connected to the instrument
%   % with the specified visa resource string 
%   agilentFcnGen('TCPIP0::A-N8241A-80238::inst0::INSTR')
%
%   % Start a GUI with a simulated instrument
%   agilentFcnGen('simulate')

if ~isempty(findobj('tag', 'AgilentFcnGenGUI'))
    disp('GUI is already running');
    return;
end

handles = guihandles(fcngen_gui);

%------

if strcmp(visaResourceString, 'simulate')
%   uiwait(msgbox('Using simulated instrument', '', 'modal'));
    fcnGen = agilentN8241A_simulator(visaResourceString);
else
    fcnGen = agilentN8241A(visaResourceString);
end

waveShapes = {'Sinusoid', 'Sinusoid-4', 'Sinusoid-8', 'AM', 'Pulses'};
waveFcns = genWaveForms;

% The Arb puts out the samples at 1.25 Gigasamples per sec.
samplingRate = 1250e6; 

%------
% paramScan, if nonempty, indicates that the GUI is in "paramter Scan" mode
% (see pscan_startScanCallback). The timer is used to periodically step through 
% the parameter levels.

currentWaveform = [];
AWGIsGenerating = false; % true if the AWG is generating a signal
paramScan = [];
timerObj = timer('period', 1, 'executionMode', 'fixedRate', ...
                 'timerFcn', @pscan_timerCallback);

%------

fprintf('Initializing instrument ...\n');
fcnGen.initialize();
fprintf('Initialization done.\n');

initializeGUI();


%% -------------------------------------------------
    function initializeGUI
        %---------------
        % generate waveform
        
        set(handles.figure1, 'name', 'MATLAB + Instrument Control Toolbox', ...
            'deleteFcn', @figureDeleteCallback, 'tag', 'AgilentFcnGenGUI', ...
            'handleVisibility', 'on');
        
        set(handles.genwf_waveShapePopup, ...
            'String',  waveShapes, ...
            'callback', @genwf_waveShapeCallback);
        
        handles.genwf_startFreq = editSliderCombo(handles.genwf_startFreqEdit, ...
                                                  handles.genwf_startFreqSlider, ...
                                                  5, 20500, 5000, 200, ...
                                                  @genwf_updateCallback);

        set(handles.genwf_startstopButton, 'callback', @genwf_startStopCallback);
        
        % invoke the callback once to put the system in a consistent state
        genwf_waveShapeCallback(handles.genwf_waveShapePopup, []);
        % create the default waveform
        genwf_updateCallback(); 
        
        %---------------
        % parameter scan 

        handles.pscan_startFreq = editSliderCombo(handles.pscan_startFreqEdit, ...
                                                  handles.pscan_startFreqSlider, ...
                                                  5, 5005, 500, 100, ...
                                                  @genwf_updateCallback);
        handles.pscan_endFreq   = editSliderCombo(handles.pscan_endFreqEdit, ...
                                                  handles.pscan_endFreqSlider, ...
                                                  5, 5005, 3000, 100, ...
                                                  @genwf_updateCallback);
        handles.pscan_incrFreq  = editSliderCombo(handles.pscan_incrFreqEdit, ...
                                                  handles.pscan_incrFreqSlider, ...
                                                  100, 600, 100, 100, ...
                                                  @genwf_updateCallback);
                
        set(handles.pscan_startScanButton, 'callback', @pscan_startScanCallback);      
        set(handles.resetValuesButton, 'callback', @resetValuesCallback);
        resetValuesCallback(handles.resetValuesButton, []); 
    end

    function resetValuesCallback(hobj, eventdata)
        handles.genwf_startFreq.setValue(300);
        set(handles.genwf_waveShapePopup, 'value', 1);
        handles.pscan_startFreq.setValue(300);
        handles.pscan_endFreq.setValue(2000);
        handles.pscan_incrFreq.setValue(200);
    end

%%
    function figureDeleteCallback(hobj, eventdata)
        if AWGIsGenerating || ~isempty(paramScan)
           fcnGen.stopPlay();
           AWGIsGenerating = false;
           paramScan = [];
        end        
        if strcmp(get(timerObj,'running'), 'on')
            stop(timerObj);
        end        
        delete(timerObj);
        fcnGen.close();
    end

%% --------------------------------------------------
% When the "Start Generation" button is clicked, the generation is started
% and the button is changed to say "Stop Generation"
    function genwf_startStopCallback(hobj, eventdata)
        if AWGIsGenerating
           fcnGen.stopPlay();
           AWGIsGenerating = false;
           setGUIInpuStatus('enable');
           set(hobj, 'string', 'Start Generation!', 'foregroundColor', 'k');
        else             
            % not currently generating, so start doing it
            set(hobj, 'string', 'Stop Generation!', 'foregroundColor', 'r');
            fcnGen.setWaveform(currentWaveform*0.6);            
            fcnGen.startPlay();
            setGUIInpuStatus('disable');
            AWGIsGenerating = true;
        end
    end



%% --------------------------------------------------
% The parameter scan is implemented as follows:
% 1) When "Start Parameter Scan" button is pressed, the PSCAN variable is
%    initialized to the sequence of frequencies
% 2) The buttons are disabled so that the user cannot change the values
%    during the parameter scan
% 3) A timer is initialized to periodically change the parameter to the
%    next leve.
    function pscan_startScanCallback(hobj, eventdata) %#ok<INUSD>
        if isempty(paramScan)
            % starting up parameter scanning; turn off the generation
            if AWGIsGenerating
                genwf_startStopCallback(handles.genwf_startstopButton, []);
            end

            startFreq = handles.pscan_startFreq.getValue()*1e3; % convert to Hz
            endFreq = handles.pscan_endFreq.getValue()*1e3;
            incrFreq = handles.pscan_incrFreq.getValue()*1e3;
            
            paramScan.freqSteps = startFreq:incrFreq:endFreq;
            if isempty(paramScan.freqSteps)
                paramScan.freqSteps = startFreq;
            end
            
            paramScan.stepCount = 0;
            setGUIInpuStatus('disable');          
            set(handles.genwf_startstopButton, 'enable', 'off');            
            set(hobj, 'string', 'Cancel scan', 'foregroundcolor', 'r');
            
            start(timerObj);
        else % stop an ongoing parameter scan
            paramScan = [];
            stop(timerObj);
            fcnGen.stopPlay();

            setGUIInpuStatus('enable');          
            set(handles.genwf_startstopButton, 'enable', 'on');            
            set(handles.pscan_startScanButton, 'string', 'Start Parameter Scan', 'foregroundcolor', 'k');        
        end

    end

%% This is called on each timer tick
    function pscan_timerCallback(hobj,eventdata)
        if ~isempty(paramScan)
            if paramScan.stepCount >= length(paramScan.freqSteps)
                % reached the limit, wrap up
                pscan_startScanCallback(handles.pscan_startScanButton, []);
            else
                % not reached the limit, continue iterating
                paramScan.stepCount = paramScan.stepCount + 1;
                startFreq = paramScan.freqSteps(paramScan.stepCount);

                handles.genwf_startFreq.setValue( startFreq/1e3 ); % convert to kHz
                genwf_updateCallback(); % create the waveform

                fcnGen.stopPlay();
                fcnGen.setWaveform(currentWaveform);
                fcnGen.startPlay();
            end
        end        
        
    end


%% -------------------------------------------------
% This is called when the wave shape is changed
    function genwf_waveShapeCallback(hobj, eventdata) %#ok<INUSD>
        genwf_updateCallback();
    end


% This called whenever the wave shape is changed, or the frequency
% parameter is updated. It generates the new waveform & stores it, BUT
% DOES NOT SEND IT TO THE AWG. 
    function genwf_updateCallback
        waveShape = waveShapes{get(handles.genwf_waveShapePopup, 'value')};
        startFreq = get(handles.genwf_startFreqSlider, 'value')*1e3;
        
        switch waveShape
            case 'Sinusoid'
                currentWaveform = waveFcns.genSinusoid(samplingRate, startFreq, 2); 
            case 'Square', % not currently implemented in GUI
                currentWaveform = waveFcns.genSquareWave(samplingRate, startFreq, 2, 50); 
            case 'Sinusoid-4', % a wave with 4 arbitrary sinusoid components
                currentWaveform = waveFcns.genSineNComponents(samplingRate, startFreq, ...
                                   2, [1 .75 .5 1], [pi/2 0 pi/2 pi/2]);
            case 'Sinusoid-8', % a wave with 8 arbitrary sinusoid components
                currentWaveform = waveFcns.genSineNComponents(samplingRate, startFreq, 2, ...
                    [1 .5 .5 .5 .25 .25 .25], [pi/2 0 pi/2 0 pi/2 0 pi/2]);      
            case 'AM', % amplitude modulated wave
                currentWaveform = waveFcns.amplitudeModulation(samplingRate, startFreq, 2);
            case 'Pulses', % a wave with a sequence of pulses
                currentWaveform = waveFcns.genSquareWave(samplingRate, startFreq, 2, 5); 
            otherwise
                error('unknown waveform');
        end
        
    end

%% Helper function: enables/disables the various input elements in the GUI
    function setGUIInpuStatus(status)
        if strcmp(status,'enable');
            handles.pscan_startFreq.setStatus('enable');
            handles.pscan_endFreq.setStatus('enable');
            handles.pscan_incrFreq.setStatus('enable');
            handles.genwf_startFreq.setStatus('enable');
            set(handles.genwf_waveShapePopup, 'enable', 'on');
        else
            handles.pscan_startFreq.setStatus('disable');
            handles.pscan_endFreq.setStatus('disable');
            handles.pscan_incrFreq.setStatus('disable');
            handles.genwf_startFreq.setStatus('disable');
            set(handles.genwf_waveShapePopup, 'enable', 'off');                        
        end
    end

%%
end

Contact us