Communicate with Hardware Using Connected IO
You can use Connected IO to communicate with the IO peripherals on the hardware.
Simulation with Connected IO is an intermediate step in the Model-Based Design workflow that bridges the gap between simulation and code generation by enabling Simulink® to communicate with the hardware before deploying the model on the hardware. Connected IO enables you to modify your model design and monitor the effect of the modified design using peripheral data from the hardware in a near real-time environment. You are not required to deploy the model on the hardware to monitor the effect of the modified design, which accelerates the simulation process. This interaction between Simulink (software) and Arduino® (hardware) is possible only when you enable Connected IO.
Note
In Connected IO, only one-time deployment of IO server is required.
These sections explain:
Arduino Boards and Blocks Supporting Connected IO
The Connected IO described here applies to the Simulink Support Package for Arduino Hardware on these Arduino boards and blocks:
Source blocks: Without Connected IO, these source blocks output zero during Normal mode simulation. With Connected IO, these blocks read data from the peripherals of the hardware.
Sink blocks: Without Connected IO, these sink blocks do not have any role during Normal mode simulation. With Connected IO, these blocks write data to the peripherals of the hardware.
Arduino Boards Source Blocksa Sink Blocks Arduino Due
Arduino Leonardo
Arduino MKR1000
Arduino MKR WIFI 1010
Arduino MKR ZERO
Arduino Mega 2560
Arduino Mega-ADK
Arduino Nano 3.0
Arduino Uno
Arduino Nano 33 IoT
Arduino Nano 33 BLE Sense
a Streaming mode is supported for all Source blocks. For information on Connected IO modes, see Connected IO Modes: Auto, Polling, and Streaming.
How Connected IO Works
Connected IO creates a communication interface that enables the Simulink model and the IO Server to communicate with each other. The Simulink model resides in your computer, and the IO Server is an engine on the hardware that contains all the peripheral functions. The transport layer formats and transmits the data using the communication interface.
This diagram shows the connection that the Connected IO creates between your computer and the hardware.
Communication in Connected IO
When you simulate a Simulink model with Connected IO:
The device driver blocks (for example, Digital Input and Digital Output blocks) in the model request peripheral data from the IO Server.
The IO Server accepts the request and responds with the requested data. You can use any Simulink sink or dashboard block to view the received data. Using the peripheral data received, you can verify that your model design meets the requirements.
For data acquisition applications, IO Server can be pre-configured to send data at a specific rate. For more information, see Data Acquisition using Connected IO.
If necessary, you can modify the design by adding, removing, or replacing any block in the Simulink model.
After the model is modified, resimulate the model. During simulation, the data request from the model is communicated to the hardware. You can continue to modify and simulate the model until the expected behavior is achieved.
Data Acquisition using Connected IO
Data acquisition can be done with connected IO using Polling and Streaming modes.
Polling
The communication in polling Connected IO is an on-demand process. The device driver blocks (for example, Digital Input and Digital Output blocks) in the model request peripheral data from the IO Server. The IO Server accepts the request and responds with the requested data. The hardware sends data only when it receives a data request from the Simulink model. In this mode, you can use any number of blocks as required in the model.
Consider the case where data needs to be read from a device connected to the target hardware via I2C, the below diagram shows the interaction between the target and Computer in the Polling mode.
Streaming
Streaming mode is used to get higher data acquisition rates for sensor/source blocks. In this mode, the Simulink pre-configures the IO Server on target to process a particular command at the sample rate specified on the block. After the configuration, data is transmitted from the target hardware proactively at the specified rate without additional commands to the target.
Response time for getting data from hardware with streaming is less compared to polling, which allows data acquisitions at high rates.
Streaming requires configuration for each block on the target. Every configuration books memory on the hardware. So, you can use this mode only with a limited number of blocks.
Only the source blocks (For example Analog Input, Digital Input, I2C Controller Read) can stream data. The sink blocks (for example, Digital Output, I2C Controller Write) works in Polling mode.
Consider the case where data needs to be read from a device connected to the target hardware via I2C at Sample time 10ms, the below diagram shows the interaction between the target and Computer in the Streaming mode.
Note
The communication in Connected IO is an on-demand process when Polling mode is enabled. The hardware sends data only after receiving a data request from the Simulink model.
When Streaming mode is enabled, it helps you to get higher data acquisition rates for source blocks. Simulink pre-configures the IO Server on target to process a particular command at the sample rate specified. The hardware proactively sends data at the specified rate without any additional commands.
You do not have to build, deploy, and run the model on the hardware to monitor the effects of your changes in your model design.
Connected IO in Model-Based Design
When you simulate a model without Connected IO, Simulink does not communicate with the hardware. Simulink communicates with the hardware only when the code is generated and the model is deployed on the hardware in External mode. Connected IO is an intermediate step in the model-based design workflow that bridges the gap between simulation and code generation by enabling Simulink to communicate with the hardware before deploying the model on the hardware.
This Model-Based Design Workflow diagram displays a model-based workflow:
Create a Simulink model.
Simulate the model in:
Simulation without Connected IO: There is no hardware interaction and no code generation.
Simulation with Connected IO: The model communicates with the hardware. There is no code generation.
External mode (Monitor & Tune): The model is deployed on the hardware and generates code.
Note
Analog input reference voltage parameter is not supported in Connected IO.
Deploy the model to the hardware.
Model-Based Design Workflow
How Connected IO Differs from External Mode
Connected IO and External mode both enable you to communicate with the hardware during simulation. However, you use Connected IO and External mode for different purposes. The table shows the actions that you can perform with each mode.
Action | External Mode | Connected IO |
---|---|---|
Obtain real-time data | You can obtain real-time data with External mode. | Enable the Simulink Pacing Option to get near real-time data.a |
Timing analysis of real-time data | Timing analysis of real-time data is possible because the Simulink model is running on the hardware in real-time. | Timing analysis of real-time data is not possible because the Simulink model is running in your computer and not on the hardware. |
Time required to start simulation | 1-2 minutes | Few seconds |
Code generation | Code is generated on the hardware. | No code is generated. |
a If your Simulink model has only Source blocks, then use Streaming mode to get real-time data. |
Run Simulink Model in Connected IO
Follow these steps to run a Simulink model in connected IO mode:
Open a Simulink model.
On the Modeling tab of the model, select Model Settings.
In the Configuration Parameters dialog box, select Hardware Implementation from the left pane and select the target hardware in the Hardware board parameter.
On the Hardware tab of the model, in the Mode section, select
Connected IO
and then click Run with IO.Additionally, you can change the rate of simulation by enabling simulation pacing. For more information, see Simulation Pacing Options.
Connected IO Modes: Auto, Polling, and Streaming
Connected IO provides these modes:
Auto: Auto is the default Connected IO mode. When you select this mode, Simulink automatically chooses the best suitable mode between Polling and Streaming.
If the Simulink model contains only source blocks and if Simulink pacing is enabled with Simulation per wall clock second is set to
1
, then Streaming mode is enabled for all source blocks.If the Simulink model contains Sink blocks or if Simulink pacing is disabled, then all the blocks (source and sink blocks) in the model run in Polling mode.
Polling: Polling is the on-demand mode. When you select this mode, both source and sink blocks run in Polling mode.
Select this mode when:
The model has both Source blocks and Sink blocks.
The model has a high number of blocks.
The model has source blocks in event-based subsystems such as Triggered Subsystem, Enabled Subsystem and so on.
The model has applications requiring lock-step operations.
Note
Use polling mode for block characterization (reviewing the properties of the blocks) before you try code generation.
Streaming: Steaming mode supports higher data acquisition rates for sensor/peripheral source blocks. When a Simulink model runs in streaming mode, Simulink pacing will be disabled. When you select this mode, Source blocks work in Streaming mode and Sink blocks work in Polling mode.
Note
Presence of sink blocks in a model configured for Streaming affects the performance of source blocks.
Select this mode when:
The model has only Source blocks and no Sink blocks.
The model has Sink blocks that requires execution rarely.
Selecting a Connected IO Mode
To simulate a model in Connected IO, select a Connected IO mode using these steps:
Open a Simulink model.
In the Simulink toolbar, set the Simulation mode to
Normal
.In the Modeling tab, select Model Settings.
In the Configuration Parameters dialog box, select Hardware Implementation.
Set the Hardware board parameter to a value such as
Arduino Mega 2560
.The parameter values under Hardware board settings are automatically populated to their default values.
Click Target hardware resources and then click Connected IO.
Select the required Connected IO mode from the list.
If you select
Auto
orStreaming
as Connected IO mode, a new parameter Action on data loss or source blocks displays. Data loss while streaming can occur due to following reasons:High sample rate set for the block.
Large number of blocks in the model.
Time consuming operations in the model .
Select the required option.
warning:
Select this option to display a warning when there is data loss from source blocks while streaming. After simulation, number of data dropped for each block is displayed in the diagnostic viewer.none:
Select this option to take no action.error:
Select this option to display an error message when there is data loss.
Click Apply and OK.
Streaming Data from Sensors
To read data from sensors, use one of the following options.
Use sensor blocks available in the library (for example MPU9250 IMU Sensor, ICM20948 IMU Sensor, and so on). To stream data from these blocks, add the block in the model, set the required sample rate, and select the streaming mode as discussed above.
Create a sensor using basic peripheral source blocks (for example,. I2C Register Read, Analog Input). Some sensors require source blocks to implement one-time configurations such as powering on the sensor, setting the range of sensor and so on. These blocks need not stream. In such cases, add such operations or blocks in Initialize Function block. The blocks in the Initialize Subsystem will not be configured for streaming. Only blocks in the main model will be configured for streaming.
Consider a case to design a BNO055 IMU sensor using the I2C blocks in the library.