Products & Services Solutions Academia Support User Community Company

Learn more about Vehicle Network Toolbox   

Building Simulink Models to Transmit and Receive Messages

Build a Message Transmit Model

This section provides an example that builds a simple model using the Vehicle Network Toolbox blocks with other blocks in the Simulink library. The example illustrates how to send data via a CAN network.

Use this section in combination with the Build a Message Receive Model, and the Save and Run The Model to build your complete model and run the simulation.

Step 1: Open the Block Library

To open the Vehicle Network Toolbox block library, start the Simulink Library Browser.

To start the Simulink Library Browser, enter

simulink

at the MATLAB Command Window. The left pane in the Simulink Library Browser lists the available block libraries. To open the Vehicle Network Toolbox block library, click its entry icon. Then, click CAN Communication to open the CAN blocks. See Using the Simulink Library Browser for more information.

Step 2: Create a New Model

To use a block, add it to an existing model or create a model.

For this example, create a model by clicking the New model button on the toolbar.

You can also select the File menu in the Simulink Library Browser and select New > Model. Simulink opens an empty model window on the display. To name the new model, use the Save option.

Step 3: Drag the Vehicle Network Toolbox Blocks into the Model

To use the blocks in a model, click a block in the library and, holding the mouse button down, drag it into the model window. For this example, you need one instance each of the CAN Configuration, CAN Pack, and the CAN Transmit block in your model.

Drag Vehicle Network Toolbox Blocks into Model Window

Step 4: Drag Other Blocks to Complete the Model

This example requires a source block that feeds data to the CAN Pack block. Add a Constant block into your model.

Drag Constant Block to the Model Window

Step 5: Connect the Blocks

Make a connection between the Constant block and the CAN Pack block. When you move the pointer near the output port of the Constant block, the pointer becomes a cross hair. Click the Constant block output port and, holding the mouse button, drag the pointer to the input port of the CAN Pack block. Then release the button.

In the same way, make a connection between the output port of the CAN Pack block and the input port of the CAN Transmit block.

The CAN Configuration block does not connect to any other block. This block configures the CAN channel used by the CAN Transmit block to transmit the packed message.

Step 6: Specify the Block Parameter Values

You set parameters for the blocks in your model by double-clicking on the block.

Configure the CAN Configuration Block.   Double-click the CAN Configuration block to open its parameters dialog box. Set the:

    Click OK.

    Configure the CAN Pack Block.   Double-click the CAN Pack block to open its parameters dialog box. Set the:

    Click OK.

    Configure the CAN Transmit Block.   Double-click the CAN Transmit to open its parameters dialog box. Set Device to Vector Virtual 1 (Channel 1). Click Apply, then OK.

    Configure the Constant Block.   Double-click the Constant block to open its parameters dialog box. On the Main tab, set the:

    On the Signal Attributes tab, set the Output data type to uint8. Click OK.

    Your model looks like this figure:

    Build a Message Receive Model

    This section provides an example that builds a simple model using the Vehicle Network Toolbox blocks with other blocks in the Simulink library. The example illustrates how to receive data via a CAN network.

    Use this section in combination with the Build a Message Transmit Model, and the Save and Run The Model to build your complete model and run the simulation.

    Step 7: Drag the Vehicle Network Toolbox Blocks into the Model

    For this example, you need one instance each of the CAN Configuration, the CAN Receive, and the CAN Unpack block in your model. However, you add only the CAN Configuration and the CAN Receive blocks here. Add the CAN Unpack block into the Function–Call Subsystem described in Step 8: Drag Other Blocks to Complete the Model.

    Drag Vehicle Network Toolbox Blocks into Model Window

    Step 8: Drag Other Blocks to Complete the Model

    Use the Function–Call Subsystem block from the Simulink Ports & Subsystems block library to build your CAN Message pack subsystem.

    1. Drag the Function–Call Subsystem block into the model.

    2. Double-click the Function–Call Subsystem block to open the subsystem model.

    3. Drop the CAN Unpack block from the Vehicle Network Toolbox block library in this subsystem.

    To see the results of the simulation visually, drag the Scope block from the Simulink block library into your model.

    Drag The Scope Block into Model Window

    Step 9: Connect the Blocks

    1. Connect the CAN Msg output port on the CAN Receive block to the In1 input port on the Function–Call Subsystem block.

    2. Open the Function–Call Subsystem block and:

      • Double-click on In1 to rename it to CAN Msg.

      • Double-click on Out1 to rename it to data.

    3. Rename the Function–Call Subsystem block to CAN Unpack Subsystem.

    4. Connect the f() output port on the CAN Receive block to the function() input port on the Function–Call Subsystem block.

    5. Connect the CAN Unpack Subsystem output port to the input port on the Scope block.

    Your model looks like this figure:

    The CAN Configuration block does not connect to any other block. This block configures the CAN channel used by the CAN Receive block to receive the CAN message.

    Step 10: Specify the Block Parameter Values

    You set parameters for the blocks in your model by double-clicking on the block.

    Configure the CAN Configuration1 Block.   Double-click the CAN Configuration block to open its parameters dialog box. Set the:

    1. Device to Vector Virtual 1 (Channel 2).

    2. Bus speed to 500000.

    3. Acknowledge Mode to Normal.

    4. Click OK.

    Configure the CAN Receive Block.   Double-click the CAN Receive block to open its Parameters dialog box. Set the :

    1. Device to Vector Virtual 1 (Channel 2).

    2. Sample time to 0.01.

    3. Number of messages received at each timestep to All.

    Click OK.

    Configure the CAN Unpack Subsystem.   Double-click the CAN Unpack subsystem to open the Function–Call Subsystem model. In the model, double click the CAN Unpack block to open its parameters dialog box. Set the:

    1. Data to be output as to raw data.

    2. Name to the default value CAN Msg.

    3. Identifier type to the default Standard (11-bit identifier) type.

    4. Identifier to 500.

    5. Length (bytes) to the default length of 8.

    Click OK.

    Your subsystem looks like this figure:

    Save and Run The Model

    This section shows you how to save the models you have built in the previous two sections, Build a Message Transmit Model and Build a Message Receive Model.

    Step 11: Save the Model

    Before you run the simulation, save your model by clicking the Save icon or selecting File > Save from the menu.

    Step 12: Change Configuration Parameters

    1. In your model window, select Simulation > Configuration Parameters. The Configuration Parameters dialog box opens.

    2. In the Solver Options section, select :

      • Fixed-step from the Type list.

      • Discrete (no continuous states) from the Solver list.

    Step 13: Run the Simulation

    To run the simulation, click the Start button on the model window toolbar. Alternatively, you can use the Simulation menu in the model window and choose the Start option.

    When you run the simulation, the CAN Transmit block gets the message from the CAN Pack block. It then transmits it via Virtual Channel 1. The CAN Receive block on Virtual Channel 2 receives this message and hands it to the CAN Unpack block to unpack the message.

    While the simulation is running, the status bar at the bottom of the model window updates the progress of the simulation.

    Step 14: View the Results

    Double-click the Scope block to view the message transfer on a graph.

    If you are not able to see all the data on the graph, click the Autoscale toolbar button, which automatically scales both axes to display all stored simulation data.

    In the graph, the horizontal axis represents the simulation time in seconds and the vertical axis represents the received data value. In Message Transmit model, you configured blocks to pack and transmit an array of constant values, [1 2 3 4 5 6 7 8], every 0.01 second of simulation time. In Message Receive model, these values are received and unpacked. The output in the scope window represents the received data values.

      


    Recommended Products

    Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

     © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS