Main Content

Implement Connected I/O to Communicate with External Peripheral Devices Using Raspberry Pi

This workflow shows how to implement connected I/O in Normal mode of simulation using Raspberry Pi™ hardware. The example also highlights on the advantages of using connected I/O to improve efficiency while prototyping a Simulink® model.

Introduction

Connected I/O gains prominence in Simulink model prototypes because it allows you to communicate with hardware peripherals before you deploy the model on the hardware. This feature not only enables you to modify your model design to meet requirements before deploying it on the hardware, but also helps monitor its immediate effect in real-time, thus, reducing the time required to prototype the model.

This table describes the Simulink Support Package for Raspberry Pi Hardware blocks that support connected I/O in the Normal mode of simulation. The source blocks when used in connected I/O read data from the peripherals connected to the Raspberry Pi board. Similarly, the sink blocks when used in connected I/O write data to the peripherals connected to the Raspberry Pi board.

For more information on how connected I/O works, refer to Communicate with Hardware in Normal Mode Simulation Using Connected I/O.

Prerequisites

We recommend that you complete the Getting Started with Simulink Support Package for Raspberry Pi Hardware example.

Required Hardware

  • Raspberry Pi board

  • You can use the input and output hardware peripherals corresponding to the respective source or sink blocks of the Simulink Support Package for Raspberry Pi Hardware that supports connected I/O in Normal mode of simulation.

  • Micro USB cable

  • Connecting wires

Hardware Setup

  1. Connect the Raspberry Pi board to the host computer.

  2. Connect the hardware peripheral device to the Raspberry Pi board.

Simulink Model for Connected I/O

This example uses a preconfigured model from the Simulink Support Package for Raspberry Pi Hardware. In this model, a simple image processing algorithm, for RGB image data, is implemented using a V4L2 Video Capture block (used to capture live video) and the SDL Video Display block (used to display live video). You can manually select the desired image processing operation that you want on the video captured by the camera and display it on the SDL video display screen.

To open the model, run this command in the MATLAB® Command Window.

open_system('raspberrypi_image_processing')

Simulink model

The Image processing algorithm subsystem receives four inputs.

  1. The red component information of the video captured by the camera.

  2. The green component information of the video captured by the camera.

  3. The blue component information of the video captured by the camera.

  4. The manual Control selection output for the desired image processing algorithm to be processed.

    1. Option 0: Convert the video to display only the red component

    2. Option 1: Convert the video to display only the green component

    3. Option 2: Convert the video to display only the blue component

    4. Option 3: Convert the video to display only the grey-scale output

    5. Option 4: Convert the video to display only the edge-detection output

    6. Option 5: Filter median noise

To open the Algorithm subsystem, run this command in the MATLAB Command Window.

open_system('raspberrypi_image_processing/Algorithm')

Algorithm subsystem

Configure Parameters and Hardware Setup

Configure the following parameters in the V4L2 Video Capture block in the Camera output area:

  1. Enter the path and name of the video device in the Device name parameter.

  2. Specify the width in pixels and height in lines of the video that you want to capture in the Image size parameter.

  3. Set the video format in the Pixel format parameter to RGB.

  4. Select the Sample time of the video device.

Configure the following parameters in the SDL Video Display block of the Output display area:

  1. Set the Pixel format of the input video stream to RGB.

Configure the following parameters in the Constant block in the Manual Control area:

  1. Enter a value in the Constant value parameter to perform the desired image processing operation on the video captured from the camera and display it on the SDL video display screen.

  2. The default value of the Sample time parameter is inf.

You can either use the USB camera or the on-board camera. In this example, an external USB camera is interfaced with the Raspberry Pi board.

Enable Connected I/O

  1. Open the Simulink model.

  2. On the Simulation tab, set the Simulation Mode to Normal.

  3. On the Modeling tab, select Model Settings.

  4. In the Configuration Parameters dialog box, select Hardware Implementation.

  5. Set the Hardware board parameter to Raspberry Pi. This selection populates the Hardware board settings parameters with the default values for the Raspberry Pi hardware.

  6. From the Groups list under Target hardware resources, select Connected I/O. The Enable Connected I/O option is selected by default.

  7. The Communication Interface parameter is set to the TCP/IP option.

Run Model

  1. Enter the image processing operation option number in the Constant value parameter in the Manual Control area.

  2. On the Simulation tab, select Run.

  3. Observe the corresponding video output on the SDL video display screen.

  4. While the model is simulating, you can change the option in the Constant value parameter and observe the corresponding output for all the image processing operations from 0 through 5.

SDL Video Display output for Option 0: Display only the red component

SDL video display red component

SDL Video Display output for Option 1: Display only the green component

SDL video display green component

SDL Video Display output for Option 2: Display only the blue component

SDL video display blue component

SDL Video Display output for Option 3: Display only the grey-scale output

SDL video display greyscale component

SDL Video Display output for Option 4: Display only the edge-detection output

SDL video display edge detection component

SDL Video Display output for Option 5: Filter median noise

SDL video display filter median noise

Using connected I/O, you can manually select the options from 0 to 5 and observe the corresponding output on the SDL video display screen without deploying the model on the Raspberry Pi hardware. You can also modify the image processing algorithm and observe its effect on the video output.

Related Links