Using CAN with the LabVIEW Simulation Interface Toolkit
Overview
The Simulation Interface Toolkit provides a link between The MathWorks, Inc. Simulink® software and LabVIEW. The Simulation Interface Toolkit enables control design engineers to run simulation models in the Simulink environment while using the power of a LabVIEW user interface to display results and modify parameters as the model runs. Using the Simulation Interface Toolkit, you can import a model developed using the Simulink environment into LabVIEW, where you can instrument the model with a variety of I/O devices.
The Simulation Interface Toolkit generates a set of custom LabVIEW VIs for each model. You can modify these VIs, called model and driver VIs, to interact with hardware, such as using NI-CAN to interface to a CAN network. This interaction enables the model to send and receive messages to and from a ECU, a dashboard instrument, and other components.
Note: The Simulation Interface Toolkit version 3.0 and later automatically generates code for NI-CAN communication. This document refers to implementing CAN communication in earlier versions of this toolkit. To upgrade your existing version of the Simulation Interface Toolkit, contact National Instruments.
Table of Contents
Overview
The Simulation Interface Toolkit provides a link between Simulink software and LabVIEW. The Simulation Interface Toolkit enables control design engineers to run models developed using the Simulink environment while using the power of a LabVIEW user interface to display results and modify parameters as the model runs.
In addition, you can use The MathWorks, Inc. Real-Time Workshop® software with the Simulation Interface Toolkit to generate a model DLL. Once you generate the model DLL, then you can run the model as a stand alone application in LabVIEW. Creating the model DLL enables you to run the model on an RT target for real-time, deterministic simulations. For example, you can use the model of a controller for prototyping or the model of the system for hardware-in-the-loop (HIL) testing.
Both prototyping and HIL require I/O to read and generate signals. With the LabVIEW Real-Time Module, you have access to a wide variety of hardware for input and output. For example, you can have a data acquisition card for analog, digital or counter signals, image acquisition, motion control, or communication through TCP/IP, serial or CAN. CAN communication provides time-bounded communication by using nondestructive arbitration to block low priority messages and send high priority messages. This is essential to preserve determinism in a time-critical loop.
See Also:
Products and Services: LabVIEW
Products and Services: LabVIEW Real-Time
Products and Services: Simulation Interface Toolkit
Products and Services: CAN
CAN Channel API
The CAN Channel API introduced in NI-CAN 2.0 makes transitioning Data Acquisition (DAQ) code to CAN very easy. CAN Read and Write work with arrays of values, similar to AI Read and AO Update in NI-DAQ. Messages and channels provide the basis for the channels API. A message is a CAN frame and can contain up to 8 bytes (64 bits) of data. The channels are the pieces of data that make up the message. You create CAN messages and channels in Measurement & Automation Explorer (MAX). You also can import messages and channels from vector database (.dbc) files. As you create channels, MAX shows which bits are in use by other channels.
To create NI-CAN messages and channels in MAX, right-click CAN Channels under Data Neighborhood and select Create Message. Specify the name of the message, the arbitration ID, number of data bytes and default CAN Interface, then click the OK button to create the message. To import channels from a vector database, right-click CAN Channels in MAX and select Import from CANdb File.

Next right-click the new message in the CAN Channels list in MAX and select Create Channel. In the CAN Channel Properties dialog box, the wheel speed is a floating point number with no scaling that fills bits 3 to 34. A 3 bit channel already occupies bits 0 to 2. Bits 35 through 63 are available for other channels. You can also create integer channels, modify the byte order and change the scaling of the data.
Once you create the channels, you can use them in LabVIEW. Use the CAN Init Start VI to initialize the operation, then use either the CAN Read VI or CAN Write VI, depending on if the operation is an input or output operation. Terminate the task with the CAN Clear VI. The channel list may contain channels from more than one message. The driver distributes the information to the correct message.

You can use the CAN channels created in MAX with the LabVIEW Real-Time Module by right-clicking CAN Channels in MAX and selecting Send to RT System.
See Also:
Drivers and Updates: NI-CAN Version 2.1.1 for Windows
model_daq_driver VI
When you build the model DLL using the Simulation Interface Toolkit and Real-Time Workshop, you get a set of model VIs and driver VIs that work with the SIT Server to call the model DLL and execute the simulation. The model VI for the data acquisition example is ModelName_daq_main.vi. This VI starts the SIT server and then calls ModelName_daq_driver.vi. The simulation takes place in the True case of the ModelName_daq_driver.vi.

