William J Turkel

Connecting Phidgets to Mathematica on Mac OS X with J/Link

December 28, 2011
Labels: Making, Mathematica
Originally published at https://williamjturkel.wordpress.com/2011/12/28/connecting-phidgets-to-mathematica-on-mac-os-x-with-jlink/

1

In my previous post2, I showed how to connect an Arduino microcontroller to Mathematica on Mac OS X using the SerialIO package.  It is also quite straightforward to interact with Phidgets1.  In this case we can take advantage of Mathematica’s J/Link3 Java interface to call the Phidgets API.  This is basically a ‘hello world’ demonstration.  For a real application you would include error handling, event driven routines, and so on.  For more details, read the Java getting started tutorial4 and Phidgets programming manual5, then look at the sample code and javadocs on this page6.

Start by installing the Mac OS X Phidgets driver on your system. Once you have run Phidgets.mpkg you can open System Preferences and there will be a pane for Phidgets.  For my test, I used a PhidgetInterfaceKit 8/8/8 with an LED on Output 2 and a 60mm slider (potentiometer) attached to Sensor 0. Once you have the hardware configuration you like, plug the InterfaceKit into the USB. It should show up in the General tab of system preferences. If you double click on the entry, it will start a demonstration program that allows you to make sure you can toggle the LED and get values back from the slider. When everything is working correctly, you can close the program and open Mathematica.

In a Mathematica notebook, you are going to load the J/Link package, install Java, and put the phidget21.jar file on your class path by editing the AddToClassPath[] command in the snippet below.

Needs["JLink`"]
InstallJava[]
AddToClassPath["/path/to/phidget21"]
phidgetsClass = LoadJavaClass["com.phidgets.InterfaceKitPhidget"]

Next, create a new instance of the InterfaceKit object, open it and wait for attachment. You can include a timeout value if you’d like. Once the InterfaceKit is attached, you can query it for basic information like device name, serial number and sensor and IO options.

ik = JavaNew[phidgetsClass]
ik@openAny[]
ik@waitForAttachment[]
ik@getSerialNumber[]
ik@getDeviceName[]
{ik@getOutputCount[], ik@getInputCount[], ik@getSensorCount[]}

Finally you can use Mathematica‘s Dynamic[] functionality to create a virtual slider in the notebook that will waggle back and forth as you move the physical slider attached to the InterfaceKit. You can also turn the LED on and off by clicking a dynamic checkbox in the notebook.

Slider[
 Dynamic[
  Refresh[ik@getSensorValue[0], UpdateInterval -> 0.1]], {0, 1000}]

bool=false;
Dynamic[ik@setOutputState[2, bool]]
Checkbox[Dynamic[bool]]

When you are finished experimenting, close the InterfaceKit object.

ik@close[]

Previous: Connecting Arduino to Mathematica on Mac OS X with SerialIO
Next: A Simple Algorithm for Finding Images in E-books
Back to the blog archive

  1. http://www.phidgets.com/index.php ↩ ↩2

  2. http://williamjturkel.net/2011/12/25/connecting-arduino-to-mathematica-on-mac-os-x-with-serialio/ ↩

  3. http://reference.wolfram.com/mathematica/JLink/guide/JavaInterface.html ↩

  4. http://www.phidgets.com/documentation/Tutorials/Getting_Started_Java.pdf ↩

  5. http://www.phidgets.com/documentation/Programming_Manual.pdf ↩

  6. http://www.phidgets.com/programming_resources.php ↩