Teaching / ProcessingPhidgets


Processing with Phidgets

Source Code with Classes for Operating Phidgets from Processing

Sample Sketches

General Instructions

  1. Install phidget drivers for your computer from http://www.phidgets.com/ - drivers tab.
  2. Make a folder to keep your phidget-related files in. This will contain files that you will re-use for phidget projects, not your Processing sketches.
  3. Download phidget21.jar from http://www.phidgets.com/ - programming tab. Put it in your phidget folder.
  4. Download the Processing code for the phidgets you plan to use from this page. Put them in your phidget folder.
  5. Create a new Processing sketch. Save the sketch, giving it a suitable name.
  6. From the Sketch menu in Processing, select Add File ... From the file selection dialogue, select phidget21.jar from your folder of phidget-related files. This will add a folder name code to your Processing sketch folder, and a copy phidget21.jar will appear there.
  7. From the Sketch menu in Processing, select Add File ... From the file selection dialogue, select the Processing code for your phidget, e.g., phidgetIK.pde. You will see a new file tab appear for your sketch with the Processing code, and the file will be copied to your sketch folder.
  8. Repeat the previous step for each type of phidget that you will use.

Your sketch is now ready for you to start writing code that uses the phidgets.

The following example illustrates how to write Processing code that uses phidgets. It is based on testPhidgetAccelerometer.zip.

  1. Every phidget needs an object to operate it, so create the object variable:

phidgetAccelerometer accelerometer;

  1. You need to create an instance of the accelerometer object. The example does this in setup().

void setup() {
  ...
  // get phidget IK started and wait up to 1s for attach
  accelerometer = new phidgetAccelerometer();
  ...
}

This will work if you use only one phidget accelerometer. If you use more, you should use the alternate constructor to specify which phidget to use with the serial number.

void setup() {
  ...
  // get phidget IK started and wait up to 1s for attach
  accelerometer = new phidgetAccelerometer( 12345 );
  ...
}

  1. It is not strictly necessary, but I like to wait for the phidget to attach in setup(). When the phidget is attached it means it is plugged in by USB and ready for the program to use it.

void setup() {
  ...
  // get phidget IK started and wait up to 1s for attach
  accelerometer = new phidgetAccelerometer();
  accelerometer.waitForAttachment( 1000 );
  ...
}

This allows one second (1000 ms) for the phidget to attach before proceeding. If the phidget does not attach within this time (i.e., times out), the program carries on without the phidget attached.
  1. In draw(), check to see if the phidget is attached. If it is, display a green circle, otherwise display a red circle.

void draw() {
  ...
  // show attach status
  if ( accelerometer.isAttached() ) fill( 0, 255, 0 );
  else fill( 255, 0, 0 );
  noStroke();
  ellipse( 20, 20, 10, 10 );
  ...
}

  1. Find and display the serial and version numbers for the accelerometer phidget.

void draw() {
  ...
  // show serial number and version
  fill(0);
  text( accelerometer.serial(), 40, 25 );
  text( accelerometer.version(), 40, 35 );
  ...
}

  1. Display the acceleration data as a bar graph. The code uses the getAxisCount() to determine the number of axes to display acceleration data for, allowing a for loop to iterate through the axes.

void draw() {
  ... 
  // show accelerations
  for( int i = 0; i < accelerometer.getAxisCount(); i++ ) {
    ...
  }
  ...
}

Compute where the bar should be placed for the i th axis, center at xc,yc.

void draw() {
  ... 
  // show accelerations
  for( int i = 0; i < accelerometer.getAxisCount(); i++ ) {
    float xc = 30.0 + i * 60.0;
    float yc = 140.0;
    ...
  }
  ...
}

Compute where the endpoint of the bar should be. Use the getAcceleration() method to get the acceleration. The getAccelerationMax() and getAccelerationMin() methods give you the range of values for the accelerometer that you are using. Scale the the acceleration into the range of bar sizes using the map() function.

