[ Home ]


Phooling with Phidgets!

By Sid Stamm

Summary

What is a Phidget? That's a great question. Basically they're physical manifestations of what we've learned to recognize as widgets: GUI interface elements that mimic (sometimes) real-world controls. So really the point is to go back to basics. Software controls, while fun, aren't always useful. You can't interact with the real world very well using just a computer monitor. This is where the Phidgets come in.

Now you can attach devices to your computer for physical interaction; you can make motors turn, lights flash, use physical buttons, sliders, weight sensors, light sensors -- you name it! If you can buy it at RadioShack, you can probably plug it in.

Until recently, getting Java to talk to physical devices required some custom programming (depending on the device). So in turn, nobody really wanted to write a library and JNI code for a light or a switch. The Phidgets project has developed a general-use library for interaction with Microsoft's .NET framework, and now the Mac OS. Then, they released a JNI extension that lets you use those same libraries through Java.

So what does it have to do with teaching Java? Kids love playing with toys -- this is a well known fact. Get them some motors and buttons to play with and watch. This is like lego mindstorm, but (in my opinion) cooler because it's easy to make them interact with pretty much any Java program. I've seen projects that make robots out of Phidgets, others have made an interactive checkers game! Right now, I'm working on deploying an interactive fossil museum exhibit at Wonderlab here in Bloomington. And it's not that hard.

Phidgets USA This company sells Phidgets in the USA for very reasonable prices. Use them in your class and let the proprietor (Matt) know! He likes to showcase projects.
jett-phidgets.zip
(or as tarball)
A compressed archive of my phidgets examples. These should work on any platform (Windows, Mac) so long as the JNI library is installed -- this comes standard with the Phidget drivers.
Javadoc Javadoc style documentation for my examples (in the above archive), as well as limited documentation for the Phidgets class provided by phidgets.com.

1. The Examples

I've created a bunch of examples to illustrate some possible uses for the interface kit, RFID kit and Servo controller. I'll present the physical assembly of the models as well as the Java application and important parts of its code.

  1. The Servo Controller
  2. The RFID Kit
  3. The Interface Kit

1.1 The Servo Kit

This is the simplest of my examples. I hooked one servo controller into my computer (via USB, because Phidgets are cool) and connected the motor... the goal was to make a rotating video camera stand. I realized, as I was writing this, that I only had one video camera and I had to use that for taking pictures. As a result, there is a small Cornflakes mascot on the camera stand in lieu of a camera.


I cut out some cardboard and made a stand for the motor (so it wouldn't fall over when topped with a much heavier camera). After shoving it in the seat I made for it, I plugged it all together then mounted the bird on top.

The next step, of course was to write software, and this was ridiculously easy. It involved making a JPanel, an action listener and adding a slider. Then I had to implement a phidgets event interface and add a few methods. That's pretty much all there is to it. Now when I drag the slider on the screen (see the third image), the bird rotates! There's a video: it's not that interesting, but it shows you the behavior and responsiveness of the servo.


Click for rooster movie.

1.1.1 The Code
try {
  servo = new PhidgetServo();
  if(!servo.Open(false)) { //not exclusive
    System.out.println("couldn't connect to servo.");
    System.exit(-1);
  }
} catch(Exception e) {
  e.printStackTrace();
  System.exit(-1)
}

//set up the GUI
this.js = new JSlider(10, 180);
this.js.addChangeListener(newChangeListener() {
          public void stateChanged(ChangeEvent e) {
            servo.setMotorPosition(0, js.getValue());
          }
        });

That's all the important code, the rest is GUI setup and stuff. All you have to do to use a servo is make a PhidgetServo object, open a connection (notice the horrible method naming conventions... part of Phidget.com's JNI stuff), then use setMotorPosition() to move it around. That's it!

1.2 The RFID Kit

The RFID example was a bit more complex. Imagine how you would like an RFID reader to work... I guess it really depends on what you want it to do. Well, the Phidget RFID Tag Reader doesn't work the way I wanted. It does a blocking scan. What this means is that when no tags are present, it just sits there. As soon as a tag comes in range, it will send a "tag" event and then keep sending them as fast as it can sense the tag over and over... until the tag exits range. Then it goes back to sleep.

Of course I have to be difficult and want something else. I want something that generates one event when a tag enters range, and then one other one when it exits. Basically these are state-change events. In order to accomplish this, I had to set up a producer-consumer pair of threads.

So I abstracted out a layer, and made a TagReader class. This class took care of modeling the tag reader in the way I wanted: it created a producer that would say "hey, I just saw a tag" and a consumer that would say "you did? I should notify my listener." Of course the consumer might also conclude, "Well, the producer hasn't said anything in a while so the tag must be gone. Hey listeners..."

I'm not going to even try to explain the code for this one. It's just a pair of threads and synchronized critical sections, but again the Phidgets part of this is very simple. I modeled a bunch of "lights" with a class called RFIDTagPanel. I put one of these on my JFrame for each tag I have. When you scan a tag, the proper one lights up. Check out the video:


Click for Tag Reader movie.

1.3 The Interface Kit

The Interface Kit is the mother of all phidgets. This is the sucker you can use with lights, sensors, switches --- pretty much anything radioshack sells. What would you do with this? You could build pong with a physical slider controller (or knob for the retro style). You could create level indicators for your music... the possibilities are endless. All the phidgets are great, but this one seems to be the most fun for those who like "tinkering."

The interface kit has three main parts. First are digital inputs (places to connect on-off switches or something of the like). It has digital outputs (places to connect LED's, buzzers, or circuits for relays. Last, but not least, are connectors for analog inputs. These are the most fun, because it allows you to measure something with precision. Examples of analog inputs are shown in the picture: a knob, a light sensor, a slider. Others include pressure sensors, temperature sensors, accelerometers, motion detectors... you name it. So what do you do with this stuff?

Good question. I did two things: I modeled the inputs as widgets (in the Java GUI), and I also created a feedback system that would visually give someone an idea of the value of an analog input (such as pressure). The input modeling was pretty easy. Each widget is simply a JPanel and they listen for events (physical changes on the interface kit). When an event occurs, the change how they look! The knob was accomplished with a little trigonometry, but the slider was straightforward.

There was only one problem: the Phidgets package for java only supports one listener for analog input events! Ack! I made the main class (the JPanel, really) into a listener and had it delegate the events out to each one of the widgets.

eventDelegates = new ArrayList<_IPhidgetInterfaceKitEvents>();

The interface _IPhidgetInterfaceKitEvents is what the phidgets classes use for interface kit event listeners. Note the clever JDK 1.5 syntax. The use of generics is nice here. Inside each of the events in the main class, it delegates to all the listeners:

for(_IPhidgetInterfaceKitEventsdel : eventDelegates) {
  del.OnInputChange(theEventObj);
}

Nice and small. Almost resembles Perl...

So what about the feedback thingy? That sounds neat... Well, just watch the video, or read the code. :)


Movie 1
   

   Movie 2
Note: The movies are pretty similar, I like the second one better. I have two up here because the first one has better audio, but the second one has better content.

© 2005 Sid Stamm