You can use external mode simulations for rapid prototyping. An external mode simulation establishes a communication channel between Simulink® on your development computer (host) and the target hardware that runs the executable file created by the code generation and build process.
Through the communication channel, you can:
Modify or tune block parameters in real time. When you change parameters in your model, Simulink downloads the new values to the executing target application.
Monitor and save signal data from the executing target application.
The low-level transport layer of the channel handles the transmission of messages. Simulink and the generated model code are independent of this layer. The transport layer and its interface code are isolated in separate modules that format, transmit, and receive messages and data packets.
Set up and run an external mode simulation that uses a TCP/IP or serial (RS-232) communication channel.
Create and configure a simple model.
Build the target executable file.
Run the target application.
You do not have to be familiar with the algorithm in the example to complete the tutorial.
The Mandelbrot set is the region in the complex plane consisting of the values z0 for which the trajectories defined by this equation remain bounded at k→∞.
The overall geometry of the Mandelbrot set is shown in the figure. This view does not have the resolution to show the richly detailed structure of the fringe just outside the boundary of the set. At increasing magnifications, the Mandelbrot set exhibits an elaborate boundary that reveals progressively finer recursive detail.
For this tutorial, pick a set of limits that specify a highly zoomed part of the Mandelbrot set in the valley between the main cardioid and the p/q bulb to its left. A 1000x1000 grid of real parts (x) and imaginary parts (y) is created between these two limits. The Mandelbrot algorithm is then iterated at each grid location. An iteration number of 500 is enough to render the image in full resolution.
maxIterations = 500; gridSize = 1000; xlim = [-0.748766713922161,-0.748766707771757]; ylim = [0.123640844894862,0.123640851045266];
This tutorial uses an implementation of the Mandelbrot set by using standard MATLAB® commands running on the CPU. This implementation is based on the code provided in the Experiments with MATLAB e-book by Cleve Moler. This calculation is vectorized such that every location is updated simultaneously.
Create a new Simulink model and insert a MATLAB Function block from the User-Defined Functions library.
Double-click on one of the MATLAB Function block. A default function signature appears in the MATLAB Function Block Editor.
Define a function called
mandelbrot_count, which implements the
Mandelbrot algorithm. The function header declares
yGrid as an argument to the
mandelbrot_count function, with
count as the
return value. Save Editor document to
function count = mandelbrot_count(maxIterations, xGrid, yGrid) % mandelbrot computation z0 = xGrid + 1i*yGrid; count = ones(size(z0)); % Map computation to GPU coder.gpu.kernelfun; z = z0; for n = 0:maxIterations z = z.*z + z0; inside = abs(z)<=2; count = count + inside; end count = log(count);
Right-click on the MATLAB Function block and select
Block Parameters (Subsystem).
On the Code Generation tab, select
function for Function packaging.
Connect these blocks as shown in the diagram. Save the model as
Set up the model and code generation parameters required for an external mode target application. Then, generate code and build the target application.
From the Apps tab on the Simulink toolstrip, in the Setup to Run on Hardware section, click Run on Hardware Board.
In the Hardware Board section, from the Hardware
Board list, select
In the Prepare section, click Hardware Settings. The Configuration Parameters dialog box opens, displaying Hardware Implementation settings that are determined by the selected board.
On the Solver pane:
In the Type field, select
In the Solver field, select
(no continuous states).
Click Solver details. In the Fixed-step
size field, specify
0.1. (Otherwise, when
you generate code, the GPU Coder™ build process produces a warning and supplies a value.)
On the Data Import/Export pane, clear the Time and Output check boxes. In this example, data is not logged to the workspace or to a MAT-file. Click Apply.
On the Code Generation > Optimization pane, check that Default parameter behavior is set to
Tunable. If you make a change, click
On the Code Generation > Interface pane, in the Data exchange interface section, select External mode.
In the External mode configuration section, make sure that the
tcpip is selected for the Transport
The MEX-file name specifies the name of a MEX-file that
implements host-target communication. The default for TCP/IP is
ext_comm, a MEX-file provided with the Simulink
The MEX-file arguments field lets you specify arguments, such as a TCP/IP server port number, to be passed to the external interface program. These arguments are specific to the external interface that you are using.
This tutorial uses the default arguments. Leave the MEX-file arguments field blank.
The Static memory allocation check box controls how memory is allocated for external mode communication buffers in the target. For this tutorial, do not select the check box.
Click Apply to save the external mode settings.
Save the model.
Select the Code Generation pane. Make sure that Generate code only is cleared.
To generate code and create the target application, in the model window, press Ctrl+B. Or, on the Hardware tab, in the Run on Hardware section, click Monitor & Tune. Then, under Step By Step Commands, click Build for Monitoring.
The software creates the
mandelbrot_top executable file in your
You now run the
mandelbrot_top target executable and use Simulink as an interactive front end to the running target application. The executable
file is in your working folder. Run the target application and establish communication
between Simulink and the target.
To run the target application:
On the Hardware tab, in the Run on Hardware section:
In the Stop Time field, specify
which makes the model run until the target application receives a stop message
Click Monitor & Tune. Then, under Step By Step Commands, click Deploy.
The target application begins execution, and enters a wait state.
On the Hardware tab, in the Run on Hardware section, click Monitor & Tune. Then, under Step By Step Commands, click Connect. This action initiates a handshake between Simulink and the target application. When Simulink and the target are connected, the Connect button changes to Disconnect.
In the Run on Hardware section, click , which starts execution of the generated model code.
You have established communication between Simulink and the running target application.
To simultaneously disconnect Simulink from the host/target communication and end execution of the target application, on the Hardware tab, in the Run on Hardware section, click Stop.
rtwbuild(Simulink Coder) |