Main Content

Model a Communications Protocol by Using Chart Objects

这个例子展示了如何使用一个独立的Stateflow® chart to model a frame-synchronization and symbol-detection component in a communications system. Standalone charts implement classic chart semantics with MATLAB® as the action language. You can program the chart by using the full functionality of MATLAB, including those functions that are restricted for code generation in Simulink®. For more information, seeCreate Stateflow Charts for Execution as MATLAB Objects.

及其ment a Symbol-Detection Algorithm

In this example, the input to the communications system consists of a binary signal of zeros and ones received every 10 milliseconds. The input signal can contain any combination of:

  • A 770-ms pulse (77 consecutive ones) to mark the beginning and end of a frame of data and to ensure system synchronization.

  • A 170-ms pulse (17 consecutive ones) to indicate symbol A.

  • A 470-ms pulse (47 consecutive ones) to indicate symbol B.

The filesf_frame_search.sfxdefines a standalone Stateflow chart that implements this communication protocol. The chart consists of two outer states in parallel decomposition. TheInitializestate resets the value of the local datasymbolat the start of each execution step. TheSearchstate contains the logic that defines the symbol-detection algorithm. When this state detects one of the pulses allowed by the communication protocol, the name of the corresponding symbol is stored assymboland displayed in the MATLAB Command Window. Parallel decomposition enables the chart to preprocess the input data. For more information, seeDefine Exclusive and Parallel Modes by Using State Decomposition.

To track the length of a pulse through several execution steps, the chart uses thecountoperator. This operator simplifies the design of the chart by eliminating the need for a manual counter. For example, the condition[count(pulse)==17]guards the outgoing transition from the substateNewFrame. This condition becomes true when the datapulseis one for 17 consecutive execution steps. In this case, the chart transitions to theCouldBeAsubstate. If this transition is followed by an input of zero, then the chart registers the reception of symbol A and transitions back to theNewFramesubstate. Otherwise, the chart transitions to theSearchForBstate from which the condition[count(pulse)==29]searches for an additional 29 ones to mark symbol B.

Execute Standalone Chart

在MATLAB脚本sf_frame_tester.m, the sample code generates a short signal consisting of several valid pulses and one transmission error. The error consists of a 470-ms pulse that is too long to represent symbol A and too short to represent symbol B.

%% Test Symbol Detection Algorithm% Generate a short signal consisting of several valid pulses and one% transmission error.f = sf_frame_search(pulse=0);% create chart objectsendPulse(f,77);% frame markersendPulse(f,17);% AsendPulse(f,47);% BsendPulse(f,37);% transmission errorsendPulse(f,47);% BsendPulse(f,17);% AsendPulse(f,77);% frame marker删除(f);% delete chart objectfunctionsendPulse(f,n)% Send a pulse of n ones and one zero to chart object f.fori = 1:n step(f,pulse=1); printDot(1)endprintDot(0) step(f,pulse=0);functionprintDot(x)persistentkifisempty(k) k = 1;endifx == 0 fprintf("\n"); k = 1;elseifk == 50 fprintf(".\n"); k = 1;elsefprintf("."); k = k+1;endendend

Running the script produces these results in the MATLAB Command Window:

.................................................. ........................... frame ................. A ............................................... B ..................................... error ............................................... B ................. A .................................................. ........................... frame

During the simulation, the chart animation provides a visual indication of the runtime behavior of the algorithm.

See Also

Related Topics