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 - MATLAB: Difference between revisions

From Phidgets Legacy Support
mNo edit summary
No edit summary
Line 1: Line 1:
__TOC__


MATLAB has a supported API for all Phidgets devices, and code samples for some devices.  For a complete list of our supported languages and their support status, [[Levels of Support|click here]].
MATLAB has a supported API for all Phidgets devices, and code samples for some devices.  For a complete list of our supported languages and their support status, [[Levels of Support|click here]].
Line 14: Line 15:


==Drivers, Libraries and Resources==
==Drivers, Libraries and Resources==
Before you can run your program, you need to set up the proper environment and get the necessary files off the Phidgets website. The functionality through MATLAB essentially means calling functions in the C library through MATLAB.  Hence, you will need the C/C++ libraries below, and you will probably find the C/C++ API documentation below useful.
Before you can run your program, you need to set up the proper environment and get the necessary files off the Phidgets website. The MATLAB functions essentially means call functions in the C library.  Hence, you will need the C/C++ libraries below, and you will probably find the C/C++ API documentation below useful.


Visit the drivers section at www.phidgets.com and get the latest:
Visit the drivers section at www.phidgets.com and get the latest:
Line 33: Line 34:
Your project will need to be able to find the phidget21Matlab.h from the MATLAB examples.
Your project will need to be able to find the phidget21Matlab.h from the MATLAB examples.


===Coding For Your Phidget===
===Setting up the Libraries===


Programming with Phidgets will make extensive use of the calllib() function.
Before you can use the Phidget, you must include a reference to the library in the code in the main body of code.  
Before you can use the Phidget, you must include a reference to the library in the code in the main body of code.  
Copy phidget21Matlab.h to your project directory and then in MATLAB:
Copy phidget21Matlab.h to your project directory and then in MATLAB:
Line 52: Line 52:
</div>
</div>


Afterwards, the Phidget object will need to be declared and then initialized.  
==Developing Code==
For example, we can declare a PhidgetInterfaceKit with:
 
===Running Examples===
 
===Writing Your Own Code===
 
Programming with Phidgets will make extensive use of the '''calllib()''' function.
 
