Simulation with Device Driver Blocks
External mode enables Simulink® on your host computer to communicate with the deployed model on your hardware board during runtime. It also provides an easy way to visualize the outputs of sources and show the effects of sink blocks in real-time.
External mode creates a communication service on the host computer and hardware board. The two services establish a communication channel between the Simulink engine and generated code deployed on the hardware board. The communication service isolates the model process on the hardware board from the code and from the transport layer that formats, transmits, and receives the data packets. The communication service on the host computer receives the data packets through the transport layer and updates the Simulink model display. The diagram shows the connection that the external mode communication service creates between Simulink on the host computer and the deployed code on the hardware board.
By executing simple models containing your device driver blocks in external mode, you can directly observe the data from the hardware board.
For more detailed information on external mode simulation, see External Mode Simulations for Parameter Tuning and Signal Monitoring.
In normal mode, the Simulink model operates entirely in
simulation, and the C/C++ device driver code in the MATLAB
System block never executes. However, Simulink models
typically require source blocks to produce either artificially generated
or previously recorded data during simulation. The use of simulated
data allows for improved algorithm development, testing, and debugging
prior to deploying the model to the hardware board. Device driver
sink blocks do not require any modification to operate in a normal
mode simulation and assume the behavior of a Terminator block.
In contrast, the device driver source blocks require modification
stepImpl method to emit data either generated
during runtime or retrieved from the base MATLAB® workspace when
the model is in simulation.
Data Generation at Runtime
The following code shows an example of how to modify the
so that the device driver block returns a randomly generated logical
value at each simulation time step.
methods(Access=protected) ... function stepImpl(obj,u) %#ok<INUSD> if isempty(coder.target) % Generate random data during simulation y = rand > 0.5; else % Call C-function implementing device output coder.ceval('writeDigitalPin', 9, u); end end ... end
stepImpl method is modified to execute
coder.target returns empty. The empty value
indicates that the model is not deployed to hardware.
Data Retrieval from MATLAB Workspace
In many cases, device driver source blocks that emit randomly generated data is insufficient to accurately evaluate models in simulation. They require data that was either previously captured or generated.
To achieve this behavior, the
can be modified to retrieve a data variable from the MATLAB workspace
properties (Access = private) Count = 1 end
methods(Access=protected) ... function stepImpl(obj,u) %#ok<INUSD> if isempty(coder.target) % Generate random data during simulation x = evalin('base','x'); y = x(obj.Count); obj.Count = obj.Count + 1; if obj.Count > numel(x) obj.Count = 1; end else % Call C-function implementing device output coder.ceval('writeDigitalPin', 9, u); end end ... end
Other Simulation Modes
Other simulation modes available, such as accelerator mode and rapid accelerator mode, compile and execute the C/C++ code included in the device driver block. In these simulation modes, the C/C++ headers must be modified to include empty macros.