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.
|
Language - MATLAB: Difference between revisions
Line 92: | Line 92: | ||
====Step Two: Wait for Attachment (plugging in) of the Phidget:==== | ====Step Two: Wait for 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: | To use the Phidget, it must be plugged in (attached). We can handle this by calling waitForAttachment. This function works for any Phidget. 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 98: | Line 98: | ||
<source lang=matlab> | <source lang=matlab> | ||
calllib('phidget21', 'CPhidget_open', | calllib('phidget21', 'CPhidget_open', device, -1); | ||
if calllib('phidget21', 'CPhidget_waitForAttachment', | if calllib('phidget21', 'CPhidget_waitForAttachment', device, 2500) == 0 | ||
% Insert your code here | % Insert your code here | ||
end | end |
Revision as of 19:32, 16 November 2011
Support
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 MATLAB specifically.
MATLAB is capable of using the non-event driven part of the Phidget API. We also provide example code in MATLAB for AdvancedServo, Encoder, InterfaceKit, Servo and Stepper.
MATLAB can be developed with any operating system that can run MATLAB code.{{{5}}}
You can compare MATLAB with our other supported languages.
Quick Downloads
Just need the MATLAB drivers, libraries, and examples? Here they are.
- MATLAB Sample Code, including the
phidget21Matlab.h
library file, needed for any MATLAB project.
MATLAB depends on the C/C++ libraries, and all MATLAB calls essentially call functions in the C library. So. you need the C/C++ drivers and libraries, and you may also find the C/C++ API reference handy:
- C/C++ API Manual Download or (HTML online version)
- General API (more help on functions common to all Phidgets)
- Device Specific APIs (more help on functions specific to your Phidget)
Libraries and Drivers which MATLAB depends on:
- 32 bit Windows (drivers, with libraries)
- 64 bit Windows (drivers, with libraries)
- General Linux Libraries ( Linux Library Setup Instructions)
- General Mac OSX Libraries ( Mac OSX Library Setup Instructions)
Getting Started
Run The Examples
One good way to start developing your application is to run and modify existing examples. You can find the MATLAB example code here:
Copy phidget21Matlab.h from the examples folder to your project directory where you will also put the .m file you want to run.
To run the example code, you'll need to find the source code for your specific device within the example package. If your device does not have an example written for it, try the HelloWorld.m example. Then, run the code within MATLAB as you would any .m file. This will allow you to:
- Make sure your libraries are properly linked
- Go from source code to a test application as quickly as possible
- Ensure your Phidget is hooked up properly
Follow The Examples
The examples for each device all have this general structure so you can follow along. We also have an in-depth general introduction to writing Phidget code (like open, read data, etc), as well as the {{{2}}} for specific syntax:
// ----- Main Code ----- Create Device Software Object
Close Device
|
{{{1}}} |
Write Your Own Code
General Library Setup
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
Create and Build Your Project
Programming with Phidgets makes extensive use of the calllib() function to the C/C++ library, so the C/C++ API reference will be helpful:
- C/C++ API (This is the complete set of functions you have available for all Phidgets)
- Device Specific APIs - The one for your Phidget can be found in its user guide.
To learn the details behind opening, configuring, using, and closing your Phidget, try the General Phidget Programming page. That page also describes using the Phidget in an event-driven manner and in a traditional manner, although you can only use the logic code type design in MATLAB.
Specific calls in MATLAB will differ in syntax from those on the General Phidget Programming page, but the concepts stay the same. For example, if we were using a Phidget Interface Kit as our device, the general calls would look like this:
Step One: Initialize and Open:
devicePointer = libpointer('int32Ptr',0);
calllib('phidget21', 'CPhidgetInterfaceKit_create', devicePointer);
device = get(devicePointer, 'Value');
The devicePointer is converted to the handle device as a handle for the Phidget. This example is specific to the Interface Kit because the call CPhidgetInterfaceKit_create is used. For another device, use the correspondingly named call in the C/C++ API.
The handle device is then used for all the C function calls where CPhidgetHandle phid is used in the C/C++ API. 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.
Step Two: Wait for Attachment (plugging in) of the Phidget:
To use the Phidget, it must be plugged in (attached). We can handle this by calling waitForAttachment. This function works for any Phidget. WaitForAttachment will block indefinitely until a connection is made to the Phidget, or an optional timeout is exceeded:
calllib('phidget21', 'CPhidget_open', device, -1);
if calllib('phidget21', 'CPhidget_waitForAttachment', device, 2500) == 0
% Insert your code here
end
Step Three: 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. For example, in the code below, we might want to read data from a sensor on the Phidget Interface Kit. 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.
Step Four: 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!
The complete set of functions you have available for all Phidgets can be found in the C/C++ API. Note, however, MATLAB does not make use of the event functions in the C/C++ API
If you are looking for more information about a particular function, we provide that as well. You can find more description on any function either in:
- Our API Overview which describes the set of calls common to all Phidgets, or
- The Device API page for calls available only on your specific Phidget.
Development Environments and Compilers
There could be differences between MATLAB and Octave (open source MATLAB). |
Common Problems and Solutions/Workarounds
Here you can put various frequent problems and our recommended solutions.