void draw() {
  ... 
  // show accelerations
  for( int i = 0; i < accelerometer.getAxisCount(); i++ ) {
    ...
    float y = map( accelerometer.getAcceleration( i ),
        accelerometer.getAccelerationMin( i ), accelerometer.getAccelerationMax( i ), 230.0, 50.0 );
    ...
  }
  ...
}

Draw the bar.

void draw() {
  ... 
  // show accelerations
  for( int i = 0; i < accelerometer.getAxisCount(); i++ ) {
    ...
    noFill();
    stroke( 0 );
    line( xc, yc, xc, y );
    fill( #0000ff );
    noStroke();
    ellipse( xc, y, 10, 10 );
  }
  ...
}

  1. It is best to use the close() method for the phidget object explicitly. It closes the phidget connection nicely, which will not happen automatically when Processing/Java deletes your phidget object. I used a keyPressed event to trigger the close() method and to shut down the program.

void draw() {
  ... 
  if (keyPressed) {
    accelerometer.close();
    exit();
  }
}

Documentation

The phidget classes currently available are:

  • phidgetAccelerometer
  • phidgetAdvancedServo
  • phidgetEncoder
  • phidgetIK
  • phidgetMotorControl

Each phidget class has two constructors as given below (TYPE is one of Acclerometer, AdvancedServo, Encoder, IK, or MotorControl).

phidgetTYPE()open with first available phidget of TYP{E
phidgetTYPE( int serial)open phidget with given serial number

Every phidget class has these methods.

void close()close the phidget
boolean isAttached()returns true if phidget is attached
void waitForAttachment( int timeout )wait for phidget to attach with timeout in miliseconds
void waitForAttachment( )wait indefinitely for phidget to attach
int serial()return serial number of phidget if attached, -1 otherwise
int version()return phidget version number if attached, -1 otherwise

The following are methods specific to each phidget type.

Accelerometer 
int getAxisCount()returns number of axes
float getAcceleration( int axis )returns acceleration for axis x=0 y=1 z=2
float getAccelerationMax( int axis )returns maximum acceleration for axis x=0 y=1 z=2
float getAccelerationMin( int axis )returns minimum acceleration for axis x=0 y=1 z=2
  
AdvancedServo 
int getMotorCount()return number of motors available
boolean getEngaged( int index )return true if indexed motor is engaged
void setEngaged( int index, boolean state )engage/disengage indexed motor (state=true for engaged)
float getPosition( int index )returns position of indexed motor in range [-1 ... 1]
void setPosition( int index, float position )set position of indexed motor in range [-1 ... 1]
  
Encoder 
int getEncoderCount()return encoder count
int getInputCount()return number of digital inputs
int getPosition( int index )return position of indexed encoder
void setPosition( int index, int position )set position of indexed encoder
boolean getInputState( int index )return state of indexed digital input
  
Interface Kit 
void setRatiometric( boolean state )set ratiometric mode for interface kit
int getSensorCount()return number of A/D converters (sensor channels) available
int getSensorValue( int index )return sensor value for A/D converter corresponding to index
int getInputCount()return number of digital inputs available
boolean getInputState( int index )return state of digital input corresponding to index
int getOutputCount()return number of digital outputs available
void setOutputState( int index, boolean state )sets state of digital output corresponding to index
  
Motor Control 
int getMotorCount()return number of motors that phidget can control
float getAcceleration( int index )return acceleration setting for indexed motor normalized to [0 .. 1]
float getCurrent( int index )return current for indexed motor
int getInputCount( )return number of digital inputs
boolean getInputState( int index )return state for indexed digital input
float getVelocity( int index )return velocity for indexed motor
void setAcceleration( int index, float accel )set acceleratin for indexed motor in range [0 .. 1]
void setVelocity( int index, float velocity )set velocity of indexed motor in range [-100 ... 100]
Page last modified on October 30, 2009, at 03:33 PM