*[http://www.phidgets.com/documentation/Phidget21_C_Doc.zip C/C++ API Reference]
 
You can use a Phidget in code by essentially by:
# Opening it,  
# Setting it up,
# Doing things with it (like reading data), and
# Closing it. 
 
These steps are generally outlined for all devices and languages on the [[General Phidget Programming]] page.  The [[General Phidget Programming]] page also introduces using the Phidget in a linear manner using logic code.  Event-driven examples are given on the [[General Phidget Programming]] page, these do not apply to MATLAB.
 
Specific calls in MATLAB will differ in syntax.  If we were using a [[Product - 1018 PhidgetInterfaceKit 8/8/8 | Phidget Interface Kit]] as our device, the general calls would look like this:
 
====1. To initialize and open:====


<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
Line 70: Line 89:
The object name for any type of Phidget is listed in the API manual. Every type of Phidget also inherits functionality from the Phidget base class.
The object name for any type of Phidget is listed in the API manual. Every type of Phidget also inherits functionality from the Phidget base class.


===Connecting to the Phidget===
Note that open() opens the software object, but not hardware.  So, it is not a guarantee you can use the Phidget immediately.


The program can try to connect to the Phidget through an open call.
The different types of open can be used with parameters to try and get the first device it can find, open based on its serial number, or even open across the network. The API manual lists all of the available modes that open provides.
Open will continuously try to connect to a Phidget, based on the parameters given, even trying to reconnect if it gets disconnected.  
 
This means that simply calling open does not guarantee you can use the Phidget immediately.  
====2. To wait for an attachment (plugging in) of the Phidget:====
We can handle this by calling waitForAttachment. WaitForAttachment will block indefinitely until a connection is made to the Phidget, or an optional timeout is exceeded.
 
To use the Phidget, it must be plugged in (attached). We can handle this by calling waitForAttachment. WaitForAttachment will block indefinitely until a connection is made to the Phidget, or an optional timeout is exceeded:


<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
Line 90: Line 110:
</div>
</div>


The different types of open can be used with parameters to try and get the first device it can find, open based on its serial number, or even open across the network.
====3. Do Things with the Phidget:====
The API manual lists all of the available modes that open provides.
 
One important thing to remember is that when working with Phidgets, a local connection will reserve the device until closed.
The most common thing you might want to do is read data from sensors. MATLAB does not support event handling, so all data must be read and sent directly.  
This prevents any other instances from retrieving data from the Phidget, including other programs.  
Simply use the C API functions such as CPhidgetInterfaceKit_getSensorValue() or CPhidgetInterfaceKit_setOutState() for Interface Kits:
The one connection per device limit does not apply when exclusively using the Phidget Webservice.
At the end of your program, don’t forget to call close to free any locks on the Phidget.


<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
Line 101: Line 119:
<source lang=matlab>
<source lang=matlab>


   calllib('phidget21', 'CPhidget_close', ikhandle);
   while n<10
  calllib('phidget21', 'CPhidget_delete', ikhandle);
    dataptr = libpointer('int32Ptr',0);
    calllib('phidget21', 'CPhidgetInterfaceKit_getSensorValue', ikhandle, 0, dataptr)
    disp(get(dataptr, 'Value'));
    n=n+1;
  end


</source>
</source>
Line 108: Line 130:
</div>
</div>


===Working directly with the Phidget===
Here a pointer is created to mark a value from the InterfaceKit inside a polling loop, and then its value is displayed to screen.


MATLAB does not support event handling, so all data must be read and sent directly.
====4. Close and Delete:====
Simply use the C API functions such as CPhidgetInterfaceKit_getSensorValue() or CPhidgetInterfaceKit_setOutState() for InterfaceKits.


<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
Line 117: Line 138:
<source lang=matlab>
<source lang=matlab>


   while n<10
   calllib('phidget21', 'CPhidget_close', ikhandle);
    dataptr = libpointer('int32Ptr',0);
  calllib('phidget21', 'CPhidget_delete', ikhandle);
    calllib('phidget21', 'CPhidgetInterfaceKit_getSensorValue', ikhandle, 0, dataptr)
    disp(get(dataptr, 'Value'));
    n=n+1;
  end


</source>
</source>
Line 128: Line 145:
</div>
</div>


Here a pointer is created to mark a value from the InterfaceKit inside a polling loop, and then its value is displayed to screen.
At the end of your program, don’t forget to call close to free any locks on the Phidget that the open() call put in place!


===Working with multiple Phidgets===
===Working with multiple Phidgets===

Revision as of 17:51, 10 November 2011

MATLAB has a supported API for all Phidgets devices, and code samples for some devices. For a complete list of our supported languages and their support status, click here.

MATLAB does not allow any form of event driven programming. Most of our other supported languages do, we recommend choosing a language that does.

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

Restrictions

In this section, list any restrictions or limitations that this particular language may impose. For example, incompatibility with certain operating systems.

Development Environments and Compilers

Describe each major compiler and notable differences or important information. (eg. framework versions) If there are known issues/workarounds mention them and link to the corresponding issue at the bottom of the page.

Drivers, Libraries and Resources

Before you can run your program, you need to set up the proper environment and get the necessary files off the Phidgets website. The MATLAB functions essentially means call functions in the C library. Hence, you will need the C/C++ libraries below, and you will probably find the C/C++ API documentation below useful.

Visit the drivers section at www.phidgets.com and get the latest:

You will need the Phidget Framework to use and to program with Phidgets. We also recommend that you download the following reference materials:

You may want to have these pages open while working through these instructions.

Getting Started

The Phidget examples were written using MATLAB m-files and this tutorial assumes its use. Your project will need to be able to find the phidget21Matlab.h from the MATLAB examples.

Setting up the Libraries

Before you can use the Phidget, you must include a reference to the library in the code in the main body of code. Copy phidget21Matlab.h to your project directory and then in MATLAB:

  function phidgettest(n)
     loadlibrary phidget21 phidget21Matlab.h;
      % More code goes here
  end

Developing Code

Running Examples

Writing Your Own Code

Programming with Phidgets will make extensive use of the calllib() function.

You can use a Phidget in code by essentially by:

  1. Opening it,
  2. Setting it up,
  3. Doing things with it (like reading data), and
  4. Closing it.

These steps are generally outlined for all devices and languages on the General Phidget Programming page. The General Phidget Programming page also introduces using the Phidget in a linear manner using logic code. Event-driven examples are given on the General Phidget Programming page, these do not apply to MATLAB.

Specific calls in MATLAB will differ in syntax. If we were using a Phidget Interface Kit as our device, the general calls would look like this:

1. To initialize and open:

  ikptr = libpointer('int32Ptr',0);
  calllib('phidget21', 'CPhidgetInterfaceKit_create', ikptr);
  ikhandle = get(ikptr, 'Value');

The ikptr is converted to ikhandle as a handle of the PhidgetInterfacekit, and is used for all the C function calls where CPhidgetHandle phid is used. The object name for any type of Phidget is listed in the API manual. Every type of Phidget also inherits functionality from the Phidget base class.

Note that open() opens the software object, but not hardware. So, it is not a guarantee you can use the Phidget immediately.

The different types of open can be used with parameters to try and get the first device it can find, open based on its serial number, or even open across the network. The API manual lists all of the available modes that open provides.

2. To wait for an attachment (plugging in) of the Phidget:

To use the Phidget, it must be plugged in (attached). We can handle this by calling waitForAttachment. WaitForAttachment will block indefinitely until a connection is made to the Phidget, or an optional timeout is exceeded:

  calllib('phidget21', 'CPhidget_open', ikhandle, -1);
  if calllib('phidget21', 'CPhidget_waitForAttachment', ikhandle, 2500) == 0
     % Insert your code here
  end

3. Do Things with the Phidget:

The most common thing you might want to do is read data from sensors. MATLAB does not support event handling, so all data must be read and sent directly. Simply use the C API functions such as CPhidgetInterfaceKit_getSensorValue() or CPhidgetInterfaceKit_setOutState() for Interface Kits:

  while n<10
    dataptr = libpointer('int32Ptr',0);
    calllib('phidget21', 'CPhidgetInterfaceKit_getSensorValue', ikhandle, 0, dataptr)
    disp(get(dataptr, 'Value'));
    n=n+1;
  end

Here a pointer is created to mark a value from the InterfaceKit inside a polling loop, and then its value is displayed to screen.

4. Close and Delete:

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

At the end of your program, don’t forget to call close to free any locks on the Phidget that the open() call put in place!

Working with multiple Phidgets

Multiple Phidgets of the same type can easily be run inside the same program. In our case, it requires another pointer and handle initialized for a PhidgetInterfaceKit. The new instance can then be set up, opened and used in the same fashion as the previous one. 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 Interfacekit, you would call CPhidgetRFID_create instead of CPhidgetInterfaceKit_create. The functions and events available would change but they can be accessed in a similar manner.

Building your Project

Describe the different ways a project could be built using this language.

Common Problems and Solutions/Workarounds

Here you can put various frequent problems and our recommended solutions.