Main Content

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.

更详细的信息在外部模式sim卡ulation, see外部模式Simulations for Parameter Tuning and Signal Monitoring.

Normal Mode

In normal mode, the Simulink model operates entirely in simulation, and the C/C++ device driver code in theMATLAB Systemblock 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 aTerminatorblock. In contrast, the device driver source blocks require modification of thestepImplmethod 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 thestepImplmethod so that the device driver block returns a randomly generated logical value at each simulation time step.

methods(Access=protected)...functionstepImpl(obj,u)%#okifisempty(coder.target)% Generate random data during simulationy =兰德> 0.5;else% Call C-function implementing device outputcoder.ceval('writeDigitalPin', 9, u);endend...end

ThestepImplmethod is modified to execute code whencoder.targetreturns 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, thestepImplmethod can be modified to retrieve a data variable from the MATLAB workspace as follows:

properties(Access = private) Count = 1end
methods(Access=protected)...functionstepImpl(obj,u)%#okifisempty(coder.target)% Generate random data during simulationx = evalin('base','x'); y = x(obj.Count); obj.Count = obj.Count + 1; if obj.Count > numel(x) obj.Count = 1; endelse% Call C-function implementing device outputcoder.ceval('writeDigitalPin', 9, u);endend...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.

See Also

||