Alert.png

Notice: This page contains information for the legacy Phidget21 Library.

Phidget21 is out of support. Bugfixes may be considered on a case by case basis.

Phidget21 does not support VINT Phidgets, or new USB Phidgets released after 2020. We maintain a selection of legacy devices for sale that are supported in Phidget21.

We recommend that new projects be developed against the Phidget22 Library.


Click on the 2phidget22.jpg button in the menu bar to go to the Phidget22 version of this page.

Alert.png

Language - Simulink: Difference between revisions

From Phidgets Legacy Support
No edit summary
No edit summary
Line 4: Line 4:
==Introduction==
==Introduction==


{{LanguageSupport|Simulink|the non-[[General Phidget Programming#Event Driven Code|event driven]] part of the Phidget API|Phidget Interface Kit|Windows, Mac, and Linux; however, we only provide a Windows walkthrough here|}}
{{LanguageSupport|Simulink|the non-[[General Phidget Programming#Event Driven Code|event driven]] part of the Phidget API|the Phidget Interface Kit.|Windows, Mac, and Linux; however, we provide a Windows walkthrough here|}}


==Quick Downloads==
==Quick Downloads==
Line 23: Line 23:
===Description of Files===
===Description of Files===


* The '''phidget21Matlab.h''' file - found in the MATLAB (not Simulink) examples -  
* The '''phidget21Matlab.h''' file - found in the MATLAB (not Simulink) examples - is the library that makes both the MATLAB and the Simulink Phidget examples and other code work.  On both Linux and Mac OS X, this file should be modified as described on the [[Language - MATLAB|main MATLAB page]].


===Use Our Examples===
===Use Our Examples===

Revision as of 20:31, 23 May 2012

Icon-Simulink.png Simulink is a graphical plug-in program to MATLAB.

Introduction

If this is your first time working with a Phidget, we suggest starting with the Getting Started page for your specific device. This can be found in the user guide for your device. That page will walk you through installing drivers and libraries for your operating system, and will then bring you back here to use Simulink specifically.

Simulink is capable of using the non-event driven part of the Phidget API. We also provide example code in Simulink for the Phidget Interface Kit.

Simulink can be developed with Windows, Mac, and Linux; however, we provide a Windows walkthrough here.

You can compare Simulink with our other supported languages.

Quick Downloads

Just need the Simulink documentation, drivers, libraries, and examples? Here they are:

Documentation

Example Code

Libraries and Drivers

Getting Started with Simulink

Simulink, like MATLAB, can run on Mac OS X and Linux systems. Once you have MATLAB set up for your system - as described on our MATLAB page - these instructions should apply no matter what your operating system behind MATLAB.

This page assumes the reader is familiar with Simulink, Simulink user-defined functions, and interacting with Phidgets using MATLAB. In particular, if you've not written working MATLAB code for Phidgets yet, please work through the main MATLAB page before working through this one. MATLAB and Simulink depend heavily on hooking MATLAB into the Phidget C library, and this may be a new aspect of MATLAB for you.

Description of Files

  • The phidget21Matlab.h file - found in the MATLAB (not Simulink) examples - is the library that makes both the MATLAB and the Simulink Phidget examples and other code work. On both Linux and Mac OS X, this file should be modified as described on the main MATLAB page.

Use Our Examples

Write Your Own Code

Setting up a Phidgets Project

The Phidget examples were written using Simulink 6.1/MATLAB 7 in Windows XP. Other recent versions should work as well and would be set up in a similar manner. To begin,

  • The first step is to set the path for phidget21Matlab.h in MATLAB(File->Set Path). The phidget21Matlab.h can be found in the MATLAB examples.
  • Launch MATLAB and enter “simulink” in the MATLAB command window to open up the Simulink Library Browser.
  • Create a new model

The project now has access to Phidgets and we are ready to begin coding.


Follow The Examples

Code Snippets

This tutorial will use MATLAB code to demonstrate how user defined functions can be used to interact with Phidgets through the use of MATLAB Fcn, M Level 1 S Functions, and M Level 2 S Function blocks. As writing C code is beyond the scope of this guide, C S Function blocks will not be discussed. However, C S Function blocks work similar to M S Function blocks.

MATLAB Fcn blocks are the easiest blocks to use, but have limited features(i.e., only 1 input port). Level 1 S Functions blocks have more featues, but are more complicated to implement. Level 2 S Functions blocks are the most complex, but benefit in supporting the most features.

Connecting to the Phidget

The Phidget object will need to be declared and initialized. For example, we can declare and initialize an instance of a PhidgetInterfaceKit inside a M file of a Level 1 S Function block with:

    global handle;
    loadlibrary('phidget21', 'phidget21Matlab.h');
    ptr = libpointer('int32Ptr',0);
    calllib('phidget21', 'CPhidgetInterfaceKit_create', ptr);
    handle = get(ptr, 'Value');
    calllib('phidget21', 'CPhidget_open', handle, -1)
    calllib('phidget21', 'CPhidget_waitForAttachment', handle, 2500)

The handle variable is declared as a global, and can be easily accessed by other blocks. Since blocks repeatedly run until the simulation is completed, the above code will need to be restricted so that it runs only once during a simulation. It is placed inside the mdlInitializeSizes function. At the end of the simulation, the Phidget object will need to be closed and deleted. The folowing lines are added to mdlTerminate.

    calllib('phidget21', 'CPhidget_close', handle);
    calllib('phidget21', 'CPhidget_delete', handle);

Working directly with the Phidget

In this section, a MATLAB Fcn block will be used to set the output states of the digital output ports on a PhidgetInterfaceKit 8/8/8. The following code is placed inside a M file. The block’s input is the desired output state.

    function setOutputStates(outputState)
        global handle;
        for i = 0:7
            if (calllib('phidget21', 'CPhidgetInterfaceKit_setOutputState', handle, i, outputState) == 1)
                display('Set output state failed')
       
            end
        end

Note that this handle is the same handle that was created earlier. Next, a M Level 2 S Function will be used to retrieve sensor values of analog sensor ports and output it to a scope. The block will contain no inputs and 1 output, with 8 dimensions(1 dimension for each analog sensor of a PhidgetInterfaceKit 8/8/8). The following code is placed in the Output function in the M file.

    function Output(block)
        dataptr = libpointer('int32Ptr',0);
        global handle;
        for i = 0:7
            if (calllib('phidget21', 'CPhidgetInterfaceKit_getSensorValue', handle, i, dataptr) == 0)
                block.OutputPort(1).Data(i+1) = get(dataptr, 'Value');
            end
        end

Event Driven Programming

MATLAB and Simulink do not support event handling.

Working with multiple Phidgets

Multiple Phidgets of the same type can easily be run inside the same model. Each instance of a Phidget corresponding to a particular handle. Create more handles to work with more Phidgets. If the application needs to distinguish between the devices, open can be called with the serial number of a specific Phidget.

Other Phidgets

The design given in this document can also be followed for almost all Phidgets. For example, if you were using a PhidgetRFID instead of an PhidgetInterfacekit, you would declare an RFID object instead of an InterfaceKit. The methods and events available would change but they can be accessed in a similar manner.

Working with Strings

It is possible for a user to specify a string of characters in the block parameters dialog box of a user defined function such that it is passed into a user defined function. Please see the PhidgetInterfaceKit_RemoteIP block library in the Simulink examples for details. Simulink cannot display strings in the GUI, however it is possible to write a C S function that displays strings in the MATLAB console.

Working in Real Time

Even though a model may finish simulating before a user has a chance to interact with Phidgets, there are workarounds that can be used to slow down simulation time to allow for user interaction. One method is to modify the Solver Options(under Simulation->Configuration Parameters->Solver). Changing the step size and stop time may slow down the simulation. Another method is to implement a user defined function that reduces the simulation speed until the system clock matches the Simulink clock. The Phidget Simulink example uses the “Real Time Block” to simulate the model in approximate real time. For more information about the “Real Time Block”, please see

http://www.mathworks.com/support/solutions/en/data/1-15JAW/

To use the “Real Time Block”, place the following files in your MATLAB working directory: slblocks.m, rtc.mdl, waitforreal.m. These files can be found in the Simulink examples. Open up the Simulink Library Browser and select “Real Time Clock”. Place the Real Time Clock block onto your model.

For applications where accurate real time simuation is needed, xPC Target can be used.

Please note that the simulation speed of these methods may differ depending on the CPU speed, complexity of the model, the amount of text being displayed in the MATLAB console, etc....

Common Problems and Solutions/Workarounds

None at this time.