This example shows how to do freerun signal tracing using an xPC Target™ host scope. After the script builds and downloads the oscillator model, xpcosc, to the target computer, it adds a scope of type 'host' to the target application and the signals 'Integrator1' and 'Signal Generator' to the scope. The application is started and the host scope is used for data acquisition and display. Note:
The model sample time is 250 usec.
The scope is set to acquire 200 samples with a decimation factor of 4.
This corresponds to a display length of 250e-6 * 200 * 4 = 0.2 seconds.
The scope is started in Freerun mode, and its status is monitored until it reaches the 'Finished' state. Next, the scope data is uploaded to the host computer and plotted. This process repeats 25 times. After every fifth run, the damping gain 'Gain1/Gain' is set to a new random value.
Use 'xpctargetping' to test for a proper host-target connection.
if ~strcmp(xpctargetping, 'success') error(message('xPCTarget:examples:Connection')); end
Open the oscillator model, xpcosc. Under the model's configuration parameter xPC Target option settings, the system target file has been set to xpctarget.tlc. Hence, building the model will create an executable image, xpcosc.dlm, that can be run on a PC booted with the xPC Target kernel.
Build the model and download the image, xpcosc.dlm, to the target computer.
set_param('xpcosc','RTWVerbose','off'); % Configure for a non-Verbose build. rtwbuild('xpcosc'); % Build and download application.
### Starting xPC Target build procedure for model: xpcosc ### Generated code for 'xpcosc' is up to date because no structural, parameter or code replacement library changes were found. ### Successful completion of build procedure for model: xpcosc ### Looking for target: TargetPC1 ### Download model onto target: TargetPC1
Create the MATLAB variable, tg, containing the xPC Target Object. This object contains all information needed to communicate with and control the target computer.
tg = xpc; % Create an xPC Target Object tg.SampleTime = 0.000250; % Set sample time to 250us tg.StopTime = 10000; % Set stop time to a high value (10000s) start(tg); % Start model execution
Create, configure, and plot to the host scope during each run.
tPar = getparamid(tg, 'Gain1', 'Gain'); % Get indx of parameter 'Gain1/Gain' signals(1) = getsignalid(tg, 'Integrator1'); % Get indx of signal 'Integrator1' signals(2) = getsignalid(tg, 'Signal Generator'); % Get indx of signal 'Signal Generator' sc = addscope(tg, 'host'); % Define (add) a host scope object addsignal(sc, signals); % Add signals to signal list of scope object sc.NumSamples = 200; % Set number of samples sc.Decimation = 4; % Set decimation factor sc.TriggerMode = 'Freerun'; % Set trigger mode figh = findobj('Name', 'scfreerundemo');% Does the plot figure exist? if isempty(figh) figh = figure; % No: Create figure set(figh, 'Name', 'scfreerundemo', 'NumberTitle', 'off'); else figure(figh); % Yes: Make it the current figure end % Loop to acquire 25 data packages from the scope object. m = 1; flag = 0; for n = 1 : 25 if isempty(find(get(0, 'Children') == figh, 1)), flag = 1; break; end % Change parameter Gain1/Gain every fifth acquisition loop % to a random value between 0 and 2000. if ~m setparam(tg, tPar, 2*1000*rand); end m = rem(m + 1, 5); start(sc); % Start scope object % Wait until scope-object has 'finished' acquiring all data. while ~strcmpi(sc.Status,'finished'); end; % Create time vector, upload scope data and display it. t = sc.Time; % Upload time vector plot(t,sc.Data); % Upload acquired data and plot title(['scfreerundemo: ',num2str(n),' of 25 data packages']); set(gca,'XLim',[t(1), t(end)]); set(gca,'YLim',[-10, 10]); drawnow; end if ~flag, title('scfreerundemo: finished'); end
When done, stop the application and close the model.
stop(tg); % Stop model close_system('xpcosc',0); % Close model % EOF scfreerundemo.m