[+] Enlarge Image
There are three parts to this operation. The top row of VIs performs analog input, the middle row of VIs executes the model, and the bottom row of VIs performs the analog output. To use NI-CAN with this VI, replace the top and bottom rows of VIs with calls to NI-CAN. Each VI generated by the Simulation Interface Toolkit is customized to contain the correct model name and use the correct number of channels.
Complete the following steps to use the CAN examples described later in this document.
- Download the example provided.
- Open the VI and edit the Model DLL Name control and controls for input and output channels. Make sure the total number of input channels match the number of inports in the model, and that the total number of output channels matches the number of outports in the model.
- Select Operate»Make Current Values Default from the top pull-down menu.
- Right-click the front panel to display the Controls palette.
- Select Select a Control. Navigate to model project folder, which contains the files generated in the build process.
- Select the ModelName_modelParamMapping.ctl.
- On the block diagram, wire the ModelName_modelParamMapping control to the mapping input of SIT Initialize Model VI.
- Save the VI.
- Open ModelName_daq_main.vi, and save it with a new name.
- In the second frame of the sequence structure, right-click the VI and select Replace»Select a VI. Select the VI you saved in step 8.
- You can now either use an existing host VI or create a new one using the SIT Connection Manager dialog box.
Refer to the Simulation Interface Toolkit User Guide for more information about creating a host VI sing the SIT Connection Manager dialog box. You can reuse the same host VI with any of the new model VIs you generate. To use a new VI, select the VI saved in step 9 in the Path to Model VI path in the Select Host dialog box.
Method I: Software Timed
This method uses the Wait Until Next ms Multiple VI to control the loop rate of the time-critical I/O loop. For each loop iteration, the VI performs a CAN Read, evaluates the model, and then performs a CAN Write.

[+] Enlarge Image
This VI replaces the AI Configure, AI Single Scan, and AI Clear VIs with the CAN Init Start, CAN Read, and CAN Clear VIs. This VI also replaces the AO Control Config, AO Update, and AO Clear VIs with the CAN Init Start, CAN Write, and CAN Clear VIs. The Wait Until Next ms Multiple VI provides the timing for the loop. The loop time can be any number of whole milliseconds. This method is limited to a maximum of 1000 Hz (1 ms wait).
See Also:
Software Timed Loops using Wait (ms) and Wait Until Next ms Multiple
Method II: Hardware Timed with a Counter
The Wait Until Next ms Multiple VI is limited in the maximum loop rate and the resolution of the wait. The fastest loop rate is 1 ms wait, which gives you a loop rate of 1000 Hz. Increasing the wait to 2 ms results in a loop rate of 500 Hz. Therefore you cannot have a loop rate faster than 1000 Hz or a loop rate between 500 and 1000 Hz using a software timed loop. To increase the maximum loop rate and provide a higher resolution, use a counter on a DAQ board.

[+] Enlarge Image
This method is the same as the first method except that a counter replaced the Wait Until Next ms Multiple VI. The counter is configured for pulse generation outside of the While Loop. Inside the While Loop the Counter Control uses the opcode wait. This makes the Counter Control wait for the next active edge of the pulse generation.
See Also:
Counter Control and the Wait Control Code to Control Loop Rates with Sleep Mode
Method III: Hardware Timed with Analog I/O
This method combines analog input and output with CAN communication. Essentially the model_daq_driver.vi and model_can_driver.vi are combined into a single VI. In the top row of VIs configure the analog input. The loop rate is hardware timed through the analog input scan rate. AI Single Scan VI sleeps until the next sample is available. The second row of VIs is for the CAN Input. The third row of VIs is the code for the executing the model. The fourth and fifth rows of VIs are for analog output and CAN output respectively.

[+] Enlarge Image
AI Single Scan VI and CAN Read VI each return an array of data. This application appends these two arrays to each other and passes them to the SIT Step Model VI. The lowest numbered inports in the model correspond to the DAQ channels. The number of DAQ input channels combined with the number of CAN input channels equals the number of inports. The Array Subset VI selects the set of channels that need to be sent to the AO Update VI and the set of channels that need to be sent to the CAN Write VI.
Conclusion
You can modify the Simulation Interface Toolkit example VIs as shown in these examples to incorporate CAN. You can use a software timed loop, a hardware timed loop (counter), or a combination of analog I/O and CAN depending on the needs of the application. The VIs described in this document serve as a starting point. You can expand the VIs to include more functionality. You also can modify the VIs generated by Simulation Interface Toolkit to include other types of hardware.
Simulink® and Real-Time Workshop® are registered trademarks of The MathWorks Inc.
Reader Comments | Submit a comment »
Legal
This tutorial (this "tutorial") was developed by National Instruments ("NI"). Although technical support of this tutorial may be made available by National Instruments, the content in this tutorial may not be completely tested and verified, and NI does not guarantee its quality in any way or that NI will continue to support this content with each new revision of related products and drivers. THIS TUTORIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND AND SUBJECT TO CERTAIN RESTRICTIONS AS MORE SPECIFICALLY SET FORTH IN NI.COM'S TERMS OF USE (http://ni.com/legal/termsofuse/unitedstates/us/).
